|
| 1 | +(* TEST |
| 2 | + include ocamlcommon |
| 3 | + flags = "-I ${ocamlsrcdir}/parsing" |
| 4 | + reference = "${test_source_directory}/reference.txt" |
| 5 | +*) |
| 6 | + |
| 7 | +(* Change these two variables to change which extension is being tested *) |
| 8 | +let extension = Clflags.Extension.Comprehensions |
| 9 | +let extension_expression = "[x for x = 1 to 10]" |
| 10 | + |
| 11 | +let extension_name = Clflags.Extension.to_string extension |
| 12 | +let extension_parsed_expression = |
| 13 | + Parse.expression (Lexing.from_string extension_expression) |
| 14 | + (* Currently, parsing always succeeds and we only fail during typechecking *) |
| 15 | + |
| 16 | +let report ~name ~text = |
| 17 | + Printf.printf "# %s [%s %s]:\n%s\n\n" |
| 18 | + name |
| 19 | + extension_name |
| 20 | + (if Clflags.Extension.is_enabled extension then "enabled" else "disabled") |
| 21 | + text |
| 22 | + |
| 23 | +let typecheck_with_extension ?(full_name = false) name = |
| 24 | + let success = |
| 25 | + match Typecore.type_expression Env.empty extension_parsed_expression with |
| 26 | + | _ -> true |
| 27 | + | exception (Extensions_parsing.Error.Error _) -> false |
| 28 | + in |
| 29 | + report |
| 30 | + ~name:(if full_name |
| 31 | + then name |
| 32 | + else "\"" ^ extension_name ^ "\" extension " ^ name) |
| 33 | + ~text:(if success |
| 34 | + then "Successfully typechecked \"" ^ extension_expression ^ "\"" |
| 35 | + else "<extension error>") |
| 36 | +;; |
| 37 | + |
| 38 | +let should_succeed name what f = |
| 39 | + report ~name ~text:(match f () with |
| 40 | + | () -> |
| 41 | + "Succeeded at " ^ what |
| 42 | + | exception Arg.Bad msg -> |
| 43 | + "FAILED at " ^ what ^ ", with the following error\n:" ^ msg) |
| 44 | +;; |
| 45 | + |
| 46 | +let should_fail name f = |
| 47 | + report ~name ~text:(match f () with |
| 48 | + | () -> "<succeeded INCORRECTLY>" |
| 49 | + | exception Arg.Bad msg -> "Failed as expected: " ^ msg) |
| 50 | +;; |
| 51 | + |
| 52 | +let try_disallowing_extensions name = |
| 53 | + should_succeed |
| 54 | + name |
| 55 | + "disallowing all extensions" |
| 56 | + Clflags.Extension.disallow_extensions |
| 57 | +;; |
| 58 | + |
| 59 | +type goal = Fail | Succeed |
| 60 | + |
| 61 | +let when_disallowed goal f_str f = |
| 62 | + let can_or_can't = match goal with |
| 63 | + | Fail -> "can't" |
| 64 | + | Succeed -> "can" |
| 65 | + in |
| 66 | + let f_code = "[" ^ f_str ^ "]" in |
| 67 | + let name = |
| 68 | + can_or_can't ^ " call " ^ f_code ^ " when extensions are disallowed" |
| 69 | + in |
| 70 | + let action () = f extension in |
| 71 | + match goal with |
| 72 | + | Fail -> should_fail name action |
| 73 | + | Succeed -> should_succeed name ("redundantly calling " ^ f_code) action |
| 74 | +;; |
| 75 | + |
| 76 | +let lift_with with_fn extension = with_fn extension Fun.id;; |
| 77 | + |
| 78 | +(* Test the ground state *) |
| 79 | + |
| 80 | +typecheck_with_extension "in its default state"; |
| 81 | + |
| 82 | +(* Disable all extensions for testing *) |
| 83 | + |
| 84 | +List.iter Clflags.Extension.disable Clflags.Extension.all; |
| 85 | +typecheck_with_extension ~full_name:true "no extensions enabled"; |
| 86 | + |
| 87 | +(* Test globally toggling a language extension *) |
| 88 | + |
| 89 | +Clflags.Extension.enable extension; |
| 90 | +typecheck_with_extension "enabled"; |
| 91 | + |
| 92 | +Clflags.Extension.enable extension; |
| 93 | +typecheck_with_extension "still enabled"; |
| 94 | + |
| 95 | +Clflags.Extension.disable extension; |
| 96 | +typecheck_with_extension "disabled"; |
| 97 | + |
| 98 | +Clflags.Extension.disable extension; |
| 99 | +typecheck_with_extension "still disabled"; |
| 100 | + |
| 101 | +Clflags.Extension.set extension ~enabled:true; |
| 102 | +typecheck_with_extension "enabled via [set]"; |
| 103 | + |
| 104 | +Clflags.Extension.enable extension; |
| 105 | +typecheck_with_extension "still enabled, via [set] and [enable]"; |
| 106 | + |
| 107 | +Clflags.Extension.set extension ~enabled:false; |
| 108 | +typecheck_with_extension "disabled via [set]"; |
| 109 | + |
| 110 | +Clflags.Extension.disable extension; |
| 111 | +typecheck_with_extension "still disabled, via [set] and [disable]"; |
| 112 | + |
| 113 | +(* Test locally toggling a language extension *) |
| 114 | + |
| 115 | +(* Globally disable the language extension (idempotent, given the prior tests, |
| 116 | + but it's more robust to do this explicitly) *) |
| 117 | +Clflags.Extension.disable extension; |
| 118 | + |
| 119 | +Clflags.Extension.with_enabled extension (fun () -> |
| 120 | + typecheck_with_extension "enabled locally and disabled globally"); |
| 121 | + |
| 122 | +Clflags.Extension.with_disabled extension (fun () -> |
| 123 | + typecheck_with_extension "disabled locally and globally"); |
| 124 | + |
| 125 | +Clflags.Extension.with_set extension ~enabled:true (fun () -> |
| 126 | + typecheck_with_extension |
| 127 | + "enabled locally via [with_set] and disabled globally"); |
| 128 | + |
| 129 | +Clflags.Extension.with_set extension ~enabled:false (fun () -> |
| 130 | + typecheck_with_extension "disabled locally via [with_set] and also globally"); |
| 131 | + |
| 132 | +(* Globally enable the language extension *) |
| 133 | +Clflags.Extension.enable extension; |
| 134 | + |
| 135 | +Clflags.Extension.with_disabled extension (fun () -> |
| 136 | + typecheck_with_extension "disabled locally and enabled globally"); |
| 137 | + |
| 138 | +Clflags.Extension.with_enabled extension (fun () -> |
| 139 | + typecheck_with_extension "enabled locally and globally"); |
| 140 | + |
| 141 | +Clflags.Extension.with_set extension ~enabled:false (fun () -> |
| 142 | + typecheck_with_extension |
| 143 | + "disabled locally via [with_set] and enabled globally"); |
| 144 | + |
| 145 | +Clflags.Extension.with_set extension ~enabled:true (fun () -> |
| 146 | + typecheck_with_extension "disabled locally via [with_set] and also globally"); |
| 147 | + |
| 148 | +(* Test disallowing extensions *) |
| 149 | + |
| 150 | +try_disallowing_extensions |
| 151 | + "can disallow extensions while extensions are enabled"; |
| 152 | + |
| 153 | +try_disallowing_extensions |
| 154 | + "can disallow extensions while extensions are already disallowed"; |
| 155 | + |
| 156 | +(* Test that disallowing extensions prevents other functions from working *) |
| 157 | + |
| 158 | +when_disallowed Fail "set ~enabled:true" |
| 159 | + (Clflags.Extension.set ~enabled:true); |
| 160 | + |
| 161 | +when_disallowed Succeed "set ~enabled:false" |
| 162 | + (Clflags.Extension.set ~enabled:false); |
| 163 | + |
| 164 | +when_disallowed Fail "enable" |
| 165 | + Clflags.Extension.enable; |
| 166 | + |
| 167 | +when_disallowed Succeed "disable" |
| 168 | + Clflags.Extension.disable; |
| 169 | + |
| 170 | +when_disallowed Fail "with_set ~enabled:true" |
| 171 | + (Clflags.Extension.with_set ~enabled:true |> lift_with); |
| 172 | + |
| 173 | +when_disallowed Succeed "with_set ~enabled:false" |
| 174 | + (Clflags.Extension.with_set ~enabled:false |> lift_with); |
| 175 | + |
| 176 | +when_disallowed Fail "with_enabled" |
| 177 | + (Clflags.Extension.with_enabled |> lift_with); |
| 178 | + |
| 179 | +when_disallowed Succeed "with_disabled" |
| 180 | + (Clflags.Extension.with_disabled |> lift_with); |
| 181 | + |
| 182 | +(* Test explicitly (rather than just via [report]) that [is_enabled] returns |
| 183 | + [false] now that we've disallowed all extensions *) |
| 184 | +report |
| 185 | + ~name:"[is_enabled] returns [false] when extensions are disallowed" |
| 186 | + ~text:("\"" ^ extension_name ^ "\" is " ^ |
| 187 | + if Clflags.Extension.is_enabled extension |
| 188 | + then "INCORRECTLY enabled" |
| 189 | + else "correctly disabled") |
0 commit comments