-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Christer van der Meeren
committed
Sep 4, 2023
1 parent
098ab0b
commit 30899c1
Showing
4 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters