From 92ae81d15eec29f393b181cde640083229fb1dc1 Mon Sep 17 00:00:00 2001 From: Yawar Amin Date: Sun, 29 Dec 2024 12:36:27 -0500 Subject: [PATCH] Go back to dune diffs for testing Due to ppx_expect requiring installing all Jane Street packages as dependencies for pure-html and dream-html. Add a warning about this in the `test/` directory readme. --- dream-html.opam | 2 - dune-project | 7 +- pure-html.opam | 1 - test/README.md | 11 + test/dream-html/dune | 7 - test/dream_html_test.expected.txt | 35 ++ test/{dream-html => }/dream_html_test.ml | 102 ++-- test/dune | 33 +- test/pure-html/dune | 7 - test/pure-html/html_tests.ml | 177 ------ test/pure_html_test.expected.txt | 501 +++++++++++++++++ .../mathml_tests.ml => pure_html_test.ml} | 529 +++--------------- ...cted_route.txt => route_test.expected.txt} | 0 13 files changed, 699 insertions(+), 713 deletions(-) create mode 100644 test/README.md delete mode 100644 test/dream-html/dune create mode 100644 test/dream_html_test.expected.txt rename test/{dream-html => }/dream_html_test.ml (56%) delete mode 100644 test/pure-html/dune delete mode 100644 test/pure-html/html_tests.ml create mode 100644 test/pure_html_test.expected.txt rename test/{pure-html/mathml_tests.ml => pure_html_test.ml} (52%) rename test/{expected_route.txt => route_test.expected.txt} (100%) diff --git a/dream-html.opam b/dream-html.opam index c7093ca..be371fa 100644 --- a/dream-html.opam +++ b/dream-html.opam @@ -15,8 +15,6 @@ depends: [ "dune" {>= "3.0"} "pure-html" {= version} "dream" {>= "1.0.0~alpha3"} - "fmt" {with-test} - "ppx_expect" {with-test} "odoc" {with-doc} ] build: [ diff --git a/dune-project b/dune-project index c9521f2..c88f59a 100644 --- a/dune-project +++ b/dune-project @@ -26,8 +26,7 @@ (uri (and (>= 4.4.0) - (< 5.0.0))) - (ppx_expect :with-test))) + (< 5.0.0))))) (package (name dream-html) @@ -40,6 +39,4 @@ (pure-html (= :version)) (dream - (>= 1.0.0~alpha3)) - (fmt :with-test) - (ppx_expect :with-test))) + (>= 1.0.0~alpha3)))) diff --git a/pure-html.opam b/pure-html.opam index 6d6084d..d59861f 100644 --- a/pure-html.opam +++ b/pure-html.opam @@ -14,7 +14,6 @@ bug-reports: "https://github.com/yawaramin/dream-html/issues" depends: [ "dune" {>= "3.0"} "uri" {>= "4.4.0" & < "5.0.0"} - "ppx_expect" {with-test} "odoc" {with-doc} ] build: [ diff --git a/test/README.md b/test/README.md new file mode 100644 index 0000000..06f63b7 --- /dev/null +++ b/test/README.md @@ -0,0 +1,11 @@ +## Snapshot tests + +> [!WARN] +> The tests are using purely dune's file diffing and promotion feature as that +> does not require any library dependencies. We tried using ppx_expect tests but +> the problem is they are _inline_ tests that must be defined in a `library` +> component, not a `test` component like the tests we have now. And `library` +> components need opam packages to be installed _without_ the `with-test` +> annotation, meaning that using ppx_expect would pull in _all_ the Jane Street +> libraries as dependencies of both pure-html and dream-html. So, we are back now +> to dune's diffing and promotion for tests, which require _no_ dependencies. diff --git a/test/dream-html/dune b/test/dream-html/dune deleted file mode 100644 index 9649d22..0000000 --- a/test/dream-html/dune +++ /dev/null @@ -1,7 +0,0 @@ -(library - (name dream_html_test) - (package dream-html) - (inline_tests) - (libraries dream_html fmt) - (preprocess - (pps ppx_expect))) diff --git a/test/dream_html_test.expected.txt b/test/dream_html_test.expected.txt new file mode 100644 index 0000000..99540ed --- /dev/null +++ b/test/dream_html_test.expected.txt @@ -0,0 +1,35 @@ + + +šŸ”Ž OK with age +{name: Bob; + age: 42; + accept_tos: true; + permissions: [r]} + +šŸ”Ž OK without age +{name: Bob; + age: ; + accept_tos: true; + permissions: [r]} + +šŸ”Ž Error without name +[name, error.required] + +šŸ”Ž Error with too low age and empty name +[age, error.range; name, Must not be empty] + +šŸ”Ž Error too many permissions +[permissions, error.length] + +šŸ”Ž Error can't have permissions if not accept TOS +[permissions, error.length] + +šŸ”Ž Indent CSRF tag correctly +200 OK + +
+ + + +
+ diff --git a/test/dream-html/dream_html_test.ml b/test/dream_html_test.ml similarity index 56% rename from test/dream-html/dream_html_test.ml rename to test/dream_html_test.ml index 50a119f..b716e3f 100644 --- a/test/dream-html/dream_html_test.ml +++ b/test/dream_html_test.ml @@ -59,58 +59,45 @@ let user_form = and+ age = optional (int ~min:16) "age" in { name; age; accept_tos; permissions } -let%expect_test "OK with age" = - Format.printf "%a" pp - (validate user_form - ["accept-tos", "true"; "age", "42"; "name", "Bob"; "permissions", "r"]); - [%expect - {| - {name: Bob; - age: 42; - accept_tos: true; - permissions: [r]} - |}] - -let%expect_test "OK without age" = - Format.printf "%a" pp - (validate user_form - ["accept-tos", "true"; "name", "Bob"; "permissions", "r"]); - [%expect - {| - {name: Bob; - age: ; - accept_tos: true; - permissions: [r]} - |}] - -let%expect_test "Error without name" = - Format.printf "%a" pp - (validate user_form ["accept-tos", "true"; "age", "42"; "permissions", "r"]); - [%expect {| [name, error.required] |}] - -let%expect_test "Error with too low age and empty name" = - Format.printf "%a" pp - (validate user_form - ["accept-tos", "true"; "age", "1"; "name", ""; "permissions", "r"]); - [%expect {| [age, error.range; name, Must not be empty] |}] - -let%expect_test "Error too many permissions" = - Format.printf "%a" pp - (validate user_form - [ "accept-tos", "true"; - "age", "42"; - "name", "Bob"; - "permissions", "r"; - "permissions", "w"; - "permissions", "x"; - "permissions", "" ]); - [%expect {| [permissions, error.length] |}] - -let%expect_test "Error can't have permissions if not accept TOS" = - Format.printf "%a" pp (validate user_form ["name", "Bob"; "permissions", "r"]); - [%expect {| [permissions, error.length] |}] - -let%expect_test "Indent CSRF tag correctly" = +let test msg output = + Printf.printf "\n\nšŸ”Ž %s\n%!" msg; + output () + +let test_form msg data = + test msg @@ fun () -> Format.printf "%a%!" pp (validate user_form data) + +let () = + test_form "OK with age" + ["accept-tos", "true"; "age", "42"; "name", "Bob"; "permissions", "r"] + +let () = + test_form "OK without age" + ["accept-tos", "true"; "name", "Bob"; "permissions", "r"] + +let () = + test_form "Error without name" + ["accept-tos", "true"; "age", "42"; "permissions", "r"] + +let () = + test_form "Error with too low age and empty name" + ["accept-tos", "true"; "age", "1"; "name", ""; "permissions", "r"] + +let () = + test_form "Error too many permissions" + [ "accept-tos", "true"; + "age", "42"; + "name", "Bob"; + "permissions", "r"; + "permissions", "w"; + "permissions", "x"; + "permissions", "" ] + +let () = + test_form "Error can't have permissions if not accept TOS" + ["name", "Bob"; "permissions", "r"] + +let () = + test "Indent CSRF tag correctly" @@ fun () -> let handler = Dream.memory_sessions (fun req -> let open Dream_html in @@ -122,13 +109,4 @@ let%expect_test "Indent CSRF tag correctly" = input [name "id"]; button [type_ "submit"] [txt "Add"] ])) in - test_handler handler "/"; - [%expect - {| - 200 OK - -
- - - -
|}] + test_handler handler "/" diff --git a/test/dune b/test/dune index e0d8ed6..bfa8fb7 100644 --- a/test/dune +++ b/test/dune @@ -1,13 +1,42 @@ +(test + (name pure_html_test) + (package pure-html) + (libraries pure-html) + (modules pure_html_test) + (action + (diff pure_html_test.expected.txt pure_html_test.got.txt))) + +(rule + (with-stdout-to + pure_html_test.got.txt + (run ./pure_html_test.exe))) + +(test + (name dream_html_test) + (package dream-html) + (libraries dream-html) + (modules dream_html_test) + (preprocess + (pps dream-html.ppx)) + (action + (diff dream_html_test.expected.txt dream_html_test.got.txt))) + +(rule + (with-stdout-to + dream_html_test.got.txt + (run ./dream_html_test.exe))) + (test (name route_test) (package dream-html) (libraries dream_html) + (modules route_test) (preprocess (pps dream-html.ppx)) (action - (diff expected_route.txt got_route.txt))) + (diff route_test.expected.txt route_test.got.txt))) (rule (with-stdout-to - got_route.txt + route_test.got.txt (run ./route_test.exe))) diff --git a/test/pure-html/dune b/test/pure-html/dune deleted file mode 100644 index a7353d0..0000000 --- a/test/pure-html/dune +++ /dev/null @@ -1,7 +0,0 @@ -(library - (name pure_html_test) - (package pure-html) - (inline_tests) - (libraries pure_html) - (preprocess - (pps ppx_expect))) diff --git a/test/pure-html/html_tests.ml b/test/pure-html/html_tests.ml deleted file mode 100644 index 20637db..0000000 --- a/test/pure-html/html_tests.ml +++ /dev/null @@ -1,177 +0,0 @@ -(* Copyright 2023 Yawar Amin - - This file is part of dream-html. - - dream-html is free software: you can redistribute it and/or modify it - under the terms of the GNU General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) any - later version. - - dream-html is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more - details. - - You should have received a copy of the GNU General Public License along with - dream-html. If not, see . *) - -open Pure_html - -let%expect_test "HTML" = - let greet nm = - let open HTML in - p [id "greet-%s" nm] [txt "Hello, %s!" nm] - in - let html_node = - let open HTML in - html - [lang "en"] - [ head [] - [ title [] "Dream_html Test"; - link [rel "preload"; as_ "style"; href "/app.css"] ]; - body - [id "test-content"] - [ main - [spellcheck true; colspan 1] - [ article - [id "article-1"; class_ "story"] - [ p - [Hx.get "/p1?a b"; Hx.target "closest article > p"] - [txt "Test para 1."]; - p [] [txt "Test para 2."]; - a [href "/a?b=cd:efg/hij"] [txt "cd:efg/hij"]; - a [href "/foo?a=1&b=2 3&c=4<5&d=6>5"] [txt "Test"]; - a [href "/šŸ˜‰"] [txt "wink"]; - MathML.math [style_ ""] [] ]; - input - [ type_ "text"; - autocomplete `name; - onblur "if (1 > 0) alert(this.value)" ]; - null - [ comment "oops -->"; - dialog [open_; title_ {|"hello"|}] [div [] []]; - template [id "idtmpl"] [p [] [txt "Template"]]; - div [translate `no] [p [translate `yes] []]; - textarea - [ required; - Hx.trigger "keyup[target.value.trim() != '']"; - autocapitalize `words ] - "'super'"; - hr [(if true then class_ "super" else null_)]; - greet "Bob" ] ] ] ] - in - html_node |> to_string |> print_endline; - [%expect - {| - - - - Dream_html Test - - - -
- - - - -
-
- -
-

-
- -
-

Hello, Bob!

-
- - - |}] - -let%expect_test "HTML pretty print corner cases" = - let open HTML in - (* test empty tags don't break line *) - let empty_tag = div [] [p [translate `yes] []] in - empty_tag |> to_string |> print_endline; - [%expect {| -
-

-
- |}]; - (* test null tag spacing *) - let null_tag = div [] [null [p [translate `yes] []]] in - null_tag |> to_string |> print_endline; - [%expect {| -
-

-
- |}]; - (* test children containing text is not broken *) - let children_with_text = - div [] - [ p [] - [ b [] [txt "Hello,"]; - txt " World."; - br []; - txt "This text should not be on the next line." ] ] - in - children_with_text |> to_string |> print_endline; - [%expect - {| -
-

Hello, World.
This text should not be on the next line.

-
- |}]; - (* text combining text with (possibly nested) null *) - let null_combining_text = - let open HTML in - div [] - [ comment "this is a comment"; - p [] - [ null [txt "Hello,"]; - txt " World."; - null [br []; txt "This text should not be on the next line."] ] ] - in - null_combining_text |> to_string |> print_endline; - [%expect - {| -
- -

Hello, World.
This text should not be on the next line.

-
- |}] - -let%expect_test "SVG" = - let svg_node = - let open SVG in - svg - [ xmlns; - fill "none"; - viewbox ~min_x:0 ~min_y:0 ~width:24 ~height:24; - stroke_width "1.5"; - stroke "currentColor"; - HTML.class_ "w-6 h-6" ] - [ path - [ stroke_linecap `round; - stroke_linejoin `round; - d "M4.5 10.5L12 3m0 0l7.5 7.5M12 3v18" ] - [] ] - in - svg_node |> to_xml ~header:true |> print_endline; - [%expect - {| - - - - - |}] diff --git a/test/pure_html_test.expected.txt b/test/pure_html_test.expected.txt new file mode 100644 index 0000000..a6589f7 --- /dev/null +++ b/test/pure_html_test.expected.txt @@ -0,0 +1,501 @@ + + +šŸ”Ž HTML + + + + Dream_html Test + + + +
+ + + + +
+
+ +
+

+
+ +
+

Hello, Bob!

+
+ + + + + +šŸ”Ž HTML pretty print corner cases +
+

+
+ + + +šŸ”Ž SVG + + + + + + + +šŸ”Ž MathML +

+ + + + + + + + + a + ā¢ + + x + 2 + + + + + b + ā¢ + x + + + + c + + + + + = + + + 0 + + + + + + + a + ā¢ + + x + 2 + + + + + b + ā¢ + x + + + + = + + + āˆ’ + c + + + + + + + + x + 2 + + + + + + b + a + + ā¤ + x + + + + = + + + + + āˆ’ + c + + a + + + + + Divide out leading coefficient. + + + + + + + + + + x + 2 + + + + + + + b + + a + + ā¤ + x + + + + + ( + + + b + + + 2 + a + + + ) + + 2 + + + + + + = + + + + + + āˆ’ + c + ( + 4 + a + ) + + + a + ( + 4 + a + ) + + + + + + + + b + 2 + + + + 4 + + a + 2 + + + + + + + + Complete the square. + + + + + + + + ( + x + + + + + b + + + 2 + a + + + ) + ( + x + + + + + b + + + 2 + a + + + ) + + + + + = + + + + + + b + 2 + + āˆ’ + 4 + a + c + + + 4 + + a + 2 + + + + + + + Discriminant revealed. + + + + + + + + + + ( + x + + + + + b + + + 2 + a + + + ) + + 2 + + + + + + = + + + + + + b + 2 + + āˆ’ + 4 + a + c + + + 4 + + a + 2 + + + + + + + + + + + + + + + + x + + + + + b + + + 2 + a + + + + + + + + = + + + + + + + b + 2 + + āˆ’ + 4 + a + c + + + 4 + + a + 2 + + + + + + + + + + + + + + x + + + = + + + + + āˆ’ + b + + + 2 + a + + + Ā± + + { + C + } + + + + + + b + 2 + + āˆ’ + 4 + a + c + + + 4 + + a + 2 + + + + + + + + There's the vertex formula. + + + + + + x + + + = + + + + + āˆ’ + b + Ā± + + { + C + } + + + + b + 2 + + āˆ’ + 4 + a + c + + + + 2 + a + + + + + + + + + + + +

+ diff --git a/test/pure-html/mathml_tests.ml b/test/pure_html_test.ml similarity index 52% rename from test/pure-html/mathml_tests.ml rename to test/pure_html_test.ml index 24e89fa..4fe5be6 100644 --- a/test/pure-html/mathml_tests.ml +++ b/test/pure_html_test.ml @@ -17,7 +17,84 @@ open Pure_html -let%expect_test "MathML" = +let test msg output = + Printf.printf "\n\nšŸ”Ž %s\n%!" msg; + output () + +let () = + test "HTML" @@ fun () -> + let greet nm = + let open HTML in + p [id "greet-%s" nm] [txt "Hello, %s!" nm] + in + let html_node = + let open HTML in + html + [lang "en"] + [ head [] + [ title [] "Dream_html Test"; + link [rel "preload"; as_ "style"; href "/app.css"] ]; + body + [id "test-content"] + [ main + [spellcheck true; colspan 1] + [ article + [id "article-1"; class_ "story"] + [ p + [Hx.get "/p1?a b"; Hx.target "closest article > p"] + [txt "Test para 1."]; + p [] [txt "Test para 2."]; + a [href "/a?b=cd:efg/hij"] [txt "cd:efg/hij"]; + a [href "/foo?a=1&b=2 3&c=4<5&d=6>5"] [txt "Test"]; + a [href "/šŸ˜‰"] [txt "wink"]; + MathML.math [style_ ""] [] ]; + input + [ type_ "text"; + autocomplete `name; + onblur "if (1 > 0) alert(this.value)" ]; + null + [ comment "oops -->"; + dialog [open_; title_ {|"hello"|}] [div [] []]; + template [id "idtmpl"] [p [] [txt "Template"]]; + div [translate `no] [p [translate `yes] []]; + textarea + [ required; + Hx.trigger "keyup[target.value.trim() != '']"; + autocapitalize `words ] + "'super'"; + hr [(if true then class_ "super" else null_)]; + greet "Bob" ] ] ] ] + in + html_node |> to_string |> print_endline + +let () = + test "HTML pretty print corner cases" @@ fun () -> + let open HTML in + (* test empty tags don't break line *) + let empty_tag = div [] [p [translate `yes] []] in + empty_tag |> to_string |> print_endline + +let () = + test "SVG" @@ fun () -> + let svg_node = + let open SVG in + svg + [ xmlns; + fill "none"; + viewbox ~min_x:0 ~min_y:0 ~width:24 ~height:24; + stroke_width "1.5"; + stroke "currentColor"; + HTML.class_ "w-6 h-6" ] + [ path + [ stroke_linecap `round; + stroke_linejoin `round; + d "M4.5 10.5L12 3m0 0l7.5 7.5M12 3v18" ] + [] ] + in + svg_node |> to_xml ~header:true |> print_endline + +let () = + test "MathML" @@ fun () -> let mathml_node = let open HTML in let open MathML in @@ -279,452 +356,4 @@ let%expect_test "MathML" = [mtext [style_ "color: red; font-size: 10pt;"] []] ] ] ] ] ] in - mathml_node |> to_xml |> print_endline; - [%expect - {| -

- - - - - - - - - a - ā¢ - - x - 2 - - - + - b - ā¢ - x - - + - c - - - - - = - - - 0 - - - - - - - a - ā¢ - - x - 2 - - - + - b - ā¢ - x - - - - = - - - āˆ’ - c - - - - - - - - x - 2 - - - + - - b - a - - ā¤ - x - - - - = - - - - - āˆ’ - c - - a - - - - - Divide out leading coefficient. - - - - - - - - - - x - 2 - - - + - - - b - - a - - ā¤ - x - + - - - ( - - - b - - - 2 - a - - - ) - - 2 - - - - - - = - - - - - - āˆ’ - c - ( - 4 - a - ) - - - a - ( - 4 - a - ) - - - + - - - - b - 2 - - - - 4 - - a - 2 - - - - - - - - Complete the square. - - - - - - - - ( - x - + - - - b - - - 2 - a - - - ) - ( - x - + - - - b - - - 2 - a - - - ) - - - - - = - - - - - - b - 2 - - āˆ’ - 4 - a - c - - - 4 - - a - 2 - - - - - - - Discriminant revealed. - - - - - - - - - - ( - x - + - - - b - - - 2 - a - - - ) - - 2 - - - - - - = - - - - - - b - 2 - - āˆ’ - 4 - a - c - - - 4 - - a - 2 - - - - - - - - - - - - - - - - x - + - - - b - - - 2 - a - - - - - - - - = - - - - - - - b - 2 - - āˆ’ - 4 - a - c - - - 4 - - a - 2 - - - - - - - - - - - - - - x - - - = - - - - - āˆ’ - b - - - 2 - a - - - Ā± - - { - C - } - - - - - - b - 2 - - āˆ’ - 4 - a - c - - - 4 - - a - 2 - - - - - - - - There's the vertex formula. - - - - - - x - - - = - - - - - āˆ’ - b - Ā± - - { - C - } - - - - b - 2 - - āˆ’ - 4 - a - c - - - - 2 - a - - - - - - - - - - - -

- |}] \ No newline at end of file + mathml_node |> to_xml |> print_endline diff --git a/test/expected_route.txt b/test/route_test.expected.txt similarity index 100% rename from test/expected_route.txt rename to test/route_test.expected.txt