From d4d91f3a0b9e457418437ad0b642acf92ed2b19c Mon Sep 17 00:00:00 2001 From: Richard Feldman Date: Fri, 14 Jul 2017 10:01:05 -0400 Subject: [PATCH] Split FuzzTest and UnitTest --- src/Expect.elm | 1 - src/Test.elm | 9 ++-- src/Test/Fuzz.elm | 2 +- src/Test/Internal.elm | 12 ++++-- src/Test/Runner.elm | 9 +++- tests/Helpers.elm | 97 ++++++++++++++++++++++++++----------------- 6 files changed, 83 insertions(+), 47 deletions(-) diff --git a/src/Expect.elm b/src/Expect.elm index e689d52..4527c5c 100644 --- a/src/Expect.elm +++ b/src/Expect.elm @@ -122,7 +122,6 @@ For example: Is `1 + 2 - 3` within `1%` of `0`? Well, if `1`, `2` and `3` have a Another example is comparing values that are on either side of zero. `0.0001` is more than `100%` away from `-0.0001`. In fact, `infinity` is closer to `0.0001` than `0.0001` is to `-0.0001`, if you are using a relative tolerance. Twice as close, actually. So even though both `0.0001` and `-0.0001` could be considered very close to zero, they are very far apart relative to each other. The same argument applies for any number of zeroes. - -} import Dict exposing (Dict) diff --git a/src/Test.elm b/src/Test.elm index 5004b04..75f1a85 100644 --- a/src/Test.elm +++ b/src/Test.elm @@ -138,7 +138,7 @@ test untrimmedDesc thunk = if String.isEmpty desc then Internal.blankDescriptionFailure else - Internal.Labeled desc (Internal.Test (\_ _ -> [ thunk () ])) + Internal.Labeled desc (Internal.UnitTest (\() -> [ thunk () ])) {-| Returns a [`Test`](#Test) that is "TODO" (not yet implemented). These tests @@ -304,8 +304,11 @@ fuzzWith options fuzzer desc getTest = fuzzWithHelp : FuzzOptions -> Test -> Test fuzzWithHelp options test = case test of - Internal.Test run -> - Internal.Test (\seed _ -> run seed options.runs) + Internal.UnitTest _ -> + test + + Internal.FuzzTest run -> + Internal.FuzzTest (\seed _ -> run seed options.runs) Internal.Labeled label subTest -> Internal.Labeled label (fuzzWithHelp options subTest) diff --git a/src/Test/Fuzz.elm b/src/Test/Fuzz.elm index f7503ce..da20458 100644 --- a/src/Test/Fuzz.elm +++ b/src/Test/Fuzz.elm @@ -51,7 +51,7 @@ validatedFuzzTest fuzzer desc getExpectation = |> Dict.toList |> List.map formatExpectation in - Labeled desc (Test run) + Labeled desc (FuzzTest run) type alias Failures = diff --git a/src/Test/Internal.elm b/src/Test/Internal.elm index 0c663f4..a9e0a27 100644 --- a/src/Test/Internal.elm +++ b/src/Test/Internal.elm @@ -6,7 +6,8 @@ import Test.Expectation exposing (Expectation(..)) type Test - = Test (Random.Seed -> Int -> List Expectation) + = UnitTest (() -> List Expectation) + | FuzzTest (Random.Seed -> Int -> List Expectation) | Labeled String Test | Skipped Test | Only Test @@ -17,8 +18,8 @@ type Test -} failNow : { description : String, reason : Test.Expectation.Reason } -> Test failNow record = - Test - (\_ _ -> [ Test.Expectation.fail record ]) + UnitTest + (\() -> [ Test.Expectation.fail record ]) blankDescriptionFailure : Test @@ -41,7 +42,10 @@ duplicatedName = Batch subtests -> List.concatMap names subtests - Test _ -> + UnitTest _ -> + [] + + FuzzTest _ -> [] Skipped subTest -> diff --git a/src/Test/Runner.elm b/src/Test/Runner.elm index 0042f76..90c5add 100644 --- a/src/Test/Runner.elm +++ b/src/Test/Runner.elm @@ -211,7 +211,14 @@ Some design notes: distributeSeeds : Int -> Random.Seed -> Test -> Distribution distributeSeeds runs seed test = case test of - Internal.Test run -> + Internal.UnitTest run -> + { seed = seed + , all = [ Runnable (Thunk (\_ -> run ())) ] + , only = [] + , skipped = [] + } + + Internal.FuzzTest run -> let ( firstSeed, nextSeed ) = Random.step Random.independentSeed seed diff --git a/tests/Helpers.elm b/tests/Helpers.elm index 3f1194b..c164fe7 100644 --- a/tests/Helpers.elm +++ b/tests/Helpers.elm @@ -7,7 +7,7 @@ import Shrink import String import Test exposing (Test) import Test.Expectation exposing (Expectation(..)) -import Test.Internal as TI +import Test.Internal as Internal expectPass : a -> Expectation @@ -38,48 +38,71 @@ succeeded expectation = False +passesToFails : + ({ reason : Test.Expectation.Reason + , description : String + , given : Maybe String + } + -> Maybe String + ) + -> List Expectation + -> List Expectation +passesToFails f expectations = + expectations + |> List.filterMap (passToFail f) + |> List.map Expect.fail + |> (\list -> + if List.isEmpty list then + [ Expect.pass ] + else + list + ) + + +passToFail : + ({ reason : Test.Expectation.Reason + , description : String + , given : Maybe String + } + -> Maybe String + ) + -> Expectation + -> Maybe String +passToFail f expectation = + case expectation of + Pass -> + Just "Expected this test to fail, but it passed!" + + Fail record -> + f record + + expectFailureHelper : ({ description : String, given : Maybe String, reason : Test.Expectation.Reason } -> Maybe String) -> Test -> Test expectFailureHelper f test = case test of - TI.Test runTest -> - TI.Test - (\seed runs -> - let - expectations = - runTest seed runs - - goodShrink expectation = - case expectation of - Pass -> - Just "Expected this test to fail, but it passed!" - - Fail record -> - f record - in - expectations - |> List.filterMap goodShrink - |> List.map Expect.fail - |> (\list -> - if List.isEmpty list then - [ Expect.pass ] - else - list - ) - ) - - TI.Labeled desc labeledTest -> - TI.Labeled desc (expectFailureHelper f labeledTest) - - TI.Batch tests -> - TI.Batch (List.map (expectFailureHelper f) tests) - - TI.Skipped subTest -> + Internal.UnitTest runTest -> + Internal.UnitTest <| + \() -> + passesToFails f (runTest ()) + + Internal.FuzzTest runTest -> + Internal.FuzzTest <| + \seed runs -> + passesToFails f (runTest seed runs) + + Internal.Labeled desc labeledTest -> + Internal.Labeled desc (expectFailureHelper f labeledTest) + + Internal.Batch tests -> + Internal.Batch (List.map (expectFailureHelper f) tests) + + Internal.Skipped subTest -> expectFailureHelper f subTest - |> TI.Skipped + |> Internal.Skipped - TI.Only subTest -> + Internal.Only subTest -> expectFailureHelper f subTest - |> TI.Only + |> Internal.Only testShrinking : Test -> Test