-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplit_test.go
55 lines (50 loc) · 1.63 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
package main
import (
"reflect"
"strings"
"testing"
)
func TestSplitString(t *testing.T) {
tests := []struct {
name string
input string
sep string
expected []string
}{
{"Comma separator", "one,two,three", ",", []string{"one", "two", "three"}},
{"Space separator", "one two three", " ", []string{"one", "two", "three"}},
{"Newline separator", "one\ntwo\nthree", "\n", []string{"one", "two", "three"}},
{"Empty separator", "onetwothree", "", []string{"o", "n", "e", "t", "w", "o", "t", "h", "r", "e", "e"}},
{"No separator match", "onetwothree", ",", []string{"onetwothree"}},
{"Leading and trailing separators", ",one,two,three,", ",", []string{"one", "two", "three"}},
{"Multiple spaces", "one two three", " ", []string{"one", "two", "three"}},
{"Empty string", "", ",", []string{}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := splitString(tt.input, tt.sep)
if !reflect.DeepEqual(result, tt.expected) {
t.Errorf("splitString(%q, %q) = %v; want %v", tt.input, tt.sep, result, tt.expected)
}
})
}
}
func TestFormatSplittedString(t *testing.T) {
tests := []struct {
input []string
expected string
}{
{[]string{"line1", "line2", "line3"}, "line1\nline2\nline3"},
{[]string{"one", "two", "three", "four"}, "one\ntwo\nthree\nfour"},
{[]string{"singleline"}, "singleline"},
{[]string{}, ""},
}
for _, tt := range tests {
t.Run(strings.Join(tt.input, ","), func(t *testing.T) {
result := formatSplittedString(tt.input)
if result != tt.expected {
t.Errorf("formatSplittedString(%v) = %q; want %q", tt.input, result, tt.expected)
}
})
}
}