Files
releases/v1/topdown/test.go
T
Johan Fylling a6fcabbba6 cmd+tester: Parameterized tests (#7366)
Adding the ability to parameterize Rego `test_*` rules, effectively declaring multiple "test cases" within the rule, which is then individually reported (grouped under their parent rule) when running `opa test`.

Fixes: #2176

Signed-off-by: Johan Fylling <johan.dev@fylling.se>
2025-02-17 22:08:27 +01:00

31 lines
738 B
Go

// Copyright 2025 The OPA Authors. All rights reserved.
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.
package topdown
import "github.com/open-policy-agent/opa/v1/ast"
const TestCaseOp Op = "TestCase"
func builtinTestCase(bctx BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
e := &Event{
Op: TestCaseOp,
QueryID: bctx.QueryID,
Node: ast.NewExpr([]*ast.Term{
ast.NewTerm(ast.InternalTestCase.Ref()),
ast.NewTerm(operands[0].Value),
}),
}
for _, tracer := range bctx.QueryTracers {
tracer.TraceEvent(*e)
}
return iter(ast.BooleanTerm(true))
}
func init() {
RegisterBuiltinFunc(ast.InternalTestCase.Name, builtinTestCase)
}