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#31 · main · 4 min 11 s
0Étoiles
0Forks
3Issues
| 1 | import form. |
| 2 | { type FieldError |
| 3 | , type Form |
| 4 | , type Schema |
| 5 | , CustomError |
| 6 | , MustBeAccepted |
| 7 | , MustBeColour |
| 8 | , MustBeEmail |
| 9 | , MustBeFloatLessThan |
| 10 | , MustBeInt |
| 11 | , MustBeIntLessThan |
| 12 | , MustBeIntMoreThan |
| 13 | , MustBePhoneNumber |
| 14 | , MustBeStringLengthLessThan |
| 15 | , MustBeStringLengthMoreThan |
| 16 | , MustConfirm |
| 17 | } |
| 18 | import std/list |
| 19 | import std/test.{assert, assert_equal} |
| 20 | |
| 21 | @derive Eq Show Debug |
| 22 | type SignUp { SignUp(email: String, password: String) } |
| 23 | |
| 24 | @derive Eq Show Debug |
| 25 | type Visitor { Guest(email: String), Member(name: String) } |
| 26 | |
| 27 | type Starter { Squirtle, Bulbasaur, Charmander } |
| 28 | |
| 29 | /// The README's example: an email, a password of 8+ characters, and its |
| 30 | /// confirmation. |
| 31 | fn signup_schema :: -> Schema<SignUp> |
| 32 | { |
| 33 | use email <- form.field "email" (form.parse_email ()) |
| 34 | use password <- |
| 35 | form.field |
| 36 | "password" |
| 37 | ({ form.parse_string () |> form.check_string_length_more_than 7 }) |
| 38 | use _ <- |
| 39 | form.field |
| 40 | "confirm" |
| 41 | ({ form.parse_string () |> form.check_confirms password }) |
| 42 | form.success SignUp(email: email, password: password) |
| 43 | } |
| 44 | |
| 45 | fn signup_form :: values: List<(String, String)> -> Result<SignUp, Form<SignUp>> |
| 46 | { |
| 47 | form.new (signup_schema ()) |> form.add_values values |> form.run |
| 48 | } |
| 49 | |
| 50 | fn expect_ok :: result: Result<a, Form<a>> -> a |
| 51 | { |
| 52 | match result |
| 53 | { Ok(value) -> value, Error(_) -> panic "expected the form to be valid" } |
| 54 | } |
| 55 | |
| 56 | fn expect_error :: result: Result<a, Form<a>> -> Form<a> |
| 57 | { |
| 58 | match result |
| 59 | { Ok(_) -> panic "expected the form to be invalid", Error(f) -> f } |
| 60 | } |
| 61 | |
| 62 | @test |
| 63 | fn a_valid_form_decodes :: |
| 64 | { |
| 65 | let signup = |
| 66 | expect_ok |
| 67 | ( signup_form |
| 68 | [ ("email", "ada@example.com") |
| 69 | , ("password", "correct horse") |
| 70 | , ("confirm", "correct horse") |
| 71 | ] |
| 72 | ) |
| 73 | assert_equal |
| 74 | signup |
| 75 | SignUp(email: "ada@example.com", password: "correct horse") |
| 76 | } |
| 77 | |
| 78 | @test |
| 79 | fn every_bad_field_reports_its_errors :: |
| 80 | { |
| 81 | let f = |
| 82 | expect_error |
| 83 | ( signup_form |
| 84 | [ ("email", "not an email") |
| 85 | , ("password", "short") |
| 86 | , ("confirm", "different") |
| 87 | ] |
| 88 | ) |
| 89 | assert_equal (form.field_errors f "email") [MustBeEmail] |
| 90 | assert_equal (form.field_errors f "password") [MustBeStringLengthMoreThan(7)] |
| 91 | assert_equal (form.field_errors f "confirm") [MustConfirm] |
| 92 | // The values stay on the form, for re-rendering. |
| 93 | assert_equal (form.field_value f "email") "not an email" |
| 94 | } |
| 95 | |
| 96 | @test |
| 97 | fn missing_fields_parse_as_absent :: |
| 98 | { |
| 99 | let f = expect_error (signup_form []) |
| 100 | assert_equal (form.field_errors f "email") [MustBeEmail] |
| 101 | assert_equal (form.field_errors f "password") [MustBeStringLengthMoreThan(7)] |
| 102 | } |
| 103 | |
| 104 | @test |
| 105 | fn a_parse_failure_skips_the_checks_after_it :: |
| 106 | { |
| 107 | let schema = |
| 108 | { |
| 109 | use age <- |
| 110 | form.field |
| 111 | "age" |
| 112 | ( { |
| 113 | form.parse_int () |
| 114 | |> form.check_int_more_than 0 |
| 115 | |> form.check_int_less_than 150 |
| 116 | } |
| 117 | ) |
| 118 | form.success age |
| 119 | } |
| 120 | let f = |
| 121 | expect_error |
| 122 | (form.new schema |> form.add_values [("age", "old")] |> form.run) |
| 123 | assert_equal (form.field_errors f "age") [MustBeInt] |
| 124 | } |
| 125 | |
| 126 | @test |
| 127 | fn checks_do_not_short_circuit_each_other :: |
| 128 | { |
| 129 | let schema = |
| 130 | { |
| 131 | use name <- |
| 132 | form.field |
| 133 | "name" |
| 134 | ( { |
| 135 | form.parse_string () |
| 136 | |> form.check_not_empty |
| 137 | |> form.check_string_length_less_than 4 |
| 138 | } |
| 139 | ) |
| 140 | form.success name |
| 141 | } |
| 142 | let f = |
| 143 | expect_error |
| 144 | (form.new schema |> form.add_values [("name", "longname")] |> form.run) |
| 145 | assert_equal (form.field_errors f "name") [MustBeStringLengthLessThan(4)] |
| 146 | } |
| 147 | |
| 148 | @test |
| 149 | fn int_and_float_ranges :: |
| 150 | { |
| 151 | let schema = |
| 152 | { |
| 153 | use age <- |
| 154 | form.field |
| 155 | "age" |
| 156 | ( { |
| 157 | form.parse_int () |
| 158 | |> form.check_int_more_than 0 |
| 159 | |> form.check_int_less_than 150 |
| 160 | } |
| 161 | ) |
| 162 | use price <- |
| 163 | form.field |
| 164 | "price" |
| 165 | ( { |
| 166 | form.parse_float () |
| 167 | |> form.check_float_more_than 0.0 |
| 168 | |> form.check_float_less_than 100.0 |
| 169 | } |
| 170 | ) |
| 171 | form.success (age, price) |
| 172 | } |
| 173 | assert_equal |
| 174 | ( expect_ok |
| 175 | ( form.new schema |
| 176 | |> form.add_values [("age", "42"), ("price", "9.99")] |
| 177 | |> form.run |
| 178 | ) |
| 179 | ) |
| 180 | (42, 9.99) |
| 181 | // An int is a valid float. |
| 182 | assert_equal |
| 183 | ( expect_ok |
| 184 | ( form.new schema |
| 185 | |> form.add_values [("age", "42"), ("price", "9")] |
| 186 | |> form.run |
| 187 | ) |
| 188 | ) |
| 189 | (42, 9.0) |
| 190 | let f = |
| 191 | expect_error |
| 192 | ( form.new schema |
| 193 | |> form.add_values [("age", "0"), ("price", "100.0")] |
| 194 | |> form.run |
| 195 | ) |
| 196 | assert_equal (form.field_errors f "age") [MustBeIntMoreThan(0)] |
| 197 | // 100.0 is not less than 100.0: the limit is strict. |
| 198 | assert_equal (form.field_errors f "price") [MustBeFloatLessThan(100.0)] |
| 199 | } |
| 200 | |
| 201 | @test |
| 202 | fn optional_fields :: |
| 203 | { |
| 204 | let schema = |
| 205 | { |
| 206 | use age <- form.field "age" (form.parse_optional (form.parse_int ())) |
| 207 | form.success age |
| 208 | } |
| 209 | assert_equal (expect_ok (form.new schema |> form.run)) None |
| 210 | assert_equal |
| 211 | (expect_ok (form.new schema |> form.add_values [("age", "")] |> form.run)) |
| 212 | None |
| 213 | assert_equal |
| 214 | (expect_ok (form.new schema |> form.add_values [("age", "42")] |> form.run)) |
| 215 | Some(42) |
| 216 | let f = |
| 217 | expect_error |
| 218 | (form.new schema |> form.add_values [("age", "old")] |> form.run) |
| 219 | assert_equal (form.field_errors f "age") [MustBeInt] |
| 220 | } |
| 221 | |
| 222 | @test |
| 223 | fn repeated_fields_parse_as_a_list :: |
| 224 | { |
| 225 | let schema = |
| 226 | { |
| 227 | use tags <- form.field "tag" (form.parse_list (form.parse_int ())) |
| 228 | form.success tags |
| 229 | } |
| 230 | assert_equal |
| 231 | ( expect_ok |
| 232 | ( form.new schema |
| 233 | |> form.add_values [("tag", "1"), ("tag", "2"), ("tag", "3")] |
| 234 | |> form.run |
| 235 | ) |
| 236 | ) |
| 237 | [1, 2, 3] |
| 238 | // A bad element fails the field; the errors deduplicate. |
| 239 | let f = |
| 240 | expect_error |
| 241 | ( form.new schema |
| 242 | |> form.add_values [("tag", "1"), ("tag", "x"), ("tag", "x")] |
| 243 | |> form.run |
| 244 | ) |
| 245 | assert_equal (form.field_errors f "tag") [MustBeInt] |
| 246 | } |
| 247 | |
| 248 | @test |
| 249 | fn checkboxes :: |
| 250 | { |
| 251 | let schema = |
| 252 | { |
| 253 | use agreed <- |
| 254 | form.field "terms" ({ form.parse_checkbox () |> form.check_accepted }) |
| 255 | form.success agreed |
| 256 | } |
| 257 | assert_equal |
| 258 | ( expect_ok |
| 259 | (form.new schema |> form.add_values [("terms", "on")] |> form.run) |
| 260 | ) |
| 261 | True |
| 262 | // An unchecked checkbox sends no value at all. |
| 263 | let f = expect_error (form.new schema |> form.run) |
| 264 | assert_equal (form.field_errors f "terms") [MustBeAccepted] |
| 265 | } |
| 266 | |
| 267 | @test |
| 268 | fn phone_numbers :: |
| 269 | { |
| 270 | let schema = |
| 271 | { |
| 272 | use phone <- form.field "phone" (form.parse_phone_number ()) |
| 273 | form.success phone |
| 274 | } |
| 275 | assert_equal |
| 276 | ( expect_ok |
| 277 | ( form.new schema |
| 278 | |> form.add_values [("phone", "+1 (555) 123-4567")] |
| 279 | |> form.run |
| 280 | ) |
| 281 | ) |
| 282 | "15551234567" |
| 283 | let f = |
| 284 | expect_error |
| 285 | (form.new schema |> form.add_values [("phone", "12")] |> form.run) |
| 286 | assert_equal (form.field_errors f "phone") [MustBePhoneNumber] |
| 287 | } |
| 288 | |
| 289 | @test |
| 290 | fn colours :: |
| 291 | { |
| 292 | let schema = |
| 293 | { |
| 294 | use colour <- form.field "colour" (form.parse_colour ()) |
| 295 | form.success colour |
| 296 | } |
| 297 | assert_equal |
| 298 | ( expect_ok |
| 299 | (form.new schema |> form.add_values [("colour", "#a0FF00")] |> form.run) |
| 300 | ) |
| 301 | "#a0FF00" |
| 302 | list.each |
| 303 | ["a0FF00", "#12345", "#1234567", "#12345g"] |
| 304 | fn value |
| 305 | { |
| 306 | let f = |
| 307 | expect_error |
| 308 | (form.new schema |> form.add_values [("colour", value)] |> form.run) |
| 309 | assert_equal (form.field_errors f "colour") [MustBeColour] |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | @test |
| 314 | fn custom_parsers :: |
| 315 | { |
| 316 | let parse_starter = |
| 317 | form.parse |
| 318 | fn input |
| 319 | { |
| 320 | match input |
| 321 | { ["Squirtle", ..] -> Ok(Squirtle) |
| 322 | , ["Bulbasaur", ..] -> Ok(Bulbasaur) |
| 323 | , ["Charmander", ..] -> Ok(Charmander) |
| 324 | , _ -> Error((Squirtle, "must be a starter Pokémon")) |
| 325 | } |
| 326 | } |
| 327 | let schema = |
| 328 | { |
| 329 | use starter <- form.field "starter" parse_starter |
| 330 | form.success starter |
| 331 | } |
| 332 | let f = |
| 333 | expect_error |
| 334 | (form.new schema |> form.add_values [("starter", "Pikachu")] |> form.run) |
| 335 | assert_equal |
| 336 | (form.field_error_messages f "starter") |
| 337 | ["must be a starter Pokémon"] |
| 338 | } |
| 339 | |
| 340 | @test |
| 341 | fn custom_checks :: |
| 342 | { |
| 343 | let schema = |
| 344 | { |
| 345 | use name <- |
| 346 | form.field |
| 347 | "name" |
| 348 | ( { |
| 349 | form.parse_string () |
| 350 | |> form.check |
| 351 | fn text |
| 352 | { |
| 353 | if text == "bother" |
| 354 | { |
| 355 | Error("must not be rude") |
| 356 | } |
| 357 | else |
| 358 | { |
| 359 | Ok(text) |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | ) |
| 364 | form.success name |
| 365 | } |
| 366 | let f = |
| 367 | expect_error |
| 368 | (form.new schema |> form.add_values [("name", "bother")] |> form.run) |
| 369 | assert_equal (form.field_errors f "name") [CustomError("must not be rude")] |
| 370 | } |
| 371 | |
| 372 | @test |
| 373 | fn prefilled_values :: |
| 374 | { |
| 375 | let f = |
| 376 | form.new (signup_schema ()) |
| 377 | |> form.add_int "one" 100 |
| 378 | |> form.add_string "one" "Hello" |
| 379 | |> form.add_string "two" "Hi!" |
| 380 | assert_equal (form.field_values f "one") ["Hello", "100"] |
| 381 | assert_equal (form.field_value f "one") "Hello" |
| 382 | assert_equal (form.field_value f "missing") "" |
| 383 | // set_values replaces rather than appends. |
| 384 | let replaced = form.set_values f [("three", "3")] |
| 385 | assert_equal (form.all_values replaced) [("three", "3")] |
| 386 | } |
| 387 | |
| 388 | @test |
| 389 | fn added_errors_surface_with_the_rest :: |
| 390 | { |
| 391 | let f = |
| 392 | form.new (signup_schema ()) |
| 393 | |> form.add_values |
| 394 | [ ("email", "taken@example.com") |
| 395 | , ("password", "correct horse") |
| 396 | , ("confirm", "correct horse") |
| 397 | ] |
| 398 | // e.g. the address is already registered: checked outside the schema. |
| 399 | let f = form.add_error f "email" form.MustBeUnique |
| 400 | assert_equal (form.field_errors f "email") [form.MustBeUnique] |
| 401 | assert_equal (form.field_error_messages f "email") ["is already in use"] |
| 402 | } |
| 403 | |
| 404 | @test |
| 405 | fn translations :: |
| 406 | { |
| 407 | assert_equal (form.en_gb MustBeColour) "must be a hex colour code" |
| 408 | assert_equal (form.en_us MustBeColour) "must be a hex color code" |
| 409 | assert_equal (form.en_gb form.MustBePresent) "must not be blank" |
| 410 | assert_equal (form.en_gb MustBeIntMoreThan(0)) "must be more than 0" |
| 411 | assert_equal |
| 412 | (form.en_gb MustBeStringLengthLessThan(20)) |
| 413 | "must be less than 20 characters" |
| 414 | assert_equal (form.en_gb CustomError("custom")) "custom" |
| 415 | // A custom translator via `language`. |
| 416 | let bad = |
| 417 | expect_error |
| 418 | ( form.new (signup_schema ()) |
| 419 | |> form.language fn _error { "ist ein Pflichtfeld" } |
| 420 | |> form.add_values [] |
| 421 | |> form.run |
| 422 | ) |
| 423 | assert_equal (form.field_error_messages bad "email") ["ist ein Pflichtfeld"] |
| 424 | } |
| 425 | |
| 426 | @test |
| 427 | fn variants_through_a_kind_field :: |
| 428 | { |
| 429 | // A hidden input says which variant the form is; the rest of the schema |
| 430 | // follows from it. |
| 431 | let schema = |
| 432 | { |
| 433 | use kind <- form.field "kind" (form.parse_string ()) |
| 434 | match kind |
| 435 | { "guest" -> |
| 436 | { |
| 437 | use email <- form.field "email" (form.parse_email ()) |
| 438 | form.success Guest(email) |
| 439 | } |
| 440 | , _ -> |
| 441 | { |
| 442 | use name <- |
| 443 | form.field |
| 444 | "username" |
| 445 | ({ form.parse_string () |> form.check_not_empty }) |
| 446 | form.success Member(name) |
| 447 | } |
| 448 | } |
| 449 | } |
| 450 | assert_equal |
| 451 | ( expect_ok |
| 452 | ( form.new schema |
| 453 | |> form.add_values [("kind", "guest"), ("email", "g@example.com")] |
| 454 | |> form.run |
| 455 | ) |
| 456 | ) |
| 457 | Guest("g@example.com") |
| 458 | assert_equal |
| 459 | ( expect_ok |
| 460 | ( form.new schema |
| 461 | |> form.add_values [("kind", "user"), ("username", "ada")] |
| 462 | |> form.run |
| 463 | ) |
| 464 | ) |
| 465 | Member("ada") |
| 466 | } |
| 467 | |
| 468 | @test |
| 469 | fn a_new_form_is_clean :: |
| 470 | { |
| 471 | let f = form.new (signup_schema ()) |
| 472 | assert_equal (form.all_errors f) [] |
| 473 | assert_equal (form.all_values f) [] |
| 474 | } |