Skip to content

Commit

Permalink
Go back to dune diffs for testing
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
yawaramin committed Dec 29, 2024
1 parent dcedb71 commit 92ae81d
Show file tree
Hide file tree
Showing 13 changed files with 699 additions and 713 deletions.
2 changes: 0 additions & 2 deletions dream-html.opam
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down
7 changes: 2 additions & 5 deletions dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
(uri
(and
(>= 4.4.0)
(< 5.0.0)))
(ppx_expect :with-test)))
(< 5.0.0)))))

(package
(name dream-html)
Expand All @@ -40,6 +39,4 @@
(pure-html
(= :version))
(dream
(>= 1.0.0~alpha3))
(fmt :with-test)
(ppx_expect :with-test)))
(>= 1.0.0~alpha3))))
1 change: 0 additions & 1 deletion pure-html.opam
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down
11 changes: 11 additions & 0 deletions test/README.md
Original file line number Diff line number Diff line change
@@ -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.
7 changes: 0 additions & 7 deletions test/dream-html/dune

This file was deleted.

35 changes: 35 additions & 0 deletions test/dream_html_test.expected.txt
Original file line number Diff line number Diff line change
@@ -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

<form method="post" action="/">
<input value="token-value" name="dream.csrf" type="hidden">
<input name="id">
<button type="submit">Add</button>
</form>

102 changes: 40 additions & 62 deletions test/dream-html/dream_html_test.ml → test/dream_html_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

<form method="post" action="/">
<input value="token-value" name="dream.csrf" type="hidden">
<input name="id">
<button type="submit">Add</button>
</form> |}]
test_handler handler "/"
33 changes: 31 additions & 2 deletions test/dune
Original file line number Diff line number Diff line change
@@ -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)))
7 changes: 0 additions & 7 deletions test/pure-html/dune

This file was deleted.

Loading

0 comments on commit 92ae81d

Please sign in to comment.