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
|
package textutils
import (
"strings"
"testing"
)
func TestEscapeMultiLine(t *testing.T) {
var tests = []struct {
Name string
Text string
Expected string
}{
{
Name: "empty",
Text: "",
Expected: "",
},
{
Name: "not needed",
Text: "some longer text that is on one line",
Expected: "some longer text that is on one line",
},
{
Name: "one newline",
Text: "A\nB",
Expected: "A \nB",
},
{
Name: "two newlines",
Text: "A\n\nB",
Expected: "A \n\\\nB",
},
{
Name: "many newlines",
// Will be max two newlines characters
Text: "line 1\n\n\n\nline 2",
Expected: "line 1 \n\\\nline 2",
},
{
Name: "multiple empty lines",
Text: `line1
line2
line3
line4`,
Expected: `line1
line2
\
line3
\
line4`,
},
{
Name: "empty line with a space",
Text: "line 1\n \nline 2",
Expected: "line 1 \n\\\nline 2",
},
{
Name: "content has a space",
Text: "a\n\n b",
Expected: "a \n\\\nb",
},
{
Name: "content is indented",
Text: "line 1\n line 2\n\tline 3",
Expected: "line 1 \nline 2 \nline 3",
},
// TODO: keep existing "\" characters?
}
for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
input := TrimConsecutiveNewlines([]byte(test.Text))
output := EscapeMultiLine(input)
if string(output) != test.Expected {
t.Errorf("expected '%s' but got '%s'", test.Expected, string(output))
}
})
}
}
func BenchmarkEscapeMultiLine(b *testing.B) {
b.Run("new", func(b *testing.B) {
input := []byte(strings.Repeat("line 1\n\n \nline 2", 100))
for i := 0; i < b.N; i++ {
_ = EscapeMultiLine(input)
}
})
}
|