-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircular_test.go
More file actions
97 lines (90 loc) · 2.55 KB
/
circular_test.go
File metadata and controls
97 lines (90 loc) · 2.55 KB
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
package strings2_test
import (
"testing"
"github.com/arran4/strings2"
)
func TestCircularRestoration(t *testing.T) {
s := "Helloworld"
tests := []struct {
name string
input string
expectedOverride *string // If set, expects this output instead of input (lossy)
partitionerCfg strings2.PartitionerConfig
formatFunc func([]strings2.Word, ...strings2.Option) (string, error) // Allow custom formatter
formatDelim string
}{
{
name: "Sentence with spaces",
input: "Hello to all the good-doers out there",
partitionerCfg: strings2.PartitionerConfig{
Delimiters: map[rune]bool{' ': true, '-': true},
PreserveSep: true,
},
formatDelim: "",
},
{
name: "Complex delimiters",
input: "user_name.with-mixed@delimiters",
partitionerCfg: strings2.PartitionerConfig{
Delimiters: map[rune]bool{'_': true, '.': true, '-': true, '@': true},
PreserveSep: true,
},
formatDelim: "",
},
{
name: "Consecutive delimiters",
input: "item1,,item2--item3",
partitionerCfg: strings2.PartitionerConfig{
Delimiters: map[rune]bool{',': true, '-': true},
PreserveSep: true,
},
formatDelim: "",
},
{
name: "Lossy conversion (eats separators)",
input: "Hello-world",
// No PreserveSep, so '-' is lost
partitionerCfg: strings2.PartitionerConfig{
Delimiters: map[rune]bool{'-': true},
},
formatDelim: "",
expectedOverride: &s,
},
{
name: "Snake to Snake Roundtrip",
input: "hello_world_test",
partitionerCfg: strings2.PartitionerConfig{
Delimiters: map[rune]bool{'_': true},
},
formatFunc: func(words []strings2.Word, opts ...strings2.Option) (string, error) {
return strings2.ToSnakeCase(words, opts...)
},
formatDelim: "_",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
words, err := strings2.Parse(tc.input, tc.partitionerCfg)
if err != nil {
t.Fatalf("Parse failed: %v", err)
}
var restored string
if tc.formatFunc != nil {
restored, err = tc.formatFunc(words)
} else {
// Use generic formatted case helper which returns (string, error) now
restored, err = strings2.WordsToFormattedCase(words, strings2.OptionDelimiter(tc.formatDelim))
}
if err != nil {
t.Fatalf("Format failed: %v", err)
}
expected := tc.input
if tc.expectedOverride != nil {
expected = *tc.expectedOverride
}
if restored != expected {
t.Errorf("Circular restoration failed.\nInput: %q\nExpected: %q\nGot: %q", tc.input, expected, restored)
}
})
}
}