Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions nri-prelude/src/Expect.hs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ module Expect

-- * Fancy Expectations
Internal.fromIO,
Internal.runExpectation,
Internal.fromIOResult,
Internal.Expectation',
Internal.Failure,
around,
Comment on lines 58 to 63
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we will also need to expose Failure right?

)
where
Expand Down
18 changes: 18 additions & 0 deletions nri-prelude/src/Test/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,24 @@ fromIO io =
Platform.Internal.Task (\_ -> map Ok io)
|> Expectation

-- | Run an expectation directly in IO.
-- Some external testing libraries required using a resource in an IO continution like `(\resource -> IO a) -> IO a`.
-- For example see warp's `testWithApplication`.
--
-- This function allows you to convert an expectation to IO inside of such a continuation. You will likely want to
-- transform the result back to an expectation with `fromIOResult`.
runExpectation :: Platform.LogHandler -> Expectation' a -> Prelude.IO (Result Failure a)
runExpectation log expectation = do
unExpectation expectation
|> Task.attempt log

-- | Convert an IO action that returns a Result into an expectation.
-- Useful in combination with 'runExpectation'.
fromIOResult :: Prelude.IO (Result Failure a) -> Expectation' a
fromIOResult io =
Platform.Internal.Task (\_ -> io)
|> Expectation

run :: Request -> Test -> Task e SuiteResult
run request (Test all) = do
let grouped = groupBy label all
Expand Down
23 changes: 22 additions & 1 deletion nri-prelude/tests/TestSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ tests =
stdoutReporter,
logfileReporter,
cliParser,
deadlockPrevention
deadlockPrevention,
runExpectationTests
]

api :: Test
Expand Down Expand Up @@ -666,3 +667,23 @@ withoutDurationLine text =
|> Text.lines
|> List.filter (\line -> line |> Text.startsWith "Duration: " |> not)
|> Text.join "\n"

runExpectationTests :: Test
runExpectationTests =
describe
"runExpectation"
[ test "returns the value on success" <| \_ -> do
log <- Expect.fromIO Platform.silentHandler
result <-
Expect.fromIO
( Internal.runExpectation log (Expect.succeeds (Task.succeed 42))
)
Expect.ok result,
test "throws Failure exception on assertion failure" <| \_ -> do
log <- Expect.fromIO Platform.silentHandler
result <-
Expect.fromIO
( Internal.runExpectation log (Expect.fail "test failure")
)
Expect.err result
]
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@
"finished": 0,
"frame": {
"endCol": 50,
"endLine": 429,
"endLine": 430,
"file": "tests/TestSpec.hs",
"module": "TestSpec",
"name": "report",
"package": "nri-prelude-0.6.1.2-inplace-tests",
"startCol": 22,
"startLine": 429
"startLine": 430
},
"name": "test run",
"started": 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
"finished": 0,
"frame": {
"endCol": 50,
"endLine": 468,
"endLine": 469,
"file": "tests/TestSpec.hs",
"module": "TestSpec",
"name": "report",
"package": "nri-prelude-0.6.1.2-inplace-tests",
"startCol": 22,
"startLine": 468
"startLine": 469
},
"name": "test run",
"started": 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@
"finished": 0,
"frame": {
"endCol": 50,
"endLine": 444,
"endLine": 445,
"file": "tests/TestSpec.hs",
"module": "TestSpec",
"name": "report",
"package": "nri-prelude-0.6.1.2-inplace-tests",
"startCol": 22,
"startLine": 444
"startLine": 445
},
"name": "test run",
"started": 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@
"finished": 0,
"frame": {
"endCol": 50,
"endLine": 459,
"endLine": 460,
"file": "tests/TestSpec.hs",
"module": "TestSpec",
"name": "report",
"package": "nri-prelude-0.6.1.2-inplace-tests",
"startCol": 22,
"startLine": 459
"startLine": 460
},
"name": "test run",
"started": 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,13 @@
"finished": 0,
"frame": {
"endCol": 50,
"endLine": 488,
"endLine": 489,
"file": "tests/TestSpec.hs",
"module": "TestSpec",
"name": "report",
"package": "nri-prelude-0.6.1.2-inplace-tests",
"startCol": 22,
"startLine": 488
"startLine": 489
},
"name": "test run",
"started": 0,
Expand Down
Loading