From 187ba40094bca4e729be23bdd984acdc311eab61 Mon Sep 17 00:00:00 2001 From: Sasha Fonseca Date: Sat, 29 Feb 2020 21:53:55 +0000 Subject: [PATCH 1/2] Add Expect.oneOf --- src/Expect.elm | 45 +++++++++++++++++++++++++++++++++++++++++++++ tests/src/Tests.elm | 13 +++++++++++++ 2 files changed, 58 insertions(+) diff --git a/src/Expect.elm b/src/Expect.elm index 86f7589e..9f33400b 100644 --- a/src/Expect.elm +++ b/src/Expect.elm @@ -715,6 +715,51 @@ allHelp list = outcome +{-| Passes if at least one of the given functions passes when applied to the +subject. + +Passing an empty list is assumed to be a mistake, so `Expect.oneOf []` +will always return a failed expectation no matter what else it is passed. + + Expect.oneOf + [ Expect.greatherThan 8 + , Expect.lessThan 5 + , Expect.lessThan 1 + ] + (List.length [1, 2, 3]) + -- Passes because (3 < 5) is True, although (3 < 8) and (3 > 1) + +-} +oneOf : List (subject -> Expectation) -> subject -> Expectation +oneOf list query = + if List.isEmpty list then + Test.Expectation.fail + { 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 query + + +oneOfHelp : List (subject -> Expectation) -> subject -> Expectation +oneOfHelp list query = + case list of + [] -> + Test.Expectation.fail + { reason = Invalid EmptyList + , description = "Expect.oneOf ran out of expectations." + } + + check :: rest -> + case check query of + Test.Expectation.Pass -> + pass + + outcome -> + oneOfHelp rest query + + {---- Private helper functions ----} diff --git a/tests/src/Tests.elm b/tests/src/Tests.elm index 7185fb3b..418d7a77 100644 --- a/tests/src/Tests.elm +++ b/tests/src/Tests.elm @@ -96,6 +96,19 @@ expectationTests = Expect.all [] |> expectToFail ] + , describe "Expect.oneOf" + [ test "fails with empty list" <| + \_ -> + "dummy subject" + |> Expect.oneOf [] + |> expectToFail + , test "fails with bad expectations" <| + \_ -> + [1, 2, 3] + |> List.length + |> Expect.oneOf [ Expect.greaterThan 5, Expect.lessThan 0 ] + |> expectToFail + ] , describe "Expect.equal" [ test "fails when equating two floats (see #230)" <| \_ -> From 2f8b904b9ae149da27c654f8f6e282c6b68ef849 Mon Sep 17 00:00:00 2001 From: Martin Janiczek Date: Wed, 23 Sep 2026 20:58:18 +0200 Subject: [PATCH 2/2] Implement using Failure.Multiple; use new shape --- CHANGELOG.md | 6 +- src/Expect.elm | 92 ++++++++++++++++++++---------- src/Test/Runner/Failure.elm | 9 +++ tests/src/Runner/String/Format.elm | 21 +++++++ tests/src/Tests.elm | 62 ++++++++++++++++++-- 5 files changed, 154 insertions(+), 36 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b7e2c69d..556a0594 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -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 diff --git a/src/Expect.elm b/src/Expect.elm index 9f33400b..e74c67de 100644 --- a/src/Expect.elm +++ b/src/Expect.elm @@ -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. @@ -23,7 +23,7 @@ module Expect exposing ## Basic Expectations -@docs Expectation, equal, notEqual, all +@docs Expectation, equal, notEqual, all, oneOf ## Numeric Comparisons @@ -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 @@ -715,49 +716,82 @@ allHelp list = outcome -{-| Passes if at least one of the given functions passes when applied to the -subject. - -Passing an empty list is assumed to be a mistake, so `Expect.oneOf []` -will always return a failed expectation no matter what else it is passed. +{-| Passes if at least one of the given expectations passes. Expect.oneOf - [ Expect.greatherThan 8 - , Expect.lessThan 5 - , Expect.lessThan 1 + [ user.isPremiumMember |> Expect.equal True + , user.cartTotal |> Expect.atLeast 50 + , user.coupon |> Expect.notEqual Nothing ] - (List.length [1, 2, 3]) - -- Passes because (3 < 5) is True, although (3 < 8) and (3 > 1) + +If none of them pass, the failure lists all the inner failures. + +`Expect.oneOf []` is reported as a test failure. -} -oneOf : List (subject -> Expectation) -> subject -> Expectation -oneOf list query = +oneOf : List Expectation -> Expectation +oneOf list = if List.isEmpty list then - Test.Expectation.fail - { reason = Invalid EmptyList + 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 query + 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 (subject -> Expectation) -> subject -> Expectation -oneOfHelp list query = +oneOfHelp : + List Expectation + -> List { given : Maybe String, description : String, reason : Reason } + -> Expectation +oneOfHelp list failuresSoFar = case list of [] -> - Test.Expectation.fail - { reason = Invalid EmptyList - , description = "Expect.oneOf ran out of expectations." + 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." } - check :: rest -> - case check query of - Test.Expectation.Pass -> - pass + (Test.Expectation.Pass _) :: _ -> + pass - outcome -> - oneOfHelp rest query + (Test.Expectation.Fail failure) :: rest -> + oneOfHelp rest + ({ given = failure.given + , description = failure.description + , reason = failure.reason + } + :: failuresSoFar + ) diff --git a/src/Test/Runner/Failure.elm b/src/Test/Runner/Failure.elm index c7c58196..becf8719 100644 --- a/src/Test/Runner/Failure.elm +++ b/src/Test/Runner/Failure.elm @@ -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. diff --git a/tests/src/Runner/String/Format.elm b/tests/src/Runner/String/Format.elm index faf25ee9..0169163c 100644 --- a/tests/src/Runner/String/Format.elm +++ b/tests/src/Runner/String/Format.elm @@ -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 = diff --git a/tests/src/Tests.elm b/tests/src/Tests.elm index 418d7a77..c7caa682 100644 --- a/tests/src/Tests.elm +++ b/tests/src/Tests.elm @@ -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 @@ -99,15 +100,64 @@ expectationTests = , describe "Expect.oneOf" [ test "fails with empty list" <| \_ -> - "dummy subject" - |> Expect.oneOf [] + Expect.oneOf [] |> expectToFail - , test "fails with bad expectations" <| + , test "passes if the subject satisfies at least one of the expectations" <| \_ -> - [1, 2, 3] - |> List.length - |> Expect.oneOf [ Expect.greaterThan 5, Expect.lessThan 0 ] + 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)" <|