Skip to content

Commit 41e7392

Browse files
authored
Add test runner option to configure a custom test suite parser (#1189)
* add test runner option to configure a custom test suite parser
1 parent a4b169a commit 41e7392

File tree

5 files changed

+77
-12
lines changed

5 files changed

+77
-12
lines changed

‎tools/celtest/BUILD.bazel‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ go_test(
6767
"//common/types:go_default_library",
6868
"//common/types/ref:go_default_library",
6969
"//policy:go_default_library",
70+
"//test:go_default_library",
7071
"//tools/compiler:go_default_library",
72+
"@dev_cel_expr//conformance/test:go_default_library",
7173
"@in_gopkg_yaml_v3//:go_default_library",
7274
]
7375
)

‎tools/celtest/test_runner.go‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,8 @@ func (p *tsParser) ParseYAML(path string) (*test.Suite, error) {
257257
return testSuite, err
258258
}
259259

260-
// DefaultTestSuiteParser returns a TestRunnerOption which configures the test runner with a test suite parser.
260+
// DefaultTestSuiteParser returns a TestRunnerOption which configures the test runner with
261+
// the default test suite parser.
261262
func DefaultTestSuiteParser(path string) TestRunnerOption {
262263
return func(tr *TestRunner) (*TestRunner, error) {
263264
if path == "" {
@@ -269,6 +270,15 @@ func DefaultTestSuiteParser(path string) TestRunnerOption {
269270
}
270271
}
271272

273+
// TestSuiteParserOption returns a TestRunnerOption which configures the test runner with
274+
// a custom test suite parser.
275+
func TestSuiteParserOption(p TestSuiteParser) TestRunnerOption {
276+
return func(tr *TestRunner) (*TestRunner, error) {
277+
tr.testSuiteParser = p
278+
return tr, nil
279+
}
280+
}
281+
272282
// TestRunner provides a structure to hold the different components required to execute tests for
273283
// a list of Input Expressions. The TestRunner can be configured with the following options:
274284
// - Compiler: The compiler used for parsing and compiling the input expressions.
@@ -353,7 +363,8 @@ func TestExpression(value string) TestRunnerOption {
353363
}
354364
}
355365

356-
// TestCompiler configures a compiler to use for testing.
366+
// TestCompiler returns a TestRunnerOption which configures the test runner with
367+
// a compiler created using the set of compiler options.
357368
func TestCompiler(compileOpts ...any) TestRunnerOption {
358369
return func(tr *TestRunner) (*TestRunner, error) {
359370
c, err := compiler.NewCompiler(compileOpts...)
@@ -365,6 +376,15 @@ func TestCompiler(compileOpts ...any) TestRunnerOption {
365376
}
366377
}
367378

379+
// CustomTestCompiler returns a TestRunnerOption which configures the test runner with
380+
// a custom compiler.
381+
func CustomTestCompiler(c compiler.Compiler) TestRunnerOption {
382+
return func(tr *TestRunner) (*TestRunner, error) {
383+
tr.Compiler = c
384+
return tr, nil
385+
}
386+
}
387+
368388
// AddFileDescriptorSet creates a Test Runner Option which adds a file descriptor set to the test
369389
// runner. The file descriptor set is used to register proto messages in the global proto registry.
370390
func AddFileDescriptorSet(path string) TestRunnerOption {

‎tools/celtest/test_runner_test.go‎

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ import (
2323
"github.com/google/cel-go/common/types"
2424
"github.com/google/cel-go/common/types/ref"
2525
"github.com/google/cel-go/policy"
26+
"github.com/google/cel-go/test"
2627
"github.com/google/cel-go/tools/compiler"
2728
"gopkg.in/yaml.v3"
29+
30+
conformancepb "cel.dev/expr/conformance/test"
2831
)
2932

3033
type testCase struct {
@@ -116,10 +119,7 @@ func TestTriggerTestsWithRunnerOptions(t *testing.T) {
116119
if err != nil {
117120
t.Fatalf("compiler.NewCompiler() failed: %v", err)
118121
}
119-
compilerOpt := TestRunnerOption(func(tr *TestRunner) (*TestRunner, error) {
120-
tr.Compiler = c
121-
return tr, nil
122-
})
122+
compilerOpt := CustomTestCompiler(c)
123123
opts := []TestRunnerOption{compilerOpt, testSuiteParser, testCELPolicy}
124124
TriggerTests(t, opts...)
125125
})
@@ -210,3 +210,49 @@ func TestTriggerTests(t *testing.T) {
210210
})
211211
}
212212
}
213+
214+
// TestCustomTestSuiteParser triggers the test runner where the tests are provided by a custom
215+
// test suite parser configured using TestSuiteParserOption.
216+
func TestCustomTestSuiteParser(t *testing.T) {
217+
t.Run("test custom test suite parser", func(t *testing.T) {
218+
celExpr := "a || i + fn(j) == 42"
219+
compilerOpts := []any{compiler.EnvironmentFile("testdata/config.yaml"), fnEnvOption()}
220+
testRunnerOpts := []TestRunnerOption{
221+
TestCompiler(compilerOpts...),
222+
TestExpression(celExpr),
223+
TestSuiteParserOption(&tsparser{}),
224+
}
225+
TriggerTests(t, testRunnerOpts...)
226+
})
227+
}
228+
229+
type tsparser struct {
230+
TestSuiteParser
231+
}
232+
233+
// ParseTextproto implements the ParseTextproto method of the TestSuiteParser interface.
234+
func (p *tsparser) ParseTextproto(_ string) (*conformancepb.TestSuite, error) {
235+
return nil, nil
236+
}
237+
238+
// ParseYAML implements the ParseYAML method of the TestSuiteParser interface.
239+
func (p *tsparser) ParseYAML(_ string) (*test.Suite, error) {
240+
testCase := &test.Case{
241+
Name: "sample test case",
242+
Input: map[string]*test.InputValue{
243+
"i": {Value: 21},
244+
"j": {Value: 42},
245+
"a": {Value: false},
246+
},
247+
Output: &test.Output{Value: true},
248+
}
249+
testSection := &test.Section{
250+
Name: "sample test section",
251+
Tests: []*test.Case{testCase},
252+
}
253+
suite := &test.Suite{
254+
Description: "sample test suite",
255+
Sections: []*test.Section{testSection},
256+
}
257+
return suite, nil
258+
}

