### Generate example values with Fuzz.examples Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/fuzz.md The signature for `examples` and a REPL session showing its usage. `examples` generates `n` example values using `n` as the PRNG seed, returning `[]` on rejection. The example output shows a list of 20 integers within the range 20-50. ```elm examples : Int -> Fuzzer a -> List a ``` ```elm > Fuzz.examples 20 (Fuzz.intRange 20 50) [42,45,32,26,33,29,41,45,23,45,34,23,22,42,29,27,41,43,30,50] : List Int ``` -------------------------------- ### examples Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/fuzz.md Function to generate example values from a fuzzer for REPL exploration. ```APIDOC ## `examples` ### Description Generates `n` example values for REPL exploration. Uses `n` as the PRNG seed. Returns `[]` in case of rejection. ### Signature ```elm examples : Int -> Fuzzer a -> List a ``` ### Parameters | Parameter | Type | Required | Default | Description | |-----------|------|----------|---------|-------------| | `n` | `Int` | yes | — | Seed *and* number of values | | `fuzzer` | `Fuzzer a` | yes | — | Source fuzzer | ### Example ```elm > Fuzz.examples 20 (Fuzz.intRange 20 50) [42,45,32,26,33,29,41,45,23,45,34,23,22,42,29,27,41,43,30,50] : List Int ``` ### Source `src/Fuzz.elm:1845` ``` -------------------------------- ### all usage example Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/expect.md Example of using Expect.all to check multiple expectations on a user record. ```elm Expect.all [ user.name |> Expect.notEqual "" , user.age |> Expect.atLeast 0 ] ``` -------------------------------- ### Classify examples with labelExamples Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/fuzz.md Signature for `labelExamples`, which generates examples and classifies them by the given predicates. Returns `(labels, example)` for each found class; all base labels are always returned (`Nothing` when no example found), overlapping labels produce combination entries, and it returns `[]` on rejection. ```elm labelExamples : Int -> List ( String, a -> Bool ) -> Fuzzer a -> List ( List String, Maybe a ) ``` -------------------------------- ### Simulate input event example Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/test-html-event.md Example of simulating an input event and expecting the resulting message. ```elm Html.input [ onInput Change ] [] |> Query.fromHtml |> Event.simulate (Event.input "cats") |> Event.expect (Change "cats") ``` -------------------------------- ### Install dependencies and run tests Source: https://github.com/elm-explorations/test/blob/master/PUBLISHING.md Installs npm dependencies and runs the test suite to ensure everything passes before publishing. ```bash npm install && npm run test ``` -------------------------------- ### equal usage example Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/expect.md Example of using Expect.equal to compare a list from splitting a string. ```elm String.split " " "Betty Botter bought some butter" |> Expect.equal [ "Betty", "Botter", "bought", "some", "butter" ] ``` -------------------------------- ### andMap usage example Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/fuzz-advanced.md Example usage of `Fuzz.andMap` to combine multiple fuzzers, starting with `Fuzz.constant fn` and piping through `andMap` for each argument. ```elm Fuzz.constant fn |> Fuzz.andMap fuzzerA |> Fuzz.andMap fuzzerB |> Fuzz.andMap fuzzerC ``` -------------------------------- ### all selector usage example Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/test-html-selector.md Example usage of the all selector to match a button with class 'btn' and text 'Reply'. ```elm replyBtnSelector : Selector replyBtnSelector = all [ class "btn", text "Reply" ] ``` -------------------------------- ### index usage example Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/test-html-query.md Example usage of Query.index to select the second li element and assert it has the text 'second item'. ```elm Query.findAll [ tag "li" ] |> Query.index 1 |> Query.has [ text "second item" ] ``` -------------------------------- ### toResult usage example Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/test-html-event.md Example of using toResult to assert the result of simulating an input event. ```elm Html.input [ onInput Change ] [] |> Query.fromHtml |> Event.simulate (Event.input "cats") |> Event.toResult |> Expect.equal (Ok (Change "cats")) ``` -------------------------------- ### fuzzWithExamples example usage Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/test.md Example usage of fuzzWithExamples with hardcoded regression inputs (NaN and Infinity) and a property that compares input to zero. ```elm Test.fuzzWithExamples "compare with zero is only EQ when input also is zero" { runs = 100, distribution = Test.noDistribution } Fuzz.float [ ( "NaN", 0 / 0 ) , ( "Infinity", 1 / 0 ) ] <| \input -> compare input 0 |> Expect.expect EQ ``` -------------------------------- ### Example: atLeast with expectDistribution Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/test-distribution.md Example usage of atLeast with expectDistribution in a fuzzWith configuration, requiring at least 30% of inputs to be multiples of 3. ```elm fuzzWith "description" { runs = 10000 , distribution = expectDistribution [ ( atLeast 30, "multiple of 3", \n -> (n |> modBy 3) == 0 ) ] } ``` -------------------------------- ### atLeast usage example Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/expect.md Example usage of atLeast with pipeline style. The compared value flows in as the second argument. ```elm List.length [] |> Expect.atMost 1 -- passes: 0 <= 1 ``` -------------------------------- ### todo usage example Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/test.md Example of using `todo` inside a `describe` block to mark unimplemented test cases. ```elm describe "a new thing" [ todo "does what is expected in the common case" , todo "correctly handles an edge case I just thought of" ] ``` -------------------------------- ### fromTest usage example Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/test-runner.md Example call to `fromTest` with 100 runs, a seed from `Random.initialSeed 42`, and a test suite. This is the recommended way to run tests with a fixed seed. ```elm Test.Runner.fromTest 100 (Random.initialSeed 42) mySuite ``` -------------------------------- ### oneOf usage example Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/expect.md Example of using Expect.oneOf to check that at least one condition holds. ```elm Expect.oneOf [ user.isPremiumMember |> Expect.equal True , user.coupon |> Expect.notEqual Nothing ] ``` -------------------------------- ### fuzzWith example usage Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/test.md Example usage of fuzzWith with a custom number of runs and no distribution. It tests that reversing a list does not affect membership. ```elm import Expect import Fuzz exposing (int, pair) import Test exposing (fuzzWith, noDistribution) fuzzWith "List.reverse never influences List.member" { runs = 4200, distribution = noDistribution } (pair (list int) int) <| \(nums, target) -> List.member target (List.reverse nums) |> Expect.equal (List.member target nums) ``` -------------------------------- ### oneOfValues usage example Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/fuzz-advanced.md Example usage of Fuzz.oneOfValues with a list of concrete values. ```elm Fuzz.oneOfValues [ 999, -42 ] ``` -------------------------------- ### Query.has usage example Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/test-html-query.md Example usage of Query.has to assert that the element matches all given selectors. On failure, each selector is marked with ✓/✗ under the query trace. ```elm Query.find [ tag "ul" ] |> Query.has [ tag "ul", classes [ "items", "active" ] ] ``` -------------------------------- ### Write unit and fuzz tests with describe, test, Expect.equal, and fuzz Source: https://github.com/elm-explorations/test/blob/master/README.md Example test suite using describe, test, Expect.equal, and fuzz. The fuzz test runs 100 times with randomly-generated inputs. Expect.equal is designed to be used in pipeline style. ```elm suite : Test suite = describe "The String module" [ describe "String.reverse" -- Nest as many descriptions as you like. [ test "has no effect on a palindrome" <| \_ -> let palindrome = "hannah" in Expect.equal palindrome (String.reverse palindrome) -- Expect.equal is designed to be used in pipeline style, like this. , test "reverses a known string" <| \_ -> "ABCDEFG" |> String.reverse |> Expect.equal "GFEDCBA" -- fuzz runs the test 100 times with randomly-generated inputs! , fuzz "restores the original string if you run it again" string <| \randomlyGeneratedString -> randomlyGeneratedString |> String.reverse |> String.reverse |> Expect.equal randomlyGeneratedString ] ] ``` -------------------------------- ### Expect.ok usage example Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/expect.md Example usage of `Expect.ok` with a `Result` from `String.toInt`. Passes if the result is `Ok _`; fails with `Comparison "Ok _" (toString e)` on `Err e`. ```elm String.toInt "20" |> Result.fromMaybe "not an int" |> Expect.ok ``` -------------------------------- ### Query.each usage example Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/test-html-query.md Example usage of Query.each to apply a Single-based expectation to each matched element. Reports which element number failed. ```elm Query.findAll [ tag "ul" ] |> Query.each (Expect.passesAll [ Query.has [ tag "ul" ] , Query.has [ classes [ "items", "active" ] ] ] ) ``` -------------------------------- ### labelExamples Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/fuzz.md Function to generate examples and classify them by given predicates. ```APIDOC ## `labelExamples` ### Description Generates examples and classifies them by the predicates; returns `(labels, example)` for each found class. All base labels are always returned (`Nothing` when no example found); overlapping labels produce combination entries. Returns `[]` on rejection. ### Signature ```elm labelExamples : Int -> List ( String, a -> Bool ) -> Fuzzer a -> List ( List String, Maybe a ) ``` ### Parameters | Parameter | Type | Required | Default | Description | |-----------|------|----------|---------|-------------| | `n` | `Int` | yes | — | Seed and value count | | `labels` | `List (String, a -> Bool)` | yes | — | Classification predicates | | `fuzzer` | `Fuzzer a` | yes | — | Source fuzzer | ### Source `src/Fuzz.elm:1902` ``` -------------------------------- ### fuzzWithExamples Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/test.md Function to run a fuzz test with hardcoded examples plus a randomized fuzz test. ```APIDOC ## `fuzzWithExamples` ### Description Runs the hardcoded example inputs as unit tests, plus one randomized `fuzzWith` test labeled `"fuzz"` (appending `_` if the label collides with an example label). ### Signature ```elm fuzzWithExamples : String -> FuzzOptions a -> Fuzzer a -> List ( String, a ) -> (a -> Expectation) -> Test ``` ### Parameters - `desc` (String) - Required - Group description - `options` (FuzzOptions a) - Required - `runs` + `distribution` - `fuzzer` (Fuzzer a) - Required - Random input generator - `examples` (List ( String, a )) - Required - Hardcoded regression inputs, each with a label - `getTest` (a -> Expectation) - Required - Property to check ### Example ```elm Test.fuzzWithExamples "compare with zero is only EQ when input also is zero" { runs = 100, distribution = Test.noDistribution } Fuzz.float [ ( "NaN", 0 / 0 ) , ( "Infinity", 1 / 0 ) ] <| \input -> compare input 0 |> Expect.expect EQ ``` ### Source `src/Test.elm:452` ``` -------------------------------- ### Runner.simplify usage example Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/test-runner.md Example of driving Runner.simplify in a loop to reach the minimum failing input, as done internally by fuzz tests via Simplify.simplify. ```elm case value of ( failingValue, simplifiable ) -> case Runner.simplify (\s -> Expect.notEqual "" s) ( failingValue, simplifiable ) of Just ( smaller, _ ) -> -- smaller still fails Nothing -> -- no smaller failing value ``` -------------------------------- ### within usage examples Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/expect.md Two examples of using within: one with absolute tolerance and one with relative tolerance in a fuzz test. ```elm 0.1 + 0.2 |> Expect.within (Absolute 0.000000001) 0.3 ``` ```elm fuzz "Circle half-circumference with relative tolerance" (floatRange 0.000001 100000) (\r -> r * 3.14 |> Expect.within (Relative 0.001) (r * pi)) ``` -------------------------------- ### Expect.equalLists usage example Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/expect.md Example usage of `Expect.equalLists` with two identical lists. Passes if the lists are equal; on failure reports reason `ListDiff`. ```elm [ 1, 2, 3 ] |> Expect.equalLists [ 1, 2, 3 ] ``` -------------------------------- ### formatLabels usage example Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/test-runner.md Example usage of formatLabels with a list of labels. The output shows the labels formatted with '↓ ' for descriptions and '✗ ' for the test label, in reversed order. ```elm [ "the actual test that failed" , "nested description failure" , "top-level description failure" ] |> formatLabels ((++) "↓ ") ((++) "✗ ") -- [ "↓ top-level description failure" -- , "↓ nested description failure" -- , "✗ the actual test that failed" -- ] ``` -------------------------------- ### find usage example Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/test-html-query.md Example usage of Query.find to locate a single ul element and assert it has the classes 'items' and 'active'. ```elm div [] [ ul [ class "items active" ] [ li [] [ text "first item" ] , li [] [ text "second item" ] ] ] |> Query.fromHtml |> Query.find [ tag "ul" ] |> Query.has [ classes [ "items", "active" ] ] ``` -------------------------------- ### keep usage example Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/test-html-query.md Example usage of Query.keep to filter a list of li elements to only those with an anchor tag. ```elm Query.findAll [ tag "li" ] |> Query.keep ( tag "a" ) ``` -------------------------------- ### Runner.fuzz usage example Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/test-runner.md Example usage of Runner.fuzz with a seeded random generator, showing the result tuple containing Ok (value, simplifiable) and the next seed. ```elm Fuzz.intRange 0 99 |> Runner.fuzz |> Random.step (Random.initialSeed 42) -- ( Ok ( 42, simplifiable ), nextSeed ) ``` -------------------------------- ### Example: Querying a button with fromHtml and has Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/test-html-query.md Shows how to use Query.fromHtml and Query.has to assert that a button element contains the text 'I'm a button!'. ```elm Html.button [] [ Html.text "I'm a button!" ] |> Query.fromHtml |> Query.has [ text "I'm a button!" ] ``` -------------------------------- ### frequency usage example Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/fuzz-advanced.md Example usage of Fuzz.frequency with weighted fuzzers. The second fuzzer is three times more likely to be chosen. ```elm Fuzz.frequency [ ( 1, Fuzz.intRange -100 -1 ) , ( 3, Fuzz.intRange 1 100 ) ] ``` -------------------------------- ### Example fuzz test with Fuzz.string Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/fuzz.md Example usage of the string fuzzer in a fuzz test. Verifies that reversing a string twice returns the original string. ```elm fuzz "reversing twice is identity" Fuzz.string <| \s -> s |> String.reverse |> String.reverse |> Expect.equal s ``` -------------------------------- ### Query.count usage example Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/test-html-query.md Example usage of Query.count to assert that the number of matched elements equals 3. On failure, the query trace is rendered. ```elm Query.findAll [ tag "li" ] |> Query.count (Expect.equal 3) ``` -------------------------------- ### getFailureReason usage examples Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/test-runner.md Two examples showing the result of `getFailureReason` for a failing and a passing expectation. The failing case returns `Just` with the reason and description, while the passing case returns `Nothing`. ```elm getFailureReason (Expect.equal 1 2) -- Just { reason = Equal 1 2, description = "Expect.equal", given = Nothing } ``` ```elm getFailureReason (Expect.equal 1 1) -- Nothing ``` -------------------------------- ### describe usage example Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/test.md Example usage of describe to create a nested test group. The outer describe labels the group "List", and the inner describe labels a subgroup "reverse" containing a single test. ```elm describe "List" [ describe "reverse" [ test "has no effect on an empty list" <| \() -> Expect.equal [] (List.reverse []) ] ] ``` -------------------------------- ### test function usage example Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/test.md Example of using the test function to create a test that checks the length of an empty list. The description is trimmed, and if blank after trimming, the test fails with Invalid BadDescription. ```elm import Expect import Test exposing (test) test "the empty list has 0 length" <| \() -> List.length [] |> Expect.equal 0 ``` -------------------------------- ### Using onFail with Expect.equal Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/expect.md Example of using Expect.onFail to replace the failure message of a failed equality expectation. ```elm "something" |> Expect.equal "something else" |> Expect.onFail "thought those two strings would be the same" ``` -------------------------------- ### fuzz2 usage example Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/test.md Example usage of fuzz2 with a list of integers and an integer, checking that List.reverse does not affect List.member. It uses the fuzz2 function with a description and two fuzzers. ```elm fuzz2 "List.reverse never influences List.member" (list int) int <| \nums target -> List.member target (List.reverse nums) |> Expect.equal (List.member target nums) ``` -------------------------------- ### fuzz usage example Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/test.md Example usage of fuzz with a list of integers, checking that List.length is never negative. It imports Expect, Fuzz, and Test, and uses the fuzz function with a description and a list int fuzzer. ```elm import Expect import Fuzz exposing (int, list) import Test exposing (fuzz) fuzz "List.length should never be negative" (list int) <| \fuzzList -> fuzzList |> List.length |> Expect.atLeast 0 ``` -------------------------------- ### Example fuzz test using Fuzz.list Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/fuzz-advanced.md Example usage of the list fuzzer in a fuzz test. Asserts that the length of a generated list is never negative. ```elm fuzz "List.length is never negative" (Fuzz.list Fuzz.int) <| \xs -> xs |> List.length |> Expect.atLeast 0 ``` -------------------------------- ### Using passesAll with Query.each Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/expect.md Example of using Expect.passesAll with Query.each to check multiple conditions on a subject. ```elm Query.each (Expect.passesAll [ Query.has [ tag "ul" ] , Query.has [ classes [ "items", "active" ] ] ] ) ``` -------------------------------- ### Example usage of intRange and intAtLeast Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/fuzz.md Example usage of intRange and intAtLeast fuzzers: Fuzz.intRange 1 6 for dice rolls and Fuzz.intAtLeast 0 for non-negative ints. ```elm Fuzz.intRange 1 6 -- dice rolls Fuzz.intAtLeast 0 -- non-negative ints ``` -------------------------------- ### Using pass and fail with decodeString Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/expect.md Example usage of Expect.pass and Expect.fail in a case expression that decodes a string as an integer. ```elm case decodeString int "42" of Ok _ -> Expect.pass Err err -> Expect.fail err ``` -------------------------------- ### fuzzWithExamples type signature Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/test.md Type signature for the fuzzWithExamples function. It takes a description, fuzz options, a fuzzer, a list of labeled examples, and a property function, returning a Test. ```elm fuzzWithExamples : String -> FuzzOptions a -> Fuzzer a -> List ( String, a ) -> (a -> Expectation) -> Test ``` -------------------------------- ### distributionReportTable type and usage Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/test-distribution.md Shows the type signature of distributionReportTable and an example usage that converts a DistributionReport to a formatted table string. The function expects a record with runsElapsed and distributionCount fields. ```elm distributionReportTable : { a | runsElapsed : Int , distributionCount : Dict (List String) Int } -> String ``` ```elm import Test.Runner as Runner import Test.Distribution as Distribution case Runner.getDistributionReport expectation of Distribution.DistributionToReport report -> Distribution.distributionReportTable report _ -> "-- no distribution --" ``` -------------------------------- ### Run benchmarks with elm make and open index.html Source: https://github.com/elm-explorations/test/blob/master/benchmarks/README.md Commands to compile the benchmark suite and open the results in a browser. Run from the benchmarks directory. ```sh cd ./benchmarks elm make Main.elm open index.html ``` -------------------------------- ### Publish with npx elm publish (first attempt) Source: https://github.com/elm-explorations/test/blob/master/PUBLISHING.md This command will fail because the version is not tagged yet. It will suggest git commands to tag and push the release. ```bash npx elm publish ``` -------------------------------- ### containing selector usage example Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/test-html-selector.md Example usage of the containing selector to find a button with the text 'click me'. ```elm Query.find [ tag "button" , containing [ text "click me" ] ] ``` -------------------------------- ### concat usage example Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/test.md Example usage of concat to combine two tests, testDecoder and testSorting, into a single test. ```elm concat [ testDecoder, testSorting ] ``` -------------------------------- ### Use skip, only, and todo to manage incomplete test runs Source: https://github.com/elm-explorations/test/blob/master/README.md Shows how to use skip, only, and todo to focus on specific tests, silence failing tests, or jot down test ideas. Running this suite results in an incomplete test run, reported to CI systems as indistinguishable from failed test runs. ```elm wipSuite : Test wipSuite = describe "skip, only, and todo" [ only <| describe "Marking this test as `only` means no other tests will be run!" [ test "This test will be run" <| \_ -> 1 + 1 |> Expect.equal 2 , skip <| test "This test will be skipped, even though it's in an `only`!" <| \_ -> 2 + 3 |> Expect.equal 4 ] , test "This test will be skipped because it has no `only`" <| \_ -> "left" |> Expect.equal "right" , todo "Make sure all splines are reticulated" ] ``` -------------------------------- ### filterMap usage example Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/fuzz-advanced.md Example usage of `Fuzz.filterMap` to keep only letters from `Fuzz.char`. The lambda returns `Just c` for letters and `Nothing` otherwise. ```elm Fuzz.char |> Fuzz.filterMap (\c -> if isLetter c then Just c else Nothing ) ``` -------------------------------- ### simulate Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/test-html-event.md Wraps an event pair and a Query.Single into an Event. Usually combined with one of the event builders. ```APIDOC ## simulate ### Description Wraps an event pair and a Query.Single into an Event. Usually combined with one of the event builders. ### Signature ```elm simulate : ( String, Value ) -> Query.Single msg -> Event msg ``` ### Parameters - **event** (`(String, Value)`) - Required - Event name + JSON event object - **query** (`Query.Single msg`) - Required - The element to simulate on ### Example ```elm Html.input [ onInput Change ] [] |> Query.fromHtml |> Event.simulate (Event.input "cats") |> Event.expect (Change "cats") ``` ``` -------------------------------- ### Configuration Reference Source: https://github.com/elm-explorations/test/blob/master/_autodocs/configuration.md Documentation for the configuration options and runner parameters in the elm-explorations/test package. ```APIDOC ## Test.fuzzWith ### Description Configures a fuzz test with custom options, including the number of runs and distribution policy. ### Method Function ### Endpoint `Test.fuzzWith : String -> FuzzOptions a -> Fuzzer a -> (a -> Expectation) -> Test` ### Parameters #### Arguments - **name** (String) - Required - The test name. - **options** (FuzzOptions a) - Required - Record with `runs` (Int) and `distribution` (Distribution a). - **fuzzer** (Fuzzer a) - Required - The fuzzer to generate values. - **testFn** (a -> Expectation) - Required - The test function. ### Request Example ```elm fuzzWith "my property" { runs = 350 , distribution = noDistribution } (list int) <| \xs -> Expect.atLeast 0 (List.length xs) ``` ### Response #### Success Response Returns a `Test` value. #### Error Handling - If `runs < 1`, produces an always-failing test with `Invalid NonpositiveFuzzCount`. ## Test.fuzzWithExamples ### Description Configures a fuzz test with custom options and additional example values. ### Method Function ### Endpoint `Test.fuzzWithExamples : String -> FuzzOptions a -> List a -> Fuzzer a -> (a -> Expectation) -> Test` ### Parameters #### Arguments - **name** (String) - Required - The test name. - **options** (FuzzOptions a) - Required - Record with `runs` (Int) and `distribution` (Distribution a). - **examples** (List a) - Required - Additional example values to test. - **fuzzer** (Fuzzer a) - Required - The fuzzer to generate values. - **testFn** (a -> Expectation) - Required - The test function. ### Response #### Success Response Returns a `Test` value. #### Error Handling - If `runs < 1`, produces an always-failing test with `Invalid NonpositiveFuzzCount`. ## Test.Runner.fromTest ### Description Creates seeded runners from a test with a specified run count and random seed. ### Method Function ### Endpoint `Test.Runner.fromTest : Int -> Random.Seed -> Test -> SeededRunners` ### Parameters #### Arguments - **runs** (Int) - Required - Default fuzz run count; must be ≥ 1 (otherwise `Invalid`). - **seed** (Random.Seed) - Required - Initial random seed for reproducible runs. - **test** (Test) - Required - The test to run. ### Response #### Success Response Returns a `SeededRunners` value. #### Error Handling - If `runs < 1`, returns `Invalid`. ``` -------------------------------- ### sequence Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/fuzz-advanced.md Runs every fuzzer in the list, collecting values. Rejections bubble up. ```APIDOC ## `sequence` ### Description Runs every fuzzer in the list, collecting values. Rejections bubble up. ### Signature ```elm sequence : List (Fuzzer a) -> Fuzzer (List a) ``` ### Source `src/Fuzz.elm:1640` ``` -------------------------------- ### Query.contains usage example Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/test-html-query.md Example usage of Query.contains to assert that the queried element has the given HTML subtrees as descendants. Fails with a numbered list of missing descendants. ```elm Query.fromHtml ul |> Query.contains [ li [] [ text "third item" ] , li [] [ text "first item" ] ] ``` -------------------------------- ### parameterized usage example Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/test.md Example usage of parameterized to create data-driven tests. It defines a test that checks addition for two cases, using a lambda to extract the inputs and expected sum. ```elm myTest : Test myTest = Test.parameterized "addition" [ ( 1, 1, 2 ) , ( 5, 0, 5 ) ] <| \( a, b, expectedSum ) -> Test.test (Debug.toString ( a, b )) <| \() -> (a + b) |> Expect.equal expectedSum ``` -------------------------------- ### first Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/test-html-query.md Returns the first element of a match; shorthand for index 0. Fails if there were no matches. ```APIDOC ## first ### Description Returns the first element of a match; shorthand for `index 0`. Fails if there were no matches. ### Signature ```elm first : Multiple msg -> Single msg ``` ### Parameters - `query` (Multiple msg) - Required - Multiple query to get the first element from ``` -------------------------------- ### andMap Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/fuzz-advanced.md The `Fuzzer`-equivalent of apply, enabling `mapN` for `N > 8`. ```APIDOC ## `andMap` ### Description The `Fuzzer`-equivalent of apply, enabling `mapN` for `N > 8`. ### Signature ```elm andMap : Fuzzer a -> Fuzzer (a -> b) -> Fuzzer b ``` ### Example ```elm Fuzz.constant fn |> Fuzz.andMap fuzzerA |> Fuzz.andMap fuzzerB |> Fuzz.andMap fuzzerC ``` ### Source `src/Fuzz.elm:1213` ``` -------------------------------- ### Example usage of floatRange in a fuzz test Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/fuzz.md Example usage of floatRange in a fuzz test: fuzz "multiplying by zero gives zero" (Fuzz.floatRange -100 100) <| \f -> Expect.equal 0 (f * 0). ```elm fuzz "multiplying by zero gives zero" (Fuzz.floatRange -100 100) <| \f -> Expect.equal 0 (f * 0) ``` -------------------------------- ### Flatten nested Multiple reasons example Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/test-runner-failure.md Example usage of `Failure.flatten` on a nested `Multiple` reason. The function collapses the inner `Multiple` into a single level, as shown in the comment output, eliminating nested indentation for nested `Expect.oneOf` failures. ```elm import Test.Runner.Failure as Failure Failure.flatten (Failure.Multiple [ { description = "outer", reason = Failure.Custom } , { description = "inner" , reason = Failure.Multiple [ { description = "leaf", reason = Failure.Custom } ] } ] ) -- Failure.Multiple [ {description = "outer", ...}, {description = "leaf", ...} ] ``` -------------------------------- ### FuzzOptions Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/test.md Options record for fuzz testing. ```APIDOC ## `FuzzOptions` ```elm type alias FuzzOptions a = { runs : Int , distribution : Distribution a } ``` ### Description Options record accepted by `fuzzWith` and `fuzzWithExamples`. See [configuration](../configuration.md) for the full option reference. ### Fields - `runs` (Int) - Required - Number of fuzz iterations; must be ≥ 1 - `distribution` (Distribution a) - Required - Input-distribution checking policy Source: `src/Test.elm:361` ``` -------------------------------- ### fuzzWith Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/test.md Function to run a fuzz test with custom options. ```APIDOC ## `fuzzWith` ### Description Runs the property `runs` times with fuzzer-generated input. If `options.runs < 1` the test immediately fails with reason `Invalid NonpositiveFuzzCount`. There is no `fuzzWith2`/`fuzzWith3`; combine inputs with `Fuzz.pair`/`Fuzz.triple`. ### Signature ```elm fuzzWith : String -> FuzzOptions a -> Fuzzer a -> (a -> Expectation) -> Test ``` ### Parameters - `desc` (String) - Required - Test description - `options` (FuzzOptions a) - Required - `runs` + `distribution` - `fuzzer` (Fuzzer a) - Required - Input generator (see [Fuzz](fuzz.md)) - `getTest` (a -> Expectation) - Required - Property to check ### Example ```elm import Expect import Fuzz exposing (int, pair) import Test exposing (fuzzWith, noDistribution) fuzzWith "List.reverse never influences List.member" { runs = 4200, distribution = noDistribution } (pair (list int) int) <| \(nums, target) -> List.member target (List.reverse nums) |> Expect.equal (List.member target nums) ``` ### Source `src/Test.elm:386` ``` -------------------------------- ### expectDistribution type signature and usage example Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/test.md Type signature for collecting statistics and failing the test when the expected distribution is not met. Uses a Wilson-score statistical test tuned so false positives/negatives occur in about 1 in 10⁹ tests; may run more iterations than options.runs to gain confidence. The example shows usage with fuzzWith and expectDistribution. Source: src/Test.elm:635 ```elm expectDistribution : List ( ExpectedDistribution, String, a -> Bool ) -> Distribution a ``` ```elm fuzzWith "Sum > Average" { runs = 350 , distribution = expectDistribution [ ( Test.Distribution.zero, "empty", \xs -> List.length xs == 0 ) , ( Test.Distribution.atLeast 10, "3+ items", \xs -> List.length xs >= 3 ) ] } (list int) <| \xs -> List.sum xs |> Expect.greaterThan (average xs) ``` -------------------------------- ### equal Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/expect.md Passes if the two arguments are equal. Failure renders as a pipeline-style diff. Floats are rejected with a specific error message. ```APIDOC ## `equal` ### Description Passes if `a == b`. Failure renders as a pipeline-style diff. Values that stringify as floats are rejected: if either argument is detected as a `Float`, the expectation fails with "Do not use Expect.equal with floats. Use Expect.within instead." ### Signature ```elm equal : a -> a -> Expectation ``` ### Parameters - `b` (first) (type `a`) - Required - Expected value - `a` (second) (type `a`) - Required - Actual value (usually piped) ### Example ```elm String.split " " "Betty Botter bought some butter" |> Expect.equal [ "Betty", "Botter", "bought", "some", "butter" ] ``` ``` -------------------------------- ### constant Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/fuzz-advanced.md Always returns the given value, no simplification. Best used as a building block. ```APIDOC ## `constant` ### Description Always returns the given value, no simplification. Best used as a building block. ### Signature ```elm constant : a -> Fuzzer a ``` ### Parameters - **value** (`a`) - The value to always return. ### Source `src/Fuzz.elm:918` ``` -------------------------------- ### Boolean attribute selector usage Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/test-html-selector.md Example usage of boolean attribute selectors: checks that an input is checked and an option is not selected. Sources: src/Test/Html/Selector.elm:347, :329, :338. ```elm Query.find [ tag "input" ] |> Query.has [ checked True ] Query.find [ tag "option" ] |> Query.has [ selected False ] ``` -------------------------------- ### todo Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/test.md Creates a test that always fails with reason TODO. ```APIDOC ## `todo` ```elm todo : String -> Test ``` ### Description Returns a `Test` that always fails with reason `TODO`. Test runners only include TODO output when there are no other failures. Not meant to be committed. ### Parameters - `desc` (String) - Required - Description ### Returns - `Test` - A test that always fails with reason `TODO`. ### Example ```elm describe "a new thing" [ todo "does what is expected in the common case" , todo "correctly handles an edge case I just thought of" ] ``` Source: `src/Test.elm:228` ``` -------------------------------- ### parameterized Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/test.md Creates data-driven tests by mapping a function over a list of cases. It behaves like describe, failing on blank descriptions or empty case lists. ```APIDOC ## parameterized ### Description Creates data-driven tests by mapping a function over a list of cases. It behaves like describe, failing on blank descriptions or empty case lists. ### Signature ```elm parameterized : String -> List a -> (a -> Test) -> Test ``` ### Parameters - `untrimmedDesc` (String) - Required - Group description, whitespace-trimmed - `cases` (List a) - Required - Input-output cases - `toTest` (a -> Test) - Required - Turns one case into a `Test` ### Behavior Data-driven tests: `describe desc (List.map toTest cases)`. Behaves like `describe`: fails on blank description or empty case list. ### Example ```elm myTest : Test myTest = Test.parameterized "addition" [ ( 1, 1, 2 ) , ( 5, 0, 5 ) ] <| \( a, b, expectedSum ) -> Test.test (Debug.toString ( a, b )) <| \() -> (a + b) |> Expect.equal expectedSum ``` ### Source `src/Test.elm:160` ``` -------------------------------- ### all Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/test-html-selector.md Combines selectors into one that requires all of them to match the same element (since 2.2.1; previously allowed different elements). ```APIDOC ## all ### Description Combines selectors into one that requires all of them to match the same element (since 2.2.1; previously allowed different elements). ### Signature ```elm all : List Selector -> Selector ``` ### Parameters - **selectors** (List Selector) - Required - The selectors that must all match the same element. ### Example ```elm replyBtnSelector : Selector replyBtnSelector = all [ class "btn", text "Reply" ] ``` ### Source `src/Test/Html/Selector.elm:58` ``` -------------------------------- ### atLeast Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/expect.md Passes if the second argument is greater than or equal to the first. ```APIDOC ## atLeast ### Description Passes if the second argument is ≥ the first (`>=`). ### Signature ```elm atLeast : comparable -> comparable -> Expectation ``` ### Parameters - `expected` (comparable) - The first argument, the value to compare against. - `actual` (comparable) - The second argument, the value being compared. ### Usage ```elm Expect.atLeast 1 (List.length [1,2]) ``` ``` -------------------------------- ### intAtLeast Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/fuzz.md A fuzzer that generates integers in the range n to 2^32 - 1. ```APIDOC ## `intAtLeast` ### Description Generates values in `n .. 2^32 - 1`. ### Signature ```elm intAtLeast : Int -> Fuzzer Int ``` ### Parameters - `n` (Int) - Required - The lower bound (inclusive). ### Returns - `Fuzzer Int` - A fuzzer that generates integers greater than or equal to n. ``` -------------------------------- ### fromHtml Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/test-html-query.md Entry point for querying Html values. Converts an Html value into a Single query for further traversal and assertions. ```APIDOC ## fromHtml ### Description Translates an `Html` value into a `Single` query. Failures are deferred: if the virtual DOM can't be decoded the query holds an `InternalError`; if `Html`-validation produced errors it holds `ValidationErrors`. Both surface when an expectation runs, as `Expect.fail` output. ### Signature ```elm fromHtml : Html msg -> Single msg ``` ### Parameters - **html** (`Html msg`) - Required - The `Html` value to query ### Example ```elm Html.button [] [ Html.text "I'm a button!" ] |> Query.fromHtml |> Query.has [ text "I'm a button!" ] ``` ``` -------------------------------- ### fromGenerator Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/fuzz-advanced.md Documentation for the `fromGenerator` helper function that converts a `Random.Generator` into a `Fuzzer`. ```APIDOC ## fromGenerator ### Description Converts a `Random.Generator` into a `Fuzzer` by drawing a seed and running `Random.step`. This is an escape hatch for using existing random generators in fuzz testing. Note that it will not simplify meaningfully (it simplifies randomly to smaller seeds), so prefer defining fuzzers with the rest of the module. ### Signature ```elm fromGenerator : Random.Generator a -> Fuzzer a ``` ### Parameters - **generator** (`Random.Generator a`) - Required - Elm Random generator to convert into a fuzzer. ### Returns - **Fuzzer a** - A fuzzer that generates values from the given random generator. ### Source `src/Fuzz.elm:1989` ``` -------------------------------- ### has Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/test-html-query.md Passes if the element matches all the given selectors; on failure marks each selector ✓/✗ under the query trace. ```APIDOC ## has ### Description Passes if the element matches all the given selectors; on failure marks each selector ✓/✗ under the query trace. ### Method has ### Endpoint has : List Selector -> Single msg -> Expectation ### Parameters #### Parameters - **selectors** (List Selector) - Required - Selectors to match - **query** (Single msg) - Required - Query whose root element is checked ### Request Example ```elm Query.find [ tag "ul" ] |> Query.has [ tag "ul", classes [ "items", "active" ] ] ``` ### Response #### Success Response - **Expectation** - The expectation result #### Response Example ```elm -- No explicit response example provided ``` ``` -------------------------------- ### test Source: https://github.com/elm-explorations/test/blob/master/_autodocs/api-reference/test.md Creates a Test that evaluates a single Expectation. The function takes a description string and a thunk that produces the expectation. ```APIDOC ## test ### Description Creates a Test that evaluates a single Expectation. The description is trimmed of leading and trailing whitespace; if the result is blank, the test fails with an invalid description error. ### Signature ```elm test : String -> (() -> Expectation) -> Test ``` ### Parameters - **untrimmedDesc** (String) - Required - Test name (leading/trailing whitespace is trimmed) - **thunk** (() -> Expectation) - Required - Function producing the expectation to check ### Returns - **Test** - A Test value representing the single expectation. ### Example ```elm import Expect import Test exposing (test) test "the empty list has 0 length" <| \() -> List.length [] |> Expect.equal 0 ``` ```