Skip to content

Commit bcd264c

Browse files
committed
feat: allow passing data to subtests
1 parent 657ea09 commit bcd264c

File tree

4 files changed

+48
-6
lines changed

4 files changed

+48
-6
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ tool4d --project YourProject.4DProject --startup-method "test" --user-param "tag
6868

6969
## Table-Driven Tests
7070

71-
Use subtests to build table-driven tests. Each call to `t.run` executes the provided function with a fresh testing context. If a subtest fails, the parent test is marked as failed. Subtests run with the same `This` object as the parent test, so helper methods and state remain accessible.
71+
Use subtests to build table-driven tests. Each call to `t.run` executes the provided function with a fresh testing context. If a subtest fails, the parent test is marked as failed. Subtests run with the same `This` object as the parent test, so helper methods and state remain accessible. Pass optional data as the third argument when the test logic lives in a separate method.
7272

7373
```4d
7474
Function test_math($t : cs.Testing)
@@ -77,8 +77,11 @@ Function test_math($t : cs.Testing)
7777
7878
var $case : Object
7979
For each ($case; $cases)
80-
$t.run($case.name; Formula($1.assert.areEqual($1; $case.want; $case.in+1; "math works")))
80+
$t.run($case.name; This._checkMathCase; $case)
8181
End for each
82+
83+
Function _checkMathCase($t : cs.Testing; $case : Object)
84+
$t.assert.areEqual($t; $case.want; $case.in+1; "math works")
8285
```
8386

8487
## Output

docs/guide.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ For each test class, the framework executes lifecycle methods in this order:
755755

756756
## Table-Driven Tests
757757

758-
Use subtests to implement table-driven tests. The `t.run` method executes a named subtest with a fresh testing context. If a subtest fails, the parent test is marked as failed and the subtest's log messages are prefixed with its name. Subtests share the same `This` object as the parent test, so any instance methods or state remain available.
758+
Use subtests to implement table-driven tests. The `t.run` method executes a named subtest with a fresh testing context. If a subtest fails, the parent test is marked as failed and the subtest's log messages are prefixed with its name. Subtests share the same `This` object as the parent test, so any instance methods or state remain available. Pass optional data as the third argument when using a helper method for the subtest logic.
759759

760760
```4d
761761
Function test_math_operations($t : cs.Testing.Testing)
@@ -764,8 +764,11 @@ Function test_math_operations($t : cs.Testing.Testing)
764764
765765
var $case : Object
766766
For each ($case; $cases)
767-
$t.run($case.name; Formula($1.assert.areEqual($1; $case.want; $case.in+1; "math works")))
767+
$t.run($case.name; This._checkMathCase; $case)
768768
End for each
769+
770+
Function _checkMathCase($t : cs.Testing.Testing; $case : Object)
771+
$t.assert.areEqual($t; $case.want; $case.in+1; "math works")
769772
```
770773

771774
## Transaction Management

testing/Project/Sources/Classes/Testing.4dm

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Function resetForNewTest()
3737
This:C1470.stats.resetStatistics()
3838
This:C1470.failureCallChain:=Null
3939

40-
Function run($name : Text; $subtest : 4D:C1709.Function) : Boolean
40+
Function run($name : Text; $subtest : 4D:C1709.Function; $data : Variant) : Boolean
4141
// Execute a named subtest with its own Testing context
4242
// Returns true if the subtest passed
4343

@@ -63,7 +63,13 @@ Function run($name : Text; $subtest : 4D:C1709.Function) : Boolean
6363
If ($context=Null:C1517)
6464
$context:=This:C1470
6565
End if
66-
$subtest.apply($context; [$subT])
66+
67+
var $args : Collection
68+
$args:=[$subT]
69+
If (Count parameters>=3)
70+
$args.push($data)
71+
End if
72+
$subtest.apply($context; $args)
6773

6874
// Propagate log messages with subtest name prefix
6975
var $message : Text

testing/Project/Sources/Classes/_TestingTest.4dm

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,33 @@ Function _setContextChild($t : cs:C1710.Testing)
151151
$t.log("running")
152152
This.contextValue:="child"
153153

154+
Function test_run_with_data_argument($t : cs:C1710.Testing)
155+
156+
var $testing : cs:C1710.Testing
157+
$testing:=cs:C1710.Testing.new()
158+
159+
var $cases : Collection
160+
$cases:=[\
161+
New object("name"; "ok"; "in"; 1; "want"; 2);\
162+
New object("name"; "bad"; "in"; 2; "want"; 4)\
163+
]
164+
165+
var $case : Object
166+
For each ($case; $cases)
167+
$testing.run($case.name; This._addOneCase; $case)
168+
End for each
169+
170+
$t.assert.areEqual($t; 2; $testing.logMessages.length; "Should log each case")
171+
$t.assert.areEqual($t; "ok: 2"; $testing.logMessages[0]; "Should prefix log with case name")
172+
$t.assert.areEqual($t; "bad: 3"; $testing.logMessages[1]; "Should prefix log with case name")
173+
$t.assert.isTrue($t; $testing.failed; "Parent should fail if any case fails")
174+
175+
Function _addOneCase($t : cs:C1710.Testing; $case : Object)
176+
177+
var $got : Integer
178+
$got:=$case.in+1
179+
$t.log(String($got))
180+
If ($got#$case.want)
181+
$t.fail()
182+
End if
183+

0 commit comments

Comments
 (0)