File size: 1,934 Bytes
6380833
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package strcase_test

import (
	"testing"

	"github.com/openmeterio/openmeter/pkg/strcase"
)

func TestSnakeToCamel(t *testing.T) {
	tt := []struct {
		name  string
		snake string
		camel string
	}{
		{
			name:  "empty",
			snake: "",
			camel: "",
		},
		{
			name:  "single",
			snake: "a",
			camel: "a",
		},
		{
			name:  "two",
			snake: "a_b",
			camel: "aB",
		},
		{
			name:  "three",
			snake: "a_b_c",
			camel: "aBC",
		},
		{
			name:  "long",
			snake: "abc_def",
			camel: "abcDef",
		},
		{
			name:  "withUppers",
			snake: "aBc_dEf_gHi",
			camel: "aBcDEfGHi",
		},
		{
			name:  "withSpecial",
			snake: "a_b-c_d/e",
			camel: "aB-cD/e",
		},
	}

	for _, tc := range tt {
		t.Run(tc.name, func(t *testing.T) {
			camel := strcase.SnakeToCamel(tc.snake)
			if camel != tc.camel {
				t.Errorf("expected %q, got %q", tc.camel, camel)
			}
		})
	}
}

func TestCamelToSnake(t *testing.T) {
	tt := []struct {
		name  string
		camel string
		snake string
	}{
		{
			name:  "empty",
			camel: "",
			snake: "",
		},
		{
			name:  "single",
			camel: "a",
			snake: "a",
		},
		{
			name:  "two",
			camel: "aB",
			snake: "a_b",
		},
		{
			name:  "three",
			camel: "aBC",
			snake: "a_b_c",
		},
		{
			name:  "long",
			camel: "abcDef",
			snake: "abc_def",
		},
		{
			name:  "withSpecial",
			camel: "aB-cD/e",
			snake: "a_b-c_d/e",
		},
	}

	for _, tc := range tt {
		t.Run(tc.name, func(t *testing.T) {
			snake := strcase.CamelToSnake(tc.camel)
			if snake != tc.snake {
				t.Errorf("expected %q, got %q", tc.snake, snake)
			}
		})
	}
}

func TestCamelToSnakeToCamel(t *testing.T) {
	tt := []string{
		"",
		"a",
		"aB",
		"aBC",
		"abcDef",
		"aBcDEfGHi",
		"aB-cD/e",
	}

	for _, camel := range tt {
		t.Run(camel, func(t *testing.T) {
			snake := strcase.CamelToSnake(camel)
			camel2 := strcase.SnakeToCamel(snake)
			if camel != camel2 {
				t.Errorf("expected %q, got %q", camel, camel2)
			}
		})
	}
}