1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
package strings
import (
"testing"
"github.com/stretchr/testify/assert"
)
type containsTest struct {
input []string
match string
found bool
}
var containsTests = map[string]containsTest{
"Found": {
input: []string{"a", "b", "c"},
match: "b",
found: true,
},
"NotFound": {
input: []string{"a", "b", "c"},
match: "d",
found: false,
},
"OneElement": {
input: []string{"a"},
match: "a",
found: true,
},
"EmptyArray": {
input: []string{},
match: "a",
found: false,
},
"EmptyArrayEmptySearch": {
input: []string{},
match: "",
found: false,
},
}
func TestContains(t *testing.T) {
t.Run("Contains", func(t *testing.T) {
for name, test := range containsTests {
t.Run(name, func(t *testing.T) {
found := Contains(test.input, test.match)
assert.Equal(t, test.found, found)
})
}
})
}
type splitLinesTest struct {
input string
splitOutput []string
}
var splitLinesTests = map[string]splitLinesTest{
"ThreeLines": {
input: "line1\nline2\nline3\n",
splitOutput: []string{"line1", "line2", "line3"},
},
"ThreeLinesNoFinalEOL": {
input: "line1\nline2\nline3",
splitOutput: []string{"line1", "line2", "line3"},
},
"WindowsEOL": {
input: "line1\r\nline2\r\nline3\r\n",
splitOutput: []string{"line1", "line2", "line3"},
},
"EmptyString": {
input: "",
splitOutput: []string{},
},
"EOLOnly": {
input: "\n",
splitOutput: []string{""},
},
"NoEOL": {
input: "line1",
splitOutput: []string{"line1"},
},
}
func TestSplitLines(t *testing.T) {
t.Run("SplitLines", func(t *testing.T) {
for name, test := range splitLinesTests {
t.Run(name, func(t *testing.T) {
output := SplitLines(test.input)
assert.Equal(t, test.splitOutput, output)
})
}
})
}
type firstLineTest struct {
input string
firstLine string
}
var firstLineTests = map[string]firstLineTest{
"ThreeLines": {
input: "line1\nline2\nline3\n",
firstLine: "line1",
},
"ThreeLinesNoFinalEOL": {
input: "line1\nline2\nline3",
firstLine: "line1",
},
"WindowsEOL": {
input: "line1\r\nline2\r\nline3\r\n",
firstLine: "line1",
},
"EmptyString": {
input: "",
firstLine: "",
},
"EOLOnly": {
input: "\n",
firstLine: "",
},
"NoEOL": {
input: "line1",
firstLine: "line1",
},
}
func TestFirstLine(t *testing.T) {
t.Run("FirstLine", func(t *testing.T) {
for name, test := range firstLineTests {
t.Run(name, func(t *testing.T) {
firstLine := FirstLine(test.input)
assert.Equal(t, test.firstLine, firstLine)
})
}
})
}
func TestTrimTrailingEOL(t *testing.T) {
assert.Equal(t, "foo\nbar", TrimTrailingEOL("foo\nbar\n"))
assert.Equal(t, "foo", TrimTrailingEOL("foo\n"))
assert.Equal(t, "foo", TrimTrailingEOL("foo\r\n"))
assert.Equal(t, "foo\r\nbar", TrimTrailingEOL("foo\r\nbar\r\n"))
assert.Equal(t, "foo\r\nbar", TrimTrailingEOL("foo\r\nbar\r\n\r\n"))
assert.Equal(t, "foo\nbar", TrimTrailingEOL("foo\nbar\n\n"))
assert.Equal(t, "foo\nbar", TrimTrailingEOL("foo\nbar\n\n\n"))
assert.Equal(t, "", TrimTrailingEOL("\r\n"))
assert.Equal(t, "", TrimTrailingEOL("\n"))
assert.Equal(t, "", TrimTrailingEOL(""))
}
|