Get updates via , Github, or RSS | About

Episode 12: Generative Testing Oct 27, 2016

elm-community/elm-test has the ability to do generative testing. In this episode we look at a basic use case for it and discover how it finds bugs that would be much harder to find with only hand-written unit tests.

Examples

import Expect
import Fuzz
import Test exposing (Test, describe, test)
import Test.Runner.Html

faceCardsFuzzer : Fuzzer Hand
faceCardsFuzzer =
  Fuzz.map (\cards -> addCardsToHand cards newHand)
    (Fuzz.list
      (Fuzz.map2 deserializeCard
        (Fuzz.intRange 8 11)
        (Fuzz.intRange 0 3)))


splitTests : Test
splitTests =
  describe "isSplittable"
    [ test "Two different cards" <| \() -> isSplittable aS9D |> Expect.equal False
    , test "Different cards, same suit" <| \() -> isSplittable aS9S |> Expect.equal False
    , test "Same cards" <| \() -> isSplittable aSaD |> Expect.equal True
    , test "Two face cards" <| \() -> isSplittable tHtC |> Expect.equal True
    , fuzz faceCardsFuzzer "Any face cards" <|
        \(BjHand cards) ->
          let expect = List.length cards == 2
          in isSplittable (BjHand cards) |> Expect.equal expect
    ]

main : Program Never
main =
  Test.Runner.Html.run splitTests