Files
regorus/tests/rvm/rego/cases/set_rules.yaml
Anand Krishnamoorthi a3a20a1235 feat!: Rego -> RVM Compiler and extensive testsuite (#506)
# RVM compiler test cases

Coverage:
- arithmetic
- arrays
- chained lookups
- comparisons
- comprehensions
- default rules
- destructuring
- function rules
- loops/quantifiers
- multiple entrypoints
- objects/sets
- variables
- negative/edge scenarios such as data/rule conflicts
- virtual data lookups
- etc

 # Modify interpreter and compiled policy for RVM Compilation

- Interpreter::eval_default_rule_for_compiler:
   evaluates a named default rule in isolation - allows compiler to emit a constant value instead of instructions
   for the default value

#  feat: Rego Compiler Scaffolding

- Introduce the rego::compiler module surface and entry point wiring
- Add the core compiler concepts:
  - register allocator
  - scope tracking
  - literal/builtin tables
  - rule worklists
  - instruction emit helpers
  - compiler-specific error types
  - context structs for rules, comprehensions, and loops to support later lowering passes.

# feat: Compile Rules/Queries

- add compiler::compile_from_policy workflow plus rule worklist, entry-point wiring, and recursion checks
- implement query lowering:
  - scheduling-aware statement ordering
  - loop hoisting
  - “every/some” semantics
  - context yields
  -  literal assertions
- finalize Program construction

# feat: Expression Lowering

- add compile_rego_expr and helpers to translate every AST expression into RVM instructions,
- interop with binding plans, comprehensions, and membership checks.
- implement collection literal builders (ArrayCreate, SetCreate, ObjectCreate)
  - dedupe literal keys and handle mixed literal/dynamic fields via instruction data blocks.
- operations:
  - arithmetic/boolean/bin operators
  - membership
  - unary minus
  - set unions/intersections
  - etc
- user-defined and builtin function calls
- reference handling
  - analyse chained refs
  - distinguishe data/input/local roots
  - perform rule dispatch or virtual document lookups
  - emits optimized Index/ChainedIndex instructions.

# feat: Comprehensions & Loops

- shared comprehension emitter
 - wraps array/set/object comprehensions with ComprehensionBegin/End
 - context management
- loop lowering utilities
 - read hoisting metadata
 - emit LoopStart/LoopNext
 - some in lowering
 - every quantifiers
 - index iteration
 - propagate binding plans into stored registers so downstream statements see bound variables.

# feat: Destructuring Lowering

- destructuring planner integration
 - assignment/parameter/loop bindings use hoisted plans instead of re-walking ASTs.
- handle :=, =, wildcard matches, and equality
 - evaluate RHS
 - applying destructuring plans
 - emit assert condition as needed
- support nested array/object destructuring, dynamic keys, and some ... in forms

# test: Shared Testing + RVM Suites

- move YAML test helpers into test_utils.rs and re-export via common.rs for use by interpreter and vm test suites
- comprehensive compiler test suite
  - compiles policies with the new Rego→RVM compiler
  - runs them through RegoVM
  - compares against interpreter behavior
  - supports multiple entry points
  - provides assembly listings
  - filterable YAML suites.

Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
2025-11-24 12:08:37 -06:00

154 lines
3.8 KiB
YAML

# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# Examples Test Suite
# Tests real-world patterns and advanced Rego constructs
cases:
- note: set_rules_with_contains
data: {}
input:
user:
role: "editor"
name: "alice"
modules:
- |
package test
# Define a set of allowed actions
allowed_actions contains "read" if {
input.user.role in ["viewer", "editor", "admin"]
}
allowed_actions contains "write" if {
input.user.role in ["editor", "admin"]
}
allowed_actions contains "admin" if {
input.user.role == "admin"
}
# Check if a specific action is allowed
allow_read := "read" in allowed_actions
allow_write := "write" in allowed_actions
allow_admin := "admin" in allowed_actions
# Main result combining all permissions
main := {
"allowed_actions": allowed_actions,
"can_read": allow_read,
"can_write": allow_write,
"can_admin": allow_admin
}
query: data.test.main
want_result:
allowed_actions:
set!: ["read", "write"]
can_read: true
can_write: true
can_admin: false
- note: set_membership_with_contains
data: {}
input:
department: "engineering"
role: "developer"
modules:
- |
package test
# Define sets using contains
valid_departments contains d if {
some dept in ["engineering", "marketing", "sales"]
d := dept
}
sensitive_roles contains role if {
some role in ["admin", "security", "finance"]
r := role
}
# Check membership
is_valid_dept := input.department in valid_departments
is_sensitive := input.role in sensitive_roles
# Access decision
allow := is_valid_dept
deny := is_sensitive
main := {
"valid_departments": valid_departments,
"sensitive_roles": sensitive_roles,
"department_valid": is_valid_dept,
"role_sensitive": is_sensitive,
"allow": allow,
"deny": deny
}
query: data.test.main
want_result:
valid_departments:
set!: ["engineering", "marketing", "sales"]
sensitive_roles:
set!: ["admin", "security", "finance"]
department_valid: true
role_sensitive: false
allow: true
deny: false
- note: conditional_set_contains
data: {}
input:
user:
active: true
level: 3
department: "engineering"
modules:
- |
package test
# Conditional set rules
permissions contains "read" if {
input.user.active == true
}
permissions contains "write" if {
input.user.active == true
input.user.level >= 2
}
permissions contains "delete" if {
input.user.active == true
input.user.level >= 5
input.user.department == "admin"
}
main := permissions
query: data.test.main
want_result:
set!: ["read", "write"]
- note: empty_set_contains
data: {}
input:
user:
role: "user"
verified: false
modules:
- |
package test
# Set that might be empty based on conditions
special_permissions contains "super_admin" if {
input.user.role == "root"
input.user.verified == true
}
special_permissions contains "audit" if {
input.user.role == "auditor"
}
main := special_permissions
query: data.test.main
want_result:
set!: []