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
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
aren't ignored anymore.
* `Expect.all`'s type signature changed to `List Expectation -> Expectation`.
The old pointfree `Expect.all` can be found under `Expect.passesAll`.
* `Test.Runner.Failure.Reason` has a new `Multiple` variant for `Expect.oneOf`
and `Expect.passesOneOf`.

### Additions

Expand All @@ -31,7 +33,9 @@
* `Expect.all` -> `Expect.passesAll`
* Do we want to detect `Expect.all [\() -> ...] ()` and instead of changing to
`Expect.passesAll {- same -}`, change to `Expect.all [...]`?
* Note the change in Node version needed (20+), coordinate with `node-test-runner`.
* Coordinate with `node-test-runner` and `elm-test-rs` on:
* the change in Node version needed (20+)
* the new `Multiple` variant of `Test.Runner.Failure.Reason`

## Releases

Expand Down
85 changes: 82 additions & 3 deletions src/Expect.elm
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module Expect exposing
( Expectation, equal, notEqual, all
( Expectation, equal, notEqual, all, oneOf
, lessThan, atMost, greaterThan, atLeast
, FloatingPointTolerance(..), within, notWithin
, ok, err, equalLists, equalDicts, equalSets
, pass, fail, onFail
, passesAll
, passesAll, passesOneOf
)

{-| A library to create `Expectation`s, which describe a claim to be tested.
Expand All @@ -23,7 +23,7 @@ module Expect exposing

## Basic Expectations

@docs Expectation, equal, notEqual, all
@docs Expectation, equal, notEqual, all, oneOf


## Numeric Comparisons
Expand All @@ -49,6 +49,7 @@ or both. For an in-depth look, see our [Guide to Floating Point Comparison](#gui
These functions will let you build your own expectations.

@docs pass, fail, onFail
@docs passesAll, passesOneOf


## Guide to Floating Point Comparison
Expand Down Expand Up @@ -715,6 +716,84 @@ allHelp list =
outcome


{-| Passes if at least one of the given expectations passes.

Expect.oneOf
[ user.isPremiumMember |> Expect.equal True
, user.cartTotal |> Expect.atLeast 50
, user.coupon |> Expect.notEqual Nothing
]

If none of them pass, the failure lists all the inner failures.

`Expect.oneOf []` is reported as a test failure.

-}
oneOf : List Expectation -> Expectation
oneOf list =
if List.isEmpty list then
Test.Expectation.Fail
{ given = Nothing
, distributionReport = Fuzz.Internal.noDistribution
, reason = Invalid EmptyList
, description = "Expect.oneOf was given an empty list. You must make at least one expectation to have a valid test!"
}

else
oneOfHelp list []


{-| Passes if at least one of the given functions passes when applied to the subject.

See also [`oneOf`](#oneOf).

Useful as an argument to [`Query.each`](Test-Html-Query#each):

Query.each
(Expect.passesOneOf
[ Query.has [ tag "ul" ]
, Query.has [ tag "ol" ]
]
)

`Expect.passesOneOf [] _` is reported as a test failure.

-}
passesOneOf : List (subject -> Expectation) -> subject -> Expectation
passesOneOf checks subject =
oneOf (List.map (\check -> check subject) checks)


oneOfHelp :
List Expectation
-> List { given : Maybe String, description : String, reason : Reason }
-> Expectation
oneOfHelp list failuresSoFar =
case list of
[] ->
Test.Expectation.Fail
{ given = Nothing
, distributionReport = Fuzz.Internal.noDistribution
, reason = Multiple (List.reverse failuresSoFar)
, description =
"Expect.oneOf: none of the "
++ String.fromInt (List.length failuresSoFar)
++ " expectations passed."
}

(Test.Expectation.Pass _) :: _ ->
pass

(Test.Expectation.Fail failure) :: rest ->
oneOfHelp rest
({ given = failure.given
, description = failure.description
, reason = failure.reason
}
:: failuresSoFar
)



{---- Private helper functions ----}

Expand Down
9 changes: 9 additions & 0 deletions src/Test/Runner/Failure.elm
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ type Reason
}
| TODO
| Invalid InvalidReason
-- The failures of each of several expectations that were all tried and
-- all failed, e.g. via `Expect.oneOf`.
| Multiple
(List
{ given : Maybe String
, description : String
, reason : Reason
}
)


{-| The reason a test run was invalid.
Expand Down
21 changes: 21 additions & 0 deletions tests/src/Runner/String/Format.elm
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,27 @@ format description reason =
, missingStr
]

Multiple failures ->
failures
|> List.indexedMap
(\index failure ->
(String.fromInt (index + 1) ++ ") " ++ format failure.description failure.reason)
|> indentAllButFirstLine " "
)
|> String.join "\n\n"
|> (\rendered -> description ++ "\n\n" ++ rendered)


indentAllButFirstLine : String -> String -> String
indentAllButFirstLine indent str =
case String.lines str of
[] ->
str

first :: rest ->
(first :: List.map ((++) indent) rest)
|> String.join "\n"


verticalBar : String -> String -> String -> String
verticalBar comparison below above =
Expand Down
63 changes: 63 additions & 0 deletions tests/src/Tests.elm
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import Test.Html.Query.MarkdownTests
import Test.Html.QueryTests
import Test.Html.SelectorTests
import Test.Runner
import Test.Runner.Failure


all : Test
Expand Down Expand Up @@ -96,6 +97,68 @@ expectationTests =
Expect.all []
|> expectToFail
]
, describe "Expect.oneOf"
[ test "fails with empty list" <|
\_ ->
Expect.oneOf []
|> expectToFail
, test "passes if the subject satisfies at least one of the expectations" <|
\_ ->
let
user =
{ isPremiumMember = False
, cartTotal = 75
, coupon = Nothing
}
in
Expect.oneOf
[ user.isPremiumMember |> Expect.equal True
, user.cartTotal |> Expect.atLeast 50
, user.coupon |> Expect.notEqual Nothing
]
, test "fails if the subject satisfies none of the expectations" <|
\_ ->
let
user =
{ isPremiumMember = False
, cartTotal = 10
, coupon = Nothing
}
in
Expect.oneOf
[ user.isPremiumMember |> Expect.equal True
, user.cartTotal |> Expect.atLeast 50
, user.coupon |> Expect.notEqual Nothing
]
|> expectToFail
, test "reports every failed expectation when none pass" <|
\_ ->
let
user =
{ isPremiumMember = False
, cartTotal = 10
, coupon = Nothing
}

checks =
[ user.isPremiumMember |> Expect.equal True
, user.cartTotal |> Expect.atLeast 50
, user.coupon |> Expect.notEqual Nothing
]
in
case Test.Runner.getFailureReason (Expect.oneOf checks) of
Just { reason } ->
case reason of
Test.Runner.Failure.Multiple failures ->
List.length failures
|> Expect.equal (List.length checks)

_ ->
Expect.fail "expected a Multiple reason"

Nothing ->
Expect.fail "expected a failure"
]
, describe "Expect.equal"
[ test "fails when equating two floats (see #230)" <|
\_ ->
Expand Down