Skip to content

Commit

Permalink
Fixes #37 ref #33, #34, improve exit msg, fix internal tests
Browse files Browse the repository at this point in the history
  • Loading branch information
haf committed Dec 25, 2016
1 parent 3e9f675 commit 0acf162
Show file tree
Hide file tree
Showing 9 changed files with 326 additions and 252 deletions.
7 changes: 3 additions & 4 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
"version": "0.2.0",
"configurations": [
{
"name": "Debug Tests",
"name": "Debug Sample",
"type": "mono",
"request": "launch",
"program": "${workspaceRoot}/Expecto.Tests/bin/Release/Expecto.Tests.exe",
"args": ["--list-tests"],
"program": "${workspaceRoot}/Expecto.Sample/bin/Debug/Expecto.Sample.exe",
"args": ["--debug"],
"cwd": "${workspaceRoot}",
"preLaunchTask": "compile",
"runtimeExecutable": null,
"env": {},
"externalConsole": false
Expand Down
5 changes: 3 additions & 2 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"command": "rake",
"command": "bundle",
"isShellCommand": true,
"args": [],
"showOutput": "always",
"tasks": [
{
"taskName": "compile",
"args": [
"compile"
"exec",
"rake"
],
"isBuildCommand": true
}
Expand Down
35 changes: 32 additions & 3 deletions Expecto.Sample/Expecto.Sample.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,42 @@ open Expecto
[<Tests>]
let tests =
testList "samples" [
testCase "universe exists" <| fun _ ->
testCase "universe exists (╭ರᴥ•́)" <| fun _ ->
let subject = true
Expect.isTrue subject "I compute, therefore I am."

testCase "should fail" <| fun _ ->
testCase "when true is not (should fail)" <| fun _ ->
let subject = false
Expect.isTrue subject "I should fail because the subject is false."
Expect.isTrue subject "I should fail because the subject is false"

testCase "I'm skipped (should skip)" <| fun _ ->
Tests.skiptest "Yup, waiting for a sunny day..."

testCase "I'm always fail (should fail)" <| fun _ ->
Tests.failtest "This was expected..."

testCase "contains things" <| fun _ ->
Expect.containsAll [| 2; 3; 4 |] [| 2; 4 |]
"This is the case; {2,3,4} contains {2,4}"

testCase "contains things (should fail)" <| fun _ ->
Expect.containsAll [| 2; 3; 4 |] [| 2; 4; 1 |]
"Expecting we have one (1) in there"

testCase "Sometimes I want to ༼ノಠل͟ಠ༽ノ ︵ ┻━┻" <| fun _ ->
Expect.equal "\
abc\n\
dëf\
"
"abc\ndef"
"These should equal"

test "I am (should fail)" {
"╰〳 ಠ 益 ಠೃ 〵╯" |> Expect.equal true false
}

testCase "You know exns" <| fun _ ->
failwith "unhandled exception from test code"
]

[<EntryPoint>]
Expand Down
2 changes: 1 addition & 1 deletion Expecto.Tests/Main.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ open Expecto

[<EntryPoint>]
let main args =
runTestsInAssembly defaultConfig args
runTestsInAssembly defaultConfig args
227 changes: 113 additions & 114 deletions Expecto.Tests/Prelude.fs
Original file line number Diff line number Diff line change
@@ -1,114 +1,113 @@
namespace Expecto
#nowarn "44"

open System
open System.Text.RegularExpressions
open FSharpx

module Seq =
let (|Empty|Cons|) l =
if Seq.isEmpty l
then Empty
else Cons(Seq.head l, Seq.skip 1 l)

let (|One|_|) l =
match Seq.toList l with
| [x] -> Some x
| _ -> None

let (|Two|_|) l =
match Seq.toList l with
| [x;y] -> Some(x,y)
| _ -> None

module String =
let internal nullBool2 f a b =
if a = null && a = null then
true
elif a = null || b = null then
false
else
f b a

let internal nullOption2 f a b =
nullBool2 f a b |> Option.ofBool

let (|StartsWith|_|) =
nullOption2 (fun (s: string) -> s.StartsWith)

let (|Contains|_|) =
nullOption2 (fun (s: string) -> s.Contains)

[<AutoOpen>]
module TestHelpers =
open Expecto
open Expecto.Impl

let evalSilent = eval TestPrinters.Default Seq.map

let inline assertTestFails test =
let test = TestCase test
match evalSilent test with
| [{ TestRunResult.result = TestResult.Failed _ }] -> ()
| x -> failtestf "Should have failed, but was %A" x

let inline assertTestFailsWithMsg (msg : string) test =
let normalize str = Regex.Replace(msg, @"\s", "")

let test = TestCase test
match evalSilent test with
| [{ TestRunResult.result = TestResult.Failed x }] when String.Equals(normalize x, normalize msg) -> ()
| [{ TestRunResult.result = TestResult.Failed x }] -> failtestf "Shold have failed with message: \"%s\" but failed with \"%s\"" msg x
| x -> failtestf "Should have failed, but was %A" x


open FsCheck

let genLimitedTimeSpan =
lazy (
Arb.generate<TimeSpan>
|> Gen.suchThat (fun t -> t.Days = 0)
)

let genTestResultCounts =
lazy (
gen {
let! passed = Arb.generate<string list>
let! ignored = Arb.generate<string list>
let! failed = Arb.generate<string list>
let! errored = Arb.generate<string list>
let! duration = genLimitedTimeSpan.Value
return
{ TestResultSummary.passed = passed
ignored = ignored
failed = failed
errored = errored
duration = duration }
}
)

let shrinkTestResultCounts (c: TestResultSummary) : TestResultSummary seq =
seq {
for passed in Arb.shrink c.passed do
for ignored in Arb.shrink c.ignored do
for failed in Arb.shrink c.failed do
for errored in Arb.shrink c.errored do
for duration in Arb.shrink c.duration ->
{
TestResultSummary.passed = passed
ignored = ignored
failed = failed
errored = errored
duration = duration
}
}

let arbTestResultCounts =
lazy (
Arb.fromGenShrink(genTestResultCounts.Value, shrinkTestResultCounts)
)

let twoTestResultCounts =
lazy (
Gen.two arbTestResultCounts.Value.Generator |> Arb.fromGen
)
namespace Expecto
#nowarn "44"

open System
open System.Text.RegularExpressions
open FSharpx

module Seq =
let (|Empty|Cons|) l =
if Seq.isEmpty l
then Empty
else Cons(Seq.head l, Seq.skip 1 l)

let (|One|_|) l =
match Seq.toList l with
| [x] -> Some x
| _ -> None

let (|Two|_|) l =
match Seq.toList l with
| [x;y] -> Some(x,y)
| _ -> None

module String =
let internal nullBool2 f a b =
if a = null && a = null then
true
elif a = null || b = null then
false
else
f b a

let internal nullOption2 f a b =
nullBool2 f a b |> Option.ofBool

let (|StartsWith|_|) =
nullOption2 (fun (s: string) -> s.StartsWith)

let (|Contains|_|) =
nullOption2 (fun (s: string) -> s.Contains)

[<AutoOpen>]
module TestHelpers =
open Expecto
open Expecto.Impl

let evalSilent = eval TestPrinters.silent Seq.map

let inline assertTestFails test =
let test = TestCase test
match evalSilent test with
| [{ TestRunResult.result = TestResult.Failed _ }] -> ()
| x -> failtestf "Should have failed, but was %A" x

let inline assertTestFailsWithMsg (msg : string) test =
let test = TestCase test
match evalSilent test with
| [{ TestRunResult.result = TestResult.Failed x }] ->
let trimmed = x.Trim('\n')
Expect.equal trimmed msg "Test failure strings should equal"
| x ->
failtestf "Should have failed, but was %A" x

open FsCheck

let genLimitedTimeSpan =
lazy (
Arb.generate<TimeSpan>
|> Gen.suchThat (fun t -> t.Days = 0)
)

let genTestResultCounts =
lazy (
gen {
let! passed = Arb.generate<string list>
let! ignored = Arb.generate<string list>
let! failed = Arb.generate<string list>
let! errored = Arb.generate<string list>
let! duration = genLimitedTimeSpan.Value
return
{ TestResultSummary.passed = passed
ignored = ignored
failed = failed
errored = errored
duration = duration }
}
)

let shrinkTestResultCounts (c: TestResultSummary) : TestResultSummary seq =
seq {
for passed in Arb.shrink c.passed do
for ignored in Arb.shrink c.ignored do
for failed in Arb.shrink c.failed do
for errored in Arb.shrink c.errored do
for duration in Arb.shrink c.duration ->
{
TestResultSummary.passed = passed
ignored = ignored
failed = failed
errored = errored
duration = duration
}
}

let arbTestResultCounts =
lazy (
Arb.fromGenShrink(genTestResultCounts.Value, shrinkTestResultCounts)
)

let twoTestResultCounts =
lazy (
Gen.two arbTestResultCounts.Value.Generator |> Arb.fromGen
)
Loading

0 comments on commit 0acf162

Please sign in to comment.