tobogganSe connecter

packages

The Louise packages, each released on its own

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

98 lignes · 2817 octetsBrut · Historique
1import std/html.{Attribute, Fragment, Node, Text}
2import std/option
3import std/string
4import std/test.{assert, assert_equal}
5import swap
6
7@test
8fn a_placeholder_is_its_skeleton_marked_as_loading ::
9{
10 let skeleton = Node("nav", [Attribute("class", "labels")], [Text("…")])
11 assert_equal
12 (html.render (swap.placeholder "swap-1" skeleton))
13 "<nav class=\"labels\" id=\"swap-1\" aria-busy=\"true\">…</nav>"
14}
15
16@test
17fn a_skeleton_of_several_elements_is_wrapped ::
18{
19 let skeleton = Fragment([Node("p", [], []), Node("p", [], [])])
20 assert_equal
21 (html.render (swap.placeholder "swap-2" skeleton))
22 "<div id=\"swap-2\" aria-busy=\"true\"><p></p><p></p></div>"
23}
24
25@test
26fn a_part_is_a_template_then_its_marker ::
27{
28 assert_equal
29 (swap.part "swap-1" "<ul><li>a</li></ul>")
30 "<template><ul><li>a</li></ul></template><swap-in for=\"swap-1\"></swap-in>"
31}
32
33@test
34fn the_script_carries_the_nonce_and_defines_the_marker ::
35{
36 let script = swap.script "abc123"
37 assert (string.starts_with script "<script nonce=\"abc123\">") script
38 assert (string.contains script "customElements.define(\"swap-in\"") script
39 assert (string.ends_with script "</script>") script
40}
41
42@test
43fn an_opened_page_has_the_script_in_its_head_and_its_end_kept_for_last ::
44{
45 let page = "<!doctype html><html><head><title>t</title></head><body><p>x</p></body></html>"
46 match swap.opened page "n1"
47 { Some((first, last)) ->
48 {
49 assert_equal
50 first
51 "<!doctype html><html><head><title>t</title>{swap.script "n1"}</head><body><p>x</p>"
52 assert_equal last "</body></html>"
53 }
54 , None -> assert False "the page should open"
55 }
56}
57
58@test
59fn a_head_that_holds_the_script_keeps_it_once ::
60{
61 let script = swap.script "n1"
62 let page = "<html><head>{script}<title>t</title></head><body><p>x</p></body></html>"
63 match swap.opened page "n1"
64 { Some((first, last)) ->
65 {
66 assert_equal
67 first
68 "<html><head>{script}<title>t</title></head><body><p>x</p>"
69 assert_equal last "</body></html>"
70 }
71 , None -> assert False "the page should open"
72 }
73}
74
75@test
76fn a_head_with_another_nonce_s_script_gets_this_one ::
77{
78 let page = "<html><head>{swap.script "old"}</head><body></body></html>"
79 let first = swap.opened page "n1" |> option.map fn split { split.0 }
80 assert_equal
81 first
82 Some("<html><head>{swap.script "old"}{swap.script "n1"}</head><body>")
83}
84
85@test
86fn a_page_splits_at_its_last_body_end ::
87{
88 let page = "<html><head></head><body><code></body></code></body></html>"
89 let last = swap.opened page "n" |> option.map fn split { split.1 }
90 assert_equal last Some("</body></html>")
91}
92
93@test
94fn a_fragment_without_head_or_body_does_not_open ::
95{
96 assert_equal (swap.opened "<p>x</p>" "n") None
97 assert_equal (swap.opened "<head></head><p>x</p>" "n") None
98}