-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcase_test.go
161 lines (145 loc) · 3.81 KB
/
case_test.go
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package main
import (
"testing"
"unicode"
)
func TestToCamelCase(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"hello world", "helloWorld"},
{"Hello World", "helloWorld"},
{"hello_world", "helloWorld"},
{"HELLO WORLD", "helloWorld"},
{"hello WORLD", "helloWorld"},
{"", ""},
{"a", "a"},
{"this is a test", "thisIsATest"},
{"convert this to camel case", "convertThisToCamelCase"},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
result := toCamelCase(tt.input)
if result != tt.expected {
t.Errorf("toCamelCase(%q) = %q; want %q", tt.input, result, tt.expected)
}
})
}
}
// Test getCaseOperation function
func TestGetCaseOperation(t *testing.T) {
tests := []struct {
name string
op caseOperation
input rune
expected rune
}{
{"Lowercase conversion", Lower, 'A', 'a'},
{"Uppercase conversion", Upper, 'a', 'A'},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
fn := getCaseOperation(tt.op)
if fn == nil {
t.Fatalf("getCaseOperation(%v) returned nil", tt.op)
}
result := fn(tt.input)
if result != tt.expected {
t.Errorf("getCaseOperation(%v)(%q) = %q; want %q", tt.op, tt.input, result, tt.expected)
}
})
}
}
func TestRandomCase(t *testing.T) {
original := "Hello, World!"
randomized := toRandomCase(original)
if len(randomized) != len(original) {
t.Fatalf("randomCase(%q) = %q; lengths differ", original, randomized)
}
// Convert randomized string to a slice of runes for comparison
randomizedRunes := []rune(randomized)
// Check that each character in the original has been either lowercased or uppercased
for i, char := range original {
if !unicode.IsLetter(char) {
// Non-letter characters should remain the same
if randomizedRunes[i] != char {
t.Errorf("randomCase(%q) changed non-letter character %q to %q", original, char, randomizedRunes[i])
}
continue
}
// Letter characters should be either uppercased or lowercased
lower := unicode.ToLower(char)
upper := unicode.ToUpper(char)
if randomizedRunes[i] != lower && randomizedRunes[i] != upper {
t.Errorf("randomCase(%q) changed %q to %q, which is not a valid case conversion", original, char, randomizedRunes[i])
}
}
}
func TestToSnakeCase(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"Hello World", "hello_world"},
{"HelloWorld", "hello_world"},
{"HELLO WORLD", "hello_world"},
{"hello WORLD", "hello_world"},
{"", ""},
{"a", "a"},
{"thisIsATest", "this_is_a_test"},
{"convert this to snake case", "convert_this_to_snake_case"},
{"Test123WithNumbers", "test123_with_numbers"},
{"Already_Snake_Case", "already_snake_case"},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
result := toSnakeCase(tt.input)
if result != tt.expected {
t.Errorf("toSnakeCase(%q) = %q; want %q", tt.input, result, tt.expected)
}
})
}
}
func TestToUpper(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"hello", "HELLO"},
{"Hello World", "HELLO WORLD"},
{"hello_world", "HELLO_WORLD"},
{"HELLO", "HELLO"},
{"123abc", "123ABC"},
{"", ""},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
result := toUpper(tt.input)
if result != tt.expected {
t.Errorf("toUpper(%q) = %q; want %q", tt.input, result, tt.expected)
}
})
}
}
func TestToLower(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"HELLO", "hello"},
{"Hello World", "hello world"},
{"HELLO_WORLD", "hello_world"},
{"hello", "hello"},
{"123ABC", "123abc"},
{"", ""},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
result := toLower(tt.input)
if result != tt.expected {
t.Errorf("toLower(%q) = %q; want %q", tt.input, result, tt.expected)
}
})
}
}