-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit_test.go
More file actions
84 lines (74 loc) · 2.92 KB
/
split_test.go
File metadata and controls
84 lines (74 loc) · 2.92 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
package fmt_test
import (
"testing"
. "github.com/tinywasm/fmt"
)
func TestStringSplit(t *testing.T) {
// Test cases with explicit separator
testCasesWithSeparator := []struct {
data string
separator string
expected []string
}{
// Original test cases
{"texto1,texto2", ",", []string{"texto1", "texto2"}},
{"apple,banana,cherry", ",", []string{"apple", "banana", "cherry"}},
{"one.two.three.four", ".", []string{"one", "two", "three", "four"}},
{"hello world", " ", []string{"hello", "world"}},
{"hello. world", ".", []string{"hello", " world"}},
{"h.", ".", []string{"h."}},
// Edge condition test cases
{"", ",", []string{""}}, // Empty string
{"abc", "", []string{"a", "b", "c"}}, // Empty separator
{"ab", ",", []string{"ab"}}, // String shorter than 3 chars
{"a", "a", []string{"a"}}, // Single char string equals separator
{"aaa", "a", []string{"", "", "", ""}}, // All chars are separators
{"abc,def,", ",", []string{"abc", "def", ""}}, // Trailing separator
{",abc,def", ",", []string{"", "abc", "def"}}, // Leading separator
{"abc,,def", ",", []string{"abc", "", "def"}}, // Adjacent separators
{"abc", "abc", []string{"", ""}}, // Separator equals data
{"abcdefghi", "def", []string{"abc", "ghi"}}, // Separator in the middle
{"abc:::def:::ghi", ":::", []string{"abc", "def", "ghi"}}, // Multi-char separator
}
// Test cases for whitespace splitting (no explicit separator)
testCasesWhitespace := []struct {
data string
expected []string
}{
{"hello world", []string{"hello", "world"}},
{" hello world ", []string{"hello", "world"}},
{"hello\tworld", []string{"hello", "world"}},
{"hello\nworld", []string{"hello", "world"}},
{"hello\rworld", []string{"hello", "world"}},
{"hello world test", []string{"hello", "world", "test"}},
{"", []string{}}, // Empty string
{"hello", []string{"hello"}}, // Single word
{" ", []string{}}, // Only whitespace
{"hello\n\tworld", []string{"hello", "world"}}, // Mixed whitespace
}
// Test with explicit separator
for _, tc := range testCasesWithSeparator {
out := Convert(tc.data).Split(tc.separator)
if !areStringSlicesEqual(out, tc.expected) {
t.Errorf("Split(%q, %q) = %v; expected %v", tc.data, tc.separator, out, tc.expected)
}
}
// Test with default whitespace separator
for _, tc := range testCasesWhitespace {
out := Convert(tc.data).Split()
if !areStringSlicesEqual(out, tc.expected) {
t.Errorf("Split(%q) = %v; expected %v", tc.data, out, tc.expected)
}
}
}
func areStringSlicesEqual(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}