Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add fmt command to format .wast file #83

Merged
merged 16 commits into from
Nov 27, 2023
5 changes: 5 additions & 0 deletions src/cmd_fmt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,8 @@ let cmd inplace (file : string) =
let fmt = Stdlib.Format.formatter_of_out_channel chan in
Format.pp fmt "%a@\n" pp () )
else Format.pp_std "%a@\n" pp ()

let format_file_to_string (file : string) =
match get_printer file with
| Error _ as e -> e
| Ok pp -> Ok (Format.asprintf "%a@\n" pp ())
42 changes: 27 additions & 15 deletions test/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,30 @@ let pp_ok () = Format.fprintf fmt "%a !@." pp_green "OK"

let pp_error msg = Format.fprintf fmt "%a: %s !@." pp_red "FAILED" msg

let test_file ~optimize f =
let test_file mode filename =
Format.fprintf fmt "testing %s: `%a`... "
(if optimize then "optimized file" else "file ")
Fpath.pp f;
( match mode with
| `Optimize -> "optimized file"
| `Formatted -> "formatted file"
| `Normal -> "file " )
Swrup marked this conversation as resolved.
Show resolved Hide resolved
Fpath.pp filename;
try
match Owi.Parse.Script.from_file ~filename:(Fpath.to_string f) with
let res =
match mode with
| `Optimize | `Normal ->
Owi.Parse.Script.from_file ~filename:(Fpath.to_string filename)
| `Formatted ->
Result.bind
Swrup marked this conversation as resolved.
Show resolved Hide resolved
(Owi.Cmd_fmt.format_file_to_string (Fpath.to_string filename))
Owi.Parse.Script.from_string
in
match res with
| Ok script -> begin
match Owi.Script.exec script ~optimize ~no_exhaustion:true with
match
Owi.Script.exec script
~optimize:(match mode with `Optimize -> true | _ -> false)
~no_exhaustion:true
with
| Ok () as ok ->
pp_ok ();
ok
Expand All @@ -42,21 +58,17 @@ let test_directory d =
List.iter
(fun file ->
incr count_total;
begin
match test_file ~optimize:false file with
let run mode =
Swrup marked this conversation as resolved.
Show resolved Hide resolved
match test_file mode file with
| Ok () -> ()
| Error _e ->
incr count_error;
incr count_total_failed
end;
in
run `Normal;
incr count_total;
begin
match test_file ~optimize:true file with
| Ok () -> ()
| Error _e ->
incr count_error;
incr count_total_failed
end )
run `Optimize;
run `Formatted )
Swrup marked this conversation as resolved.
Show resolved Hide resolved
(List.sort compare l);
if !count_error > 0 then
Error (Format.sprintf "%d test failed" !count_error)
Expand Down
Loading