Skip to content

Commit

Permalink
ast/compile_test: add unused assigned var in "every" body case
Browse files Browse the repository at this point in the history
Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
  • Loading branch information
srenatus committed Jan 29, 2022
1 parent 46478b6 commit 5f5da56
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions ast/compile_test.go
Expand Up @@ -4019,13 +4019,23 @@ func TestCompilerCheckUnusedAssignedVar(t *testing.T) {
&Error{Message: "assigned var y unused"},
},
},
{
note: "every: unused assigned var in body",
module: `package test
p { every i in [1] { y := 10; i == 1 } }
`,
expectedErrors: Errors{
&Error{Message: "assigned var y unused"},
},
},
}

makeTestRunner := func(tc testCase, strict bool) func(t *testing.T) {
return func(t *testing.T) {
compiler := NewCompiler().WithStrict(strict)
opts := ParserOptions{AllFutureKeywords: true, unreleasedKeywords: true}
compiler.Modules = map[string]*Module{
"test": MustParseModule(tc.module),
"test": MustParseModuleWithOpts(tc.module, opts),
}
compileStages(compiler, compiler.rewriteLocalVars)

Expand Down Expand Up @@ -5509,12 +5519,18 @@ func TestQueryCompilerWithUnusedAssignedVar(t *testing.T) {
query: "{1: 2 | x := 2}",
expectedErrors: fmt.Errorf("1 error occurred: 1:9: rego_compile_error: assigned var x unused"),
},
{
note: "every: unused var in body",
query: "every _ in [] { x := 10 }",
expectedErrors: fmt.Errorf("1 error occurred: 1:17: rego_compile_error: assigned var x unused"),
},
}

makeTestRunner := func(tc testCase, strict bool) func(t *testing.T) {
return func(t *testing.T) {
c := NewCompiler().WithStrict(strict)
result, err := c.QueryCompiler().Compile(MustParseBody(tc.query))
opts := ParserOptions{AllFutureKeywords: true, unreleasedKeywords: true}
result, err := c.QueryCompiler().Compile(MustParseBodyWithOpts(tc.query, opts))

if strict {
if err == nil {
Expand Down

0 comments on commit 5f5da56

Please sign in to comment.