github.com/wynshop-open-source/gomplate@v3.5.0+incompatible/strings/strings_test.go (about) 1 package strings 2 3 import ( 4 "strings" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 ) 9 10 func TestIndent(t *testing.T) { 11 actual := "hello\nworld\n!" 12 expected := " hello\n world\n !" 13 assert.Equal(t, actual, Indent(0, " ", actual)) 14 assert.Equal(t, expected, Indent(1, " ", actual)) 15 assert.Equal(t, "\n", Indent(1, " ", "\n")) 16 assert.Equal(t, " foo\n", Indent(1, " ", "foo\n")) 17 assert.Equal(t, " foo", Indent(1, " ", "foo")) 18 assert.Equal(t, " foo", Indent(3, " ", "foo")) 19 } 20 21 func TestTrunc(t *testing.T) { 22 assert.Equal(t, "", Trunc(5, "")) 23 assert.Equal(t, "", Trunc(0, "hello, world")) 24 assert.Equal(t, "hello", Trunc(5, "hello, world")) 25 assert.Equal(t, "hello, world", Trunc(12, "hello, world")) 26 assert.Equal(t, "hello, world", Trunc(42, "hello, world")) 27 assert.Equal(t, "hello, world", Trunc(-1, "hello, world")) 28 } 29 30 func TestSort(t *testing.T) { 31 in := []string{} 32 expected := []string{} 33 assert.EqualValues(t, expected, Sort(in)) 34 35 in = []string{"c", "a", "b"} 36 expected = []string{"a", "b", "c"} 37 assert.EqualValues(t, expected, Sort(in)) 38 39 in = []string{"42", "45", "18"} 40 expected = []string{"18", "42", "45"} 41 assert.EqualValues(t, expected, Sort(in)) 42 } 43 44 func TestCaseFuncs(t *testing.T) { 45 testdata := []struct{ in, s, k, c string }{ 46 {" Foo bar ", "Foo_bar", "Foo-bar", "FooBar"}, 47 {"foo bar", "foo_bar", "foo-bar", "fooBar"}, 48 {" baz\tqux ", "baz_qux", "baz-qux", "bazQux"}, 49 {"Hello, World!", "Hello_world", "Hello-world", "HelloWorld"}, 50 {"grüne | Straße", "grüne_straße", "grüne-straße", "grüneStraße"}, 51 } 52 for _, d := range testdata { 53 assert.Equal(t, d.s, SnakeCase(d.in)) 54 assert.Equal(t, d.k, KebabCase(d.in)) 55 assert.Equal(t, d.c, CamelCase(d.in)) 56 } 57 } 58 59 func TestWordWrap(t *testing.T) { 60 in := "a short line that needs no wrapping" 61 assert.Equal(t, in, WordWrap(in, WordWrapOpts{Width: 40})) 62 63 in = "a short line that needs wrapping" 64 out := `a short 65 line that 66 needs 67 wrapping` 68 69 assert.Equal(t, out, WordWrap(in, WordWrapOpts{Width: 9})) 70 in = "a short line that needs wrapping" 71 out = `a short \ 72 line that \ 73 needs \ 74 wrapping` 75 assert.Equal(t, out, WordWrap(in, WordWrapOpts{Width: 9, LBSeq: " \\\n"})) 76 77 out = `There shouldn't be any wrapping of long words or URLs because that would break 78 things very badly. To wit: 79 https://example.com/a/super-long/url/that-shouldnt-be?wrapped=for+fear+of#the-breaking-of-functionality 80 should appear on its own line, regardless of the desired word-wrapping width 81 that has been set.` 82 in = strings.Replace(out, "\n", " ", -1) 83 assert.Equal(t, out, WordWrap(in, WordWrapOpts{})) 84 85 // TODO: get these working - need to switch to a word-wrapping package that 86 // can handle multi-byte characters! 87 // 88 // out = `ΤΟΙΣ πᾶσι χρόνος καὶ καιρὸς τῷ παντὶ πράγματι ὑπὸ τὸν οὐρανόν. καιρὸς τοῦ 89 // τεκεῖν καὶ καιρὸς τοῦ ἀποθανεῖν, καιρὸς τοῦ φυτεῦσαι καὶ καιρὸς τοῦ ἐκτῖλαι τὸ 90 // πεφυτευμένον, καιρὸς τοῦ ἀποκτεῖναι καὶ καιρὸς τοῦ ἰάσασθαι, καιρὸς τοῦ 91 // καθελεῖν καὶ καιρὸς τοῦ οἰκοδομεῖν, καιρὸς τοῦ κλαῦσαι καὶ καιρὸς τοῦ γελάσαι, 92 // καιρὸς τοῦ κόψασθαι καὶ καιρὸς τοῦ ὀρχήσασθαι, καιρὸς τοῦ βαλεῖν λίθους καὶ 93 // καιρὸς τοῦ συναγαγεῖν λίθους, καιρὸς τοῦ περιλαβεῖν καὶ καιρὸς τοῦ μακρυνθῆναι 94 // ἀπὸ περιλήψεως, καιρὸς τοῦ ζητῆσαι καὶ καιρὸς τοῦ ἀπολέσαι, καιρὸς τοῦ φυλάξαι 95 // καὶ καιρὸς τοῦ ἐκβαλεῖν, καιρὸς τοῦ ρῆξαι καὶ καιρὸς τοῦ ράψαι, καιρὸς τοῦ 96 // σιγᾶν καὶ καιρὸς τοῦ λαλεῖν, καιρὸς τοῦ φιλῆσαι καὶ καιρὸς τοῦ μισῆσαι, καιρὸς 97 // πολέμου καὶ καιρὸς εἰρήνης.` 98 // in = strings.Replace(out, "\n", " ", -1) 99 // assert.Equal(t, out, WordWrap(in, WordWrapOpts{})) 100 101 // TODO: get these working - need to switch to a word-wrapping package that 102 // understands multi-byte and correctly identifies line-breaking opportunities 103 // for non-latin languages. 104 // 105 // out = `何事にも定まった時があります。 106 // 生まれる時、死ぬ時、植える時、収穫の時、 107 // 殺す時、病気が治る時、壊す時、やり直す時、 108 // 泣く時、笑う時、悲しむ時、踊る時、 109 // 石をばらまく時、石をかき集める時、 110 // 抱きしめる時、抱きしめない時、 111 // 何かを見つける時、物を失う時、 112 // 大切にしまっておく時、遠くに投げ捨てる時、 113 // 引き裂く時、修理する時、黙っている時、口を開く時、 114 // 愛する時、憎む時、戦う時、和解する時。` 115 // in = strings.Replace(out, "\n", " ", -1) 116 // assert.Equal(t, out, WordWrap(in, WordWrapOpts{Width: 100})) 117 }