2021-01-06 20:43:58 +00:00
|
|
|
package strings
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-01-10 15:39:10 +00:00
|
|
|
"github.com/mickael-menu/zk/util/test/assert"
|
2021-01-06 20:43:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestPrepend(t *testing.T) {
|
|
|
|
test := func(text string, prefix string, expected string) {
|
|
|
|
assert.Equal(t, Prepend(text, prefix), expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
test("", "> ", "")
|
|
|
|
test("One line", "> ", "> One line")
|
|
|
|
test("One line\nTwo lines", "> ", "> One line\n> Two lines")
|
|
|
|
test("One line\nTwo lines\nThree lines", "> ", "> One line\n> Two lines\n> Three lines")
|
|
|
|
test("Newline\n", "> ", "> Newline\n")
|
|
|
|
}
|
2021-01-15 21:21:33 +00:00
|
|
|
|
|
|
|
func TestPluralize(t *testing.T) {
|
|
|
|
test := func(word string, count int, expected string) {
|
|
|
|
assert.Equal(t, Pluralize(word, count), expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
test("", 1, "")
|
|
|
|
test("", 2, "")
|
|
|
|
test("word", -2, "words")
|
|
|
|
test("word", -1, "word")
|
|
|
|
test("word", 0, "word")
|
|
|
|
test("word", 1, "word")
|
|
|
|
test("word", 2, "words")
|
|
|
|
test("word", 1000, "words")
|
|
|
|
}
|
2021-01-19 18:56:43 +00:00
|
|
|
|
|
|
|
func TestSplitLines(t *testing.T) {
|
|
|
|
test := func(text string, expected ...string) {
|
|
|
|
assert.Equal(t, SplitLines(text), expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
test("")
|
|
|
|
test("One line", "One line")
|
|
|
|
test("One line\nTwo lines", "One line", "Two lines")
|
|
|
|
test("One line\nTwo lines\n\nThree lines", "One line", "Two lines", "", "Three lines")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJoinLines(t *testing.T) {
|
|
|
|
test := func(text string, expected string) {
|
|
|
|
assert.Equal(t, JoinLines(text), expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
test("", "")
|
|
|
|
test("One line", "One line")
|
|
|
|
test("One line\nTwo lines", "One line Two lines")
|
|
|
|
test("One line\nTwo lines\n\nThree lines", "One line Two lines Three lines")
|
|
|
|
test("One line\nTwo lines\n Three lines", "One line Two lines Three lines")
|
|
|
|
}
|
2021-01-25 20:44:44 +00:00
|
|
|
|
2021-01-25 21:57:12 +00:00
|
|
|
func TestJoinInt64(t *testing.T) {
|
|
|
|
test := func(ints []int64, expected string) {
|
|
|
|
assert.Equal(t, JoinInt64(ints, ","), expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
test([]int64{}, "")
|
|
|
|
test([]int64{1}, "1")
|
|
|
|
test([]int64{1, 2}, "1,2")
|
|
|
|
test([]int64{1, 2, 3}, "1,2,3")
|
|
|
|
}
|
|
|
|
|
2021-01-25 20:44:44 +00:00
|
|
|
func TestIsURL(t *testing.T) {
|
|
|
|
test := func(text string, expected bool) {
|
|
|
|
assert.Equal(t, IsURL(text), expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
test("", false)
|
|
|
|
test("example.com/", false)
|
|
|
|
test("path", false)
|
|
|
|
test("http://example.com", true)
|
|
|
|
test("https://example.com/dir", true)
|
|
|
|
test("http://example.com/dir", true)
|
|
|
|
test("ftp://example.com/", true)
|
|
|
|
}
|
2021-01-31 11:24:33 +00:00
|
|
|
|
|
|
|
func TestRemoveDuplicates(t *testing.T) {
|
|
|
|
test := func(items []string, expected []string) {
|
|
|
|
assert.Equal(t, RemoveDuplicates(items), expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
test([]string{}, []string{})
|
|
|
|
test([]string{"One"}, []string{"One"})
|
|
|
|
test([]string{"One", "Two"}, []string{"One", "Two"})
|
|
|
|
test([]string{"One", "Two", "One"}, []string{"One", "Two"})
|
|
|
|
test([]string{"Two", "One", "Two", "One"}, []string{"Two", "One"})
|
|
|
|
test([]string{"One", "Two", "OneTwo"}, []string{"One", "Two", "OneTwo"})
|
|
|
|
}
|