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 Expect.oneOf #126

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
45 changes: 45 additions & 0 deletions src/Expect.elm
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,51 @@ allHelp list query =
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."
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the feedback should be a lot more informative. This doesn't tell you at all how to fix your test.

Copy link
Author

@sashaafm sashaafm Sep 6, 2023

Choose a reason for hiding this comment

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

@gampleman could you please help provide a solution for that? Would it need to keep track of why all expectations failed and print that?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think so.

Copy link
Contributor

Choose a reason for hiding this comment

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

Although perhaps in a more condensed format? I think there's some experimentation to be done on this.

}

check :: rest ->
case check query of
Test.Expectation.Pass ->
pass

outcome ->
oneOfHelp rest query



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

Expand Down
13 changes: 13 additions & 0 deletions tests/src/Tests.elm
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,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)" <|
\_ ->
Expand Down