Skip to content

Commit

Permalink
Add BeOneOf/NotBeOneOf
Browse files Browse the repository at this point in the history
  • Loading branch information
Christer van der Meeren committed Sep 8, 2023
1 parent c768995 commit f28ac31
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 0 deletions.
136 changes: 136 additions & 0 deletions src/Faqt.Tests/BasicAssertions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,142 @@ WithCustomEquality: true
"""


module BeOneOf =


[<Fact>]
let ``Can be chained with AndDerived with found value`` () =
let x = (1, 2)
let y = (1, 2)

x
.Should()
.BeOneOf([ y ])
.Id<AndDerived<int * int, int * int>>()
.That.Should(())
.BeSameAs(y)


let passData = [
[| box<string> null; [ (null: string) ] |]
[| box "a"; [ "a" ] |]
[| box "a"; [ "a"; "b" ] |]
]


[<Theory>]
[<MemberData(nameof passData)>]
let ``Passes if found`` (subject: string) (expected: seq<string>) = subject.Should().BeOneOf(expected)


let failData = [
[| box<string> null; List<string>.Empty |]
[| "a"; List<string>.Empty |]
[| box "a"; List<string>.Empty |]
[| box "a"; [ "b" ] |]
[| box "a"; [ "b"; "c" ] |]
]


[<Theory>]
[<MemberData(nameof failData)>]
let ``Fails if not found`` (subject: string) (expected: seq<string>) =
assertFails (fun () -> subject.Should().BeOneOf(expected))


[<Fact>]
let ``Fails with expected message`` () =
fun () ->
let x = 1
x.Should().BeOneOf([ 2; 3 ])
|> assertExnMsg
"""
Subject: x
Should: BeOneOf
Candidates: [2, 3]
But was: 1
"""


[<Fact>]
let ``Fails with expected message with because`` () =
fun () ->
let x = 1
x.Should().BeOneOf([ 2; 3 ], "Some reason")
|> assertExnMsg
"""
Subject: x
Because: Some reason
Should: BeOneOf
Candidates: [2, 3]
But was: 1
"""


module NotBeOneOf =


[<Fact>]
let ``Can be chained with And`` () =
(1).Should().NotBeOneOf([ 2 ]).Id<And<int>>().And.Be(1)


let passData = [
[| box<string> null; List<string>.Empty |]
[| "a"; List<string>.Empty |]
[| box "a"; List<string>.Empty |]
[| box "a"; [ "b" ] |]
[| box "a"; [ "b"; "c" ] |]
]


[<Theory>]
[<MemberData(nameof passData)>]
let ``Passes if not found`` (subject: string) (expected: seq<string>) = subject.Should().NotBeOneOf(expected)


let failData = [
[| box<string> null; [ (null: string) ] |]
[| box "a"; [ "a" ] |]
[| box "a"; [ "a"; "b" ] |]
]


[<Theory>]
[<MemberData(nameof failData)>]
let ``Fails if found`` (subject: string) (expected: seq<string>) =
assertFails (fun () -> subject.Should().NotBeOneOf(expected))


[<Fact>]
let ``Fails with expected message`` () =
fun () ->
let x = 1
x.Should().NotBeOneOf([ 1; 2 ])
|> assertExnMsg
"""
Subject: x
Should: NotBeOneOf
Candidates: [1, 2]
But was: 1
"""


[<Fact>]
let ``Fails with expected message with because`` () =
fun () ->
let x = 1
x.Should().NotBeOneOf([ 1; 2 ], "Some reason")
|> assertExnMsg
"""
Subject: x
Because: Some reason
Should: NotBeOneOf
Candidates: [1, 2]
But was: 1
"""


module BeSameAs =


Expand Down
21 changes: 21 additions & 0 deletions src/Faqt/BasicAssertions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,27 @@ type BasicAssertions =
And(t)


/// Asserts that the subject is equal to one of the specified values.
[<Extension>]
static member BeOneOf(t: Testable<'a>, candidates: seq<'a>, ?because) : AndDerived<'a, 'a> =
use _ = t.Assert()

match candidates |> Seq.tryFind ((=) t.Subject) with
| None -> t.With("Candidates", candidates).With("But was", t.Subject).Fail(because)
| Some x -> AndDerived(t, x)


/// Asserts that the subject is not equal to one of the specified values. Passes if the candidate list is empty.
[<Extension>]
static member NotBeOneOf(t: Testable<'a>, candidates: seq<'a>, ?because) : And<'a> =
use _ = t.Assert()

if candidates |> Seq.exists ((=) t.Subject) then
t.With("Candidates", candidates).With("But was", t.Subject).Fail(because)

And(t)


/// Asserts that the subject is reference equal to the specified value (which must not be null).
[<Extension>]
static member BeSameAs(t: Testable<'a>, expected: 'a, ?because) : And<'a> =
Expand Down

0 comments on commit f28ac31

Please sign in to comment.