Skip to content

Instantly share code, notes, and snippets.

@ursenzler
Created February 11, 2021 12:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ursenzler/293b9f23affe7543f8dac288f6fc199b to your computer and use it in GitHub Desktop.
Save ursenzler/293b9f23affe7543f8dac288f6fc199b to your computer and use it in GitHub Desktop.
Our wrappers around FluentAssertions to make them simpler to use from F# code.
[<AutoOpen>]
module FluentAssertionsExtensionMethods
open System
open System.Collections
open System.Runtime.CompilerServices
open FluentAssertions
open FluentAssertions.Collections
open FluentAssertions.Equivalency
open FluentAssertions.Numeric
open FluentAssertions.Primitives
/// structurally compares two enumerables
let shouldBeEquivalentsTo expected (actual : 'a when 'a :> IEnumerable) =
actual.Should().BeEquivalentTo(expected :> IEnumerable, "") |> ignore
/// structurally compares two enumerables with custom error message
let shouldBeEquivalentsTof expected (because : string) (actual : 'a when 'a :> IEnumerable)=
actual.Should().BeEquivalentTo(expected :> IEnumerable, because) |> ignore
/// checks for emptyness
let shouldBeEmpty (actual : 'a when 'a :> IEnumerable) =
actual.Should().BeEmpty("", []) |> ignore
/// structurally compares two values
let shouldBe (expected : 'a) (actual : 'a) =
actual.Should().BeEquivalentTo(
expected,
"",
[]
)
/// structurally compares two values with custom error message
let shouldBef (expected : 'a) because (actual : 'a) =
actual.Should().BeEquivalentTo(
expected,
because,
[]
)
/// structurally compares two values and enforces structural comparison even on types implementing Equals
let shouldBeEquivalentTo (expected : 'a) (actual : 'a) =
actual.Should().BeEquivalentTo(
expected,
Func<_, _>(fun (config : EquivalencyAssertionOptions<'a>) ->
config.ComparingByMembers<obj>()),
"",
[]
)
/// structurally compares two values with custom error message and enforces structural comparison even on types implementing Equals
let shouldBeEquivalentTof (expected : 'a) (because : string) (actual : 'a) =
actual.Should().BeEquivalentTo(
expected,
Func<_, _>(fun (config : EquivalencyAssertionOptions<'a>) ->
config.ComparingByMembers<obj>()),
because,
[]
)
/// helpers to eliminate casts in test code
[<Extension>]
type FluentAssertionsHelpers =
[<Extension>]
static member inline BeEquivalentTo(assertion : CollectionAssertions<'subject, 'assertions>, expected : 't list) =
assertion.BeEquivalentTo(expected :> IEnumerable, "") |> ignore
[<Extension>]
static member inline BeEquivalentTo(assertion : CollectionAssertions<'subject, 'assertions>, expected : 't seq) =
assertion.BeEquivalentTo(expected :> IEnumerable, "") |> ignore
[<Extension>]
static member inline BeEquivalentTo(assertion : CollectionAssertions<'subject, 'assertions>, expected : 't array) =
assertion.BeEquivalentTo(expected :> IEnumerable, "") |> ignore
[<Extension>]
static member inline ContainEquivalentOf(assertion : CollectionAssertions<'subject, 'assertions>, expected : 't) =
assertion.ContainEquivalentOf(expected, "") |> ignore
[<Extension>]
static member inline BeEquivalentTo(assertion : ObjectAssertions, expected : 't) =
assertion.BeEquivalentTo(expected, "")
[<Extension>]
static member inline Should<'a>(actualValue : 'a list) =
(actualValue |> List.toSeq).Should()
[<Extension>]
static member inline Be(should : ComparableTypeAssertions<'a>, expected : 'a) =
should.Be(expected, "", []) |> ignore
[<Extension>]
static member inline Be(should : ObjectAssertions, expected : 'a) =
should.Be(expected, "", []) |> ignore
[<Extension>]
static member inline BeEquivalentTo(should : ComparableTypeAssertions<'a>, expected : 'a) =
should.BeEquivalentTo(expected, "", [])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment