Skip to content

Commit

Permalink
Add EnumAssertions
Browse files Browse the repository at this point in the history
  • Loading branch information
Christer van der Meeren committed Sep 4, 2023
1 parent 098ab0b commit 30899c1
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 0 deletions.
118 changes: 118 additions & 0 deletions src/Faqt.Tests/EnumAssertions.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
module EnumAssertions

open System.Text.RegularExpressions
open Faqt
open Xunit


module HaveFlag =


[<Fact>]
let ``Can be chained with And`` () =
RegexOptions.Compiled
.Should()
.HaveFlag(RegexOptions.Compiled)
.Id<And<RegexOptions>>()
.And.Be(RegexOptions.Compiled)


[<Theory>]
[<InlineData(RegexOptions.Compiled, RegexOptions.Compiled)>]
[<InlineData(RegexOptions.Compiled ||| RegexOptions.Multiline, RegexOptions.Multiline)>]
[<InlineData(RegexOptions.Compiled ||| RegexOptions.Multiline, RegexOptions.Compiled ||| RegexOptions.Multiline)>]
let ``Passes if has flag`` (subject: RegexOptions) (expected: RegexOptions) = subject.Should().HaveFlag(expected)


[<Theory>]
[<InlineData(RegexOptions.Compiled, RegexOptions.Multiline)>]
[<InlineData(RegexOptions.Compiled ||| RegexOptions.Multiline, RegexOptions.IgnoreCase)>]
[<InlineData(RegexOptions.Compiled ||| RegexOptions.Multiline, RegexOptions.Multiline ||| RegexOptions.IgnoreCase)>]
let ``Fails if not has flag`` (subject: RegexOptions) (expected: RegexOptions) =
assertFails (fun () -> subject.Should().HaveFlag(expected))


[<Fact>]
let ``Fails with expected message`` () =
fun () ->
let x = RegexOptions.Compiled ||| RegexOptions.Multiline
x.Should().HaveFlag(RegexOptions.IgnoreCase)
|> assertExnMsg
"""
Subject: x
Should: HaveFlag
Flag: IgnoreCase
But was: Multiline, Compiled
"""


[<Fact>]
let ``Fails with expected message with because`` () =
fun () ->
let x = RegexOptions.Compiled
x.Should().HaveFlag(RegexOptions.IgnoreCase, "Some reason")
|> assertExnMsg
"""
Subject: x
Because: Some reason
Should: HaveFlag
Flag: IgnoreCase
But was: Compiled
"""


module NotHaveFlag =


[<Fact>]
let ``Can be chained with And`` () =
RegexOptions.Compiled
.Should()
.NotHaveFlag(RegexOptions.IgnoreCase)
.Id<And<RegexOptions>>()
.And.Be(RegexOptions.Compiled)


[<Theory>]
[<InlineData(RegexOptions.Compiled, RegexOptions.Multiline)>]
[<InlineData(RegexOptions.Compiled ||| RegexOptions.Multiline, RegexOptions.IgnoreCase)>]
[<InlineData(RegexOptions.Compiled ||| RegexOptions.Multiline, RegexOptions.Multiline ||| RegexOptions.IgnoreCase)>]
let ``Passes if not has flag`` (subject: RegexOptions) (expected: RegexOptions) =
subject.Should().NotHaveFlag(expected)


[<Theory>]
[<InlineData(RegexOptions.Compiled, RegexOptions.Compiled)>]
[<InlineData(RegexOptions.Compiled ||| RegexOptions.Multiline, RegexOptions.Multiline)>]
[<InlineData(RegexOptions.Compiled ||| RegexOptions.Multiline, RegexOptions.Compiled ||| RegexOptions.Multiline)>]
let ``Fails if has flag`` (subject: RegexOptions) (expected: RegexOptions) =
assertFails (fun () -> subject.Should().NotHaveFlag(expected))


[<Fact>]
let ``Fails with expected message`` () =
fun () ->
let x = RegexOptions.Compiled ||| RegexOptions.Multiline
x.Should().NotHaveFlag(RegexOptions.Compiled)
|> assertExnMsg
"""
Subject: x
Should: NotHaveFlag
Flag: Compiled
But was: Multiline, Compiled
"""


[<Fact>]
let ``Fails with expected message with because`` () =
fun () ->
let x = RegexOptions.Compiled
x.Should().NotHaveFlag(RegexOptions.Compiled, "Some reason")
|> assertExnMsg
"""
Subject: x
Because: Some reason
Should: NotHaveFlag
Flag: Compiled
But was: Compiled
"""
1 change: 1 addition & 0 deletions src/Faqt.Tests/Faqt.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<Compile Include="DictionaryAssertions.fs" />
<Compile Include="SeqAssertions.fs" />
<Compile Include="GuidAssertions.fs" />
<Compile Include="EnumAssertions.fs" />
</ItemGroup>

<ItemGroup>
Expand Down
31 changes: 31 additions & 0 deletions src/Faqt/EnumAssertions.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace Faqt

open System
open System.Runtime.CompilerServices
open AssertionHelpers


[<Extension>]
type EnumAssertions =


/// Asserts that the subject enum has the specified flag.
[<Extension>]
static member HaveFlag<'a when 'a :> Enum>(t: Testable<'a>, flag: 'a, ?because) : And<'a> =
use _ = t.Assert()

if not (t.Subject.HasFlag(flag)) then
t.With("Flag", flag).With("But was", t.Subject).Fail(because)

And(t)


/// Asserts that the subject enum does not have the specified flag.
[<Extension>]
static member NotHaveFlag<'a when 'a :> Enum>(t: Testable<'a>, flag: 'a, ?because) : And<'a> =
use _ = t.Assert()

if t.Subject.HasFlag(flag) then
t.With("Flag", flag).With("But was", t.Subject).Fail(because)

And(t)
1 change: 1 addition & 0 deletions src/Faqt/Faqt.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<Compile Include="DictionaryAssertions.fs" />
<Compile Include="SeqAssertions.fs" />
<Compile Include="GuidAssertions.fs" />
<Compile Include="EnumAssertions.fs" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 30899c1

Please sign in to comment.