Skip to content

Commit

Permalink
Fix testCaseTask to accept a task, not an Async (#484)
Browse files Browse the repository at this point in the history
* Fix testCaseTask to accept a task, not an Async

Also fix test name conficts

* Remove tests that flake on linux
There appears to be other tests on the same functionality anyway
https://github.com/haf/expecto/blob/d9c19fbd1d0868ed81b4ab27143f65bd3bec00cb/Expecto.Tests/Tests.fs#L1453
  • Loading branch information
farlee2121 authored Nov 24, 2023
1 parent d9c19fb commit a36ff46
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 75 deletions.
16 changes: 2 additions & 14 deletions Expecto.Tests/PerformanceTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,15 @@ let runMD5() = md5.ComputeHash data
let runSHA256() = sha256.ComputeHash data


open System.Runtime.InteropServices;
[<Tests>]
let performance =
testSequenced <| testList "performance cryptography tests" [

testCase "md5 equals sha256" (fun _ ->
Expect.isFasterThan runMD5 runSHA256 "MD5 equals SHA256 should fail"
) |> assertTestFailsWithMsgContaining "same"

testCase "sha256 versus md5" (fun _ ->
Expect.isFasterThan
(runSHA256 >> ignore |> repeat10)
(runMD5 >> ignore |> repeat10)
"SHA256 is faster than MD5 should fail"
) |> assertTestFailsWithMsgContaining "slower"

testCase "md5 versus sha256" <| fun _ ->
Expect.isFasterThan
(runMD5 >> ignore |> repeat10)
(runSHA256 >> ignore |> repeat10)
"MD5 is faster than SHA256"
]
]

[<Tests>]
let findFastest =
Expand Down
92 changes: 48 additions & 44 deletions Expecto.Tests/Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1396,57 +1396,61 @@ let taskTests =
fun ms -> task { return ms.CanWrite ==? true }
]

testCaseTask "simple" <| task {
Expect.equal 1 1 "1=1"
}

testCaseTask "let" <| task {
let! n = async { return 1 }
Expect.equal n 1 "n=1"
}

testCaseTask "can fail" <| task {
let! n = async { return 2 }
Expect.equal n 1 "n=1"
} |> assertTestFails

testTask "simple" {
do! Task.Delay 1
Expect.equal 1 1 "1=1"
}
testList "testCaseTask" [
testCaseTask "simple" <| task {
Expect.equal 1 1 "1=1"
}

testTask "let" {
let! n = Task.FromResult 23
Expect.equal n 23 "n=23"
}
testCaseTask "let" <| task {
let! n = async { return 1 }
Expect.equal n 1 "n=1"
}

testTask "can fail" {
let! n = Task.FromResult 2
testCaseTask "can fail" <| task {
let! n = async { return 2 }
Expect.equal n 1 "n=1"
} |> assertTestFails
} |> assertTestFails
]

testTask "two" {
let! n = Task.FromResult 2
let! m = Task.FromResult (3*n)
Expect.equal m 6 "m=6"
}
testList "testTask" [
testTask "simple" {
do! Task.Delay 1
Expect.equal 1 1 "1=1"
}

testTask "two can fail" {
let! n = Task.FromResult 2
let! m = Task.FromResult (3*n)
Expect.equal m 7 "m=7"
} |> assertTestFails
testTask "let" {
let! n = Task.FromResult 23
Expect.equal n 23 "n=23"
}

testTask "two can fail middle" {
let! n = Task.FromResult 2
Expect.equal n 3 "n=3"
let! m = Task.FromResult (3*n)
Expect.equal m 6 "m=6"
} |> assertTestFails
testTask "can fail" {
let! n = Task.FromResult 2
Expect.equal n 1 "n=1"
} |> assertTestFails

testTask "inner skip" {
skiptest "skipped"
}
testTask "two" {
let! n = Task.FromResult 2
let! m = Task.FromResult (3*n)
Expect.equal m 6 "m=6"
}

testTask "two can fail" {
let! n = Task.FromResult 2
let! m = Task.FromResult (3*n)
Expect.equal m 7 "m=7"
} |> assertTestFails

testTask "two can fail middle" {
let! n = Task.FromResult 2
Expect.equal n 3 "n=3"
let! m = Task.FromResult (3*n)
Expect.equal m 6 "m=6"
} |> assertTestFails

testTask "inner skip" {
skiptest "skipped"
}
]
]

