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

Solution cleanup #261

Merged
merged 4 commits into from
Dec 20, 2023
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
12 changes: 6 additions & 6 deletions build.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ let (|Fsproj|Csproj|Vbproj|) (projFileName: string) =
| f when f.EndsWith("fsproj") -> Fsproj
| f when f.EndsWith("csproj") -> Csproj
| f when f.EndsWith("vbproj") -> Vbproj
| _ -> failwith (sprintf "Project file %s not supported. Unknown project type." projFileName)
| _ -> failwith $"Project file %s{projFileName} not supported. Unknown project type."

// Generate assembly info files with the right version & up-to-date information
Target.create "AssemblyInfo" (fun _ ->
Expand Down Expand Up @@ -141,7 +141,7 @@ Target.create "CheckFormat" (fun _ ->
elif result.ExitCode = 99 then
failwith "Some files need formatting, check output for more info"
else
Trace.logf "Errors while formatting: %A" result.Errors)
Trace.logf $"Errors while formatting: %A{result.Errors}")

Target.create "Format" (fun _ ->
let result =
Expand All @@ -151,7 +151,7 @@ Target.create "Format" (fun _ ->
|> DotNet.exec id "fantomas"

if not result.OK then
printfn "Errors while formatting all files: %A" result.Messages)
printfn $"Errors while formatting all files: %A{result.Messages}")

// --------------------------------------------------------------------------------------
// Build library & test project
Expand All @@ -169,19 +169,19 @@ Target.create "NUnit" (fun _ ->
let result = DotNet.exec id "test" "tests/FsUnit.NUnit.Test/"

if not result.OK then
failwithf "NUnit test failed: %A" result.Errors)
failwithf $"NUnit test failed: %A{result.Errors}")

Target.create "xUnit" (fun _ ->
let result = DotNet.exec id "test" "tests/FsUnit.Xunit.Test/"

if not result.OK then
failwithf "xUnit test failed: %A" result.Errors)
failwithf $"xUnit test failed: %A{result.Errors}")

Target.create "MsTest" (fun _ ->
let result = DotNet.exec id "test" "tests/FsUnit.MsTest.Test/"

if not result.OK then
failwithf "MsTest test failed: %A" result.Errors)
failwithf $"MsTest test failed: %A{result.Errors}")

Target.create "RunTests" ignore

Expand Down
4 changes: 2 additions & 2 deletions src/Common.fs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module Common =
| _ -> None

/// <summary>
/// Checks wether the given value is of the same case of a union type as the
/// Checks whether the given value is of the same case of a union type as the
/// case defined by the given expression.
/// </summary>
/// <exception cref="System.Exception">If the expression is not an union case or does not result in an union case.</exception>
Expand All @@ -37,7 +37,7 @@ module Common =
| Lambda(_, expr)
| Let(_, _, expr) -> isOfCase expr
| NewUnionCase(case, _) ->
// Returns a function that check wether the tag of the argument matches
// Returns a function that check whether the tag of the argument matches
// the tag of the union given in the expression.
let readTag = FSharpValue.PreComputeUnionTagReader case.DeclaringType