‎tools/go.mod‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ module github.com/google/cel-go/tools
33
go 1.23.0
44

55
require (
6-
cel.dev/expr v0.23.1
7-
github.com/bazelbuild/rules_go v0.54.0
6+
cel.dev/expr v0.24.0
87
github.com/google/cel-go v0.22.0
98
github.com/google/cel-go/policy v0.0.0-20250311174852-f5ea07b389a1
109
github.com/google/go-cmp v0.6.0

‎tools/go.sum‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
cel.dev/expr v0.23.1 h1:K4KOtPCJQjVggkARsjG9RWXP6O4R73aHeJMa/dmCQQg=
2-
cel.dev/expr v0.23.1/go.mod h1:hLPLo1W4QUmuYdA72RBX06QTs6MXw941piREPl3Yfiw=
1+
cel.dev/expr v0.24.0 h1:56OvJKSH3hDGL0ml5uSxZmz3/3Pq4tJ+fb1unVLAFcY=
2+
cel.dev/expr v0.24.0/go.mod h1:hLPLo1W4QUmuYdA72RBX06QTs6MXw941piREPl3Yfiw=
33
github.com/antlr4-go/antlr/v4 v4.13.1 h1:SqQKkuVZ+zWkMMNkjy5FZe5mr5WURWnlpmOuzYWrPrQ=
44
github.com/antlr4-go/antlr/v4 v4.13.1/go.mod h1:GKmUxMtwp6ZgGwZSva4eWPC5mS6vUAmOABFgjdkM7Nw=
5-
github.com/bazelbuild/rules_go v0.54.0 h1:D9aCU7j5rdRxg2rXOZX5zHZ395XC0KbgC4rnyaQ3ofM=
6-
github.com/bazelbuild/rules_go v0.54.0/go.mod h1:T90Gpyq4HDFlsrvtQa2CBdHNJ2P4rAu/uUTmQbanzf0=
75
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
86
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
97
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

0 commit comments

Comments
 (0)