Improve coverage of ground-ness checks

This commit is contained in:
Torin Sandall
2016-06-08 17:06:38 -07:00
parent d25f2a4c30
commit fe37cc03a8
2 changed files with 42 additions and 0 deletions
+6
View File
@@ -133,6 +133,12 @@ func TestExprEquals(t *testing.T) {
assertExprNotEqual(t, expr20, expr23)
}
func TestBodyIsGround(t *testing.T) {
if MustParseBody(`a.b[0] = 1, a = [1,2,x]`).IsGround() {
t.Errorf("Expected body to be non-ground")
}
}
func TestExprOutputVars(t *testing.T) {
body := MustParseBody(`{"a": [{x: y}, b[z]]} = c[i], [{"a": d[j][k]}] != xs`)
one := body[0]
+36
View File
@@ -181,6 +181,42 @@ func TestHash(t *testing.T) {
}
}
func TestTermIsGround(t *testing.T) {
tests := []struct {
note string
term string
expected bool
}{
{"null", "null", true},
{"string", `"foo"`, true},
{"number", "42.1", true},
{"boolean", "false", true},
{"var", "x", false},
{"ref ground", "a.b[0]", true},
{"ref non-ground", "a.b[i].x", false},
{"array ground", "[1,2,3]", true},
{"array non-ground", "[1,2,x]", false},
{"object ground", `{"a": 1}`, true},
{"object non-ground key", `{"x": 1, y: 2}`, false},
{"object non-ground value", `{"x": 1, "y": y}`, false},
{"array compr ground", `["a" | true]`, true},
{"array compr non-ground", `[x | x = a[i]]`, false},
}
for i, tc := range tests {
term := MustParseTerm(tc.term)
if term.IsGround() != tc.expected {
expected := "ground"
if !tc.expected {
expected = "non-ground"
}
t.Errorf("Expected term %v to be %s (test case %d: %v)", term, expected, i, tc.note)
}
}
}
func TestTermString(t *testing.T) {
assertToString(t, Null{}, "null")
assertToString(t, Boolean(true), "true")