2021-01-17 16:41:28 -09:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2021-01-18 07:29:38 -09:00
|
|
|
"testing"
|
2021-01-17 16:41:28 -09:00
|
|
|
)
|
|
|
|
|
2021-01-18 07:29:38 -09:00
|
|
|
func TestSimpleSpaces(t *testing.T) {
|
|
|
|
runTest(t, "simple-spaces",
|
2021-01-17 16:41:28 -09:00
|
|
|
`[["one","two","three","four","five","six"]]`,
|
|
|
|
)
|
2021-01-18 07:29:38 -09:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSimpleMultiline(t *testing.T) {
|
|
|
|
runTest(t, "simple-multiline",
|
2021-01-17 16:41:28 -09:00
|
|
|
`[["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"]]`,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestComments(t *testing.T) {
|
2021-01-17 16:41:28 -09:00
|
|
|
runTest(t, "comments",
|
2021-01-18 07:29:38 -09:00
|
|
|
`[[],["one"],[],["two"],[],["three"]]`,
|
2021-01-17 16:41:28 -09:00
|
|
|
)
|
2021-01-18 07:29:38 -09:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommentsInline(t *testing.T) {
|
|
|
|
runTest(t, "comments-inline",
|
|
|
|
`[["one"],["two#three"],[],["four"]]`,
|
2021-01-17 16:41:28 -09:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func runTest(t *testing.T, bn string, exp string) {
|
2021-01-18 07:29:38 -09:00
|
|
|
t.Logf("Running testdata/%s.tok.. ", bn)
|
2021-01-17 16:41:28 -09:00
|
|
|
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()
|
2021-01-17 16:41:28 -09:00
|
|
|
}
|
|
|
|
}
|