diff --git a/src/FsUnit.MsTestUnit/FsUnit.fs b/src/FsUnit.MsTestUnit/FsUnit.fs index 8c1717a7..f7842f29 100644 --- a/src/FsUnit.MsTestUnit/FsUnit.fs +++ b/src/FsUnit.MsTestUnit/FsUnit.fs @@ -15,18 +15,18 @@ type Assert with static member That<'a>(actual, matcher: IMatcher<'a>) = assertThat(actual, matcher) -let inline should (f: 'a -> ^b) x (y: obj) = - let c = f x +let inline should (f: 'a -> ^b) x (actual: obj) = + let matcher = f x - let y = - match y with + let actual = + match actual with | :? (unit -> unit) as assertFunc -> box assertFunc - | _ -> y + | _ -> actual - if isNull(box c) then - assertThat(y, Is.Null()) + if isNull(box matcher) then + assertThat(actual, Is.Null()) else - assertThat(y, c) + assertThat(actual, matcher) let inline shouldFail(f: unit -> unit) = let failed = diff --git a/src/FsUnit.NUnit/FsUnit.fs b/src/FsUnit.NUnit/FsUnit.fs index cde05ddd..d85f0ec0 100644 --- a/src/FsUnit.NUnit/FsUnit.fs +++ b/src/FsUnit.NUnit/FsUnit.fs @@ -29,70 +29,70 @@ module TopLevelOperators = let unique = UniqueItemsConstraint() [] - let should (f: 'a -> #Constraint) x (y: obj) = - let c = f x + let should (f: 'a -> #Constraint) x (actual: obj) = + let expression = f x let y = - match y with - | :? (unit -> unit) -> box(TestDelegate(y :?> unit -> unit)) - | _ -> y + match actual with + | :? (unit -> unit) -> box(TestDelegate(actual :?> unit -> unit)) + | _ -> actual - if isNull(box c) then + if isNull(box expression) then Assert.That(y, Is.Null) else - Assert.That(y, c) + Assert.That(y, expression) - let equal x = - Equality.IsEqualTo(x) + let equal expected = + Equality.IsEqualTo(expected) - let equivalent x = - CollectionEquivalentConstraint(x) + let equivalent expected = + CollectionEquivalentConstraint(expected) - let equalWithin tolerance x = - equal(x).Within tolerance + let equalWithin tolerance expected = + equal(expected).Within tolerance - let contain x = - ContainsConstraint(x) + let contain expected = + ContainsConstraint(expected) - let haveLength n = - Has.Length.EqualTo(n) + let haveLength expected = + Has.Length.EqualTo(expected) - let haveCount n = - Has.Count.EqualTo(n) + let haveCount expected = + Has.Count.EqualTo(expected) let be = id - let sameAs x = - SameAsConstraint(x) + let sameAs expected = + SameAsConstraint(expected) let throw = Throws.TypeOf - let throwWithMessage (m: string) (t: System.Type) = - Throws.TypeOf(t).And.Message.EqualTo(m) + let throwWithMessage (expected: string) (t: System.Type) = + Throws.TypeOf(t).And.Message.EqualTo(expected) - let greaterThan x = - GreaterThanConstraint(x) + let greaterThan expected = + GreaterThanConstraint(expected) - let greaterThanOrEqualTo x = - GreaterThanOrEqualConstraint(x) + let greaterThanOrEqualTo expected = + GreaterThanOrEqualConstraint(expected) - let lessThan x = - LessThanConstraint(x) + let lessThan expected = + LessThanConstraint(expected) - let lessThanOrEqualTo x = - LessThanOrEqualConstraint(x) + let lessThanOrEqualTo expected = + LessThanOrEqualConstraint(expected) let shouldFail(f: unit -> unit) = TestDelegate(f) |> should throw typeof - let endWith(s: string) = - EndsWithConstraint s + let endWith(expected: string) = + EndsWithConstraint expected - let startWith(s: string) = - StartsWithConstraint s + let startWith(expected: string) = + StartsWithConstraint expected - let haveSubstring(s: string) = - SubstringConstraint s + let haveSubstring(expected: string) = + SubstringConstraint expected let ofExactType<'a> = ExactTypeConstraint(typeof<'a>) @@ -102,11 +102,11 @@ module TopLevelOperators = let descending = Is.Ordered.Descending - let not' x = - if isNull(box x) then + let not' baseConstraint = + if isNull(box baseConstraint) then NotConstraint(Null) else - NotConstraint(x) + NotConstraint(baseConstraint) let inRange min max = RangeConstraint(min, max) @@ -114,8 +114,8 @@ module TopLevelOperators = let ofCase case = OfSameCaseConstraint(case) - let supersetOf x = - CollectionSupersetConstraint(x) + let supersetOf expected = + CollectionSupersetConstraint(expected) - let subsetOf x = - CollectionSubsetConstraint(x) + let subsetOf expected = + CollectionSubsetConstraint(expected) diff --git a/src/FsUnit.NUnit/FsUnitTyped.fs b/src/FsUnit.NUnit/FsUnitTyped.fs index 24c4fb67..abb431c1 100644 --- a/src/FsUnit.NUnit/FsUnitTyped.fs +++ b/src/FsUnit.NUnit/FsUnitTyped.fs @@ -16,44 +16,44 @@ module TopLevelOperators = Assert.That(actual, FsUnit.Equality.IsNotEqualTo(expected)) [] - let shouldContain (x: 'a) (y: 'a seq) = + let shouldContain (expected: 'a) (actual: 'a seq) = let list = List<_>() - for a in y do + for a in actual do list.Add a - Assert.Contains(x, list) + Assert.Contains(expected, list) [] - let shouldBeEmpty(list: 'a seq) = - Assert.IsEmpty(list) + let shouldBeEmpty(actual: 'a seq) = + Assert.IsEmpty(actual) [] - let shouldNotContain (x: 'a) (y: 'a seq) = - if Seq.exists ((=) x) y then - failwith $"Seq %A{y} should not contain %A{x}" + let shouldNotContain (expected: 'a) (actual: 'a seq) = + if Seq.exists ((=) expected) actual then + failwith $"Seq %A{actual} should not contain %A{expected}" [] - let shouldBeSmallerThan (x: 'a) (y: 'a) = - Assert.Less(y, x, $"Expected: %A{x}\nActual: %A{y}") + let shouldBeSmallerThan (expected: 'a) (actual: 'a) = + Assert.Less(actual, expected, $"Expected: %A{expected}\nActual: %A{actual}") [] - let shouldBeGreaterThan (x: 'a) (y: 'a) = - Assert.Greater(y, x, $"Expected: %A{x}\nActual: %A{y}") + let shouldBeGreaterThan (expected: 'a) (actual: 'a) = + Assert.Greater(actual, expected, $"Expected: %A{expected}\nActual: %A{actual}") [] let shouldFail<'exn when 'exn :> exn>(f: unit -> unit) = Assert.Throws(Is.InstanceOf<'exn>(), TestDelegate(f)) |> ignore [] - let shouldContainText (x: string) (y: string) = - if y.Contains(x) |> not then - failwith $"\"{x}\" is not a substring of \"{y}\"" + let shouldContainText (expected: string) (actual: string) = + if actual.Contains(expected) |> not then + failwith $"\"{expected}\" is not a substring of \"{actual}\"" [] - let shouldNotContainText (x: string) (y: string) = - if y.Contains(x) then - failwith $"\"{x}\" is a substring of \"{y}\"" + let shouldNotContainText (expected: string) (actual: string) = + if actual.Contains(expected) then + failwith $"\"{expected}\" is a substring of \"{actual}\"" [] let shouldHaveLength expected list = diff --git a/src/FsUnit.Xunit/CustomMatchers.fs b/src/FsUnit.Xunit/CustomMatchers.fs index d0398b42..b8cd9a9f 100644 --- a/src/FsUnit.Xunit/CustomMatchers.fs +++ b/src/FsUnit.Xunit/CustomMatchers.fs @@ -7,48 +7,48 @@ open NHamcrest open NHamcrest.Core open System.Reflection -let equal x = - CustomMatcher($"Equals %A{x}", (fun a -> a = x)) +let equal expected = + CustomMatcher($"Equals %A{expected}", (fun actual -> expected = actual)) -let equivalent f x = - let matches(c: obj) = +let equivalent f expected = + let matches(actual: obj) = try let toCollection o = o |> Seq.cast |> Seq.toArray :> ICollection - match c with + match actual with | :? IEnumerable as e -> - f (toCollection x) (toCollection e) + f (toCollection expected) (toCollection e) true | _ -> false with | _ -> false - CustomMatcher($"Equivalent to %A{x}", Func<_, _> matches) + CustomMatcher($"Equivalent to %A{expected}", Func<_, _> matches) //TODO: Look into a better way of doing this. -let equalWithin (t: obj) (x: obj) = - let matches(a: obj) = +let equalWithin (tolerance: obj) (expected: obj) = + let matches(actual: obj) = let actualParsed, actual = - Double.TryParse(string a, NumberStyles.Any, CultureInfo("en-US")) + Double.TryParse(string actual, NumberStyles.Any, CultureInfo("en-US")) let expectedParsed, expect = - Double.TryParse(string x, NumberStyles.Any, CultureInfo("en-US")) + Double.TryParse(string expected, NumberStyles.Any, CultureInfo("en-US")) let toleranceParsed, tol = - Double.TryParse(string t, NumberStyles.Any, CultureInfo("en-US")) + Double.TryParse(string tolerance, NumberStyles.Any, CultureInfo("en-US")) if actualParsed && expectedParsed && toleranceParsed then abs(actual - expect) <= tol else false - CustomMatcher($"%A{x} with a tolerance of %A{t}", Func<_, _> matches) + CustomMatcher($"%A{expected} with a tolerance of %A{tolerance}", Func<_, _> matches) let not'(x: obj) = match box x with | null -> Is.Not(Is.Null()) - | :? (IMatcher) as matcher -> Is.Not(matcher) + | :? IMatcher as matcher -> Is.Not(matcher) | x -> Is.Not(CustomMatcher($"Equals %A{x}", (fun a -> a = x)) :> IMatcher) let throw(t: Type) = @@ -100,8 +100,8 @@ let True = CustomMatcher("True", (fun b -> unbox b = true)) let False = CustomMatcher("False", (fun b -> unbox b = false)) let NaN = - let matches(x: obj) = - match x with + let matches(actual: obj) = + match actual with | :? single as s -> Single.IsNaN(s) | :? double as d -> Double.IsNaN(d) | _ -> false @@ -109,8 +109,8 @@ let NaN = CustomMatcher("NaN", Func<_, _> matches) let unique = - let matches(x: obj) = - match x with + let matches(actual: obj) = + match actual with | :? IEnumerable as e -> let isAllItemsUnique x = let y = Seq.distinct x @@ -121,41 +121,45 @@ let unique = CustomMatcher("All items unique", Func<_, _> matches) -let sameAs x = - Is.SameAs(x) +let sameAs expected = + Is.SameAs(expected) -let greaterThan(x: obj) = +let greaterThan(expected: obj) = let matches(actual: obj) = - (unbox actual :> IComparable).CompareTo(unbox x) > 0 + let comparable = unbox actual :> IComparable + comparable.CompareTo(unbox expected) > 0 - CustomMatcher($"Greater than %A{x}", Func<_, _> matches) + CustomMatcher($"Greater than %A{expected}", Func<_, _> matches) -let greaterThanOrEqualTo(x: obj) = +let greaterThanOrEqualTo(expected: obj) = let matches(actual: obj) = - (unbox actual :> IComparable).CompareTo(unbox x) >= 0 + let comparable = unbox actual :> IComparable + comparable.CompareTo(unbox expected) >= 0 - CustomMatcher($"Greater than or equal to %A{x}", Func<_, _> matches) + CustomMatcher($"Greater than or equal to %A{expected}", Func<_, _> matches) -let lessThan(x: obj) = +let lessThan(expected: obj) = let matches(actual: obj) = - (unbox actual :> IComparable).CompareTo(unbox x) < 0 + let comparable = unbox actual :> IComparable + comparable.CompareTo(unbox expected) < 0 - CustomMatcher($"Less than %A{x}", Func<_, _> matches) + CustomMatcher($"Less than %A{expected}", Func<_, _> matches) -let lessThanOrEqualTo(x: obj) = +let lessThanOrEqualTo(expected: obj) = let matches(actual: obj) = - (unbox actual :> IComparable).CompareTo(unbox x) <= 0 + let comparable = unbox actual :> IComparable + comparable.CompareTo(unbox expected) <= 0 - CustomMatcher($"Less than or equal to %A{x}", Func<_, _> matches) + CustomMatcher($"Less than or equal to %A{expected}", Func<_, _> matches) -let endWith(x: string) = - CustomMatcher(string x, (fun s -> (string s).EndsWith x)) +let endWith(expected: string) = + CustomMatcher(string expected, (fun s -> (string s).EndsWith expected)) -let startWith(x: string) = - CustomMatcher(string x, (fun s -> (string s).StartsWith x)) +let startWith(expected: string) = + CustomMatcher(string expected, (fun s -> (string s).StartsWith expected)) -let haveSubstring(x: string) = - CustomMatcher(string x, (fun s -> (string s).Contains x)) +let haveSubstring(expected: string) = + CustomMatcher(string expected, (fun s -> (string s).Contains expected)) let ofExactType<'a> = CustomMatcher(typeof<'a>.ToString (), (fun x -> (unbox x).GetType() = typeof<'a>)) @@ -163,16 +167,16 @@ let ofExactType<'a> = let instanceOfType<'a> = CustomMatcher(typeof<'a>.ToString (), (fun x -> typeof<'a>.IsInstanceOfType (x))) -let contain x = - let matches(c: obj) = - match c with - | :? (list<_>) as l -> l |> List.exists(fun i -> i = x) - | :? (array<_>) as a -> a |> Array.exists(fun i -> i = x) - | :? (seq<_>) as s -> s |> Seq.exists(fun i -> i = x) - | :? IEnumerable as e -> e |> Seq.cast |> Seq.exists(fun i -> i = x) +let contain expected = + let matches(actual: obj) = + match actual with + | :? (list<_>) as l -> l |> List.exists(fun i -> i = expected) + | :? (array<_>) as a -> a |> Array.exists(fun i -> i = expected) + | :? (seq<_>) as s -> s |> Seq.exists(fun i -> i = expected) + | :? IEnumerable as e -> e |> Seq.cast |> Seq.exists(fun i -> i = expected) | _ -> false - CustomMatcher($"Contains %A{x}", Func<_, _> matches) + CustomMatcher($"Contains %A{expected}", Func<_, _> matches) let private (?) (this: 'Source) (name: string) : 'Result = let bindingFlags = @@ -188,15 +192,15 @@ let private (?) (this: 'Source) (name: string) : 'Result = property.GetValue(this, null) :?> 'Result -let haveLength n = - CustomMatcher($"Have Length %d{n}", (fun x -> x?Length = n)) +let haveLength expected = + CustomMatcher($"Have Length %d{expected}", (fun x -> x?Length = expected)) -let haveCount n = - CustomMatcher($"Have Count %d{n}", (fun x -> x?Count = n)) +let haveCount expected = + CustomMatcher($"Have Count %d{expected}", (fun x -> x?Count = expected)) let containf f = - let matches(c: obj) = - match c with + let matches(actual: obj) = + match actual with | :? (list<_>) as l -> l |> List.exists f | :? (array<_>) as a -> a |> Array.exists f | :? (seq<_>) as s -> s |> Seq.exists f @@ -205,11 +209,11 @@ let containf f = CustomMatcher($"Contains %A{f}", Func<_, _> matches) -let supersetOf x = - CustomMatcher($"Is superset of %A{x}", (fun c -> Set.isSuperset (Set(unbox c)) (Set x))) +let supersetOf expected = + CustomMatcher($"Is superset of %A{expected}", (fun c -> Set.isSuperset (Set(unbox c)) (Set expected))) -let subsetOf x = - CustomMatcher($"Is subset of %A{x}", (fun c -> Set.isSubset (Set(unbox c)) (Set x))) +let subsetOf expected = + CustomMatcher($"Is subset of %A{expected}", (fun c -> Set.isSubset (Set(unbox c)) (Set expected))) let matchList xs = let matches(ys: obj) = @@ -221,8 +225,8 @@ let matchList xs = CustomMatcher($"All elements from list %A{xs}", Func<_, _> matches) let private makeOrderedMatcher description comparer = - let matches(c: obj) = - match c with + let matches(actual: obj) = + match actual with | :? (list) as l -> l = List.sortWith comparer l | :? (array) as a -> a = Array.sortWith comparer a | :? (seq) as s -> diff --git a/src/FsUnit.Xunit/FsUnit.fs b/src/FsUnit.Xunit/FsUnit.fs index a57dfa5a..23651e6f 100644 --- a/src/FsUnit.Xunit/FsUnit.fs +++ b/src/FsUnit.Xunit/FsUnit.fs @@ -16,18 +16,18 @@ type Xunit.Assert with matcher.DescribeTo(description) raise(MatchException(description.ToString(), ($"%A{actual}"), null)) -let inline should (f: 'a -> ^b) x (y: obj) = - let c = f x +let inline should (f: 'a -> ^b) x (actual: obj) = + let matcher = f x - let y = - match y with + let actual = + match actual with | :? (unit -> unit) as assertFunc -> box assertFunc - | _ -> y + | _ -> actual - if isNull(box c) then - Assert.That(y, Is.Null()) + if isNull(box matcher) then + Assert.That(actual, Is.Null()) else - Assert.That(y, c) + Assert.That(actual, matcher) let inline shouldFail(f: unit -> unit) = let failed = @@ -105,11 +105,11 @@ let instanceOfType<'a> = CustomMatchers.instanceOfType<'a> let contain expected = CustomMatchers.contain expected -let haveLength n = - CustomMatchers.haveLength n +let haveLength expected = + CustomMatchers.haveLength expected -let haveCount n = - CustomMatchers.haveCount n +let haveCount expected = + CustomMatchers.haveCount expected let matchList = CustomMatchers.matchList