Skip to content

Commit

Permalink
Merge branch 'global-name-is-module-id' into instance-syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
lukemaurer committed Oct 20, 2023
2 parents 8b42e26 + 70b77ea commit dc1a3eb
Show file tree
Hide file tree
Showing 3 changed files with 251 additions and 5 deletions.
163 changes: 163 additions & 0 deletions ocaml/testsuite/tests/templates/basic/gen_test.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
open! StdLabels

let test_tree_token = "<TEST TREE HERE>"

type test_tree =
| Act of string * (string * string) list
| Seq of test_tree list
| Par of test_tree list
| Nop
| Branch of test_tree

let stars n =
String.make n '*'

let backslash_newlines s =
let lines = String.split_on_char ~sep:'\n' s in
String.concat ~sep:"\\\n" lines

let out_test_tree ppf tree =
let rec out n tree after =
match tree with
| Act (action, vars) ->
Format.fprintf ppf "%s %s\n" (stars n) action;
List.iter vars ~f:(fun (var, value) ->
Format.fprintf ppf "%s = \"%s\"\n" var (backslash_newlines value));
out_next (n+1) after
| Seq trees -> out_next n (trees @ after)
| Par trees ->
begin
match after with
| [] -> List.iter trees ~f:(fun tree -> out n tree [])
| _ -> failwith "Par in middle of Seq"
end
| Nop -> out_next n after
| Branch tree ->
out n tree [];
out_next n after
and out_next n after =
match after with
| [] -> ()
| tree :: trees ->
out n tree trees
in
out 1 tree []

let generate ~in_ ~out test_tree =
In_channel.with_open_text in_ @@ fun ic ->
Out_channel.with_open_text out @@ fun oc ->
let ppf = Format.formatter_of_out_channel oc in
let rec loop () =
match In_channel.input_line ic with
| Some line ->
begin match String.equal line test_tree_token with
| true -> out_test_tree ppf test_tree
| false -> Format.fprintf ppf "%s\n" line
end;
loop ()
| None -> ()
in
loop ();
Format.pp_print_flush ppf ()

type mode = Byte | Native

let (!%) = Format.sprintf

let sep_unless_empty s1 s2 =
match s1, s2 with
| "", _ -> s2
| _, "" -> s1
| _, _ -> !%"%s %s" s1 s2

let tree_for_mode mode =
let ocamlc =
match mode with
| Byte -> "ocamlc"
| Native -> "ocamlopt"
in
let cmo =
match mode with
| Byte -> "cmo"
| Native -> "cmx"
in
let bc =
match mode with
| Byte -> "bc"
| Native -> "exe"
in
let compiler = !%"%s.byte" ocamlc in
let compiler_under = !%"%s_byte" ocamlc in
let add_flags ~flags vars =
match flags with
| None -> vars
| Some flags -> ("flags", flags) :: vars
in
let add_extra ?(extra = []) vars = extra @ vars in
let compile ?flags ?extra filenames =
Act (compiler, add_flags ~flags (add_extra ?extra [
"module", filenames;
]))
in
let compile_bad ~ext ?flags module_ =
Branch (Seq [
Act (compiler, add_flags ~flags [
"module", !%"%s.%s" module_ ext;
"compiler_output", !%"%s.output" module_;
!%"%s_exit_status" compiler_under, "2";
]);
Act (!%"check-%s-output" compiler, [
"compiler_reference", !%"%s.reference" module_
]);
])
in
let compile_bad_ml = compile_bad ~ext:"ml" in
let compile_bad_mli = compile_bad ~ext:"mli" in
let link ?flags main modules =
let all_modules =
List.concat [
[ "" ];
List.map modules ~f:(fun m -> !%" %s.%s " m cmo);
[ "" ];
]
|> String.concat ~sep:"\n"
in
Act (compiler, add_flags ~flags [
"program", !%"${test_build_directory}/%s.%s" main bc;
"module", "";
"all_modules", all_modules
])
in
let link_and_run ?flags main modules =
Seq [
link ?flags main modules;
Act ("run", [ "output", !%"%s.output" main ]);
Act ("check-program-output", [ "reference", !%"%s.reference" main ]);
]
in
Seq [
Act (!%"setup-%s-build-env" compiler, []);
compile "monoid.mli" ~flags:"-as-parameter";
compile_bad_ml "bad_ref_direct" ~flags:"";
compile_bad_ml "bad_arg_impl" ~flags:"-as-argument-for Monoid";
compile_bad_mli "bad_arg_intf" ~flags:"-as-argument-for Monoid";
Branch (Seq [
Act ("copy", [
"src", "string_monoid.ml";
"dst", "string_monoid_no_mli.ml"
]);
compile "string_monoid_no_mli.ml string_monoid.mli string_monoid.ml"
~flags:"-as-argument-for Monoid";
Branch (Seq [
compile "test_direct_access.ml" ~flags:"";
link_and_run "test_direct_access" ~flags:""
["string_monoid"; "string_monoid_no_mli"; "test_direct_access"];
]);
]);
compile "monoid_utils.mli monoid_utils.ml" ~flags:"-parameter Monoid";
compile_bad_ml "bad_ref_indirect" ~flags:"";
]

