From b4f29af63b809e5a84fcd4835bad6b47860a08da Mon Sep 17 00:00:00 2001 From: Constantin Tews Date: Wed, 6 Apr 2022 21:55:14 +0200 Subject: [PATCH] Use & simplify NUnit/FsUnitTyped assert methods. (#209) * 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 --- build.fsx | 2 +- src/FsUnit.NUnit/FsUnitTyped.fs | 28 ++++++++-------------------- 2 files changed, 9 insertions(+), 21 deletions(-) diff --git a/build.fsx b/build.fsx index 764a4db8..1d3d727b 100644 --- a/build.fsx +++ b/build.fsx @@ -137,7 +137,7 @@ Target.create "CleanDocs" (fun _ -> let sourceFiles = !! "src/**/*.fs" ++ "tests/**/*.fs" - -- "./**/*AssemblyInfo.fs" + -- "./**/*Assembly*.fs" Target.create "CheckFormat" (fun _ -> let result = diff --git a/src/FsUnit.NUnit/FsUnitTyped.fs b/src/FsUnit.NUnit/FsUnitTyped.fs index abb431c1..0ac6cca8 100644 --- a/src/FsUnit.NUnit/FsUnitTyped.fs +++ b/src/FsUnit.NUnit/FsUnitTyped.fs @@ -2,7 +2,6 @@ namespace FsUnitTyped open System.Diagnostics open NUnit.Framework -open System.Collections.Generic [] module TopLevelOperators = @@ -17,12 +16,7 @@ module TopLevelOperators = [] 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) [] let shouldBeEmpty(actual: 'a seq) = @@ -30,16 +24,15 @@ module TopLevelOperators = [] 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}") [] let shouldBeSmallerThan (expected: 'a) (actual: 'a) = - Assert.Less(actual, expected, $"Expected: %A{expected}\nActual: %A{actual}") + Assert.Less(actual, expected) [] let shouldBeGreaterThan (expected: 'a) (actual: 'a) = - Assert.Greater(actual, expected, $"Expected: %A{expected}\nActual: %A{actual}") + Assert.Greater(actual, expected) [] let shouldFail<'exn when 'exn :> exn>(f: unit -> unit) = @@ -47,17 +40,12 @@ module TopLevelOperators = [] let shouldContainText (expected: string) (actual: string) = - if actual.Contains(expected) |> not then - failwith $"\"{expected}\" is not a substring of \"{actual}\"" + StringAssert.Contains(expected, actual) [] let shouldNotContainText (expected: string) (actual: string) = - if actual.Contains(expected) then - failwith $"\"{expected}\" is a substring of \"{actual}\"" + StringAssert.DoesNotContain(expected, actual) [] - 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}")