Expand Down
4 changes: 2 additions & 2 deletions src/FsUnit.MsTestUnit/FsUnit.fs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ let inline private assertThat(actual, matcher: IMatcher<'a>) =
raise(AssertFailedException($"%A{description} was %A{value}", null))

match box actual with
| :? (unit -> unit) as actualfunc ->
| :? (unit -> unit) as actualFunc ->
(try
actualfunc()
actualFunc()
String.Empty
with ex ->
ex.ToString())
Expand Down
4 changes: 1 addition & 3 deletions src/FsUnit.NUnit/CustomConstraints.fs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,4 @@ module CustomConstraints =
let result = Common.isOfCase this.Expected actual
ConstraintResult(this, actual, result)
else
let actualType = actual.GetType()
do printf $"Got a {actualType.Name}"
CaptnCodr marked this conversation as resolved.
Show resolved Hide resolved
failwith "Value (not expression) is not a union case."
failwith $"Value (not expression) is not a union case. Got a {actual.GetType().Name}."
15 changes: 7 additions & 8 deletions src/FsUnit.NUnit/FsUnitTyped.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ namespace FsUnitTyped

open System.Diagnostics
open NUnit.Framework
open NUnit.Framework.Legacy

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

[<DebuggerStepThrough>]
let shouldContain (expected: 'a) (actual: 'a seq) =
CollectionAssert.Contains(actual, expected)
Assert.That(actual, Does.Contain(expected))

[<DebuggerStepThrough>]
let shouldBeEmpty(actual: 'a seq) =
ClassicAssert.IsEmpty(actual)
Assert.That(actual, Is.Empty)

[<DebuggerStepThrough>]
let shouldNotContain (expected: 'a) (actual: 'a seq) =
CollectionAssert.DoesNotContain(actual, expected, $"Seq %A{actual} should not contain %A{expected}")
Assert.That(actual, Does.Not.Contain(expected), $"Seq %A{actual} should not contain %A{expected}")

[<DebuggerStepThrough>]
let shouldBeSmallerThan (expected: 'a) (actual: 'a) =
ClassicAssert.Less(actual, expected)
Assert.That(actual, Is.LessThan(expected))

[<DebuggerStepThrough>]
let shouldBeGreaterThan (expected: 'a) (actual: 'a) =
ClassicAssert.Greater(actual, expected)
Assert.That(actual, Is.GreaterThan(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) =
StringAssert.Contains(expected, actual)
Assert.That(actual, Does.Contain(expected))

[<DebuggerStepThrough>]
let shouldNotContainText (expected: string) (actual: string) =
StringAssert.DoesNotContain(expected, actual)
Assert.That(actual, Does.Not.Contain(expected))

[<DebuggerStepThrough>]
let shouldHaveLength (expected: int) actual =
Expand Down
93 changes: 40 additions & 53 deletions src/FsUnit.Xunit/CustomMatchers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ let equalWithin (tolerance: obj) (expected: obj) =
let matches(actual: obj) =
let parseValue(v: string) =
match Double.TryParse(v, NumberStyles.Any, CultureInfo("en-US")) with
| (true, x) -> Some(x)
| (false, _) -> None
| true, x -> Some(x)
| false, _ -> None

let actual = string actual |> parseValue
let expect = string expected |> parseValue
Expand Down Expand Up @@ -185,10 +185,10 @@ let instanceOfType<'a> =
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)
| :? list<_> as l -> l |> List.exists((=) expected)
| :? array<_> as a -> a |> Array.exists((=) expected)
| :? seq<_> as s -> s |> Seq.exists((=) expected)
| :? IEnumerable as e -> e |> Seq.cast |> Seq.exists((=) expected)
| _ -> false

CustomMatcher<obj>($"Contains %A{expected}", Func<_, _> matches)
Expand All @@ -213,17 +213,6 @@ let haveLength expected =
let haveCount expected =
CustomMatcher<obj>($"Have Count %d{expected}", (fun x -> x?Count = expected))

let containf f =
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
| :? IEnumerable as e -> e |> Seq.cast |> Seq.exists f
| _ -> false

CustomMatcher<obj>($"Contains %A{f}", Func<_, _> matches)

let supersetOf expected =
CustomMatcher<obj>($"Is superset of %A{expected}", (fun c -> Set.isSuperset (Set(unbox c)) (Set expected)))

Expand All @@ -233,7 +222,7 @@ let subsetOf expected =
let matchList xs =
let matches(ys: obj) =
match ys with
| :? (list<_>) as ys' -> List.sort xs = List.sort ys'
| :? list<_> as ys' -> List.sort xs = List.sort ys'
| :? IEnumerable as e -> e |> Seq.cast |> Seq.isEmpty && xs |> Seq.isEmpty
| _ -> false

Expand All @@ -242,9 +231,9 @@ let matchList xs =
let private makeOrderedMatcher description comparer =
let matches(actual: obj) =
match actual with
| :? (list<IComparable>) as l -> l = List.sortWith comparer l
| :? (array<IComparable>) as a -> a = Array.sortWith comparer a
| :? (seq<IComparable>) as s ->
| :? list<IComparable> as l -> l = List.sortWith comparer l
| :? array<IComparable> as a -> a = Array.sortWith comparer a
| :? seq<IComparable> as s ->
let a = s |> Seq.toArray
a = (a |> Array.sortWith comparer)
| :? IEnumerable as e ->
Expand All @@ -262,52 +251,51 @@ type ChoiceDiscriminator(n: int) =

member this.check(c: Choice<'a, 'b>) : bool =
match c with
| Choice1Of2(_) -> n = 1
| Choice2Of2(_) -> n = 2
| Choice1Of2 _ -> n = 1
| Choice2Of2 _ -> n = 2

member this.check(c: Choice<'a, 'b, 'c>) : bool =
match c with
| Choice1Of3(_) -> n = 1
| Choice2Of3(_) -> n = 2
| Choice3Of3(_) -> n = 3
| Choice1Of3 _ -> n = 1
| Choice2Of3 _ -> n = 2
| Choice3Of3 _ -> n = 3

member this.check(c: Choice<'a, 'b, 'c, 'd>) : bool =
match c with
| Choice1Of4(_) -> n = 1
| Choice2Of4(_) -> n = 2
| Choice3Of4(_) -> n = 3
| Choice4Of4(_) -> n = 4
| Choice1Of4 _ -> n = 1
| Choice2Of4 _ -> n = 2
| Choice3Of4 _ -> n = 3
| Choice4Of4 _ -> n = 4

member this.check(c: Choice<'a, 'b, 'c, 'd, 'e>) : bool =
match c with
| Choice1Of5(_) -> n = 1
| Choice2Of5(_) -> n = 2
| Choice3Of5(_) -> n = 3
| Choice4Of5(_) -> n = 4
| Choice5Of5(_) -> n = 5
| Choice1Of5 _ -> n = 1
| Choice2Of5 _ -> n = 2
| Choice3Of5 _ -> n = 3
| Choice4Of5 _ -> n = 4
| Choice5Of5 _ -> n = 5

member this.check(c: Choice<'a, 'b, 'c, 'd, 'e, 'f>) : bool =
match c with
| Choice1Of6(_) -> n = 1
| Choice2Of6(_) -> n = 2
| Choice3Of6(_) -> n = 3
| Choice4Of6(_) -> n = 4
| Choice5Of6(_) -> n = 5
| Choice6Of6(_) -> n = 6
| Choice1Of6 _ -> n = 1
| Choice2Of6 _ -> n = 2
| Choice3Of6 _ -> n = 3
| Choice4Of6 _ -> n = 4
| Choice5Of6 _ -> n = 5
| Choice6Of6 _ -> n = 6

member this.check(c: Choice<'a, 'b, 'c, 'd, 'e, 'f, 'g>) : bool =
match c with
| Choice1Of7(_) -> n = 1
| Choice2Of7(_) -> n = 2
| Choice3Of7(_) -> n = 3
| Choice4Of7(_) -> n = 4
| Choice5Of7(_) -> n = 5
| Choice6Of7(_) -> n = 6
| Choice7Of7(_) -> n = 7
| Choice1Of7 _ -> n = 1
| Choice2Of7 _ -> n = 2
| Choice3Of7 _ -> n = 3
| Choice4Of7 _ -> n = 4
| Choice5Of7 _ -> n = 5
| Choice6Of7 _ -> n = 6
| Choice7Of7 _ -> n = 7

member this.check(c: obj) : bool =
let cType = c.GetType()
let cArgs = cType.GetGenericArguments()
let cArgs = c.GetType().GetGenericArguments()
let cArgCount = Seq.length cArgs

try
Expand All @@ -318,7 +306,7 @@ type ChoiceDiscriminator(n: int) =
false

let choice n =
CustomMatcher<obj>($"The choice %d{n}", (fun x -> (ChoiceDiscriminator(n)).check(x)))
CustomMatcher<obj>($"The choice %d{n}", (fun x -> ChoiceDiscriminator(n).check(x)))

let inRange min max =
let matches(actual: obj) =
Expand All @@ -329,8 +317,7 @@ let inRange min max =

let ofCase(case: Quotations.Expr) =
let expected =
case |> Common.caseName |> defaultArg
<| "<The given type is not a union case and the matcher won't work.>"
defaultArg (Common.caseName case) "<The given type is not a union case and the matcher won't work.>"

let matcher = CustomMatcher(expected, (fun x -> x |> Common.isOfCase case))

Expand Down
6 changes: 3 additions & 3 deletions src/FsUnit.Xunit/FsUnit.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ open Xunit.Sdk
open NHamcrest
open NHamcrest.Core

type Xunit.Assert with
type Assert with

static member That<'a>(actual, matcher: IMatcher<'a>) =
if not(matcher.Matches(actual)) then
Expand All @@ -17,9 +17,9 @@ type Xunit.Assert with
raise(EqualException.ForMismatchedValues(description.ToString(), value))

match box actual with
| :? (unit -> unit) as actualfunc ->
| :? (unit -> unit) as actualFunc ->
(try
actualfunc()
actualFunc()
String.Empty
with ex ->
ex.ToString())
Expand Down
4 changes: 0 additions & 4 deletions src/FsUnit.Xunit/FsUnitTyped.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,10 @@ open FsUnit.Xunit
[<AutoOpen>]
module TopLevelOperators =

/// Asserts that `expected` is equal to `actual`.
/// The equality instance on `actual` is used, if available.
[<DebuggerStepThrough>]
let shouldEqual<'a> (expected: 'a) (actual: 'a) =
actual |> should equal expected

/// Asserts that `expected` is not equal to `actual`.
/// The equality instance on `actual` is used, if available.
[<DebuggerStepThrough>]
let shouldNotEqual<'a> (expected: 'a) (actual: 'a) =
actual |> should not' (equal expected)
Expand Down
4 changes: 2 additions & 2 deletions tests/FsUnit.MsTest.Test/FsUnit.MsTest.Test.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
<ItemGroup>
<Compile Include="ofCaseTests.fs" />
<Compile Include="beOfCaseTests.fs" />
<Compile Include="beAscendingTests.fs" />
<Compile Include="beChoiceTests.fs" />
<Compile Include="beDescendingTests.fs" />
Expand Down Expand Up @@ -37,7 +37,7 @@
<Compile Include="startWithTests.fs" />
<Compile Include="haveSubstringTests.fs" />
<Compile Include="instanceOfTests.fs" />
<Compile Include="NaNTests.fs" />
<Compile Include="beNaNTests.fs" />
<Compile Include="beUniqueTests.fs" />
<None Include="paket.references" />
<None Include="MSTest.runsettings" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ open FsUnit.MsTest
open System

[<TestClass>]
type ``NotANumberTests``() =
type ``beNaNTests``() =

[<TestMethod>]
member _.``Number 1 should be a number``() =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type TestUnion =
| Third of string

[<TestClass>]
type ``ofCaseTests``() =
type ``beOfCaseTests``() =

[<TestMethod>]
member _.``Given a (parameterless) union case of matching case returns true``() =
Expand Down
2 changes: 1 addition & 1 deletion tests/FsUnit.MsTest.Test/beUniqueTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ open Microsoft.VisualStudio.TestTools.UnitTesting
open FsUnit.MsTest

[<TestClass>]
type ``haveUniqueTests``() =
type ``beUniqueTests``() =

[<TestMethod>]
member _.``empty list should be considered as unique``() =
Expand Down
Loading