Skip to content
This repository has been archived by the owner on Oct 9, 2019. It is now read-only.

Commit

Permalink
Split FuzzTest and UnitTest
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Feldman committed Jul 14, 2017
1 parent 2dba38b commit d4d91f3
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 47 deletions.
1 change: 0 additions & 1 deletion src/Expect.elm
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 6 additions & 3 deletions src/Test.elm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/Test/Fuzz.elm
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
12 changes: 8 additions & 4 deletions src/Test/Internal.elm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -41,7 +42,10 @@ duplicatedName =
Batch subtests ->
List.concatMap names subtests

Test _ ->
UnitTest _ ->
[]

FuzzTest _ ->
[]

Skipped subTest ->
Expand Down
9 changes: 8 additions & 1 deletion src/Test/Runner.elm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
97 changes: 60 additions & 37 deletions tests/Helpers.elm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d4d91f3

Please sign in to comment.