Skip to content

Commit

Permalink
After each (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
NonlinearFruit committed Apr 18, 2024
1 parent d26a3ae commit 8c10119
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 5 deletions.
11 changes: 8 additions & 3 deletions nuunit.nu
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,23 @@ export def discover-tests [module testImportScript] {
{
id: ($it.index + 1)
name: $it.item
exec: $'($testImportScript)
exec: $"($testImportScript)
try {
if (scope commands | where name == "before each" | is-empty) {
if \(scope commands | where name == 'before each' | is-empty) {
null
} else {
before each
}
| ($it.item)
| if \(scope commands | where name == 'after each' | is-empty) {
null
} else {
after each
}
} catch {|err|
print -e $err.debug
exit 1
}'
}"
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions test-spec.nu
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ export def "test that the before each passes context to a test" [] {
| verify json results
}

export def "test that the after each passes context to a test" [] {
use tests/test-spec-with-after-each.nu "verify json results"
let specFile = "tests/test-spec-with-after-each.nu"

run-test-spec $specFile
| verify json results
}

def run-test-spec [specFile] {
(^$nu.current-exe --no-config-file nuunit.nu --test-spec-module-name $specFile --as-json)
| from json
Expand Down
29 changes: 29 additions & 0 deletions tests/test-spec-with-after-each.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

export def "test that after each gets context from test" [] {
let context = { some: stuff, that: is, useful: for, multiple: tests }
$context
}

export def "test that after each context gets passed to multiple tests" [] {
let context = { some: stuff, that: is, useful: for, multiple: tests }
$context
}

export def "after each" [] {
let actual = $in
let expected = { some: stuff, that: is, useful: for, multiple: tests }
use std assert

print "after each happened"

assert equal $expected $actual
}

export def "verify json results" [] {
let results = $in
use std assert

assert ($results | get exit_code | all {|it| $it == 0})
assert ($results | get stdout | all {|it| $it =~ "after each happened"})
}

3 changes: 1 addition & 2 deletions tests/test-spec-with-before-each.nu
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export def "verify json results" [] {
let results = $in
use std assert

print ($results | get stdout)
assert equal 0 ($results | get exit_code | first)
assert ($results | get exit_code | all {|it| $it == 0})
}

0 comments on commit 8c10119

Please sign in to comment.