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.

132 lignes · 3144 octetsBrut · Historique
1import log.{type log, Debug, Entry, Error, Info, Warn}
2import std/list
3import std/test.{assert, assert_equal}
4import std/time
5
6fn at :: -> time.Timestamp
7{
8 time.from_unix_ms 1793887331000 // 2026-11-05T14:02:11.000Z
9}
10
11// ---- levels ------------------------------------------------------------------------
12
13@test
14fn levels_rank_least_first ::
15{
16 assert
17 (Debug < Info && Info < Warn && Warn < Error)
18 "debug < info < warn < error"
19}
20
21@test
22fn every_level_is_read_back_from_its_name ::
23{
24 assert_equal
25 ( [Debug, Info, Warn, Error]
26 |> list.map fn l { log.level_named (log.name l) }
27 )
28 [Some(Debug), Some(Info), Some(Warn), Some(Error)]
29}
30
31@test
32fn a_level_name_ignores_case_and_spaces ::
33{
34 assert_equal (log.level_named " WARN ") Some(Warn)
35}
36
37@test
38fn an_unknown_level_name_is_none ::
39{
40 assert_equal (log.level_named "verbose") None
41 assert_equal (log.level_named "") None
42}
43
44// ---- the line ----------------------------------------------------------------------
45
46@test
47fn a_line_has_the_time_the_padded_level_the_message_and_the_fields ::
48{
49 assert_equal
50 (log.line (at ()) Entry(Info, "GET / 200 8ms", [("client", "127.0.0.1")]))
51 "2026-11-05T14:02:11.000Z info GET / 200 8ms client=127.0.0.1"
52}
53
54@test
55fn the_time_always_has_three_fraction_digits ::
56{
57 let stamped = fn ms { log.line (time.from_unix_ms ms) Entry(Info, "m", []) }
58 assert_equal (stamped 1793887331007) "2026-11-05T14:02:11.007Z info m"
59 assert_equal (stamped 1793887331250) "2026-11-05T14:02:11.250Z info m"
60 assert_equal (stamped (-1)) "1969-12-31T23:59:59.999Z info m"
61}
62
63@test
64fn a_line_without_fields_ends_with_the_message ::
65{
66 assert_equal
67 (log.line (at ()) Entry(Error, "boom", []))
68 "2026-11-05T14:02:11.000Z error boom"
69}
70
71@test
72fn a_value_that_would_not_split_cleanly_is_quoted ::
73{
74 let fields =
75 [ ("reason", "no TLS")
76 , ("empty", "")
77 , ("pair", "a=b")
78 , ("said", "a \"b\"")
79 , ("path", "C:\\x")
80 , ("lines", "one\ntwo")
81 ]
82 assert_equal
83 (log.line (at ()) Entry(Warn, "m", fields))
84 "2026-11-05T14:02:11.000Z warn m reason=\"no TLS\" empty=\"\" pair=\"a=b\" said=\"a \\\"b\\\"\" path=\"C:\\\\x\" lines=\"one\\ntwo\""
85}
86
87@test
88fn a_message_stays_on_one_line ::
89{
90 assert_equal
91 (log.line (at ()) Entry(Debug, "one\ntwo", []))
92 "2026-11-05T14:02:11.000Z debug one\\ntwo"
93}
94
95// ---- handlers ----------------------------------------------------------------------
96
97fn chatty :: -> Int / <log>
98{
99 log.debug "d" []
100 log.info "i" [("k", "v")]
101 log.warn "w" []
102 log.error "e" []
103 7
104}
105
106@test
107fn captured_keeps_the_entries_in_order_with_the_result ::
108{
109 assert_equal
110 (log.captured chatty)
111 ( 7
112 , [ Entry(Debug, "d", [])
113 , Entry(Info, "i", [("k", "v")])
114 , Entry(Warn, "w", [])
115 , Entry(Error, "e", [])
116 ]
117 )
118}
119
120@test
121fn at_least_passes_the_entries_at_its_level_and_above_outward ::
122{
123 let (result, entries) = log.captured fn { log.at_least Warn chatty }
124 assert_equal result 7
125 assert_equal entries [Entry(Warn, "w", []), Entry(Error, "e", [])]
126}
127
128@test
129fn silently_drops_every_entry ::
130{
131 assert_equal (log.silently chatty) 7
132}