/
sitnikovik
/
stringo
Обзор
Документация
Войти
/
sitnikovik
/
stringo
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
common_test.go
208 строк
4 KB
K.S.
feat: add Between string helper with tests (#13)
11 фев 2026, 07:10
Не верифицирован
11 фев 2026, 07:10
4d12392
Код
Авторство
О чём код?
package stringo import ( "testing" "unicode" "github.com/stretchr/testify/require" ) func TestSplitFunc(t *testing.T) { t.Parallel() type args struct { s string cond func(rune, int) bool incSep bool } tests := []struct { name string args args want []string }{ { name: "nil func provied", args: args{ s: "hello world", cond: nil, }, want: []string{"hello world"}, }, { name: "split by space", args: args{ s: "hello world", cond: func(r rune, i int) bool { return unicode.IsSpace(r) }, }, want: []string{"hello", "world"}, }, { name: "split camelCase", args: args{ s: "helloWorldItWorks", cond: func(r rune, i int) bool { return unicode.IsUpper(r) }, incSep: true, }, want: []string{"hello", "World", "It", "Works"}, }, { name: "split camelCase in string with spaces", args: args{ s: "helloWorld itWorks ", cond: func(r rune, i int) bool { return unicode.IsUpper(r) || unicode.IsSpace(r) }, incSep: true, }, want: []string{"hello", "World", " it", "Works", " "}, }, { name: "split camelCase string but only with space and include separator", args: args{ s: "helloWorld itWorks ", cond: func(r rune, i int) bool { return unicode.IsSpace(r) }, incSep: true, }, want: []string{"helloWorld", " itWorks", " "}, }, } for _, tt := range tests { tt := tt t.Run(tt.name, func(t *testing.T) { t.Parallel() got := SplitFunc(tt.args.s, tt.args.cond, tt.args.incSep) require.Equal(t, tt.want, got) }) } } func TestBetween(t *testing.T) { t.Parallel() type args struct { s string left string right string } tests := []struct { name string args args want string }{ { name: "basic case", args: args{ s: "hello [world]!", left: "[", right: "]", }, want: "world", }, { name: "left delimiter missing", args: args{ s: "hello world", left: "[", right: "]", }, want: "", }, { name: "right delimiter missing", args: args{ s: "hello [world", left: "[", right: "]", }, want: "", }, { name: "multiple occurrences - takes first", args: args{ s: "a[1]b[2]", left: "[", right: "]", }, want: "1", }, { name: "mixed delimiters", args: args{ s: "idx: 123, val: 456", left: "idx: ", right: ",", }, want: "123", }, { name: "nested delimiters (native scan)", args: args{ s: "a[b[c]d]", left: "[", right: "]", }, want: "b[c", }, { name: "empty result", args: args{ s: "[]", left: "[", right: "]", }, want: "", }, { name: "default args", args: args{ s: "", left: "", right: "", }, want: "", }, { name: "from [ to end", args: args{ s: "hello [world]", left: "[", right: "", }, want: "world]", }, { name: "from start to ]", args: args{ s: "hello [world]", left: "", right: "]", }, want: "hello [world", }, { name: "empty delimiters return full string", args: args{ s: "some text", left: "", right: "", }, want: "some text", }, { name: "same delimiters", args: args{ s: "|content|", left: "|", right: "|", }, want: "content", }, } for _, tt := range tests { tt := tt t.Run(tt.name, func(t *testing.T) { t.Parallel() got := Between(tt.args.s, tt.args.left, tt.args.right) require.Equal(t, tt.want, got) }) } }