aboutsummaryrefslogtreecommitdiff
path: root/rego-proofs/compiler-rewrite/test/compiler_rewrite.ml
blob: cbb7b04e335bdba39d6738941dd6245e5c5f7770 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
open Core
open Compiler_rewrite

module To_test = struct
  let extract_variables = Compiler.extract_variables_wrapper
  let fmt = Lean.fmt_lean_expression
end

let strings =
  Alcotest.testable
    (Format.pp_print_list Format.pp_print_string)
    (List.equal String.equal)

let extract_query policy =
  match Ast.ast_of_string policy with
  | { policy = [ { body; _ } ]; _ } -> body
  | _ -> failwith "failed to parse"

let test_local_extraction_simple_1 () =
  let policy_code = {|
package test

q {
  x := 2
}
  |} in
  let open Compiler_rewrite.Compiler in
  let { bound; _ } = To_test.extract_variables (extract_query policy_code) in
  Alcotest.(check strings)
    "expected identifier list" [ "x" ] bound

let test_local_extraction_simple_2 () =
  let policy_code = {|
package test

q {
  x := 2
  y := 3
  z := 4
}
  |} in
  let open Compiler_rewrite.Compiler in
  let { bound; _ } = To_test.extract_variables (extract_query policy_code) in
  Alcotest.(check strings)
    "expected identifier list" [ "x"; "y"; "z" ] bound

let test_local_dependent_on_previously_defined_local () =
  let policy_code = {|
package test

q {
  x := 2
  y := x
}
  |} in
  let open Compiler_rewrite.Compiler in
  let { bound; _ } = To_test.extract_variables (extract_query policy_code) in
  Alcotest.(check strings)
    "expected identifier list" [ "x"; "y" ] bound

let test_free_extraction_simple_1 () =
  let policy_code = {|
package test

q {
  y := 7
  y == 8
}
  |} in
  let open Compiler_rewrite.Compiler in
  let { free; bound; _ } = To_test.extract_variables (extract_query policy_code) in
  Alcotest.(check strings)
    "expected identifier list" [] free;
  Alcotest.(check strings)
    "expected identifier list" [ "y" ] bound

let test_free_extraction_simple_2 () =
  let policy_code = {|
package test

q {
  y := z
  z = 7
}
  |} in
  let open Compiler_rewrite.Compiler in
  let { free; bound; _ } = To_test.extract_variables (extract_query policy_code) in
  Alcotest.(check strings)
    "expected identifier list" [ "z" ] free;
  Alcotest.(check strings)
    "expected identifier list" [ "y" ] bound

let test_identifier () =
  let open Compiler_rewrite.Lean in
  let ast = Identifier "test" in
  Alcotest.(check string) "expected string" "test" (To_test.fmt ast)

let () =
  let open Alcotest in
  run "Compiler"
    [
      ( "free variable extraction",
        [
          test_case "free variable extraction simple (1)" `Quick
            test_free_extraction_simple_1;
          test_case "free variable extraction simple (2)" `Quick
            test_free_extraction_simple_2;
        ] );
      ( "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
        ] );
      ( "ast formatting ",
        [ test_case "identifier compilation" `Quick test_identifier ] );
    ]

© 2015 - 2026 Jakob L. Kreuze