aboutsummaryrefslogtreecommitdiff
path: root/rego-proofs
diff options
context:
space:
mode:
authorJakob L. Kreuze2023-10-21 13:47:30 -0400
committerJakob L. Kreuze2023-10-21 13:47:30 -0400
commitc29a790f63c578ee1059e0d73b5cff4036de90ab (patch)
treea75a6220412b6be448c58588dcdeaa298a6e2825 /rego-proofs
parent82db84c44e349b3a755dbc65ba3f80511a315e06 (diff)
Integrate test suite
Diffstat (limited to 'rego-proofs')
-rw-r--r--rego-proofs/compiler-rewrite/compiler_rewrite.opam31
-rw-r--r--rego-proofs/compiler-rewrite/test/compiler_rewrite.ml95
-rw-r--r--rego-proofs/compiler-rewrite/test/dune3
3 files changed, 128 insertions, 1 deletions
diff --git a/rego-proofs/compiler-rewrite/compiler_rewrite.opam b/rego-proofs/compiler-rewrite/compiler_rewrite.opam
index e69de29..7b13bb9 100644
--- a/rego-proofs/compiler-rewrite/compiler_rewrite.opam
+++ b/rego-proofs/compiler-rewrite/compiler_rewrite.opam
@@ -0,0 +1,31 @@
+# This file is generated by dune, edit dune-project instead
+opam-version: "2.0"
+synopsis: "A short synopsis"
+description: "A longer description"
+maintainer: ["Maintainer Name"]
+authors: ["Author Name"]
+license: "LICENSE"
+tags: ["topics" "to describe" "your" "project"]
+homepage: "https://github.com/username/reponame"
+doc: "https://url/to/documentation"
+bug-reports: "https://github.com/username/reponame/issues"
+depends: [
+ "ocaml"
+ "dune" {>= "3.11"}
+ "odoc" {with-doc}
+]
+build: [
+ ["dune" "subst"] {dev}
+ [
+ "dune"
+ "build"
+ "-p"
+ name
+ "-j"
+ jobs
+ "@install"
+ "@runtest" {with-test}
+ "@doc" {with-doc}
+ ]
+]
+dev-repo: "git+https://github.com/username/reponame.git"
diff --git a/rego-proofs/compiler-rewrite/test/compiler_rewrite.ml b/rego-proofs/compiler-rewrite/test/compiler_rewrite.ml
index e69de29..e447473 100644
--- a/rego-proofs/compiler-rewrite/test/compiler_rewrite.ml
+++ b/rego-proofs/compiler-rewrite/test/compiler_rewrite.ml
@@ -0,0 +1,95 @@
+open Core
+
+module To_test = struct
+ let extract_locals = Compiler_rewrite.Compiler.extract_local_variables_wrapper
+end
+
+let strings = Alcotest.testable
+ (Format.pp_print_list Format.pp_print_string)
+ (List.equal String.equal)
+
+let invoke ?(stdin="") ?(env=[| |]) cmd =
+ let ic, oc, ec = UnixLabels.open_process_full cmd ~env:env in
+ Out_channel.output_string oc stdin ;
+ Out_channel.close oc;
+ (In_channel.input_all ic,
+ In_channel.input_all ec)
+
+let rego_to_ast policy_code =
+ let (stdout, _) = invoke ("/home/jakob/.local/bin/opa parse -f json /dev/stdin") ~stdin:policy_code ~env:[| |] in
+ Yojson.Basic.from_string stdout |> Compiler_rewrite.Compiler.parse_tree
+
+let extract_query policy =
+ match (rego_to_ast policy) with
+ | { policy = [{ body = body; _ }]; _ } -> body
+ | _ -> failwith "failed to parse"
+
+let test_local_extraction_simple_1 () =
+ let policy_code = {|
+package test
+
+q {
+ x := 2
+}
+ |} in
+ Alcotest.(check strings)
+ "expected identifier list"
+ ["x"]
+ (To_test.extract_locals (extract_query policy_code))
+
+let test_local_extraction_simple_2 () =
+ let policy_code = {|
+package test
+
+q {
+ x := 2
+ y := 3
+ z := 4
+}
+ |} in
+ Alcotest.(check strings)
+ "expected identifier list"
+ ["x"; "y"; "z"]
+ (To_test.extract_locals (extract_query policy_code))
+
+let test_local_dependent_on_previously_defined_local () =
+ let policy_code = {|
+package test
+
+q {
+ x := 2
+ y := x
+}
+ |} in
+ Alcotest.(check strings)
+ "expected identifier list"
+ ["x"; "y"]
+ (To_test.extract_locals (extract_query policy_code))
+
+let test_local_not_ground_because_of_ordering () =
+ let policy_code = {|
+package test
+
+q {
+ y := x
+ x := 2
+}
+ |} in
+ Alcotest.(check strings)
+ "expected identifier list"
+ ["x"]
+ (To_test.extract_locals (extract_query policy_code))
+
+
+let () =
+ let open Alcotest in
+ run "Compiler"
+ [ "local extraction", [
+ test_case "local extraction simple (1)"
+ `Quick test_local_extraction_simple_1;
+ test_case "local extraction simple (2)"
+ `Quick test_local_extraction_simple_2;
+ test_case "local extraction with local dependent on previously-defined local (1)"
+ `Quick test_local_dependent_on_previously_defined_local;
+ test_case "local extraction with local dependent on previously-defined local (2)"
+ `Quick test_local_not_ground_because_of_ordering ] ]
diff --git a/rego-proofs/compiler-rewrite/test/dune b/rego-proofs/compiler-rewrite/test/dune
index d6ad7d2..273167d 100644
--- a/rego-proofs/compiler-rewrite/test/dune
+++ b/rego-proofs/compiler-rewrite/test/dune
@@ -1,2 +1,3 @@
(test
- (name compiler_rewrite))
+ (name compiler_rewrite)
+ (libraries alcotest compiler_rewrite core unix yojson))

© 2015 - 2026 Jakob L. Kreuze