예제 #1
0
파일: trace_test.go 프로젝트: tsandall/opa
func TestEventEqual(t *testing.T) {

	a := ast.NewValueMap()
	a.Put(ast.String("foo"), ast.Number("1"))
	b := ast.NewValueMap()
	b.Put(ast.String("foo"), ast.Number("2"))

	tests := []struct {
		a     *Event
		b     *Event
		equal bool
	}{
		{&Event{}, &Event{}, true},
		{&Event{Op: EvalOp}, &Event{Op: EnterOp}, false},
		{&Event{QueryID: 1}, &Event{QueryID: 2}, false},
		{&Event{ParentID: 1}, &Event{ParentID: 2}, false},
		{&Event{Node: ast.MustParseBody("true")}, &Event{Node: ast.MustParseBody("false")}, false},
		{&Event{Node: ast.MustParseBody("true")[0]}, &Event{Node: ast.MustParseBody("false")[0]}, false},
		{&Event{Node: ast.MustParseRule("p :- true")}, &Event{Node: ast.MustParseRule("p :- false")}, false},
		{&Event{Node: "foo"}, &Event{Node: "foo"}, false}, // test some unsupported node type
	}

	for _, tc := range tests {
		if tc.a.Equal(tc.b) != tc.equal {
			var s string
			if tc.equal {
				s = "=="
			} else {
				s = "!="
			}
			t.Errorf("Expected %v %v %v", tc.a, s, tc.b)
		}
	}

}
예제 #2
0
func TestTruth(t *testing.T) {

	module := `
    package test
	p :- q[x], r[x]
	q[x] :- a = [1,2,3,4], x = a[_]
	r[z] :- z = 3
	r[a] :- a = 4
    `

	q := ast.MustParseRule(`q[x] :- a = [1,2,3,4], x = a[_]`)
	ra := ast.MustParseRule(`r[a] :- a = 4`)

	runTruthTestCase(t, "", module, 14, map[int]*topdown.Event{
		6: &topdown.Event{
			Op:       topdown.RedoOp,
			Node:     parseExpr("x = a[_]", 1),
			QueryID:  4,
			ParentID: 3,
		},
		7: &topdown.Event{
			Op:       topdown.ExitOp,
			Node:     q,
			QueryID:  4,
			ParentID: 3,
			Locals:   parseBindings(`{x: 4}`),
		},
		9: &topdown.Event{
			Op:       topdown.RedoOp,
			Node:     ra,
			QueryID:  12,
			ParentID: 3,
			Locals:   parseBindings(`{a: 4}`),
		},
	})
}