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
|
package textutils
import (
"bytes"
"testing"
)
func TestSurroundingSpaces(t *testing.T) {
testCases := []struct {
desc string
input []byte
expectedLeft []byte
expectedTrimmed []byte
expectedRight []byte
}{
{
desc: "empty string",
input: []byte(""),
expectedLeft: []byte(""),
expectedTrimmed: []byte(""),
expectedRight: []byte(""),
},
{
desc: "one space",
input: []byte(" "),
expectedLeft: []byte(""),
expectedTrimmed: []byte(""),
expectedRight: []byte(" "),
},
{
desc: "simple string",
input: []byte("some text"),
expectedLeft: []byte(""),
expectedTrimmed: []byte("some text"),
expectedRight: []byte(""),
},
{
desc: "spaces around",
input: []byte(" text "),
expectedLeft: []byte(" "),
expectedTrimmed: []byte("text"),
expectedRight: []byte(" "),
},
{
desc: "newlines around",
input: []byte("\n\n text \n\n"),
expectedLeft: []byte("\n\n "),
expectedTrimmed: []byte("text"),
expectedRight: []byte(" \n\n"),
},
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
leftExtra, trimmed, rightExtra := SurroundingSpaces(tC.input)
if !bytes.Equal(leftExtra, tC.expectedLeft) {
t.Errorf("expected %q but got %q for the left extra", string(tC.expectedLeft), string(leftExtra))
}
if !bytes.Equal(trimmed, tC.expectedTrimmed) {
t.Errorf("expected %q but got %q for the trimmed text", string(tC.expectedTrimmed), string(trimmed))
}
if !bytes.Equal(rightExtra, tC.expectedRight) {
t.Errorf("expected %q but got %q for the right extra", string(tC.expectedRight), string(rightExtra))
}
})
}
}
|