Skip to content

Commit

Permalink
Extract common helper-functions in P-tests (#2378)
Browse files Browse the repository at this point in the history
* Cleanup module removal in P-tests

* Extract Verify-PathEqual to PTestHelpers

* Extract Verify-Property to PTestHelpers

* Extract Verify-XmlTime to PTestHelpers

* Fix tests
  • Loading branch information
fflaten committed Apr 12, 2024
1 parent 0206448 commit e90392a
Show file tree
Hide file tree
Showing 19 changed files with 136 additions and 236 deletions.
99 changes: 99 additions & 0 deletions tst/PTestHelpers.psm1
Expand Up @@ -20,3 +20,102 @@

& $powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -Command $cmd
}

function Verify-PathEqual {
[CmdletBinding()]
param (
[Parameter(ValueFromPipeline = $true)]
$Actual,
[Parameter(Mandatory = $true, Position = 0)]
$Expected
)

if ([string]::IsNullOrEmpty($Expected)) {
throw 'Expected is null or empty.'
}

if ([string]::IsNullOrEmpty($Actual)) {
throw 'Actual is null or empty.'
}

$e = ($expected -replace '\\', '/').Trim('/')
$a = ($actual -replace '\\', '/').Trim('/')

if ($e -ne $a) {
throw "Expected path '$e' to be equal to '$a'."
}
}

function Verify-Property {
param (
[Parameter(ValueFromPipeline = $true)]
$Actual,
[Parameter(Mandatory = $true, Position = 0)]
[String] $PropertyName,
[Parameter(Position = 1)]
$Value
)

if ($null -eq $PropertyName) {
throw 'PropertyName value is $null.'
}

if ($null -eq $Actual) {
throw 'Actual value is $null.'
}

if (-not $Actual.PSObject.Properties.Item($PropertyName)) {
throw "Expected object to have property $PropertyName!"
}

if ($null -ne $Value -and $Value -ne $Actual.$PropertyName) {
throw "Expected property $PropertyName to have value '$Value', but it was '$($Actual.$PropertyName)'!"
}
}

function Verify-XmlTime {
param (
[Parameter(ValueFromPipeline = $true)]
$Actual,
[Parameter(Mandatory = $true, Position = 0)]
[AllowNull()]
[Nullable[TimeSpan]]
$Expected,
[switch]$AsJUnitFormat
)

if ($null -eq $Expected) {
throw [Exception]'Expected value is $null.'
}

if ($null -eq $Actual) {
throw [Exception]'Actual value is $null.'
}

if ('0.0000' -eq $Actual) {
# it is unlikely that anything takes less than
# 0.0001 seconds (one tenth of a millisecond) so
# throw when we see 0, because that probably means
# we are not measuring at all
throw [Exception]'Actual value is zero.'
}

# Consider using standardized time-format for JUnit and NUnit
if ($AsJUnitFormat) {
# using this over Math.Round because it will output all the numbers for 0.1
$e = $Expected.TotalSeconds.ToString('0.000', [CultureInfo]::InvariantCulture)
}
else {
$e = [string][Math]::Round($Expected.TotalSeconds, 4)
}

if ($e -ne $Actual) {
$message = "Expected and actual values differ!`n" +
"Expected: '$e' seconds (raw '$($Expected.TotalSeconds)' seconds)`n" +
"Actual : '$Actual' seconds"

throw [Exception]$message
}

$Actual
}
2 changes: 1 addition & 1 deletion tst/Pester.RSpec.BackCompat.ts.ps1
@@ -1,6 +1,6 @@
param ([switch] $PassThru, [switch] $NoBuild)

Get-Module Pester.Runtime, Pester.Utility, P, Pester, Axiom, Stack | Remove-Module
Get-Module P, PTestHelpers, Pester, Axiom | Remove-Module

Import-Module $PSScriptRoot\p.psm1 -DisableNameChecking
Import-Module $PSScriptRoot\axiom\Axiom.psm1 -DisableNameChecking
Expand Down
30 changes: 3 additions & 27 deletions tst/Pester.RSpec.Configuration.ts.ps1
@@ -1,9 +1,10 @@
param ([switch] $PassThru, [switch] $NoBuild)

Get-Module Pester.Runtime, Pester.Utility, P, Pester, Axiom, Stack | Remove-Module
Get-Module P, PTestHelpers, Pester, Axiom | Remove-Module

Import-Module $PSScriptRoot\p.psm1 -DisableNameChecking
Import-Module $PSScriptRoot\axiom\Axiom.psm1 -DisableNameChecking
Import-Module $PSScriptRoot\PTestHelpers.psm1 -DisableNameChecking

if (-not $NoBuild) { & "$PSScriptRoot\..\build.ps1" }
Import-Module $PSScriptRoot\..\bin\Pester.psd1
Expand All @@ -12,36 +13,11 @@ $global:PesterPreference = @{
Debug = @{
ShowFullErrors = $true
WriteDebugMessages = $false
WriteDebugMessagesFrom = "Mock"
WriteDebugMessagesFrom = 'Mock'
ReturnRawResultObject = $true
}
}

function Verify-PathEqual {
[CmdletBinding()]
param (
[Parameter(ValueFromPipeline = $true)]
$Actual,
[Parameter(Mandatory = $true, Position = 0)]
$Expected
)

if ([string]::IsNullOrEmpty($Expected)) {
throw "Expected is null or empty."
}

if ([string]::IsNullOrEmpty($Actual)) {
throw "Actual is null or empty."
}

$e = ($expected -replace "\\", '/').Trim('/')
$a = ($actual -replace "\\", '/').Trim('/')

if ($e -ne $a) {
throw "Expected path '$e' to be equal to '$a'."
}
}

