Skip to content

Instantly share code, notes, and snippets.

@swlaschin
Last active April 29, 2019 09:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swlaschin/6cc747dccdf678d192d816d1820b63ac to your computer and use it in GitHub Desktop.
Save swlaschin/6cc747dccdf678d192d816d1820b63ac to your computer and use it in GitHub Desktop.
Test utilities
module TestUtils
// Utilities for tests
/// Helper for testing only! Do not use in production!
let getOk result =
match result with
| Ok v -> v
| Error _ -> failwith "Error not expected"
/// Helper for testing only! Do not use in production!
let getError result =
match result with
| Error v -> v
| Ok _ -> failwith "Ok not expected"
// ======================================
// Assertions
// ======================================
/// Assert that the Result is OK, then pass the ok value to f for further assertions
let assertOkWithAssertion f result =
match result with
| Ok v ->
f v
| Error e ->
failwithf "Error not expected. Error value=%A" e
/// Assert that the Result is Error then pass the error value to f for further assertions
let assertErrorWithAssertion f result =
match result with
| Ok v ->
failwithf "Ok not expected. Ok value=%A" v
| Error e ->
f e
/// Assert that the Result is OK and the ok value passes the validation specified by f
let assertOkWithValidation f result =
result |> assertOkWithAssertion (fun ok ->
if f ok then
()
else
failwithf "Ok value failed to validate. Actual value=%A" ok
)
/// Assert that the Result is Error and the error value passes the validation specified by f
let assertErrorWithValidation f result =
result |> assertErrorWithAssertion (fun err ->
if f err then
()
else
failwithf "Error value failed to validate. Actual value=%A" err
)
/// Assert that the Result is OK without caring about the actual ok value
let assertIsOk result =
result |> assertOkWithValidation (fun _ok -> true)
/// Assert that the Result is Error without caring about the actual error value
let assertIsError result =
result |> assertErrorWithValidation (fun _err -> true)
// notice that main param is last, to make piping easier
let assertEquals expected actual =
if actual = expected then
()
else
failwithf "Not equal. Actual '%A' Expected '%A' " actual expected
/// Assert that the Result is OK and the ok value is as expected
let assertOkEquals result expected =
result |> assertOkWithAssertion (fun actual -> actual |> assertEquals expected )
/// Assert that the Result is Error and the error value is as expected
let assertErrorEquals result expected =
result |> assertErrorWithAssertion (fun err -> err |> assertEquals expected )
// notice that main param is last, to make piping easier
let assertContains expectedSubstring (actual:string) =
if actual.Contains(expectedSubstring) then
()
else
failwithf "Expected actual '%s' to contain '%s'" actual expectedSubstring
/// Assert that the Result is OK and the ok value contains a certain string
let assertOkContains result expectedSubstring =
result |> assertOkWithAssertion (fun actual -> actual |> assertContains expectedSubstring )
/// Assert that the Result is Error and the error value contains a certain string
let assertErrorContains result expectedSubstring =
result |> assertErrorWithAssertion (fun err -> err |> assertContains expectedSubstring )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment