alrm/config/tokenizer_test.go

84 lines
1.6 KiB
Go
Raw Normal View History

package config
import (
"encoding/json"
2021-01-18 07:29:38 -09:00
"testing"
)
2021-01-18 07:29:38 -09:00
func TestSimpleSpaces(t *testing.T) {
runTest(t, "simple-spaces",
`[["one","two","three","four","five","six"]]`,
)
2021-01-18 07:29:38 -09:00
}
func TestSimpleMultiline(t *testing.T) {
runTest(t, "simple-multiline",
`[["one","two","three"],["four","five"],[],[],["six"]]`,
)
2021-01-18 07:29:38 -09:00
}
func TestQuotes(t *testing.T) {
runTest(t, "quotes",
`[["one","two","three"],[],["four","five","six"]]`,
)
}
func TestQuotesMultiline(t *testing.T) {
runTest(t, "quotes-multiline",
`[["one\ntwo"],["three\nfour"],[],[],["five\n six"]]`,
)
}
2021-01-18 07:40:56 -09:00
func TestQuotesEmpty(t *testing.T) {
runTest(t, "quotes-empty",
`[["one","","three"],["","five",""],["seven"]]`,
)
}
2021-01-18 07:29:38 -09:00
func TestComments(t *testing.T) {
runTest(t, "comments",
2021-01-18 07:29:38 -09:00
`[[],["one"],[],["two"],[],["three"]]`,
)
2021-01-18 07:29:38 -09:00
}
func TestCommentsInline(t *testing.T) {
runTest(t, "comments-inline",
`[["one"],["two#three"],[],["four"]]`,
)
}
func runTest(t *testing.T, bn string, exp string) {
2021-01-18 07:29:38 -09:00
t.Logf("Running testdata/%s.tok.. ", bn)
tok, err := NewTokenizer("testdata/" + bn + ".tok")
if err != nil {
t.Fatalf("%s", err.Error())
}
defer tok.Close()
tokens := [][]string{}
for tok.Scan() {
ln := tok.Line()
tl := len(tokens)
if tl < ln {
for i := tl; i < ln; i++ {
tokens = append(tokens, []string{})
}
}
tokens[ln-1] = append(tokens[ln-1], tok.Text())
}
if tok.Err() != nil {
t.Fatalf("%s", tok.Err())
}
out, err := json.Marshal(tokens)
if err != nil {
t.Fatalf("%s", err)
}
if exp != string(out) {
t.Logf("Expected: %s", exp)
2021-01-18 07:29:38 -09:00
t.Logf("Got: %s", out)
t.FailNow()
}
}