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

Commit

Permalink
Add tests for seed distribution.
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Feldman committed Jul 14, 2017
1 parent bb46170 commit 5bfcedd
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 1 deletion.
46 changes: 46 additions & 0 deletions tests/Main.elm
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ Note that this always uses an initial seed of 902101337, since it can't do effec
-}

import Platform
import Random.Pcg as Random
import Runner.Log
import Runner.String exposing (Summary)
import SeedTests
import Test exposing (Test)
import Tests


Expand All @@ -21,3 +25,45 @@ main =
, subscriptions = \_ -> Sub.none
}
|> Runner.Log.run Tests.all
-- All seed tests should pass because they receive the same initial seed.
|> runAllTests 1 SeedTests.fixedSeed SeedTests.tests


runAllTests : Int -> Random.Seed -> List Test -> a -> a
runAllTests runs seed tests =
tests
|> List.foldl
(\test summary ->
Runner.String.runWithOptions runs seed test
|> combineSummaries summary
)
emptySummary
|> Runner.Log.logOutput


emptySummary : Summary
emptySummary =
{ output = "", passed = 0, failed = 0, autoFail = Nothing }


combineSummaries : Summary -> Summary -> Summary
combineSummaries first second =
{ output = first.output ++ second.output
, passed = first.passed + second.passed
, failed = first.failed + second.failed
, autoFail =
case ( first.autoFail, second.autoFail ) of
( Nothing, Nothing ) ->
Nothing

( Nothing, second ) ->
second

( first, Nothing ) ->
first

( Just first, Just second ) ->
[ first, second ]
|> String.join "\n"
|> Just
}
2 changes: 1 addition & 1 deletion tests/Runner/Log.elm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Runner.Log exposing (run, runWithOptions)
module Runner.Log exposing (logOutput, run, runWithOptions)

{-| Log Runner
Expand Down
72 changes: 72 additions & 0 deletions tests/SeedTests.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
module SeedTests exposing (fixedSeed, tests)

import Expect exposing (FloatingPointTolerance(Absolute, AbsoluteOrRelative, Relative))
import Fuzz exposing (..)
import Random.Pcg as Random
import Test exposing (..)


fixedSeed : Random.Seed
fixedSeed =
Random.initialSeed 133742


expectedNum : Int
expectedNum =
1083264770


{-| Most of the tests will use this, but we won't run it directly.
When these tests are run using fixedSeed and a run count of 1, this is the
exact number they will get when the description around this fuzz test is
exactly the string "Seed test".
-}
fuzzTest : Test
fuzzTest =
fuzz int "It receives the expected number" <|
\num ->
Expect.equal num expectedNum


tests : List Test
tests =
[ describe "Seed test"
[ fuzzTest ]
, describe "Seed test"
[ fuzz int "It receives the expected number even though this text is different" <|
\num ->
Expect.equal num expectedNum
]
, describe "Seed test"
[ describe "Nested describes shouldn't affect seed distribution"
[ fuzzTest ]
]
, describe "Seed test"
[ test "Unit tests before should not affect seed distribution" <|
\_ ->
Expect.pass
, fuzzTest
, test "Unit tests after should not affect seed distribution" <|
\_ ->
Expect.pass
]
, -- Wrapping in a Test.concat shouldn't change anything
Test.concat
[ describe "Seed test"
[ fuzzTest ]
]
, -- Wrapping in a Test.concat wth unit tests shouldn't change anything
Test.concat
[ describe "Seed test"
[ test "Unit tests before should not affect seed distribution" <|
\_ ->
Expect.pass
, fuzzTest
, test "Unit tests after should not affect seed distribution" <|
\_ ->
Expect.pass
]
]
]

0 comments on commit 5bfcedd

Please sign in to comment.