From 8bbe8fbdad81c31dc9391d47c4f86aa08e71be2f Mon Sep 17 00:00:00 2001 From: Christer van der Meeren Date: Fri, 8 Sep 2023 15:07:49 +0200 Subject: [PATCH] Add AllBe --- src/Faqt.Tests/SeqAssertions.fs | 104 ++++++++++++++++++++++++++++++++ src/Faqt/SeqAssertions.fs | 31 ++++++++++ 2 files changed, 135 insertions(+) diff --git a/src/Faqt.Tests/SeqAssertions.fs b/src/Faqt.Tests/SeqAssertions.fs index 6996af4..3c1f3af 100644 --- a/src/Faqt.Tests/SeqAssertions.fs +++ b/src/Faqt.Tests/SeqAssertions.fs @@ -625,6 +625,110 @@ But was: [1, 2] """ +module AllBe = + + + [] + let ``Can be chained with And`` () = + [ 1 ].Should().AllBe(1).Id>().And.Be([ 1 ]) + + + let passData = [ + [| box List.Empty; "a" |] + [| [ "a" ]; "a" |] + [| [ "a"; "a" ]; "a" |] + [| [ (null: string) ]; (null: string) |] + ] + + + [] + [] + let ``Passes if all items are equal to the specified value`` (subject: seq) (expected: string) = + subject.Should().AllBe(expected) + + + let failData = [ + [| box> null; "a" |] + [| [ "a" ]; "b" |] + [| [ "a"; "b" ]; "a" |] + [| [ "a"; null ]; (null: string) |] + ] + + + [] + [] + let ``Fails if null or not all items are equal to the specified value`` (subject: seq) (expected: string) = + assertFails (fun () -> subject.Should().AllBe(expected)) |> ignore + + + [] + let ``Fails with expected message if only subject is null`` () = + fun () -> + let x: seq = null + x.Should().AllBe(1) + |> assertExnMsg + """ +Subject: x +Should: AllBe +Expected: 1 +But was: null +""" + + + [] + let ``Fails with expected message with because if only subject is null`` () = + fun () -> + let x: seq = null + x.Should().AllBe(1, "Some reason") + |> assertExnMsg + """ +Subject: x +Because: Some reason +Should: AllBe +Expected: 1 +But was: null +""" + + + [] + 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] +""" + + + [] + 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 = diff --git a/src/Faqt/SeqAssertions.fs b/src/Faqt/SeqAssertions.fs index ffb18ef..765815c 100644 --- a/src/Faqt/SeqAssertions.fs +++ b/src/Faqt/SeqAssertions.fs @@ -213,6 +213,37 @@ type SeqAssertions = And(t) + /// Asserts that all items in the subject are equal to the specified value. + [] + 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. []