Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor toward actual/expected #208

Merged
merged 4 commits into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/FsUnit.MsTestUnit/FsUnit.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
88 changes: 44 additions & 44 deletions src/FsUnit.NUnit/FsUnit.fs
Original file line number Diff line number Diff line change
Expand Up @@ -29,70 +29,70 @@ module TopLevelOperators =
let unique = UniqueItemsConstraint()

[<DebuggerNonUserCode>]
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<AssertionException>

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>)

Expand All @@ -102,20 +102,20 @@ 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)

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)
36 changes: 18 additions & 18 deletions src/FsUnit.NUnit/FsUnitTyped.fs
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,44 @@ module TopLevelOperators =
Assert.That(actual, FsUnit.Equality.IsNotEqualTo(expected))

[<DebuggerStepThrough>]
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)

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

[<DebuggerStepThrough>]
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}"

[<DebuggerStepThrough>]
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}")

[<DebuggerStepThrough>]
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}")

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

[<DebuggerStepThrough>]
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}\""

[<DebuggerStepThrough>]
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}\""

[<DebuggerStepThrough>]
let shouldHaveLength expected list =
Expand Down
Loading