[<Tests>]
Expand Down
10 changes: 5 additions & 5 deletions Expecto/Expecto.fs
Original file line number Diff line number Diff line change
Expand Up @@ -88,23 +88,23 @@ module Tests =
/// Builds an async test case
let inline testCaseAsync name test = TestLabel(name, TestCase (Async test,Normal), Normal)
/// Builds an async test case from a task
let inline testCaseTask name test = TestLabel(name, TestCase (Async test,Normal), Normal)
let inline testCaseTask name test = TestLabel(name, TestCase (Async (Async.AwaitTask test),Normal), Normal)
/// Builds a test case that will make Expecto to ignore other unfocused tests
let inline ftestCase name test = TestLabel(name, TestCase (Sync test, Focused), Focused)
/// Builds a test case with cancel that will make Expecto to ignore other unfocused tests
let inline ftestCaseWithCancel name test = TestLabel(name, TestCase (SyncWithCancel test, Focused), Focused)
/// Builds an async test case that will make Expecto to ignore other unfocused tests
let inline ftestCaseAsync name test = TestLabel(name, TestCase (Async test, Focused), Focused)
/// Builds an async test case from a task, that will make Expecto to ignore other unfocused tests
let inline ftestCaseTask name test = TestLabel(name, TestCase (Async test, Focused), Focused)
let inline ftestCaseTask name test = TestLabel(name, TestCase (Async (Async.AwaitTask test), Focused), Focused)
/// Builds a test case that will be ignored by Expecto
let inline ptestCase name test = TestLabel(name, TestCase (Sync test, Pending), Pending)
/// Builds a test case with cancel that will be ignored by Expecto
let inline ptestCaseWithCancel name test = TestLabel(name, TestCase (SyncWithCancel test, Pending), Pending)
/// Builds an async test case that will be ignored by Expecto
let inline ptestCaseAsync name test = TestLabel(name, TestCase (Async test, Pending), Pending)
/// Builds an async test case from a task, that will be ignored by Expecto
let inline ptestCaseTask name test = TestLabel(name, TestCase (Async test, Pending), Pending)
let inline ptestCaseTask name test = TestLabel(name, TestCase (Async (Async.AwaitTask test), Pending), Pending)
/// Test case or list needs to run sequenced. Use for any benchmark code or
/// for tests using `Expect.isFasterThan`
let inline testSequenced test = Sequenced (Synchronous,test)
Expand Down Expand Up @@ -235,8 +235,8 @@ module Tests =
member inline __.TryFinally(p, cf) = task.TryFinally(p, cf)
member inline __.TryWith(p, cf) = task.TryWith(p, cf)
member __.Run f =
let a = async {
do! task.Run f |> Async.AwaitTask
let a = task {
do! task.Run f

Check warning on line 239 in Expecto/Expecto.fs

View workflow job for this annotation

GitHub Actions / build

This state machine is not statically compilable. The resumable code value(s) 'f' does not have a definition. An alternative dynamic implementation will be used, which may be slower. Consider adjusting your code to ensure this state machine is statically compilable, or else suppress this warning.
}
match focusState with
| Normal -> testCaseTask name a
Expand Down
24 changes: 12 additions & 12 deletions build.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -169,17 +169,17 @@ Target.create "Release" (fun _ ->

Target.create "All" ignore

// "CheckEnv"
// ==> "Release"

// "Clean"
// ==> "BuildExpecto"
// ==> "BuildBenchmarkDotNet"
// ==> "BuildTest"
// ==> "RunTest"
// ==> "Pack"
// ==> "All"
// ==> "Push"
// ==> "Release"
"CheckEnv"
==> "Release"

"Clean"
==> "BuildExpecto"
==> "BuildBenchmarkDotNet"
==> "BuildTest"
==> "RunTest"
==> "Pack"
==> "All"
==> "Push"
==> "Release"

Target.runOrDefaultWithArguments "All"

0 comments on commit a36ff46

Please sign in to comment.