Skip to content

Commit

Permalink
Move our minify tests to be markdown based
Browse files Browse the repository at this point in the history
  • Loading branch information
SquidDev committed Jan 10, 2024
1 parent a068bc8 commit 1c56dea
Show file tree
Hide file tree
Showing 14 changed files with 125 additions and 75 deletions.
8 changes: 0 additions & 8 deletions test/data/minify/basic.lua

This file was deleted.

3 changes: 0 additions & 3 deletions test/data/minify/basic.out

This file was deleted.

13 changes: 0 additions & 13 deletions test/data/minify/literals.lua

This file was deleted.

3 changes: 0 additions & 3 deletions test/data/minify/literals.out

This file was deleted.

8 changes: 6 additions & 2 deletions test/dune
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
(libraries containers)
(modules md_test_runner))

(executable
(name md_test_gen)
(modules md_test_gen))

(test
(name test)
(libraries
Expand All @@ -22,7 +26,6 @@
illuaminate.parser
illuaminate.parser_md
illuaminate.lint
illuaminate.minify
cmarkit
fpath
illuaminate.semantics
Expand All @@ -49,7 +52,8 @@
diff
doc_emit_summary
leak_trace_gc
md_test_runner)
md_test_runner
md_test_gen)
(action
(run %{test} --base-dir "../")))

Expand Down
32 changes: 32 additions & 0 deletions test/illuaminate_minify/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
(executable
(name main)
(libraries
illuaminate
illuaminate.core
illuaminate.parser
illuaminate.minify
md_test_runner
ppx_deriving.runtime))

(rule
(deps
(:markdown
(glob_files t_*.md)))
(action
(with-stdout-to
rules.new.inc
(with-stdin-from
%{dep:rules.inc}
(run %{dep:../md_test_gen.exe} --extra %{markdown})))))

(rule
(alias rules)
(action
(diff rules.inc rules.new.inc)))

(alias
(name runtest)
(deps
(alias rules)))

(include rules.inc)
23 changes: 11 additions & 12 deletions test/minify.ml → test/illuaminate_minify/main.ml
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
open IlluaminateMinify
let () =
Md_test_runner.run @@ fun out _ contents ->
let name = Illuaminate.File_id.mk "=input" in

let process ~name contents =
let lexbuf = Lexing.from_string contents in
let name = Illuaminate.File_id.mk name in
Format.asprintf "%t" @@ fun out ->
match IlluaminateParser.program name lexbuf with
match IlluaminateParser.program name (Lexing.from_string contents) with
| Error err ->
Illuaminate.Console_reporter.display_of_string ~out ~with_summary:false
(fun _ -> Some contents)
[ IlluaminateParser.Error.to_error err ]
| Ok parsed -> (
(* Minify our program. *)
let updated =
let buffer = Buffer.create 512 in
let out = Format.formatter_of_buffer buffer in
(minify parsed |> Emit.(with_wrapping out "%a" program));
(IlluaminateMinify.minify parsed
|> IlluaminateMinify.Emit.(with_wrapping out "%a@\n" program));
Buffer.contents buffer
in

Format.pp_print_string out updated;

let lexbuf = Lexing.from_string updated in
match IlluaminateParser.program name lexbuf with
(* Attempt to reparse it. This doesn't check the two are syntactically equivalent, only that
it is syntactically valid. *)
match IlluaminateParser.program name (Lexing.from_string updated) with
| Ok _ -> ()
| Error err ->
Format.pp_print_string out "\n--[==[\n";
Illuaminate.Console_reporter.display_of_string ~out ~with_summary:false
(fun _ -> Some updated)
[ IlluaminateParser.Error.to_error err ];
Format.pp_print_string out "]==]\n")

