Skip to content

Commit 7b4d570

Browse files
committed
fix: update Sprout.fs and Tests.fsx with minor adjustments
1 parent 5ef2ac9 commit 7b4d570

File tree

2 files changed

+52
-31
lines changed

2 files changed

+52
-31
lines changed

Sprout.fs

Lines changed: 52 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ type CollectedStep =
7878
| CollectedIt of Path * HookFunction list * HookFunction list * It
7979
| CollectedLog of Path * LogLevel
8080

81+
type CollectedDescribe = {
82+
Name: string
83+
Path: Path
84+
Steps: CollectedStep list
85+
Children: CollectedDescribe list
86+
}
87+
8188
type ITestReporter =
8289
abstract Begin : totalCount:int -> unit
8390
abstract BeginSuite : name:string * path:Path -> unit
@@ -240,16 +247,40 @@ type TestContext = {
240247
ParentAfterHooks: HookFunction list
241248
Reporter: ITestReporter
242249
Log: string -> unit
250+
Order: CollectedStep list -> CollectedStep list // <-- Add this line
243251
}
244252
with
245253
static member New = {
246254
ParentBeforeHooks = []
247255
ParentAfterHooks = []
248256
Reporter = Reporters.ConsoleReporter() :> ITestReporter
249257
Log = printfn "%s"
258+
Order = id // Default: preserve order
250259
}
251260

252261
module Runner =
262+
let collectDescribes (describe: Describe) =
263+
let rec loop parentPath parentBefore parentAfter (d: Describe) =
264+
let beforeHooks, afterHooks = d.CollectHooks()
265+
let beforeHooks = List.rev beforeHooks @ parentBefore
266+
let afterHooks = parentAfter @ List.rev afterHooks
267+
let path = parentPath @ [d.Name]
268+
let steps =
269+
d.Steps
270+
|> List.map (function
271+
| ItStep it -> CollectedIt (Path path, beforeHooks, afterHooks, it)
272+
| LogStatementStep log -> CollectedLog (Path path, log))
273+
let children =
274+
d.Children
275+
|> List.map (loop path beforeHooks afterHooks)
276+
{
277+
Name = d.Name
278+
Path = Path path
279+
Steps = steps
280+
Children = children
281+
}
282+
loop [] [] [] describe
283+
253284
let private runTestCase path (testCase: It) beforeHooks afterHooks: Async<TestResult> =
254285
async {
255286
// setup logging functions
@@ -283,33 +314,16 @@ module Runner =
283314
}
284315
}
285316

286-
let collectSteps (describe: Describe) =
287-
let rec loop parentPath parentBefore parentAfter (describe: Describe) =
288-
let beforeHooks, afterHooks = describe.CollectHooks()
289-
let beforeHooks = List.rev beforeHooks @ parentBefore
290-
let afterHooks = parentAfter @ List.rev afterHooks
291-
let path = parentPath @ [describe.Name]
292-
let steps =
293-
describe.Steps
294-
|> List.map (function
295-
| ItStep it -> CollectedIt (Path path, beforeHooks, afterHooks, it)
296-
| LogStatementStep log -> CollectedLog (Path path, log))
297-
let children =
298-
describe.Children
299-
|> List.collect (loop path beforeHooks afterHooks)
300-
steps @ children
301-
loop [] [] [] describe
302-
303-
let runCollectedSteps (context: TestContext) (steps: CollectedStep list) (order: CollectedStep list -> CollectedStep list) =
317+
let rec runCollectedDescribe (context: TestContext) (cd: CollectedDescribe) =
304318
async {
305-
let orderedSteps = order steps
306-
let! results =
307-
orderedSteps
319+
context.Reporter.BeginSuite(cd.Name, cd.Path)
320+
let! stepResults =
321+
cd.Steps
322+
|> context.Order
308323
|> List.map (function
309324
| CollectedIt (path, beforeHooks, afterHooks, it) ->
310325
async {
311326
let! result = runTestCase path it beforeHooks afterHooks
312-
// Report logs and result
313327
for log in result.Logs do
314328
match log with
315329
| Info message -> context.Reporter.Info(message, path)
@@ -325,26 +339,34 @@ module Runner =
325339
return None
326340
})
327341
|> Async.Sequential
328-
let testResults = results |> Array.choose id
329-
context.Reporter.End testResults
330-
return testResults
342+
let! childResults =
343+
cd.Children
344+
|> List.map (runCollectedDescribe context)
345+
|> Async.Sequential
346+
context.Reporter.EndSuite(cd.Name, cd.Path)
347+
348+
let allResults =
349+
Array.concat [
350+
Array.choose id stepResults
351+
Array.concat childResults
352+
]
353+
return allResults
331354
}
332355

333-
let runTestSuiteWithContext (context: TestContext) (suite: Describe) (order: CollectedStep list -> CollectedStep list) =
356+
let runTestSuiteWithContext (context: TestContext) (suite: Describe) =
334357
async {
335358
context.Reporter.Begin suite.TotalCount
336-
let steps = Runner.collectSteps suite
337-
let! testResults = Runner.runCollectedSteps context steps order
359+
let collected = Runner.collectDescribes suite
360+
let! testResults = Runner.runCollectedDescribe context collected
338361
context.Reporter.EndSuite(suite.Name, Path [suite.Name])
339362
context.Reporter.End testResults
340-
return testResults |> Array.filter _.Outcome.IsFailed
363+
return testResults
341364
}
342365

343366
let runTestSuite (describe: Describe) =
344367
runTestSuiteWithContext
345368
TestContext.New
346369
describe
347-
id
348370

349371
[<AutoOpen>]
350372
module Constraints =

Tests.fsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ let asyncSuite = describe "Async Tests" {
8888
runTestSuiteWithContext
8989
{ TestContext.New with Reporter = Reporters.TapReporter() }
9090
suite
91-
id
9291
runTestSuite asyncSuite
9392
]
9493
|> Async.Sequential

0 commit comments

Comments
 (0)