2018-08-19 13:20:50 +02:00
|
|
|
package utils
|
|
|
|
|
2018-08-19 08:51:37 +02:00
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSplitLines(t *testing.T) {
|
|
|
|
type scenario struct {
|
|
|
|
multilineString string
|
|
|
|
expected []string
|
|
|
|
}
|
|
|
|
|
|
|
|
scenarios := []scenario{
|
|
|
|
{
|
|
|
|
"",
|
|
|
|
[]string{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"\n",
|
|
|
|
[]string{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"hello world !\nhello universe !\n",
|
|
|
|
[]string{
|
|
|
|
"hello world !",
|
|
|
|
"hello universe !",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range scenarios {
|
|
|
|
assert.EqualValues(t, s.expected, SplitLines(s.multilineString))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWithPadding(t *testing.T) {
|
|
|
|
type scenario struct {
|
|
|
|
str string
|
|
|
|
padding int
|
|
|
|
expected string
|
|
|
|
}
|
|
|
|
|
|
|
|
scenarios := []scenario{
|
|
|
|
{
|
|
|
|
"hello world !",
|
|
|
|
1,
|
|
|
|
"hello world !",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"hello world !",
|
|
|
|
14,
|
|
|
|
"hello world ! ",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range scenarios {
|
|
|
|
assert.EqualValues(t, s.expected, WithPadding(s.str, s.padding))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTrimTrailingNewline(t *testing.T) {
|
|
|
|
type scenario struct {
|
|
|
|
str string
|
|
|
|
expected string
|
|
|
|
}
|
|
|
|
|
|
|
|
scenarios := []scenario{
|
|
|
|
{
|
|
|
|
"hello world !\n",
|
|
|
|
"hello world !",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"hello world !",
|
|
|
|
"hello world !",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range scenarios {
|
|
|
|
assert.EqualValues(t, s.expected, TrimTrailingNewline(s.str))
|
|
|
|
}
|
|
|
|
}
|
2018-08-19 13:20:50 +02:00
|
|
|
|
|
|
|
var testCases = []struct {
|
2018-08-19 14:48:03 +02:00
|
|
|
Input []byte
|
2018-08-19 13:20:50 +02:00
|
|
|
Expected []byte
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
// \r\n
|
2018-08-19 14:48:03 +02:00
|
|
|
Input: []byte{97, 115, 100, 102, 13, 10},
|
|
|
|
Expected: []byte{97, 115, 100, 102},
|
2018-08-19 13:20:50 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
// \r
|
2018-08-19 14:48:03 +02:00
|
|
|
Input: []byte{97, 115, 100, 102, 13},
|
|
|
|
Expected: []byte{97, 115, 100, 102},
|
2018-08-19 13:20:50 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
// \n
|
2018-08-19 14:48:03 +02:00
|
|
|
Input: []byte{97, 115, 100, 102, 10},
|
2018-08-19 13:20:50 +02:00
|
|
|
Expected: []byte{97, 115, 100, 102, 10},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNormalizeLinefeeds(t *testing.T) {
|
|
|
|
for _, tc := range testCases {
|
2018-08-19 14:48:03 +02:00
|
|
|
input := NormalizeLinefeeds(string(tc.Input))
|
|
|
|
expected := string(tc.Expected)
|
|
|
|
if input != expected {
|
|
|
|
t.Error("Expected " + expected + ", got " + input)
|
2018-08-19 13:20:50 +02:00
|
|
|
}
|
|
|
|
}
|