// Property checks find a failing case and shrink it to a small one, the // same for a seed. import check import gen import std/list import std/random.{type random} import std/string import std/test.{assert, assert_equal} fn sums_of_lists :: -> List / { gen.list ~min: 0 ~max: 10 fn { gen.int ~low: 0 ~high: 100 } } fn small_sum :: xs: List -> Bool { list.fold xs 0 fn a b { a + b } <= 10 } fn texts :: -> String / { gen.string ~alphabet: "abc" ~min: 0 ~max: 10 } fn has_no_a :: text: String -> Bool { !string.contains text "a" } @test fn a_property_that_holds_has_no_counterexample :: { assert_equal ( check.counterexample ~seed: 1 ~runs: 100 sums_of_lists fn xs { list.length xs <= 10 } ) None } @test fn a_failing_list_shrinks_to_one_item_just_over_the_limit :: { assert_equal (check.counterexample ~seed: 1 ~runs: 100 sums_of_lists small_sum) Some([11]) } @test fn a_failing_string_shrinks_to_the_one_character_that_breaks_it :: { assert_equal (check.counterexample ~seed: 2 ~runs: 100 texts has_no_a) Some("a") } @test fn a_seed_finds_the_same_case :: { let first = check.counterexample ~seed: 9 ~runs: 50 texts has_no_a assert_equal (check.counterexample ~seed: 9 ~runs: 50 texts has_no_a) first } @test @should_panic "property does not hold (seed 1" fn a_seeded_check_panics_with_the_seed :: { check.seeded ~seed: 1 ~runs: 100 sums_of_lists small_sum } @test @should_panic "[11]" fn its_message_shows_the_shrunk_case :: { check.seeded ~seed: 1 ~runs: 100 sums_of_lists small_sum } @test fn a_property_takes_its_seed_from_random :: { random.with_seed 4 fn { check.property ~runs: 50 texts fn t { string.length t <= 10 } } assert True "no panic" }