Skip to content

Commit

Permalink
Use & simplify NUnit/FsUnitTyped assert methods. (#209)
Browse files Browse the repository at this point in the history
* Use & simplify NUnit/FsUnitTyped assert methods.
* CollectionAssert for contain funcs & type annotation for 'shouldHaveLength'.
* Fix globbing for formatting.
* feat: use StringAssert

Co-authored-by: Sergey Tihon <sergey.tihon@gmail.com>
  • Loading branch information
CaptnCodr and sergey-tihon committed Apr 6, 2022
1 parent fdd3508 commit b4f29af
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 21 deletions.
2 changes: 1 addition & 1 deletion build.fsx
Expand Up @@ -137,7 +137,7 @@ Target.create "CleanDocs" (fun _ ->
let sourceFiles =
!! "src/**/*.fs"
++ "tests/**/*.fs"
-- "./**/*AssemblyInfo.fs"
-- "./**/*Assembly*.fs"

Target.create "CheckFormat" (fun _ ->
let result =
Expand Down
28 changes: 8 additions & 20 deletions src/FsUnit.NUnit/FsUnitTyped.fs
Expand Up @@ -2,7 +2,6 @@ namespace FsUnitTyped

open System.Diagnostics
open NUnit.Framework
open System.Collections.Generic

[<AutoOpen>]
module TopLevelOperators =
Expand All @@ -17,47 +16,36 @@ module TopLevelOperators =

[<DebuggerStepThrough>]
let shouldContain (expected: 'a) (actual: 'a seq) =
let list = List<_>()

for a in actual do
list.Add a

Assert.Contains(expected, list)
CollectionAssert.Contains(actual, expected)

[<DebuggerStepThrough>]
let shouldBeEmpty(actual: 'a seq) =
Assert.IsEmpty(actual)

[<DebuggerStepThrough>]
let shouldNotContain (expected: 'a) (actual: 'a seq) =
if Seq.exists ((=) expected) actual then
failwith $"Seq %A{actual} should not contain %A{expected}"
CollectionAssert.DoesNotContain(actual, expected, $"Seq %A{actual} should not contain %A{expected}")

[<DebuggerStepThrough>]
let shouldBeSmallerThan (expected: 'a) (actual: 'a) =
Assert.Less(actual, expected, $"Expected: %A{expected}\nActual: %A{actual}")
Assert.Less(actual, expected)

[<DebuggerStepThrough>]
let shouldBeGreaterThan (expected: 'a) (actual: 'a) =
Assert.Greater(actual, expected, $"Expected: %A{expected}\nActual: %A{actual}")
Assert.Greater(actual, expected)

[<DebuggerStepThrough>]
let shouldFail<'exn when 'exn :> exn>(f: unit -> unit) =
Assert.Throws(Is.InstanceOf<'exn>(), TestDelegate(f)) |> ignore

[<DebuggerStepThrough>]
let shouldContainText (expected: string) (actual: string) =
if actual.Contains(expected) |> not then
failwith $"\"{expected}\" is not a substring of \"{actual}\""
StringAssert.Contains(expected, actual)

[<DebuggerStepThrough>]
let shouldNotContainText (expected: string) (actual: string) =
if actual.Contains(expected) then
failwith $"\"{expected}\" is a substring of \"{actual}\""
StringAssert.DoesNotContain(expected, actual)

[<DebuggerStepThrough>]
let shouldHaveLength expected list =
let actual = Seq.length list

if actual <> expected then
failwith $"Invalid length in %A{list}\r\nExpected: {expected}\r\nActual: {actual}"
let shouldHaveLength (expected: int) actual =
Assert.That(Seq.length actual, Is.EqualTo(expected), $"Invalid length in %A{actual}")

0 comments on commit b4f29af

Please sign in to comment.