-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplit_test.go
77 lines (71 loc) · 1.41 KB
/
split_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
package main
import (
"bufio"
"bytes"
"reflect"
"testing"
)
func TestSplitOnPlus(t *testing.T) {
tests := []struct {
name string
input string
expected []string
}{
{
name: "Simple split",
input: "hello+world",
expected: []string{"hello", "world"},
},
{
name: "Multiple splits",
input: "a+b+c+d",
expected: []string{"a", "b", "c", "d"},
},
{
name: "No split character",
input: "hello world",
expected: []string{"hello world"},
},
{
name: "Empty string",
input: "",
expected: nil,
},
{
name: "Only split character",
input: "+",
expected: []string{""},
},
{
name: "Split at beginning",
input: "+hello",
expected: []string{"", "hello"},
},
{
name: "Split at end",
input: "hello+",
expected: []string{"hello"},
},
{
name: "Multiple empty splits",
input: "+++",
expected: []string{"", "", ""},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
scanner := bufio.NewScanner(bytes.NewReader([]byte(tt.input)))
scanner.Split(SplitOnPlus)
var result []string
for scanner.Scan() {
result = append(result, scanner.Text())
}
if err := scanner.Err(); err != nil {
t.Errorf("Scanner error: %v", err)
}
if !reflect.DeepEqual(result, tt.expected) {
t.Errorf("Expected %q, got %q", tt.expected, result)
}
})
}
}