Skip to content

Commit

Permalink
Add a basic test to kick off the build script if I forgot to run it b…
Browse files Browse the repository at this point in the history
…efore Invoke-Pester and validate mock params explicitly because parameterfilter failures are hard to debug
  • Loading branch information
ShaunLawrie committed Jan 8, 2024
1 parent ba14cf6 commit 9cc67eb
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
19 changes: 19 additions & 0 deletions PwshSpectreConsole.Tests/@init/Start-SpectreDemo.tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Remove-Module PwshSpectreConsole -Force -ErrorAction SilentlyContinue
try {
Import-Module "$PSScriptRoot\..\..\PwshSpectreConsole\PwshSpectreConsole.psd1" -Force
} catch {
Write-Warning "Failed to import PwshSpectreConsole module, rebuilding..."
& "$PSScriptRoot\..\..\PwshSpectreConsole\build.ps1"
}

Import-Module "$PSScriptRoot\..\..\PwshSpectreConsole\PwshSpectreConsole.psd1" -Force

Describe "Start-SpectreDemo" {
InModuleScope "PwshSpectreConsole" {

It "Should have a demo function available, we're just testing the module was loaded correctly" {
$demo = Get-Command Start-SpectreDemo
$demo.Name | Should -Be "Start-SpectreDemo"
}
}
}
31 changes: 20 additions & 11 deletions PwshSpectreConsole.Tests/formatting/Format-SpectreTable.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,31 @@ Import-Module "$PSScriptRoot\..\TestHelpers.psm1" -Force
Describe "Format-SpectreTable" {
InModuleScope "PwshSpectreConsole" {
BeforeEach {
$data = $null
$border = Get-RandomBoxBorder
$color = Get-RandomColor
$testData = $null
$testBorder = "None" #Get-RandomBoxBorder
$testColor = Get-RandomColor

Mock Write-AnsiConsole -Verifiable -ParameterFilter {
$RenderableObject -is [Spectre.Console.Table] `
-and ($border -eq "None" -or $RenderableObject.Border.GetType().Name -like "*$border*") `
-and $RenderableObject.BorderStyle.Foreground.ToMarkup() -eq $color `
-and $RenderableObject.Rows.Count -eq $data.Count
Mock Write-AnsiConsole {
if($RenderableObject -isnot [Spectre.Console.Table]) {
throw "Found $($RenderableObject.GetType().Name), expected [Spectre.Console.Table]"
}
$borderType = ($testBorder -eq "None") ? "NoTableBorder" : $testBorder
if($RenderableObject.Border.GetType().Name -notlike "*$borderType*") {
throw "Found $($RenderableObject.Border.GetType().Name), expected border like *$borderType*"
}
if($RenderableObject.BorderStyle.Foreground.ToMarkup() -ne $testColor) {
throw "Found $($RenderableObject.BorderStyle.Foreground.ToMarkup()), expected $testColor"
}
if($RenderableObject.Rows.Count -ne $testData.Count) {
throw "Found $($RenderableObject.Rows.Count), expected $($testData.Count)"
}
Write-Debug "Input data was $($RenderableObject.Rows.Count) rows, $($RenderableObject.Columns.Count) columns, border $($RenderableObject.BorderStyle.Foreground.ToMarkup()), borderstyle, $($RenderableObject.BorderStyle.GetType().Name)"
}
# -and $RenderableObject.Columns.Count -eq ($data | Get-DefaultDisplayMembers).Properties.Count
}

It "Should create a table when default display members for a command are required" {
$data = Get-ChildItem "$PSScriptRoot"
Format-SpectreTable -Data $data -Border $border -Color $color
$testData = Get-ChildItem "$PSScriptRoot"
Format-SpectreTable -Data $testData -Border $testBorder -Color $testColor
Assert-MockCalled -CommandName "Write-AnsiConsole" -Times 1 -Exactly
Should -InvokeVerifiable
}
Expand Down

0 comments on commit 9cc67eb

Please sign in to comment.