let test_tree = Par [ tree_for_mode Byte; tree_for_mode Native ]

let () = generate ~in_:"test.in.ml" ~out:"test.ml" test_tree
18 changes: 18 additions & 0 deletions ocaml/testsuite/tests/templates/basic/test.in.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
(* If this is [test.ml], instead change [test.in.ml] and/or [gen_test.ml] and
re-run [gen_test.ml]. *)

(* TEST
readonly_files = "\
bad_arg_impl.ml bad_arg_impl.reference \
bad_arg_intf.mli bad_arg_intf.reference \
bad_ref_direct.ml bad_ref_direct.reference \
bad_ref_indirect.ml bad_ref_indirect.reference \
monoid.mli \
monoid_utils.ml monoid_utils.mli \
string_monoid.ml string_monoid.mli \
test_direct_access.ml test_direct_access.reference \
"
<TEST TREE HERE>
*)
75 changes: 70 additions & 5 deletions ocaml/testsuite/tests/templates/basic/test.ml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
(* If this is [test.ml], instead change [test.in.ml] and/or [gen_test.ml] and
re-run [gen_test.ml]. *)

(* TEST
readonly_files = "\
Expand All @@ -16,6 +19,7 @@ readonly_files = "\
flags = "-as-parameter"
module = "monoid.mli"
*** ocamlc.byte
flags = ""
module = "bad_ref_direct.ml"
compiler_output = "bad_ref_direct.output"
ocamlc_byte_exit_status = "2"
Expand Down Expand Up @@ -46,21 +50,82 @@ flags = ""
module = "test_direct_access.ml"
****** ocamlc.byte
flags = ""
program = "${test_build_directory}/test_direct_access.exe"
program = "${test_build_directory}/test_direct_access.bc"
module = ""
all_modules = "string_monoid.cmo string_monoid_no_mli.cmo test_direct_access.cmo"
all_modules = "\
string_monoid.cmo \
string_monoid_no_mli.cmo \
test_direct_access.cmo \
"
******* run
output = "test_direct_access.output"
******** check-program-output
reference = "test_direct_access.reference"
********* ocamlc.byte
*** ocamlc.byte
flags = "-parameter Monoid"
module = "monoid_utils.mli monoid_utils.ml"
********** ocamlc.byte
**** ocamlc.byte
flags = ""
module = "bad_ref_indirect.ml"
compiler_output = "bad_ref_indirect.output"
ocamlc_byte_exit_status = "2"
*********** check-ocamlc.byte-output
***** check-ocamlc.byte-output
compiler_reference = "bad_ref_indirect.reference"
* setup-ocamlopt.byte-build-env
** ocamlopt.byte
flags = "-as-parameter"
module = "monoid.mli"
*** ocamlopt.byte
flags = ""
module = "bad_ref_direct.ml"
compiler_output = "bad_ref_direct.output"
ocamlopt_byte_exit_status = "2"
**** check-ocamlopt.byte-output
compiler_reference = "bad_ref_direct.reference"
*** ocamlopt.byte
flags = "-as-argument-for Monoid"
module = "bad_arg_impl.ml"
compiler_output = "bad_arg_impl.output"
ocamlopt_byte_exit_status = "2"
**** check-ocamlopt.byte-output
compiler_reference = "bad_arg_impl.reference"
*** ocamlopt.byte
flags = "-as-argument-for Monoid"
module = "bad_arg_intf.mli"
compiler_output = "bad_arg_intf.output"
ocamlopt_byte_exit_status = "2"
**** check-ocamlopt.byte-output
compiler_reference = "bad_arg_intf.reference"
*** copy
src = "string_monoid.ml"
dst = "string_monoid_no_mli.ml"
**** ocamlopt.byte
flags = "-as-argument-for Monoid"
module = "string_monoid_no_mli.ml string_monoid.mli string_monoid.ml"
***** ocamlopt.byte
flags = ""
module = "test_direct_access.ml"
****** ocamlopt.byte
flags = ""
program = "${test_build_directory}/test_direct_access.exe"
module = ""
all_modules = "\
string_monoid.cmx \
string_monoid_no_mli.cmx \
test_direct_access.cmx \
"
******* run
output = "test_direct_access.output"
******** check-program-output
reference = "test_direct_access.reference"
*** ocamlopt.byte
flags = "-parameter Monoid"
module = "monoid_utils.mli monoid_utils.ml"
**** ocamlopt.byte
flags = ""
module = "bad_ref_indirect.ml"
compiler_output = "bad_ref_indirect.output"
ocamlopt_byte_exit_status = "2"
***** check-ocamlopt.byte-output
compiler_reference = "bad_ref_indirect.reference"
*)

0 comments on commit dc1a3eb

Please sign in to comment.