123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482 |
- package stringx
- import (
- "path"
- "testing"
- "github.com/stretchr/testify/assert"
- )
- func TestNotEmpty(t *testing.T) {
- cases := []struct {
- args []string
- expect bool
- }{
- {
- args: []string{"a", "b", "c"},
- expect: true,
- },
- {
- args: []string{"a", "", "c"},
- expect: false,
- },
- {
- args: []string{"a"},
- expect: true,
- },
- {
- args: []string{""},
- expect: false,
- },
- {
- args: []string{},
- expect: true,
- },
- }
- for _, each := range cases {
- t.Run(path.Join(each.args...), func(t *testing.T) {
- assert.Equal(t, each.expect, NotEmpty(each.args...))
- })
- }
- }
- func TestContainsString(t *testing.T) {
- cases := []struct {
- slice []string
- value string
- expect bool
- }{
- {[]string{"1"}, "1", true},
- {[]string{"1"}, "2", false},
- {[]string{"1", "2"}, "1", true},
- {[]string{"1", "2"}, "3", false},
- {nil, "3", false},
- {nil, "", false},
- }
- for _, each := range cases {
- t.Run(path.Join(each.slice...), func(t *testing.T) {
- actual := Contains(each.slice, each.value)
- assert.Equal(t, each.expect, actual)
- })
- }
- }
- func TestFilter(t *testing.T) {
- cases := []struct {
- input string
- ignores []rune
- expect string
- }{
- {``, nil, ``},
- {`abcd`, nil, `abcd`},
- {`ab,cd,ef`, []rune{','}, `abcdef`},
- {`ab, cd,ef`, []rune{',', ' '}, `abcdef`},
- {`ab, cd, ef`, []rune{',', ' '}, `abcdef`},
- {`ab, cd, ef, `, []rune{',', ' '}, `abcdef`},
- }
- for _, each := range cases {
- t.Run(each.input, func(t *testing.T) {
- actual := Filter(each.input, func(r rune) bool {
- for _, x := range each.ignores {
- if x == r {
- return true
- }
- }
- return false
- })
- assert.Equal(t, each.expect, actual)
- })
- }
- }
- func TestFirstN(t *testing.T) {
- tests := []struct {
- name string
- input string
- n int
- ellipsis string
- expect string
- }{
- {
- name: "english string",
- input: "anything that we use",
- n: 8,
- expect: "anything",
- },
- {
- name: "english string with ellipsis",
- input: "anything that we use",
- n: 8,
- ellipsis: "...",
- expect: "anything...",
- },
- {
- name: "english string more",
- input: "anything that we use",
- n: 80,
- expect: "anything that we use",
- },
- {
- name: "chinese string",
- input: "我是中国人",
- n: 2,
- expect: "我是",
- },
- {
- name: "chinese string with ellipsis",
- input: "我是中国人",
- n: 2,
- ellipsis: "...",
- expect: "我是...",
- },
- {
- name: "chinese string",
- input: "我是中国人",
- n: 10,
- expect: "我是中国人",
- },
- }
- for _, test := range tests {
- t.Run(test.name, func(t *testing.T) {
- assert.Equal(t, test.expect, FirstN(test.input, test.n, test.ellipsis))
- })
- }
- }
- func TestJoin(t *testing.T) {
- tests := []struct {
- name string
- input []string
- expect string
- }{
- {
- name: "all blanks",
- input: []string{"", ""},
- expect: "",
- },
- {
- name: "two values",
- input: []string{"012", "abc"},
- expect: "012.abc",
- },
- {
- name: "last blank",
- input: []string{"abc", ""},
- expect: "abc",
- },
- {
- name: "first blank",
- input: []string{"", "abc"},
- expect: "abc",
- },
- }
- for _, test := range tests {
- test := test
- t.Run(test.name, func(t *testing.T) {
- assert.Equal(t, test.expect, Join('.', test.input...))
- })
- }
- }
- func TestRemove(t *testing.T) {
- cases := []struct {
- input []string
- remove []string
- expect []string
- }{
- {
- input: []string{"a", "b", "a", "c"},
- remove: []string{"a", "b"},
- expect: []string{"c"},
- },
- {
- input: []string{"b", "c"},
- remove: []string{"a"},
- expect: []string{"b", "c"},
- },
- {
- input: []string{"b", "a", "c"},
- remove: []string{"a"},
- expect: []string{"b", "c"},
- },
- {
- input: []string{},
- remove: []string{"a"},
- expect: []string{},
- },
- }
- for _, each := range cases {
- t.Run(path.Join(each.input...), func(t *testing.T) {
- assert.ElementsMatch(t, each.expect, Remove(each.input, each.remove...))
- })
- }
- }
- func TestReverse(t *testing.T) {
- cases := []struct {
- input string
- expect string
- }{
- {
- input: "abcd",
- expect: "dcba",
- },
- {
- input: "",
- expect: "",
- },
- {
- input: "我爱中国",
- expect: "国中爱我",
- },
- }
- for _, each := range cases {
- t.Run(each.input, func(t *testing.T) {
- assert.Equal(t, each.expect, Reverse(each.input))
- })
- }
- }
- func TestSubstr(t *testing.T) {
- cases := []struct {
- input string
- start int
- stop int
- err error
- expect string
- }{
- {
- input: "abcdefg",
- start: 1,
- stop: 4,
- expect: "bcd",
- },
- {
- input: "我爱中国3000遍,even more",
- start: 1,
- stop: 9,
- expect: "爱中国3000遍",
- },
- {
- input: "abcdefg",
- start: -1,
- stop: 4,
- err: ErrInvalidStartPosition,
- expect: "",
- },
- {
- input: "abcdefg",
- start: 100,
- stop: 4,
- err: ErrInvalidStartPosition,
- expect: "",
- },
- {
- input: "abcdefg",
- start: 1,
- stop: -1,
- err: ErrInvalidStopPosition,
- expect: "",
- },
- {
- input: "abcdefg",
- start: 1,
- stop: 100,
- err: ErrInvalidStopPosition,
- expect: "",
- },
- }
- for _, each := range cases {
- t.Run(each.input, func(t *testing.T) {
- val, err := Substr(each.input, each.start, each.stop)
- assert.Equal(t, each.err, err)
- if err == nil {
- assert.Equal(t, each.expect, val)
- }
- })
- }
- }
- func TestTakeOne(t *testing.T) {
- cases := []struct {
- valid string
- or string
- expect string
- }{
- {"", "", ""},
- {"", "1", "1"},
- {"1", "", "1"},
- {"1", "2", "1"},
- }
- for _, each := range cases {
- t.Run(each.valid, func(t *testing.T) {
- actual := TakeOne(each.valid, each.or)
- assert.Equal(t, each.expect, actual)
- })
- }
- }
- func TestTakeWithPriority(t *testing.T) {
- tests := []struct {
- fns []func() string
- expect string
- }{
- {
- fns: []func() string{
- func() string {
- return "first"
- },
- func() string {
- return "second"
- },
- func() string {
- return "third"
- },
- },
- expect: "first",
- },
- {
- fns: []func() string{
- func() string {
- return ""
- },
- func() string {
- return "second"
- },
- func() string {
- return "third"
- },
- },
- expect: "second",
- },
- {
- fns: []func() string{
- func() string {
- return ""
- },
- func() string {
- return ""
- },
- func() string {
- return "third"
- },
- },
- expect: "third",
- },
- {
- fns: []func() string{
- func() string {
- return ""
- },
- func() string {
- return ""
- },
- func() string {
- return ""
- },
- },
- expect: "",
- },
- }
- for _, test := range tests {
- t.Run(RandId(), func(t *testing.T) {
- val := TakeWithPriority(test.fns...)
- assert.Equal(t, test.expect, val)
- })
- }
- }
- func TestToCamelCase(t *testing.T) {
- tests := []struct {
- input string
- expect string
- }{
- {
- input: "",
- expect: "",
- },
- {
- input: "A",
- expect: "a",
- },
- {
- input: "a",
- expect: "a",
- },
- {
- input: "hello_world",
- expect: "hello_world",
- },
- {
- input: "Hello_world",
- expect: "hello_world",
- },
- {
- input: "hello_World",
- expect: "hello_World",
- },
- {
- input: "helloWorld",
- expect: "helloWorld",
- },
- {
- input: "HelloWorld",
- expect: "helloWorld",
- },
- {
- input: "hello World",
- expect: "hello World",
- },
- {
- input: "Hello World",
- expect: "hello World",
- },
- }
- for _, test := range tests {
- test := test
- t.Run(test.input, func(t *testing.T) {
- assert.Equal(t, test.expect, ToCamelCase(test.input))
- })
- }
- }
- func TestUnion(t *testing.T) {
- first := []string{
- "one",
- "two",
- "three",
- }
- second := []string{
- "zero",
- "two",
- "three",
- "four",
- }
- union := Union(first, second)
- contains := func(v string) bool {
- for _, each := range union {
- if v == each {
- return true
- }
- }
- return false
- }
- assert.Equal(t, 5, len(union))
- assert.True(t, contains("zero"))
- assert.True(t, contains("one"))
- assert.True(t, contains("two"))
- assert.True(t, contains("three"))
- assert.True(t, contains("four"))
- }
|