Test a declared datatype over a column read, repeats counting as written, null alone on stdin, one leading slash, and the CLI's lone reference against the grammar
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/larvit/fejkdata/internal/grammar"
|
||||
)
|
||||
|
||||
// grammarLoneReference is the path a format reads when the grammar parses it as one path
|
||||
// read of one reference from the root, and nothing else.
|
||||
func grammarLoneReference(text string) (string, bool) {
|
||||
toks, err := grammar.ParseFormat(text)
|
||||
if err != nil || len(toks) != 1 || toks[0].Kind != grammar.PathRead || len(toks[0].Arms) != 1 {
|
||||
return "", false
|
||||
}
|
||||
sigil, path, err := grammar.RefShape(toks[0].Arms[0])
|
||||
if err != nil || sigil != "/" {
|
||||
return "", false
|
||||
}
|
||||
return path, true
|
||||
}
|
||||
|
||||
func TestLoneReferenceAgreesWithTheGrammar(t *testing.T) {
|
||||
for _, text := range []string{
|
||||
"{/sv_SE.person}",
|
||||
"{/misc.currency[US Dollar (Next day)]}",
|
||||
"{/t[a as b].c}",
|
||||
"{/t[f(x)]}",
|
||||
"{/a}{/b}",
|
||||
"{/x}}",
|
||||
"{/}",
|
||||
"{/.x}",
|
||||
" {/x}",
|
||||
"{/a|/b}",
|
||||
"{/x as p}",
|
||||
"{//x}",
|
||||
"{/x(1)}",
|
||||
"{.x}",
|
||||
"x",
|
||||
} {
|
||||
path, lone := loneReference(text)
|
||||
want, wantLone := grammarLoneReference(text)
|
||||
if lone != wantLone || path != want {
|
||||
t.Errorf("loneReference(%q) = %q, %v; the grammar reads %q, %v", text, path, lone, want, wantLone)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -249,6 +249,7 @@ func TestRunTakesNoOperand(t *testing.T) {
|
||||
want string
|
||||
}{
|
||||
{[]string{"sv_SE.person"}, "echo '{/sv_SE.person}' | fejkdata"},
|
||||
{[]string{"/en_US.address"}, "echo '{/en_US.address}' | fejkdata"},
|
||||
{[]string{"{/en_US.person}"}, "echo '{/en_US.person}' | fejkdata"},
|
||||
{[]string{"name: {/sv_SE.person.last}"}, "echo 'name: {/sv_SE.person.last}' | fejkdata"},
|
||||
{[]string{"geo.US.locality[O'Fallon].name"}, `echo '{/geo.US.locality[O'\''Fallon].name}' | fejkdata`},
|
||||
@@ -494,6 +495,8 @@ func TestRunTemplateMisuse(t *testing.T) {
|
||||
"42": "number",
|
||||
"42\n": "number",
|
||||
"true\n": "boolean",
|
||||
"null\n": "only null",
|
||||
" null ": "only null",
|
||||
"{//sv_SE.person}": "write {/sv_SE.person}",
|
||||
} {
|
||||
code, out, errb := runOut(stdin, "--seed", "1")
|
||||
|
||||
Reference in New Issue
Block a user