let tests =
OmnomnomGolden.of_directory process ~group:"Minify" ~directory:"data/minify" ~extension:".lua" ()
7 changes: 7 additions & 0 deletions test/illuaminate_minify/rules.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(rule (deps (glob_files extra/*.lua)) (action (with-stdout-to t_minify.new (with-stdin-from %{dep:t_minify.md} (run %{dep:./main.exe} md)))))
(rule (alias t_minify) (action (diff t_minify.md t_minify.new)))
(alias
(name runtest)
(deps
(alias t_minify)))
42 changes: 42 additions & 0 deletions test/illuaminate_minify/t_minify.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
```lua
local function printf(str, ...)
print(string.format(str, ...))
end

local h, s = {}, 1
for i = 1, 10 do
printf("Hello", i * 2)
end
```

```txt
local function e(t,...)print(string.format(t,...))end local a,o={},1 for i=1,10
do
e("Hello",i*2)end
```


# Literals
Various literals are minified correctly.

```lua
local x

x = 0
x = 0., .0, 0.0
x = 0. .. .0
x = - - 2
x = 2 and 2
do local x = 0 end

x = "hello"
x = 'hello'
x = [[hello]]
x = x[ [[hello]] ]
```

```txt
local e e=0 e=0.,.0,0.0 e=0. .. .0 e=- -2 e=2 and 2 do local e=0 end
e="hello"e='hello'e=[[hello]]e=e[
[[hello]]]
```
2 changes: 1 addition & 1 deletion test/lint/dune
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
rules.new.inc
(with-stdin-from
%{dep:rules.inc}
(run %{dep:./main.exe} rules %{markdown})))))
(run %{dep:../md_test_gen.exe} --extra %{markdown})))))

(rule
(alias rules)
Expand Down
32 changes: 1 addition & 31 deletions test/lint/main.ml
Original file line number Diff line number Diff line change
@@ -1,26 +1,3 @@
(** Generate our [rules.inc] file. *)
let gen_rules () =
let files =
Array.to_seq Sys.argv |> Seq.drop 2 |> Seq.map Fpath.v
|> Seq.filter (Fpath.has_ext "md")
|> Seq.map (fun x -> Fpath.rem_ext x |> Fpath.to_string)
|> List.of_seq |> List.sort String.compare
in
List.iter
(fun name ->
let println x = Printf.printf (x ^^ "\n") in
println
{|(rule (deps (glob_files extra/*.lua)) (action (with-stdout-to %s.new (with-stdin-from %%{dep:%s.md} (run %%{dep:./main.exe} md)))))|}
name name;
println {|(rule (alias %s) (action (diff %s.md %s.new)))|} name name name)
files;
print_string {|
(alias
(name runtest)
(deps|};
List.iter (Printf.printf "\n (alias %s)") files;
print_endline "))"

let regex =
let open Re in
let code_block lang =
Expand All @@ -35,7 +12,7 @@ let regex =
|> compile

(** Process a markdown file. *)
let markdown () =
let () =
let input = In_channel.input_all stdin in

let limit = String.length input in
Expand Down Expand Up @@ -84,10 +61,3 @@ let markdown () =
in

go 0

let () =
Printexc.record_backtrace true;
match Sys.argv.(1) with
| "rules" -> gen_rules ()
| "md" -> markdown ()
| _ -> exit 1
25 changes: 25 additions & 0 deletions test/md_test_gen.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
(** Generate our [rules.inc] file. *)

let () =
let files = ref [] in
let extra = ref false in
Arg.parse
[ ("--extra", Arg.Set extra, "Add a dependency on files in the extra/ directory.") ]
(fun x -> files := Filename.remove_extension x :: !files)
__MODULE__;
let files = List.sort String.compare !files and extra = !extra in
List.iter
(fun name ->
let println x = Printf.printf (x ^^ "\n") in
println
{|(rule %s(action (with-stdout-to %s.new (with-stdin-from %%{dep:%s.md} (run %%{dep:./main.exe} md)))))|}
(if extra then "(deps (glob_files extra/*.lua)) " else "")
name name;
println {|(rule (alias %s) (action (diff %s.md %s.new)))|} name name name)
files;
print_string {|
(alias
(name runtest)
(deps|};
List.iter (Printf.printf "\n (alias %s)") files;
print_endline "))"
1 change: 0 additions & 1 deletion test/minify.mli

This file was deleted.

1 change: 0 additions & 1 deletion test/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ let () =
Lint.tests;
Pattern.tests;
Reprint.tests;
Minify.tests;
group "Documentation"
[ Doc_parser.tests;
Doc_emit.Json_summary.tests;
Expand Down

0 comments on commit 1c56dea

Please sign in to comment.