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.

188 lignes · 4653 octetsBrut · Historique
1import decode.{type Decoder}
2import json.{Malformed, Mismatch, Missing}
3import std/option
4import std/test.{assert_equal}
5
6@derive Eq Debug Show
7type Ship { Ship(symbol: String, fuel: Int, route: List<String>) }
8
9fn ship :: -> Decoder<Ship>
10{
11 use symbol <- decode.field "symbol" (decode.string ())
12 use fuel <- decode.optional_field "fuel" (decode.int ())
13 use route <- decode.field "route" (decode.list (decode.string ()))
14 decode.succeed Ship(symbol, option.unwrap_or fuel 0, route)
15}
16
17@test
18fn a_record_decodes_field_by_field ::
19{
20 assert_equal
21 ( decode.from_string
22 "\{\"symbol\": \"ORION-1\", \"fuel\": 80, \"route\": [\"A\", \"B\"]\}"
23 (ship ())
24 )
25 Ok(Ship("ORION-1", 80, ["A", "B"]))
26}
27
28@test
29fn an_optional_field_may_be_absent_or_null ::
30{
31 assert_equal
32 (decode.from_string "\{\"symbol\": \"X\", \"route\": []\}" (ship ()))
33 Ok(Ship("X", 0, []))
34 assert_equal
35 ( decode.from_string
36 "\{\"symbol\": \"X\", \"fuel\": null, \"route\": []\}"
37 (ship ())
38 )
39 Ok(Ship("X", 0, []))
40}
41
42@test
43fn fields_not_asked_for_are_ignored ::
44{
45 assert_equal
46 ( decode.from_string
47 "\{\"symbol\": \"X\", \"route\": [], \"extra\": true\}"
48 (ship ())
49 )
50 Ok(Ship("X", 0, []))
51}
52
53@test
54fn an_error_names_its_path ::
55{
56 assert_equal
57 (decode.from_string "\{\"symbol\": 7, \"route\": []\}" (ship ()))
58 Error(Mismatch(["symbol"], "a string", "an integer"))
59 assert_equal
60 ( decode.from_string
61 "\{\"symbol\": \"X\", \"route\": [\"A\", 2]\}"
62 (ship ())
63 )
64 Error(Mismatch(["route", "1"], "a string", "an integer"))
65 assert_equal
66 (decode.from_string "\{\"route\": []\}" (ship ()))
67 Error(Missing([], "symbol"))
68 assert_equal
69 (decode.from_string "[1]" (ship ()))
70 Error(Mismatch([], "an object", "an array"))
71}
72
73@test
74fn nested_paths_accumulate ::
75{
76 let fleet = decode.field "ships" (decode.list (ship ())) decode.succeed
77 assert_equal
78 ( decode.from_string
79 "\{\"ships\": [\{\"symbol\": \"A\", \"route\": []\}, \{\"route\": []\}]\}"
80 fleet
81 )
82 Error(Missing(["ships", "1"], "symbol"))
83 assert_equal
84 "{Missing(["ships", "1"], "symbol")}"
85 "$.ships.1: missing field \"symbol\""
86}
87
88@test
89fn a_parse_error_comes_through ::
90{
91 assert_equal
92 (decode.from_string "\{" (ship ()))
93 Error(Malformed(1, "expected a key (a string)"))
94}
95
96@test
97fn scalars ::
98{
99 assert_equal (decode.from_string "1.5" (decode.float ())) Ok(1.5)
100 assert_equal (decode.from_string "2" (decode.float ())) Ok(2.0)
101 assert_equal
102 (decode.from_string "2.0" (decode.int ()))
103 Error(Mismatch([], "an integer", "a number"))
104 assert_equal (decode.from_string "true" (decode.bool ())) Ok(True)
105 assert_equal
106 (decode.from_string "null" (decode.nullable (decode.int ())))
107 Ok(None)
108 assert_equal
109 (decode.from_string "3" (decode.nullable (decode.int ())))
110 Ok(Some(3))
111}
112
113@derive Eq Debug Show
114type Command { Orbit, Navigate(String) }
115
116fn command :: -> Decoder<Command>
117{
118 decode.field
119 "action"
120 (decode.string ())
121 fn action
122 {
123 match action
124 { "orbit" -> decode.succeed Orbit
125 , "navigate" ->
126 decode.field
127 "waypoint"
128 (decode.string ())
129 fn w { decode.succeed Navigate(w) }
130 , _ -> decode.fail "orbit or navigate"
131 }
132 }
133}
134
135@test
136fn then_and_fail_read_a_tagged_union ::
137{
138 assert_equal
139 (decode.from_string "\{\"action\": \"orbit\"\}" (command ()))
140 Ok(Orbit)
141 assert_equal
142 ( decode.from_string
143 "\{\"action\": \"navigate\", \"waypoint\": \"SOL-A2\"\}"
144 (command ())
145 )
146 Ok(Navigate("SOL-A2"))
147 assert_equal
148 (decode.from_string "\{\"action\": \"jump\"\}" (command ()))
149 Error(Mismatch([], "orbit or navigate", "an object"))
150}
151
152@test
153fn one_of_tries_each_in_turn ::
154{
155 let id =
156 decode.one_of
157 (decode.int () |> decode.map fn n { "#{n}" })
158 [decode.string ()]
159 assert_equal (decode.from_string "7" id) Ok("#7")
160 assert_equal (decode.from_string "\"seven\"" id) Ok("seven")
161 assert_equal
162 (decode.from_string "true" id)
163 Error(Mismatch([], "an integer", "a bool"))
164}
165
166@test
167fn map_and_then ::
168{
169 let doubled = decode.int () |> decode.map fn n { n * 2 }
170 assert_equal (decode.from_string "21" doubled) Ok(42)
171 let positive =
172 decode.int ()
173 |> decode.then
174 fn n
175 {
176 if n > 0
177 {
178 decode.succeed n
179 }
180 else
181 {
182 decode.fail "a positive integer"
183 }
184 }
185 assert_equal
186 (decode.from_string "-1" positive)
187 Error(Mismatch([], "a positive integer", "an integer"))
188}