mirror of
https://github.com/kbwo/testing-language-server.git
synced 2025-07-19 09:15:57 +00:00
40 lines
900 B
Go
40 lines
900 B
Go
package main
|
|
|
|
import (
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestSplit(t *testing.T) {
|
|
tests := map[string]struct {
|
|
input string
|
|
sep string
|
|
want []string
|
|
}{
|
|
"simple": {input: "a/b/c", sep: "/", want: []string{"a", "b", "c"}},
|
|
"wrong sep": {input: "a/b/c", sep: ",", want: []string{"a/b/c"}},
|
|
"no sep": {input: "abc", sep: "/", want: []string{"abc"}},
|
|
"trailing sep": {input: "a/b/c/", sep: "/", want: []string{"a", "b", "c"}},
|
|
}
|
|
|
|
for name, tc := range tests {
|
|
t.Run(name, func(t *testing.T) {
|
|
got := Split(tc.input, tc.sep)
|
|
if !reflect.DeepEqual(tc.want, got) {
|
|
t.Fatalf("%s: expected: %v, got: %v", name, tc.want, got)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func Split(s, sep string) []string {
|
|
var result []string
|
|
i := strings.Index(s, sep)
|
|
for i > -1 {
|
|
result = append(result, s[:i])
|
|
s = s[i+len(sep):]
|
|
i = strings.Index(s, sep)
|
|
}
|
|
return append(result, s)
|
|
}
|