Files
releases/ast/parser_test.go
T
Johan Fylling a179a24c48 v1 API
All packages, except for `cmd` and `internal`, have been moved into a new `v1` root package.

Old packages are kept for backwards-compatibility reasons. All contained code is replaced with simple type aliases and proxy functions to `v1` implementations.

Old packages default to the Rego v0 syntax, new `v1` packages default to the Rego v1 syntax.

Signed-off-by: Johan Fylling <johan.dev@fylling.se>
2024-12-12 15:27:34 +01:00

53 lines
1.1 KiB
Go

// Copyright 2024 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 ast
import (
"bytes"
"testing"
)
func TestParser_DefaultRegoVersion(t *testing.T) {
tests := []struct {
note string
input string
expStmtCount int
}{
{
note: "v0",
input: `package test
p[x] {
c = ["a", "b", "c"][i]
}`,
expStmtCount: 2, //package, p
},
{
note: "v1",
input: `package test
p contains x if {
c = ["a", "b", "c"][i]
}`,
// v1 Keywords are not recognized, and interpreted as individual statements
expStmtCount: 5, //package, p, contains, x, if
},
}
for _, tc := range tests {
t.Run(tc.note, func(t *testing.T) {
parser := NewParser().
WithFilename("test.rego").
WithReader(bytes.NewBufferString(tc.input))
stmts, _, err := parser.Parse()
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if len(stmts) != tc.expStmtCount {
t.Fatalf("Expected %d statements but got %d:\n\n%v", tc.expStmtCount, len(stmts), stmts)
}
})
}
}