Skip to content

Commit

Permalink
Add AllBe
Browse files Browse the repository at this point in the history
  • Loading branch information
Christer van der Meeren committed Sep 8, 2023
1 parent 1eb6ec3 commit 8bbe8fb
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 0 deletions.
104 changes: 104 additions & 0 deletions src/Faqt.Tests/SeqAssertions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,110 @@ But was: [1, 2]
"""


module AllBe =


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


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


[<Theory>]
[<MemberData(nameof passData)>]
let ``Passes if all items are equal to the specified value`` (subject: seq<string>) (expected: string) =
subject.Should().AllBe(expected)


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


[<Theory>]
[<MemberData(nameof failData)>]
let ``Fails if null or not all items are equal to the specified value`` (subject: seq<string>) (expected: string) =
assertFails (fun () -> subject.Should().AllBe(expected)) |> ignore


[<Fact>]
let ``Fails with expected message if only subject is null`` () =
fun () ->
let x: seq<int> = null
x.Should().AllBe(1)
|> assertExnMsg
"""
Subject: x
Should: AllBe
Expected: 1
But was: null
"""


[<Fact>]
let ``Fails with expected message with because if only subject is null`` () =
fun () ->
let x: seq<int> = null
x.Should().AllBe(1, "Some reason")
|> assertExnMsg
"""
Subject: x
Because: Some reason
Should: AllBe
Expected: 1
But was: null
"""


[<Fact>]
let ``Fails with expected message if items are not equal`` () =
fun () ->
let x = [ 1; 3; 2 ]
x.Should().AllBe(3)
|> assertExnMsg
"""
Subject: x
Should: AllBe
Expected: 3
Failures:
- Index: 0
Value: 1
- Index: 2
Value: 2
Subject value: [1, 3, 2]
"""


[<Fact>]
let ``Fails with expected message with because if items are not equal`` () =
fun () ->
let x = [ 1; 3; 2 ]
x.Should().AllBe(3, "Some reason")
|> assertExnMsg
"""
Subject: x
Because: Some reason
Should: AllBe
Expected: 3
Failures:
- Index: 0
Value: 1
- Index: 2
Value: 2
Subject value: [1, 3, 2]
"""


module SequenceEqual =


Expand Down
31 changes: 31 additions & 0 deletions src/Faqt/SeqAssertions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,37 @@ type SeqAssertions =
And(t)


/// Asserts that all items in the subject are equal to the specified value.
[<Extension>]
static member AllBe(t: Testable<#seq<'a>>, expected: 'a, ?because) : And<_> =
use _ = t.Assert()

if isNull (box t.Subject) then
t.With("Expected", expected).With("But was", t.Subject).Fail(because)
else
let differentItems =
t.Subject
|> Seq.indexed
|> Seq.choose (fun (i, actualItem) ->
if actualItem <> expected then
Some {|
Index = i
Value = TryFormat actualItem
|}
else
None
)

if not (Seq.isEmpty differentItems) then
t
.With("Expected", expected)
.With("Failures", differentItems)
.With("Subject value", t.Subject)
.Fail(because)

And(t)


/// Asserts that the subject contains the same items in the same order as the specified sequence. Passes if both
/// sequences are null.
[<Extension>]
Expand Down

0 comments on commit 8bbe8fb

Please sign in to comment.