Skip to content

Commit d001504

Browse files
committed
fix: update Sprout and Tests logic, adjust expected log
1 parent dace2ea commit d001504

File tree

3 files changed

+70
-14
lines changed

3 files changed

+70
-14
lines changed

Sprout.fs

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,37 +36,41 @@ type ItBuilder(name: string) =
3636
let it name = ItBuilder name
3737
let pending name = It.Pending name
3838

39-
type DescribeStep =
39+
type Step =
4040
| It of It
4141
| LogStatement of LogLevel
4242

4343
type Describe = {
4444
Name: string
45-
Steps: DescribeStep list
45+
Steps: Step list
4646
Each: EachFunction list
4747
Children: Describe list
4848
}
4949
with
50-
static member Empty name = {
50+
member this.TotalCount =
51+
let rec countSteps (describe: Describe) =
52+
describe.Children.Length + (describe.Children |> List.map countSteps |> List.sum)
53+
countSteps this
54+
static member New name = {
5155
Name = name
5256
Steps = []
5357
Each = []
5458
Children = []
5559
}
5660

5761
type DescribeBuilder(name) =
58-
member _.Zero() = Describe.Empty name
62+
member _.Zero() = Describe.New name
5963
member _.Yield(each: EachFunction) =
60-
{ Describe.Empty name with Each = [each] }
64+
{ Describe.New name with Each = [each] }
6165
member _.Yield(tc: It) =
62-
{ Describe.Empty name with Steps = [It tc] }
66+
{ Describe.New name with Steps = [It tc] }
6367
member _.Yield(log: LogLevel) =
64-
{ Describe.Empty name with Steps = [LogStatement log] }
68+
{ Describe.New name with Steps = [LogStatement log] }
6569
member _.Yield(describe: Describe) =
66-
{ Describe.Empty name with Children = [describe] }
70+
{ Describe.New name with Children = [describe] }
6771
member _.Combine(a, b) =
6872
{
69-
Describe.Empty name
73+
Describe.New name
7074
with
7175
Each = a.Each @ b.Each
7276
Children = a.Children @ b.Children
@@ -77,7 +81,7 @@ type DescribeBuilder(name) =
7781
let sb =
7882
sequence
7983
|> Seq.map body
80-
|> Seq.fold (fun s a -> this.Combine(s, a)) (Describe.Empty name)
84+
|> Seq.fold (fun s a -> this.Combine(s, a)) (Describe.New name)
8185
sb
8286
member _.Run(f: Describe) = f
8387

@@ -99,6 +103,7 @@ type TestResult =
99103
| Pending of Path * string
100104

101105
type ITestReporter =
106+
abstract Begin : totalCount:int -> unit
102107
abstract BeginSuite : name:string * path:Path -> unit
103108
abstract ReportResult : result:TestResult * path:Path -> unit
104109
abstract EndSuite : name:string * path:Path -> unit
@@ -124,6 +129,9 @@ module Reporters =
124129
ConsoleReporter("", "", "", " ")
125130

126131
interface ITestReporter with
132+
member _.Begin(totalCount) =
133+
printfn $"Running %d{totalCount} tests..."
134+
sw.Restart()
127135
member _.BeginSuite(name, path) =
128136
let indent = indent path
129137
printfn $"%s{indent}{AnsiColours.green}{name}{AnsiColours.reset}"
@@ -165,6 +173,32 @@ module Reporters =
165173
printfn $"Summary: {passedCount} passed, {failedCount} failed, {pendingCount} pending"
166174
printfn $"Total time: %s{sw.Elapsed.ToString()}"
167175

176+
type TapReporter() =
177+
interface ITestReporter with
178+
member this.Begin(totalCount: int): unit =
179+
printfn "TAP version 14"
180+
printfn "1..%d" totalCount
181+
member this.BeginSuite(name: string, path: Path): unit =
182+
()
183+
member this.Debug(message: string, path: Path): unit =
184+
()
185+
member this.End(arg1: TestResult list): unit = ()
186+
member this.EndSuite(name: string, path: Path): unit =
187+
printfn ""
188+
member this.Info(message: string, path: Path): unit =
189+
()
190+
member this.ReportResult(result: TestResult, path: Path): unit =
191+
match result with
192+
| Passed (_, name) ->
193+
printf "ok %s\n" name
194+
| Failed (_, name, ex) ->
195+
printf
196+
"not ok %s\n ---\n message: %s\n severity: fail\n ...\n"
197+
name
198+
ex.Message
199+
| Pending (_, name) ->
200+
printf "ok %s # SKIP\n" name
201+
168202
type TestContext = {
169203
Path: Path
170204
ParentBeforeHooks: HookFunction list
@@ -173,7 +207,7 @@ type TestContext = {
173207
Log: string -> unit
174208
}
175209
with
176-
static member Empty = {
210+
static member New = {
177211
Path = Path []
178212
ParentBeforeHooks = []
179213
ParentAfterHooks = []
@@ -251,11 +285,12 @@ let rec private doRunTestSuite (suite: Describe) (context: TestContext): TestRes
251285
allResults
252286

253287
let runTestSuiteWithContext (sb: Describe) (context: TestContext) =
288+
context.Reporter.Begin(sb.TotalCount)
254289
let testResults = doRunTestSuite sb { context with Path = Path (context.Path.Value @ [sb.Name]) }
255290
context.Reporter.EndSuite(sb.Name, context.Path)
256291
context.Reporter.End testResults
257292

258-
let runTestSuite (describe: Describe) = runTestSuiteWithContext describe TestContext.Empty
293+
let runTestSuite (describe: Describe) = runTestSuiteWithContext describe TestContext.New
259294

260295
[<AutoOpen>]
261296
module Constraints =

Tests.fsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,4 @@ let suite = describe "A larger test suite" {
6565
}
6666

6767
runTestSuite suite
68+
runTestSuiteWithContext suite { TestContext.New with Reporter = Reporters.TapReporter() }

expected.log

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
Running 2 tests...
12
Main Suite
23
Suite 1
34
Suite 2
@@ -6,7 +7,8 @@
67
 ✅ passed: should pass in Suite 2
78
All tests passed!
89
Summary: 1 passed, 0 failed, 0 pending
9-
Total time: 00:00:00.0046286
10+
Total time: 00:00:00.0042149
11+
Running 4 tests...
1012
A larger test suite
1113
Top level info message
1214
Before each test
@@ -42,4 +44,22 @@ There were 2 test failures:
4244
- A larger test suite / should fail - Intentional failure
4345
- A larger test suite / Arithmetic / Faulty Addition / should fail when adding incorrect numbers - Expected 5 but got 4
4446
Summary: 4 passed, 2 failed, 1 pending
45-
Total time: 00:00:00.0036246
47+
Total time: 00:00:00.0034352
48+
TAP version 14
49+
1..4
50+
ok should pass
51+
not ok should fail
52+
---
53+
message: Intentional failure
54+
severity: fail
55+
...
56+
ok This is a pending test # SKIP
57+
ok should also pass
58+
ok should add two numbers correctly
59+
ok should handle negative numbers
60+
not ok should fail when adding incorrect numbers
61+
---
62+
message: Expected 5 but got 4
63+
severity: fail
64+
...
65+

0 commit comments

Comments
 (0)