Files
regorus/tests/rvm/rego/cases/function_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

231 lines
5.5 KiB
YAML

# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# Function Rules Test Suite
# Tests user-defined function rule calls with arguments
# Covers function definitions, argument passing, return values, and consistency
cases:
- note: simple_function_call
description: Test basic function rule definition and call
data: {}
modules:
- |
package test
# Define a simple function rule
add_ten(x) := x + 10
# Call the function
main := add_ten(5)
query: data.test.main
want_result: 15
- note: function_with_multiple_args
description: Test function rule with multiple arguments
data: {}
modules:
- |
package test
# Function that adds two numbers
add(x, y) := x + y
# Call with two arguments
main := add(7, 3)
query: data.test.main
want_result: 10
- note: function_with_variable_args
description: Test function call with variables as arguments
data: {}
modules:
- |
package test
multiply(x, y) := x * y
main := result if {
a := 4
b := 6
result := multiply(a, b)
}
query: data.test.main
want_result: 24
- note: function_returning_object
description: Test function that returns an object
data: {}
modules:
- |
package test
make_person(name, age) := {"name": name, "age": age}
main := make_person("Alice", 30)
query: data.test.main
want_result: {"name": "Alice", "age": 30}
- note: function_returning_array
description: Test function that returns an array
data: {}
modules:
- |
package test
make_range(start, end) := [start, end] if start <= end
main := make_range(1, 3)
query: data.test.main
want_result: [1, 3]
- note: nested_function_calls
description: Test nested function calls
data: {}
modules:
- |
package test
double(x) := x * 2
add_one(x) := x + 1
main := double(add_one(5))
query: data.test.main
want_result: 12
- note: function_with_condition
description: Test function rule with conditional body
data: {}
modules:
- |
package test
max(x, y) := x if x >= y
max(x, y) := y if y > x
main := max(7, 3)
query: data.test.main
want_result: 7
- note: function_consistency_check
description: Test that function definitions must be consistent
data: {}
modules:
- |
package test
# These definitions would be inconsistent if both conditions were true
inconsistent_func(x) := x + 1 if x < 5
inconsistent_func(x) := x + 2 if x < 5
# This should work for x >= 5
main := inconsistent_func(10)
query: data.test.main
want_result: "#undefined"
- note: function_with_undefined_result
description: Test function that can return undefined
data: {}
modules:
- |
package test
# Function only defined for positive numbers
positive_double(x) := x * 2 if x > 0
# Calling with negative number should return undefined
main := positive_double(-1)
query: data.test.main
want_result: "#undefined"
- note: function_using_data
description: Test function that accesses global data
data: {"multiplier": 3}
modules:
- |
package test
scale(x) := x * data.multiplier
main := scale(5)
query: data.test.main
want_result: 15
- note: function_using_input
description: Test function that accesses input
data: {}
input: {"base": 10}
modules:
- |
package test
add_to_base(x) := x + input.base
main := add_to_base(5)
query: data.test.main
want_result: 15
- note: function_with_complex_logic
description: Test function with complex conditional logic
data: {}
modules:
- |
package test
classify_number(x) := "negative" if x < 0
classify_number(x) := "zero" if x == 0
classify_number(x) := "small positive" if {
x > 0
x <= 10
}
classify_number(x) := "large positive" if x > 10
main := classify_number(5)
query: data.test.main
want_result: "small positive"
- note: function_with_array_processing
description: Test function that processes arrays
data: {}
modules:
- |
package test
first_element(arr) := arr[0]
main := first_element([1, 2, 3])
query: data.test.main
want_result: 1
- note: function_with_object_processing
description: Test function that processes objects
data: {}
modules:
- |
package test
get_field(obj, field) := obj[field]
main := get_field({"name": "Bob", "age": 25}, "name")
query: data.test.main
want_result: "Bob"
- note: function_call_chain
description: Test chain of function calls
data: {}
modules:
- |
package test
step1(x) := x + 1
step2(x) := x * 2
step3(x) := x - 3
main := result if {
a := step1(5) # 6
b := step2(a) # 12
result := step3(b) # 9
}
query: data.test.main
want_result: 9