elmfuzz-testing

How to write fuzz tests for List.partition function in ELM?


I don't have much experience with elm and I am trying to understand how fuzz tests work. For example how can I write a fuzz test for the List.partition function?

It is defined like:

partition : comparable -> List comparable -> (List comparable, List comparable)
partition pivot l = 
  (filter (\x -> x < pivot) l, filter (\x -> x >= pivot) l)

Solution

  • I guess one approach would be to install the required modules first. Using the terminal (you can follow the steps here):

    $ npm install elm-test -g
    $ elm-test init
    

    After installing, you should import the 3 required libraries:

    import Expect exposing (Expectation)
    import Fuzz exposing (Fuzzer, int, list, string)
    import Test exposing (..)
    

    Finally, you should be able to write your tests, similar to the testing of the addsOne function from here.

    In order to test the partition method, I can think of 2 approaches:

    1. I would create a helper method that sorts a List of comparables. Then I would make a union between the 2 partitions, sort the union and the initial list and then I would compare them with Expect.equal.

    2. I would create 2 testing methods, one that checks that all comparables in the given list are smaller than the given pivot, and one method that checks if all the comparables are higher than the given pivot.

    I will try to come back with some code if needed.