i -PassThru:$PassThru {
b "Default configuration" {

Expand Down
2 changes: 1 addition & 1 deletion tst/Pester.RSpec.Coverage.ts.ps1
@@ -1,6 +1,6 @@
param ([switch] $PassThru, [switch] $NoBuild)

Get-Module Pester.Runtime, Pester.Utility, P, Pester, Axiom, Stack | Remove-Module
Get-Module P, PTestHelpers, Pester, Axiom | Remove-Module

Import-Module $PSScriptRoot\p.psm1 -DisableNameChecking
Import-Module $PSScriptRoot\axiom\Axiom.psm1 -DisableNameChecking
Expand Down
2 changes: 1 addition & 1 deletion tst/Pester.RSpec.Demo.ts.ps1
@@ -1,6 +1,6 @@
param ([switch] $PassThru, [switch] $NoBuild)

Get-Module Pester.Runtime, Pester.Utility, P, Pester, Axiom, Stack | Remove-Module
Get-Module P, PTestHelpers, Pester, Axiom | Remove-Module

Import-Module $PSScriptRoot\p.psm1 -DisableNameChecking
Import-Module $PSScriptRoot\axiom\Axiom.psm1 -DisableNameChecking
Expand Down
32 changes: 2 additions & 30 deletions tst/Pester.RSpec.Pester4ResultObject.ts.ps1
Expand Up @@ -2,15 +2,15 @@
# excluding this, as it produces errors because errors are processed differently between v4 and v5, but it is still useful to have around to confirm the overall shape of the result object is correct
return (i -PassThru:$PassThru { })

Get-Module Pester.Runtime, Pester.Utility, P, Pester, Axiom, Stack | Remove-Module
Get-Module P, PTestHelpers, Pester, Axiom | Remove-Module

Import-Module $PSScriptRoot\p.psm1 -DisableNameChecking
Import-Module $PSScriptRoot\axiom\Axiom.psm1 -DisableNameChecking
Import-Module $PSScriptRoot\PTestHelpers.psm1 -DisableNameChecking

if (-not $NoBuild) { & "$PSScriptRoot\..\build.ps1" }
Import-Module $PSScriptRoot\..\bin\Pester.psd1


function Invoke-Pester4 ($Arguments) {
$sb = {
param ($Arguments)
Expand All @@ -22,34 +22,6 @@ function Invoke-Pester4 ($Arguments) {
Start-Job -ScriptBlock $sb -ArgumentList $Arguments | Wait-Job | Receive-Job
}

function Verify-Property {
param (
[Parameter(ValueFromPipeline = $true)]
$Actual,
[Parameter(Mandatory = $true, Position = 0)]
[String] $PropertyName,
[Parameter(Position = 1)]
$Value
)

if ($null -eq $PropertyName) {
throw 'PropertyName value is $null.'
}

if ($null -eq $Actual) {
throw 'Actual value is $null.'
}

if (-not $Actual.PSObject.Properties.Item($PropertyName)) {
throw "Expected object to have property $PropertyName!"
}

if ($null -ne $Value -and $Value -ne $Actual.$PropertyName) {
throw "Expected property $PropertyName to have value '$Value', but it was '$($Actual.$PropertyName)'!"
}
}


i -PassThru:$PassThru {

b "ConvertTo-PesterLegacyResult" {
Expand Down
33 changes: 3 additions & 30 deletions tst/Pester.RSpec.ResultObject.ts.ps1
@@ -1,49 +1,22 @@
param ([switch] $PassThru)

Get-Module Pester.Runtime, Pester.Utility, P, Pester, Axiom, Stack | Remove-Module
Get-Module P, PTestHelpers, Pester, Axiom | Remove-Module

Import-Module $PSScriptRoot\p.psm1 -DisableNameChecking
Import-Module $PSScriptRoot\axiom\Axiom.psm1 -DisableNameChecking
Import-Module $PSScriptRoot\PTestHelpers.psm1 -DisableNameChecking

Import-Module $PSScriptRoot/../bin/Pester.psd1


$global:PesterPreference = @{
Debug = @{
ShowFullErrors = $false
WriteDebugMessages = $false
WriteDebugMessagesFrom = "Mock"
WriteDebugMessagesFrom = 'Mock'
ReturnRawResultObject = $true
}
}

function Verify-Property {
param (
[Parameter(ValueFromPipeline = $true)]
$Actual,
[Parameter(Mandatory = $true, Position = 0)]
[String] $PropertyName,
[Parameter(Position = 1)]
$Value
)

if ($null -eq $PropertyName) {
throw 'PropertyName value is $null.'
}

if ($null -eq $Actual) {
throw 'Actual value is $null.'
}

if (-not $Actual.PSObject.Properties.Item($PropertyName)) {
throw "Expected object to have property $PropertyName!"
}

if ($null -ne $Value -and $Value -ne $Actual.$PropertyName) {
throw "Expected property $PropertyName to have value '$Value', but it was '$($Actual.$PropertyName)'!"
}
}

# template
# b "<" {
# t ">" {
Expand Down

0 comments on commit e90392a

Please sign in to comment.