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.
Dernier runRéussi#28 · main · 3 min 34 s
0Étoiles
0Forks
3Issues
| 1 | // Expected values come from RFC 8259 and from the cases of |
| 2 | // JSONTestSuite (github.com/nst/JSONTestSuite): `y_` must parse, `n_` |
| 3 | // must be refused. They are written here by hand, not produced by this |
| 4 | // package. |
| 5 | |
| 6 | import gen |
| 7 | import gen/check |
| 8 | import json. |
| 9 | { type ToJson |
| 10 | , type Value |
| 11 | , Array |
| 12 | , Bool |
| 13 | , Float |
| 14 | , Int |
| 15 | , Malformed |
| 16 | , Null |
| 17 | , Object |
| 18 | , String |
| 19 | } |
| 20 | import std/bytes |
| 21 | import std/list |
| 22 | import std/random.{type random} |
| 23 | import std/result |
| 24 | import std/string |
| 25 | import std/test.{assert, assert_equal} |
| 26 | |
| 27 | // ---- parsing ------------------------------------------------------------------- |
| 28 | |
| 29 | @test |
| 30 | fn the_rfc_8259_object_example :: |
| 31 | { |
| 32 | let text = "\{\"Image\": \{\"Width\": 800, \"Height\": 600, \"Title\": \"View from 15th Floor\", |
| 33 | \"Thumbnail\": \{\"Url\": \"http://www.example.com/image/481989943\", |
| 34 | \"Height\": 125, \"Width\": 100\}, \"Animated\" : false, \"IDs\": [116, 943, 234, 38793]\}\}" |
| 35 | let thumbnail = |
| 36 | Object( [ ("Url", String("http://www.example.com/image/481989943")) |
| 37 | , ("Height", Int(125)) |
| 38 | , ("Width", Int(100)) |
| 39 | ] |
| 40 | ) |
| 41 | let expected = |
| 42 | Object( [ ( "Image" |
| 43 | , Object( [ ("Width", Int(800)) |
| 44 | , ("Height", Int(600)) |
| 45 | , ("Title", String("View from 15th Floor")) |
| 46 | , ("Thumbnail", thumbnail) |
| 47 | , ("Animated", Bool(False)) |
| 48 | , ("IDs", Array([Int(116), Int(943), Int(234), Int(38793)])) |
| 49 | ] |
| 50 | ) |
| 51 | ) |
| 52 | ] |
| 53 | ) |
| 54 | assert_equal (json.parse text) Ok(expected) |
| 55 | } |
| 56 | |
| 57 | @test |
| 58 | fn the_rfc_8259_array_example :: |
| 59 | { |
| 60 | let text = "[\{\"precision\": \"zip\", \"Latitude\": 37.7668, \"Longitude\": -122.3959, |
| 61 | \"Address\": \"\", \"Zip\": \"94107\"\}]" |
| 62 | let expected = |
| 63 | Array( [ Object( [ ("precision", String("zip")) |
| 64 | , ("Latitude", Float(37.7668)) |
| 65 | , ("Longitude", Float(-122.3959)) |
| 66 | , ("Address", String("")) |
| 67 | , ("Zip", String("94107")) |
| 68 | ] |
| 69 | ) |
| 70 | ] |
| 71 | ) |
| 72 | assert_equal (json.parse text) Ok(expected) |
| 73 | } |
| 74 | |
| 75 | @test |
| 76 | fn scalars_alone_are_json_texts :: |
| 77 | { |
| 78 | assert_equal (json.parse "null") Ok(Null) |
| 79 | assert_equal (json.parse " true ") Ok(Bool(True)) |
| 80 | assert_equal (json.parse "\"hi\"") Ok(String("hi")) |
| 81 | assert_equal (json.parse "\n42\t") Ok(Int(42)) |
| 82 | } |
| 83 | |
| 84 | @test |
| 85 | fn numbers :: |
| 86 | { |
| 87 | assert_equal (json.parse "0") Ok(Int(0)) |
| 88 | assert_equal (json.parse "-0") Ok(Int(0)) |
| 89 | assert_equal (json.parse "-12") Ok(Int(-12)) |
| 90 | assert_equal (json.parse "1.5") Ok(Float(1.5)) |
| 91 | assert_equal (json.parse "1e3") Ok(Float(1000.0)) |
| 92 | assert_equal (json.parse "1E+2") Ok(Float(100.0)) |
| 93 | assert_equal (json.parse "-2.5e-3") Ok(Float(-0.0025)) |
| 94 | assert_equal (json.parse "0.0") Ok(Float(0.0)) |
| 95 | } |
| 96 | |
| 97 | @test |
| 98 | fn an_integer_of_any_size_stays_exact :: |
| 99 | { |
| 100 | let big = "123456789012345678901234567890" |
| 101 | assert_equal (json.parse big |> result.map json.write) Ok(big) |
| 102 | } |
| 103 | |
| 104 | @test |
| 105 | fn escapes :: |
| 106 | { |
| 107 | assert_equal |
| 108 | (json.parse "\"a\\\"b\\\\c\\/d\\be\\ff\\ng\\rh\\ti\"") |
| 109 | Ok(String("a\"b\\c/d\u{8}e\u{c}f\ng\rh\ti")) |
| 110 | assert_equal (json.parse "\"\\u0041\\u00e9\\u20ac\"") Ok(String("Aé€")) |
| 111 | assert_equal (json.parse "\"\\uD83D\\uDE80\"") Ok(String("🚀")) |
| 112 | assert_equal (json.parse "\"\\u00E9\"") Ok(String("é")) |
| 113 | } |
| 114 | |
| 115 | @test |
| 116 | fn text_that_is_not_ascii_passes_through :: |
| 117 | { |
| 118 | assert_equal (json.parse "\"Café 🚀 日本\"") Ok(String("Café 🚀 日本")) |
| 119 | } |
| 120 | |
| 121 | /// JSONTestSuite's `y_` cases (a selection): each must parse. |
| 122 | @test |
| 123 | fn texts_that_must_parse :: |
| 124 | { |
| 125 | let accepted = |
| 126 | [ "[]" |
| 127 | , "\{\}" |
| 128 | , "[[] ]" |
| 129 | , "[\"\"]" |
| 130 | , "[1,null,null,null,2]" |
| 131 | , "[-0]" |
| 132 | , "[1E22]" |
| 133 | , "[0e+1]" |
| 134 | , "[0e1]" |
| 135 | , "[-123]" |
| 136 | , "[123e-10000000]" |
| 137 | , "\{\"a\":\"b\",\"c\":\"d\"\}" |
| 138 | , "\{\"\":0\}" |
| 139 | , "[\"\\u0000\"]" |
| 140 | , "[\"\\uFFFF\"]" |
| 141 | , "[\"\\u005C\"]" |
| 142 | , " [] " |
| 143 | , "[\"\u{7f}\"]" |
| 144 | ] |
| 145 | let refused = list.filter accepted fn t { result.is_error (json.parse t) } |
| 146 | assert_equal refused [] |
| 147 | } |
| 148 | |
| 149 | /// JSONTestSuite's `n_` cases (a selection): each must be refused. |
| 150 | @test |
| 151 | fn texts_that_must_not_parse :: |
| 152 | { |
| 153 | let refused = |
| 154 | [ "" |
| 155 | , " " |
| 156 | , "[" |
| 157 | , "[1,]" |
| 158 | , "[,1]" |
| 159 | , "[1 2]" |
| 160 | , "\{\"a\":1,\}" |
| 161 | , "\{\"a\" 1\}" |
| 162 | , "\{1:1\}" |
| 163 | , "\{'a':1\}" |
| 164 | , "['single']" |
| 165 | , "[01]" |
| 166 | , "[-01]" |
| 167 | , "[1.]" |
| 168 | , "[.5]" |
| 169 | , "[+1]" |
| 170 | , "[1e]" |
| 171 | , "[1e+]" |
| 172 | , "[-]" |
| 173 | , "[Infinity]" |
| 174 | , "[NaN]" |
| 175 | , "[1e999]" |
| 176 | , "[tru]" |
| 177 | , "[nul]" |
| 178 | , "[True]" |
| 179 | , "[\"\\x41\"]" |
| 180 | , "[\"\\u12\"]" |
| 181 | , "[\"\\uD800\"]" |
| 182 | , "[\"\\uDC00\"]" |
| 183 | , "[\"\\uD800\\u0041\"]" |
| 184 | , "[\"a\nb\"]" |
| 185 | , "[\"a\tb\"]" |
| 186 | , "[\"unterminated]" |
| 187 | , "[] []" |
| 188 | , "[]x" |
| 189 | , "\{\"a\":1,\"a\":2\}" |
| 190 | ] |
| 191 | let accepted = list.filter refused fn t { result.is_ok (json.parse t) } |
| 192 | assert_equal accepted [] |
| 193 | } |
| 194 | |
| 195 | @test |
| 196 | fn an_error_says_where :: |
| 197 | { |
| 198 | assert_equal (json.parse "[1, 2,, 3]") Error(Malformed(6, "expected a value")) |
| 199 | assert_equal (json.parse "[1] x") Error(Malformed(4, "text after the value")) |
| 200 | assert_equal |
| 201 | (json.parse "\{\"a\":1,\"a\":2\}") |
| 202 | Error(Malformed(7, "the key \"a\" appears twice")) |
| 203 | } |
| 204 | |
| 205 | @test |
| 206 | fn nesting_is_bounded :: |
| 207 | { |
| 208 | let deep = fn n { string.repeat "[" n <> string.repeat "]" n } |
| 209 | assert (result.is_ok (json.parse (deep (json.max_depth ())))) "at the bound" |
| 210 | assert |
| 211 | ( json.parse (deep (json.max_depth () + 1)) |
| 212 | == Error(Malformed(json.max_depth (), "nesting deeper than 512 levels")) |
| 213 | ) |
| 214 | "past the bound" |
| 215 | } |
| 216 | |
| 217 | @test |
| 218 | fn a_body_must_be_utf8 :: |
| 219 | { |
| 220 | assert_equal (json.parse_bytes (bytes.from_string "[1]")) Ok(Array([Int(1)])) |
| 221 | assert |
| 222 | (result.is_error (json.parse_bytes (bytes.from_list [91, 255, 93]))) |
| 223 | "refused" |
| 224 | } |
| 225 | |
| 226 | // ---- writing ------------------------------------------------------------------- |
| 227 | |
| 228 | @test |
| 229 | fn writing_is_compact :: |
| 230 | { |
| 231 | let v = |
| 232 | Object( [ ("name", String("Orion")) |
| 233 | , ("fuel", Int(80)) |
| 234 | , ("speed", Float(2.5)) |
| 235 | , ("cargo", Array([])) |
| 236 | , ("docked", Bool(True)) |
| 237 | , ("captain", Null) |
| 238 | ] |
| 239 | ) |
| 240 | assert_equal |
| 241 | (json.write v) |
| 242 | "\{\"name\":\"Orion\",\"fuel\":80,\"speed\":2.5,\"cargo\":[],\"docked\":true,\"captain\":null\}" |
| 243 | } |
| 244 | |
| 245 | @test |
| 246 | fn writing_escapes_what_json_needs :: |
| 247 | { |
| 248 | assert_equal |
| 249 | (json.write (String("say \"hi\"\\\n\t\u{1}/é"))) |
| 250 | "\"say \\\"hi\\\"\\\\\\n\\t\\u0001/é\"" |
| 251 | } |
| 252 | |
| 253 | @test |
| 254 | fn floats_are_written_as_json_numbers :: |
| 255 | { |
| 256 | assert_equal (json.write (Float(1.0))) "1.0" |
| 257 | assert_equal (json.write (Float(1.0e300))) "1e300" |
| 258 | assert_equal (json.write (Float(-1.5e-7))) "-1.5e-7" |
| 259 | } |
| 260 | |
| 261 | @should_panic "is not a JSON number" |
| 262 | @test |
| 263 | fn an_infinity_is_not_written :: |
| 264 | { |
| 265 | let _ = json.write (Float(1.0 / 0.0)) |
| 266 | } |
| 267 | |
| 268 | @test |
| 269 | fn pretty_indents_by_two :: |
| 270 | { |
| 271 | let v = |
| 272 | Object( [ ("a", Array([Int(1), Int(2)])) |
| 273 | , ("b", Object([])) |
| 274 | , ("c", Array([])) |
| 275 | ] |
| 276 | ) |
| 277 | assert_equal |
| 278 | (json.pretty v) |
| 279 | "\{\n \"a\": [\n 1,\n 2\n ],\n \"b\": \{\},\n \"c\": []\n\}" |
| 280 | } |
| 281 | |
| 282 | @test |
| 283 | fn show_is_write :: |
| 284 | { |
| 285 | assert_equal "{Array([Int(1), Null])}" "[1,null]" |
| 286 | } |
| 287 | |
| 288 | @test |
| 289 | fn get_reads_a_field :: |
| 290 | { |
| 291 | let v = Object([("a", Int(1))]) |
| 292 | assert_equal (json.get v "a") Some(Int(1)) |
| 293 | assert_equal (json.get v "b") None |
| 294 | assert_equal (json.get (Int(1)) "a") None |
| 295 | } |
| 296 | |
| 297 | // ---- deriving ------------------------------------------------------------------ |
| 298 | |
| 299 | @derive Eq Debug ToJson |
| 300 | type Status { Docked, InOrbit, InTransit(String, Int) } |
| 301 | |
| 302 | @derive Eq Debug ToJson |
| 303 | type Ship { Ship(symbol: String, fuel: Int, status: Status, crew: Option<Int>) } |
| 304 | |
| 305 | @test |
| 306 | fn a_derived_record_is_an_object :: |
| 307 | { |
| 308 | assert_equal |
| 309 | (json.encode (Ship("ORION-1", 80, Docked, None))) |
| 310 | "\{\"symbol\":\"ORION-1\",\"fuel\":80,\"status\":\"Docked\",\"crew\":null\}" |
| 311 | assert_equal |
| 312 | (json.encode [InTransit("SOL-A2", 30), InOrbit]) |
| 313 | "[\{\"InTransit\":[\"SOL-A2\",30]\},\"InOrbit\"]" |
| 314 | } |
| 315 | |
| 316 | // ---- properties ---------------------------------------------------------------- |
| 317 | |
| 318 | fn any_value :: depth: Int -> Value / <random> |
| 319 | { |
| 320 | let leaves = |
| 321 | [ (1, fn { Null }) |
| 322 | , (1, fn { Bool(gen.bool ()) }) |
| 323 | , (3, fn { Int(gen.int ~low: (-1000000000000) ~high: 1000000000000) }) |
| 324 | , (2, fn { Float(float_of (gen.int ~low: (-1000000) ~high: 1000000)) }) |
| 325 | , (3, fn { String(any_text ()) }) |
| 326 | ] |
| 327 | let branches = |
| 328 | [ (2, fn { Array(gen.list ~min: 0 ~max: 4 fn { any_value (depth - 1) }) }) |
| 329 | , ( 2 |
| 330 | , fn |
| 331 | { |
| 332 | Object( distinct |
| 333 | ( gen.list |
| 334 | ~min: 0 |
| 335 | ~max: 4 |
| 336 | fn { (any_text (), any_value (depth - 1)) } |
| 337 | ) |
| 338 | ) |
| 339 | } |
| 340 | ) |
| 341 | ] |
| 342 | let pick = |
| 343 | gen.weighted (if depth <= 0 { leaves } else { list.append leaves branches }) |
| 344 | pick () |
| 345 | } |
| 346 | |
| 347 | fn float_of :: n: Int -> Float |
| 348 | { |
| 349 | json.parse "{n}.25" |
| 350 | |> result.map fn v { match v { Float(x) -> x, _ -> 0.0 } } |
| 351 | |> result.unwrap_or 0.0 |
| 352 | } |
| 353 | |
| 354 | fn any_text :: -> String / <random> |
| 355 | { |
| 356 | gen.string ~alphabet: "ab\"\\/\n\t\u{1}\u{1f} é€🚀" ~min: 0 ~max: 6 |
| 357 | } |
| 358 | |
| 359 | fn distinct :: entries: List<(String, Value)> -> List<(String, Value)> |
| 360 | { |
| 361 | list.fold |
| 362 | entries |
| 363 | [] |
| 364 | fn kept entry |
| 365 | { |
| 366 | if list.any kept fn k: (String, Value) { k.0 == entry.0 } |
| 367 | { |
| 368 | kept |
| 369 | } |
| 370 | else |
| 371 | { |
| 372 | list.append kept [entry] |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | @test |
| 378 | fn parsing_undoes_writing :: -> () / <random> |
| 379 | { |
| 380 | check.property |
| 381 | ~runs: 300 |
| 382 | fn { any_value 3 } |
| 383 | fn v { json.parse (json.write v) == Ok(v) } |
| 384 | } |
| 385 | |
| 386 | @test |
| 387 | fn parsing_undoes_pretty_writing :: -> () / <random> |
| 388 | { |
| 389 | check.property |
| 390 | ~runs: 200 |
| 391 | fn { any_value 3 } |
| 392 | fn v { json.parse (json.pretty v) == Ok(v) } |
| 393 | } |
| 394 | |
| 395 | @test |
| 396 | fn every_string_round_trips :: -> () / <random> |
| 397 | { |
| 398 | check.property |
| 399 | ~runs: 300 |
| 400 | fn { gen.string ~alphabet: (gen.printable ()) ~min: 0 ~max: 40 } |
| 401 | fn s { json.parse (json.write (String(s))) == Ok(String(s)) } |
| 402 | } |