lotus/postfix-config/comma-list-scanner/comma-list-scanner_test.go

46 lines
962 B
Go
Raw Normal View History

2023-08-21 00:26:22 +01:00
package comma_list_scanner
import (
"github.com/stretchr/testify/assert"
"strings"
"testing"
)
var testCommaList = []string{
"hello, wow-this-is-cool, amazing",
"hello, wow-this-is-cool",
"hello, wow-this-is-cool, ",
"hello, wow-this-is-cool,",
",hello, wow-this-is-cool",
",hello, wow-this-is-cool,",
"hello, wow-this-is-cool,,,",
2023-08-21 00:26:22 +01:00
}
func TestNewCommaListScanner(t *testing.T) {
for _, i := range testCommaList {
t.Run(i, func(t *testing.T) {
// use comma list scanner
s := NewCommaListScanner(strings.NewReader(i))
n := strings.Count(i, ",")
a := make([]string, 0, n+1)
2023-08-21 00:26:22 +01:00
for s.Scan() {
a = append(a, s.Text())
2023-08-21 00:26:22 +01:00
}
assert.NoError(t, s.Err())
// test against splitting and trimming strings
b := strings.Split(i, ",")
for i := 0; i < len(b); i++ {
c := strings.TrimSpace(b[i])
if c == "" {
b = append(b[0:i], b[i+1:]...)
i--
} else {
b[i] = c
}
}
assert.Equal(t, b, a)
2023-08-21 00:26:22 +01:00
})
}
}