mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-12 19:32:48 -06:00
Improve coverage of ground-ness checks
This commit is contained in:
@@ -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]
|
||||
|
||||
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user