Test internal/grammar, and allow the root to import it
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
package grammar
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestParseCalcPlacesEachOperandAtItsFirstReading(t *testing.T) {
|
||||
n, err := ParseCalc("b * (a - b) / -2")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := CalcBin{'/', CalcBin{'*', CalcVar{"b", 0}, CalcBin{'-', CalcVar{"a", 1}, CalcVar{"b", 0}}}, CalcNeg{CalcNum(2)}}
|
||||
if !reflect.DeepEqual(n, want) {
|
||||
t.Errorf("ParseCalc = %#v, want %#v", n, want)
|
||||
}
|
||||
if got := CalcVars(n); !reflect.DeepEqual(got, []string{"b", "a"}) {
|
||||
t.Errorf("CalcVars = %q, want [b a]", got)
|
||||
}
|
||||
if got := CalcText(n); got != "((b * (a - b)) / -2)" {
|
||||
t.Errorf("CalcText = %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseCalcRefusesAnUnfinishedExpression(t *testing.T) {
|
||||
for _, expr := range []string{"", " ", "1 +", "(1", "1 2", "a.b", "postal-code *"} {
|
||||
if _, err := ParseCalc(expr); err == nil {
|
||||
t.Errorf("ParseCalc(%q) = nil error, want one", expr)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package grammar
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestSplitArgsQuotesOutsideSelectors pins the arg grammar: a comma splits outside a
|
||||
// selector and outside a quoted layout, and a quote inside a selector is a name's text.
|
||||
func TestSplitArgsQuotesOutsideSelectors(t *testing.T) {
|
||||
for in, want := range map[string][]string{
|
||||
"a, b": {"a", "b"},
|
||||
"1990-01-01,1990-12-31,'January 2, 2006'": {"1990-01-01", "1990-12-31", "'January 2, 2006'"},
|
||||
"/geo.US.locality[O'Fallon].name, 2": {"/geo.US.locality[O'Fallon].name", "2"},
|
||||
"'[a,b]'": {"'[a,b]'"},
|
||||
} {
|
||||
if got := SplitArgs(in); !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("SplitArgs(%q) = %q, want %q", in, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseFormatReadsEachTokenKind(t *testing.T) {
|
||||
toks, err := ParseFormat("a{{{x|/y.z[a.b]}{/p as q}{f(1, n)}", func(fn string, args []string) []string {
|
||||
return args[1:]
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := []Token{
|
||||
{Kind: LiteralRun, Lit: "a{"},
|
||||
{Kind: NameRead, Body: "x|/y.z[a.b]", Names: []string{"x", "/y.z[a.b]"}},
|
||||
{Kind: NameBind, Body: "/p as q", BoundRef: "/p", Bound: "q"},
|
||||
{Kind: BuiltinCall, Body: "f(1, n)", Fn: "f", Args: []string{"1", "n"}, Names: []string{"n"}},
|
||||
}
|
||||
if !reflect.DeepEqual(toks, want) {
|
||||
t.Errorf("ParseFormat = %+v, want %+v", toks, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseFormatRefusesUnbalancedBraces(t *testing.T) {
|
||||
for _, format := range []string{"{a", "a}", "{a{b}}", "{f(}"} {
|
||||
if _, err := ParseFormat(format, nil); err == nil {
|
||||
t.Errorf("ParseFormat(%q) = nil error, want one", format)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package grammar
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestCheckNameRefusesWhatAGrammarReserves(t *testing.T) {
|
||||
for _, name := range []string{"", "-", "a as b", "a.b", "a|b", "a(b", "a{b", "a}b", "a/b", "a[b", "a]b", `a"b`} {
|
||||
if CheckName(name) == nil {
|
||||
t.Errorf("CheckName(%q) = nil, want an error", name)
|
||||
}
|
||||
}
|
||||
for _, name := range []string{"a", "postal-code", "é", "a_b"} {
|
||||
if err := CheckName(name); err != nil {
|
||||
t.Errorf("CheckName(%q) = %v, want nil", name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package grammar
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSplitPathKeepsSelectorsAndStepsUpWhole(t *testing.T) {
|
||||
for path, want := range map[string][]string{
|
||||
"a": {"a"},
|
||||
"a.b": {"a", "b"},
|
||||
"misc.territory[SE].name": {"misc", "territory", "[SE]", "name"},
|
||||
"x[a.b]": {"x", "[a.b]"},
|
||||
"city[Oslo]..country": {"city", "[Oslo]", "..", "country"},
|
||||
"a..b": {"a", "..", "b"},
|
||||
} {
|
||||
segs, err := SplitPath(path)
|
||||
if err != nil || !reflect.DeepEqual(segs, want) {
|
||||
t.Errorf("SplitPath(%q) = %q, %v, want %q", path, segs, err, want)
|
||||
continue
|
||||
}
|
||||
if got := JoinSegments(segs); got != path {
|
||||
t.Errorf("JoinSegments(%q) = %q, want %q", segs, got, path)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSplitPathRefusesAStraySelectorBracket(t *testing.T) {
|
||||
for _, path := range []string{"[a]", "a.[b]", "a[b", "a]", "a[]", "a[b]c", "a[b[c]]"} {
|
||||
if _, err := SplitPath(path); err == nil {
|
||||
t.Errorf("SplitPath(%q) = nil error, want one", path)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNameSegmentsDropSelectorsAndStepsUp(t *testing.T) {
|
||||
if got := NameSegments([]string{"a", "[b]", "..", "c"}); !reflect.DeepEqual(got, []string{"a", "c"}) {
|
||||
t.Errorf("NameSegments = %q, want [a c]", got)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package grammar
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestRefShapeSplitsTheSigil(t *testing.T) {
|
||||
for ref, want := range map[string][2]string{
|
||||
"/a.b": {"/", "a.b"},
|
||||
".a": {".", "a"},
|
||||
"..a.b": {"..", "a.b"},
|
||||
} {
|
||||
sigil, rest, err := RefShape(ref)
|
||||
if err != nil || sigil != want[0] || rest != want[1] {
|
||||
t.Errorf("RefShape(%q) = %q, %q, %v, want %q", ref, sigil, rest, err, want)
|
||||
}
|
||||
}
|
||||
for _, ref := range []string{"/", "//a", ".../a", "/a..b.", "/a.[b]"} {
|
||||
if _, _, err := RefShape(ref); err == nil {
|
||||
t.Errorf("RefShape(%q) = nil error, want one", ref)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user