@@ -5,6 +5,7 @@ let mutable debug: string -> unit = ignore
55
66type HookFunction = unit -> unit
77type EachFunction = Before of f : HookFunction | After of f : HookFunction
8+ type LogLevel = Debug of string | Info of string
89
910type EachBuilder ( factory : ( unit -> unit ) -> EachFunction ) =
1011 member _.Zero () = ()
@@ -35,16 +36,20 @@ type ItBuilder(name: string) =
3536let it name = ItBuilder name
3637let pending name = It.Pending name
3738
39+ type DescribeStep =
40+ | It of It
41+ | LogStatement of LogLevel
42+
3843type Describe = {
3944 Name: string
40- TestCases : It list
45+ Steps : DescribeStep list
4146 Each: EachFunction list
4247 Children: Describe list
4348}
4449with
4550 static member Empty name = {
4651 Name = name
47- TestCases = []
52+ Steps = []
4853 Each = []
4954 Children = []
5055 }
@@ -54,16 +59,18 @@ type DescribeBuilder(name) =
5459 member _.Yield ( each : EachFunction ) =
5560 { Describe.Empty name with Each = [ each] }
5661 member _.Yield ( tc : It ) =
57- { Describe.Empty name with TestCases = [ tc] }
58- member _.Yield ( sub : Describe ) =
59- { Describe.Empty name with Children = [ sub] }
62+ { Describe.Empty name with Steps = [ It tc] }
63+ member _.Yield ( log : LogLevel ) =
64+ { Describe.Empty name with Steps = [ LogStatement log] }
65+ member _.Yield ( describe : Describe ) =
66+ { Describe.Empty name with Children = [ describe] }
6067 member _.Combine ( a , b ) =
6168 {
6269 Describe.Empty name
6370 with
6471 Each = a.Each @ b.Each
6572 Children = a.Children @ b.Children
66- TestCases = a.TestCases @ b.TestCases
73+ Steps = a.Steps @ b.Steps
6774 }
6875 member _.Delay ( f : unit -> Describe ) = f()
6976 member this.For ( sequence : seq < 'T >, body : 'T -> Describe ) =
@@ -76,6 +83,8 @@ type DescribeBuilder(name) =
7683
7784let describe name = DescribeBuilder name
7885
86+ type LogState = { Messages: string list }
87+
7988type Path = Path of string list
8089with
8190 member this.Length =
@@ -107,9 +116,13 @@ module Reporters =
107116 let white = " \u001b [37m"
108117 let reset = " \u001b [0m"
109118
110- type ConsoleReporter () =
119+ type ConsoleReporter ( passedChar , failedChar , pendingChar ) =
111120 let sw = Stopwatch.StartNew()
112121 let indent ( path : Path ) = String.replicate (( path.Length - 1 ) * 2 ) " "
122+
123+ new () =
124+ ConsoleReporter( " ✅" , " ❌" , " ❔" )
125+
113126 interface ITestReporter with
114127 member _.BeginSuite ( name , path ) =
115128 let indent = indent path
@@ -119,11 +132,11 @@ module Reporters =
119132 let indent = indent path
120133 match result with
121134 | Passed (_, name) ->
122- printfn $" %s {indent}%s {AnsiColours.green} ✅ passed: %s {name}%s {AnsiColours.reset}"
135+ printfn $" %s {indent}%s {AnsiColours.green} %s {passedChar} passed: %s {name}%s {AnsiColours.reset}"
123136 | Failed (_, name, ex) ->
124- printfn $" %s {indent}%s {AnsiColours.red} ❌ failed: %s {name} - %s {ex.Message}%s {AnsiColours.reset}"
137+ printfn $" %s {indent}%s {AnsiColours.red} %s {failedChar} failed: %s {name} - %s {ex.Message}%s {AnsiColours.reset}"
125138 | Pending (_, name) ->
126- printfn $" %s {indent}%s {AnsiColours.grey} ❔ pending: %s {name}%s {AnsiColours.reset}"
139+ printfn $" %s {indent}%s {AnsiColours.grey} %s {pendingChar} pending: %s {name}%s {AnsiColours.reset}"
127140
128141 member _.EndSuite ( _ , _ ) = ()
129142 member _.Debug ( message : string , path : Path ): unit =
169182 }
170183
171184type TestSuiteRunner = Describe -> TestContext -> unit
172- type private LogLevel = Debug of string | Info of string
173185
174186let private runTestCase path ( testCase : It ) beforeHooks afterHooks =
175187 // setup logging functions
@@ -207,8 +219,14 @@ let rec private doRunTestSuite (suite: Describe) (context: TestContext): TestRes
207219 let afterHooks = context.ParentAfterHooks |> List.append ( List.rev afterHooks)
208220
209221 let testResults = [
210- for testCase in suite.TestCases do
211- runTestCase context.Path testCase beforeHooks afterHooks
222+ for testCase in suite.Steps do
223+ match testCase with
224+ | It itCase ->
225+ runTestCase context.Path itCase beforeHooks afterHooks
226+ | LogStatement logStatement ->
227+ match logStatement with
228+ | Info message -> context.Reporter.Info( message, context.Path)
229+ | Debug message -> context.Reporter.Debug( message, context.Path)
212230 ]
213231
214232 for result, logs in testResults do
@@ -248,3 +266,11 @@ module Constraints =
248266 let shouldNotEqual unexpected actual =
249267 if unexpected = actual then
250268 failwithf " Expected not to be %A but got %A " unexpected actual
269+
270+ let shouldBeTrue condition =
271+ if not condition then
272+ failwith " Expected condition to be true"
273+
274+ let shouldBeFalse condition =
275+ if condition then
276+ failwith " Expected condition to be false"
0 commit comments