Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'just' and 'exactly' expectations #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
53 changes: 53 additions & 0 deletions src/Expect/Extra.elm
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module Expect.Extra exposing
( match, MatchPattern, stringPattern, regexPattern
, contain, member
, just
, exactly
)

{-| Extends `Expect` with more `Expectation`s.
Expand All @@ -15,6 +17,16 @@ module Expect.Extra exposing

@docs contain, member


## Maybes

@docs just


## Floats

@docs exactly

-}

import Expect exposing (..)
Expand Down Expand Up @@ -102,3 +114,44 @@ Reads better with bdd style tests.
contain : a -> (a -> String) -> List a -> Expectation
contain value toString =
member toString value


{-| Passes if the actual value is a `Just` and the contained value passes the
given expectation function.

-- Passes:
just (Expect.equal "foo") (Just "foo")

-- Fails:
just (Expect.equal "foo") (Just "bar")

-- Fails:
just (Expect.equal "foo") Nothing

-}
just : (actual -> Expectation) -> Maybe actual -> Expectation
just expectation actualMaybe =
case actualMaybe of
Just actualValue ->
expectation actualValue

Nothing ->
Expect.fail "Expected a Just but got Nothing"


{-| Passes if the actual value is _exactly_ equal to the expected value.

Note that this is usually only desirable in low-level numeric code and most
tests should use [`Expect.within`](https://package.elm-lang.org/packages/elm-explorations/test/latest/Expect#floating-point-comparisons)
instead.

-- Passes:
exactly 1.5 1.5

-- Fails:
exactly 1.5 1.50000000001

-}
exactly : Float -> Float -> Expectation
exactly expected actual =
Expect.within (Expect.Absolute 0.0) expected actual
29 changes: 29 additions & 0 deletions tests/ExpectTests.elm
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,33 @@ all =
(Expect.fail "Expected:\n [1, 2, 3]\nto contain:\n 4")
(member Debug.toString 4 [ 1, 2, 3 ])
]
, describe "just" <|
[ test "matching Just" <|
\_ ->
Expect.equal
Expect.pass
(just (Expect.equal "foo") (Just "foo"))
, test "non-matching Just" <|
\_ ->
Expect.equal
(Expect.equal "foo" "bar")
(just (Expect.equal "foo") (Just "bar"))
, test "Nothing" <|
\_ ->
Expect.equal
(Expect.fail "Expected a Just but got Nothing")
(just (Expect.equal "foo") Nothing)
]
, describe "exactly" <|
[ test "equal" <|
\_ ->
Expect.equal
Expect.pass
(exactly 1.0 1.0)
, test "not equal" <|
\_ ->
Expect.equal
(Expect.within (Expect.Absolute 0) 1.0 1.0001)
(exactly 1.0 1.0001)
]
]