2023-08-21 00:26:22 +01:00
|
|
|
package comma_list_scanner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2023-08-22 13:38:01 +01:00
|
|
|
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 {
|
2023-08-22 13:38:01 +01:00
|
|
|
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() {
|
2023-08-22 13:38:01 +01:00
|
|
|
a = append(a, s.Text())
|
2023-08-21 00:26:22 +01:00
|
|
|
}
|
2023-08-22 13:38:01 +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
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|