Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/functions/assert/Time/Should-BeFasterThan.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,11 @@
}
return
}

# Neither branch matched. Both branches above return, so reaching here means we were handed
# something we cannot measure or compare. Without this the assertion would return having done
# nothing and the test would pass, which is the worst way to fail. It also hid a CI flake: a
# test asserting that a 10ms sleep is slower than 1ms failed while the whole test took 3ms,
# because the scriptblock was never run and nothing said so.
$assert.Fail("Expected a [scriptblock] to measure or a [timespan] to compare,<because> but got <actualType> <actual>.", @{ Actual = $Actual; Because = $Because })
}
7 changes: 7 additions & 0 deletions src/functions/assert/Time/Should-BeSlowerThan.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,11 @@
}
return
}

# Neither branch matched. Both branches above return, so reaching here means we were handed
# something we cannot measure or compare. Without this the assertion would return having done
# nothing and the test would pass, which is the worst way to fail. It also hid a CI flake: a
# test asserting that a 10ms sleep is slower than 1ms failed while the whole test took 3ms,
# because the scriptblock was never run and nothing said so.
$assert.Fail("Expected a [scriptblock] to measure or a [timespan] to compare,<because> but got <actualType> <actual>.", @{ Actual = $Actual; Because = $Because })
}
12 changes: 12 additions & 0 deletions tst/functions/assert/Time/Should-BeFasterThan.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ Describe "Should-BeFasterThan" {
$err.Exception.Message | Verify-Like '*because I said so*'
}

It "Throws when actual is neither a scriptblock nor a timespan" -ForEach @(
@{ Actual = 'a string' }
@{ Actual = 42 }
@{ Actual = $null }
) {
# Without this the assertion returned having done nothing and the test passed. That also
# made a CI flake unreadable: a scriptblock that was never run looked like a scriptblock
# that ran impossibly fast.
$err = { $Actual | Should-BeFasterThan -Expected 1ms } | Verify-AssertionFailed
$err.Exception.Message | Verify-Like '*Expected a `[scriptblock`] to measure or a `[timespan`] to compare*'
}

It "Requires Expected" {
# Don't invoke with Expected missing to test this: a missing mandatory parameter makes
# PowerShell prompt for it, which hangs an interactive test.ps1 run and the release build.
Expand Down
12 changes: 12 additions & 0 deletions tst/functions/assert/Time/Should-BeSlowerThan.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ Describe "Should-BeSlowerThan" {
$err.Exception.Message | Verify-Like '*because I said so*'
}

It "Throws when actual is neither a scriptblock nor a timespan" -ForEach @(
@{ Actual = 'a string' }
@{ Actual = 42 }
@{ Actual = $null }
) {
# Without this the assertion returned having done nothing and the test passed. That also
# made a CI flake unreadable: a scriptblock that was never run looked like a scriptblock
# that ran impossibly fast.
$err = { $Actual | Should-BeSlowerThan -Expected 1ms } | Verify-AssertionFailed
$err.Exception.Message | Verify-Like '*Expected a `[scriptblock`] to measure or a `[timespan`] to compare*'
}

It "Requires Expected" {
# Don't invoke with Expected missing to test this: a missing mandatory parameter makes
# PowerShell prompt for it, which hangs an interactive test.ps1 run and the release build.
Expand Down
Loading