-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcase_converter_test.go
118 lines (110 loc) · 3.75 KB
/
case_converter_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
package main
import (
"reflect"
"testing"
)
func TestTokenizers(t *testing.T) {
var examples = []struct {
testCase string
input string
expectedOutput []string
}{
{"snake", "test", []string{"test"}},
{"snake", "snake_case", []string{"snake", "case"}},
{"snake", "c_c_c_c", []string{"c", "c", "c", "c"}},
{"camel", "test", []string{"test"}},
{"camel", "camelCase", []string{"camel", "Case"}},
{"camel", "cCCC", []string{"c", "CCC"}},
{"camel", "pleaseSplitHTMLCorrectly", []string{"please", "Split", "HTML", "Correctly"}},
{"pascal", "Test", []string{"Test"}},
{"pascal", "PascalCase", []string{"Pascal", "Case"}},
{"pascal", "CCCC", []string{"CCCC"}},
{"pascal", "PleaseSplitHTMLCorrectly", []string{"Please", "Split", "HTML", "Correctly"}},
{"lower", "test", []string{"test"}},
{"lower", "lower case", []string{"lower", "case"}},
{"lower", "c c c c", []string{"c", "c", "c", "c"}},
{"upper", "TEST", []string{"TEST"}},
{"upper", "UPPER CASE", []string{"UPPER", "CASE"}},
{"upper", "C C C C", []string{"C", "C", "C", "C"}},
{"title", "Test", []string{"Test"}},
{"title", "Title Case", []string{"Title", "Case"}},
{"title", "C C C C", []string{"C", "C", "C", "C"}},
{"sentence", "Test", []string{"Test"}},
{"sentence", "Sentence case", []string{"Sentence", "case"}},
{"sentence", "C c c c", []string{"C", "c", "c", "c"}},
{"kebab", "test", []string{"test"}},
{"kebab", "kebab-case", []string{"kebab", "case"}},
{"kebab", "c-c-c-c", []string{"c", "c", "c", "c"}},
{"dot", "test", []string{"test"}},
{"dot", "dot.case", []string{"dot", "case"}},
{"dot", "c.c.c.c", []string{"c", "c", "c", "c"}},
}
for _, example := range examples {
actualOutput := inputCaseConfigurations[example.testCase].tokenize(example.input)
if !reflect.DeepEqual(actualOutput, example.expectedOutput) {
t.Errorf("Failed testing %s case tokenizer: got %s for given input %s, expected %s", example.testCase, actualOutput, example.input, example.expectedOutput)
}
}
}
func TestGuardInputCaseIsValid(t *testing.T) {
_, error := ConvertCase("randomString", "camel", "text")
if error == nil {
t.Error("Expected failure for input case: 'randomString', didn't get a failure.")
}
}
func TestGuardOutputCaseIsValid(t *testing.T) {
_, error := ConvertCase("camel", "randomString", "text")
if error == nil {
t.Error("Expected failure for output case: 'randomString', didn't get a failure.")
}
}
func TestConvertAllCasesOnHappyPath(t *testing.T) {
caseToFormattedText := map[string]string{
"snake": "some_example_sentence",
"camel": "someExampleSentence",
"pascal": "SomeExampleSentence",
"lower": "some example sentence",
"upper": "SOME EXAMPLE SENTENCE",
"title": "Some Example Sentence",
"sentence": "Some example sentence",
"flat": "someexamplesentence",
"kebab": "some-example-sentence",
"dot": "some.example.sentence",
}
for _, from := range inputCases {
for _, to := range outputCases {
inputText := caseToFormattedText[from]
expectedOutput := caseToFormattedText[to]
actualOutput, _ := ConvertCase(from, to, inputText)
if actualOutput != expectedOutput {
t.Errorf(
"Conversion from '%s' to '%s' with input text '%s' failed, got: '%s', want: '%s'.",
from,
to,
inputText,
actualOutput,
expectedOutput,
)
}
}
}
}
func TestEmptyInput(t *testing.T) {
for _, from := range inputCases {
for _, to := range outputCases {
inputText := ""
expectedOutput := ""
actualOutput, _ := ConvertCase(from, to, inputText)
if actualOutput != expectedOutput {
t.Errorf(
"Conversion from '%s' to '%s' with input text '%s' failed, got: '%s', want: '%s'.",
from,
to,
inputText,
actualOutput,
expectedOutput,
)
}
}
}
}