Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaunLawrie committed Aug 30, 2024
1 parent c91dd83 commit 8f3e8ea
Show file tree
Hide file tree
Showing 22 changed files with 529 additions and 5 deletions.
4 changes: 1 addition & 3 deletions PwshSpectreConsole.Docs/src/powershell/UpdateDocs.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,7 @@ foreach ($doc in $docs) {

# Work out the tag to apply to the current help file
$tag = $null
if ($content -like "*This is experimental*") {
$tag = "Experimental"
} elseif([string]::IsNullOrEmpty($created) -or ((Get-Date) - ([datetime]$created)).TotalDays -lt $recentThresholdDays) {
if([string]::IsNullOrEmpty($created) -or ((Get-Date) - ([datetime]$created)).TotalDays -lt $recentThresholdDays) {
$tag = "New"
} elseif ((((Get-Date) - ([datetime]$modified)).TotalDays -lt $recentThresholdDays) -and $ignoreUpdatesFor -notcontains $commandName) {
$tag = "Updated"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
╭──────────────────────────────────────────────────────────────────────────────╮
│ │
│ │
│ testing panel │
│ │
│ │
╰──────────────────────────────────────────────────────────────────────────────╯
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
╭──────╮ ╭────────╮ ╭───────╮
│ left │ │ middle │ │ right │
╰──────╯ ╰────────╯ ╰───────╯
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ParameterBindingException: A parameter cannot be found that matches parameter 
name 'BadParam'.
at void CheckActionPreference(FunctionContext funcContext, Exception exception
)
at int Run(InterpretedFrame frame)
at int Run(InterpretedFrame frame)
at int Run(InterpretedFrame frame)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ParameterBindingException: A parameter cannot be found that matches parameter 
name 'BadParam'.
at void CheckActionPreference(FunctionContext funcContext, Exception exception
)
at int Run(InterpretedFrame frame)
at int Run(InterpretedFrame frame)
at int Run(InterpretedFrame frame)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Row 1, Col 1 Row 1, Col 2 Row 1, Col Row 1, Col Row 1, Col Row 1, Col
3 4 5 6
Row 2, Col 1 Row 2, Col 2 Row 2, Col Row 2, Col Row 2, Col Row 2, Col
3 4 5 6
Row 3, Col 1 Row 3, Col 2 Row 3, Col Row 3, Col Row 3, Col Row 3, Col
3 4 5 6
Row 4, Col 1 Row 4, Col 2 Row 4, Col Row 4, Col Row 4, Col Row 4, Col
3 4 5 6
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
╭────────────────────────╮
│ │
│ │
│ │
│ │
│ Item to pad │
│ │
╰────────────────────────╯
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
lorem
ipsum
dolor
sit
amet
consectetur
adipiscing
elit,
sed
do
eiusmod
tempor
incididunt
ut
labore
et
dolore
magna
aliqua.
Ut
enim
ad
minim
veniam,
quis
nostrud
exercitation
ullamco
laboris
nisi
ut
aliquip
ex
ea
commodo
consequat
duis
aute
irure
dolor
in
reprehenderit
in
voluptate
velit
esse
cillum
dolore
eu
fugiat
nulla
pariatur
excepteur
sint
occaecat
cupidatat
non
proident
sunt
in
culpa
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
C:/Windows/System32/cmd.exe
24 changes: 24 additions & 0 deletions PwshSpectreConsole.Tests/@snapshots/New-SpectreLayout.snapshot.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
╭─panel 1 (align bottom right)─────────────────────────────────────────────────╮
│ what │
│ │
│ │
│ │
│ │
│ │
╰──────────────────────────────────────────────────────────────────────────────╯
╭─panel 2──────────────────────────────╮╭─panel 3 (align middle center)────────╮
│ hello row 2 ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ test │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
╰──────────────────────────────────────╯╰──────────────────────────────────────╯
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
Remove-Module PwshSpectreConsole -Force -ErrorAction SilentlyContinue
Import-Module "$PSScriptRoot\..\..\PwshSpectreConsole\PwshSpectreConsole.psd1" -Force
Import-Module "$PSScriptRoot\..\TestHelpers.psm1" -Force

Describe "Format-SpectreAligned" {
InModuleScope "PwshSpectreConsole" {

BeforeEach {
$testConsole = [Spectre.Console.Testing.TestConsole]::new()
$testConsole.EmitAnsiSequences = $true

Mock Write-AnsiConsole {
$RenderableObject | Should -BeOfType [Spectre.Console.Rendering.Renderable]
$testConsole.Write($RenderableObject)
}
}

It "Should align an item" {
$renderable = "testing" | Format-SpectreAligned -HorizontalAlignment Right
$renderable | Should -BeOfType [Spectre.Console.Align]
$renderable | Out-SpectreHost
Assert-MockCalled -CommandName "Write-AnsiConsole" -Times 1 -Exactly
}

It "Should align an item in a panel horizontally and vertically" {
$renderable = "testing panel" | Format-SpectreAligned -HorizontalAlignment Center -VerticalAlignment Middle | Format-SpectrePanel -Height 7 -Expand
$renderable | Should -BeOfType [Spectre.Console.Panel]
$renderable | Out-SpectreHost
Assert-MockCalled -CommandName "Write-AnsiConsole" -Times 1 -Exactly
{ Assert-OutputMatchesSnapshot -SnapshotName "Format-SpectreAligned" -Output $testConsole.Output } | Should -Not -Throw
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
Remove-Module PwshSpectreConsole -Force -ErrorAction SilentlyContinue
Import-Module "$PSScriptRoot\..\..\PwshSpectreConsole\PwshSpectreConsole.psd1" -Force
Import-Module "$PSScriptRoot\..\TestHelpers.psm1" -Force

Describe "Format-SpectreColumns" {
InModuleScope "PwshSpectreConsole" {

BeforeEach {
$testConsole = [Spectre.Console.Testing.TestConsole]::new()
$testConsole.EmitAnsiSequences = $true
Mock Write-AnsiConsole {
$RenderableObject | Should -BeOfType [Spectre.Console.Rendering.Renderable]
$testConsole.Write($RenderableObject)
}
}

It "Should format an array of strings into columns" {
$renderable = Format-SpectreColumns -Data @("lorem", "ipsum", "dolor", "sit", "amet", "consectetur", "adipiscing", "elit,", "sed", "do", "eiusmod",
"tempor", "incididunt", "ut", "labore", "et", "dolore", "magna", "aliqua.", "Ut", "enim", "ad", "minim",
"veniam,", "quis", "nostrud", "exercitation", "ullamco", "laboris", "nisi", "ut", "aliquip", "ex", "ea",
"commodo", "consequat", "duis", "aute", "irure", "dolor", "in", "reprehenderit", "in", "voluptate", "velit",
"esse", "cillum", "dolore", "eu", "fugiat", "nulla", "pariatur", "excepteur", "sint", "occaecat",
"cupidatat", "non", "proident", "sunt", "in", "culpa")
$renderable | Should -BeOfType [Spectre.Console.Columns]
$renderable | Out-SpectreHost
Assert-MockCalled -CommandName "Write-AnsiConsole" -Times 1 -Exactly
}

It "Should expand items to fill the available space" {
$renderable = @("left", "middle", "right") | Foreach-Object { $_ | Format-SpectrePanel } | Format-SpectreColumns -Expand
$renderable | Should -BeOfType [Spectre.Console.Columns]
$renderable | Out-SpectreHost
Assert-MockCalled -CommandName "Write-AnsiConsole" -Times 1 -Exactly
{ Assert-OutputMatchesSnapshot -SnapshotName "Format-SpectreColumns.Expanded" -Output $testConsole.Output } | Should -Not -Throw
}

It "Should allow padding to be set" {
$renderable = @("left", "middle", "right") | Foreach-Object { $_ | Format-SpectrePanel } | Format-SpectreColumns -Padding 4
$renderable | Should -BeOfType [Spectre.Console.Columns]
$renderable | Out-SpectreHost
$renderable.Padding.Top | Should -Be 4
$renderable.Padding.Right | Should -Be 4
$renderable.Padding.Bottom | Should -Be 4
$renderable.Padding.Left | Should -Be 4
Assert-MockCalled -CommandName "Write-AnsiConsole" -Times 1 -Exactly
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
Remove-Module PwshSpectreConsole -Force -ErrorAction SilentlyContinue
Import-Module "$PSScriptRoot\..\..\PwshSpectreConsole\PwshSpectreConsole.psd1" -Force
Import-Module "$PSScriptRoot\..\TestHelpers.psm1" -Force

Describe "Format-SpectreException" {
InModuleScope "PwshSpectreConsole" {

BeforeEach {
$testConsole = [Spectre.Console.Testing.TestConsole]::new()
$testConsole.EmitAnsiSequences = $true
Mock Write-AnsiConsole {
$RenderableObject | Should -BeOfType [Spectre.Console.Rendering.Renderable]
$testConsole.Write($RenderableObject)
}
}

It "Should format an error record" {
try {
Get-ChildItem -BadParam -ErrorAction Stop
} catch {
$_ | Should -BeOfType [System.Management.Automation.ErrorRecord]
$renderable = $_ | Format-SpectreException -ExceptionFormat ShortenEverything
}
$renderable | Should -BeOfType [Spectre.Console.Rows]
$renderable | Out-SpectreHost
Assert-MockCalled -CommandName "Write-AnsiConsole" -Times 1 -Exactly
{ Assert-OutputMatchesSnapshot -SnapshotName "Format-SpectreException" -Output $testConsole.Output } | Should -Not -Throw
}

It "Should format an exception" {
$testException = [System.Exception]::new("Test exception")
$renderable = Format-SpectreException -Exception $testException -ExceptionFormat ShortenEverything
$renderable | Should -BeOfType [Spectre.Console.Rows]
$renderable | Out-SpectreHost
Assert-MockCalled -CommandName "Write-AnsiConsole" -Times 1 -Exactly
}

It "Should format an exception with custom styles" {
try {
Get-ChildItem -BadParam -ErrorAction Stop
} catch {
$_ | Should -BeOfType [System.Management.Automation.ErrorRecord]
$renderable = Format-SpectreException -Exception $_ -ExceptionFormat ShortenEverything -ExceptionStyle @{
Message = "Red"
Exception = "White"
Method = [Spectre.Console.Color]::Pink3
ParameterType = "Grey69"
ParameterName = "Silver"
Parenthesis = "#ff0000"
Path = [Spectre.Console.Color]::Pink3
LineNumber = "Blue"
Dimmed = "Grey"
NonEmphasized = "Red"
}
}

$renderable | Should -BeOfType [Spectre.Console.Rows]
$renderable | Out-SpectreHost
Assert-MockCalled -CommandName "Write-AnsiConsole" -Times 1 -Exactly
{ Assert-OutputMatchesSnapshot -SnapshotName "Format-SpectreException.CustomStyles" -Output $testConsole.Output } | Should -Not -Throw
}
}
}
37 changes: 37 additions & 0 deletions PwshSpectreConsole.Tests/formatting/Format-SpectreGrid.tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
Remove-Module PwshSpectreConsole -Force -ErrorAction SilentlyContinue
Import-Module "$PSScriptRoot\..\..\PwshSpectreConsole\PwshSpectreConsole.psd1" -Force
Import-Module "$PSScriptRoot\..\TestHelpers.psm1" -Force

Describe "Format-SpectreGrid" {
InModuleScope "PwshSpectreConsole" {

BeforeEach {
$testConsole = [Spectre.Console.Testing.TestConsole]::new()
$testConsole.EmitAnsiSequences = $true
Mock Write-AnsiConsole {
$RenderableObject | Should -BeOfType [Spectre.Console.Rendering.Renderable]
$testConsole.Write($RenderableObject)
}
}

It "Should format data in a grid" {
$rows = 4
$cols = 6

$gridRows = @()
for ($row = 1; $row -le $rows; $row++) {
$columns = @()
for ($col = 1; $col -le $cols; $col++) {
$columns += "Row $row, Col $col"
}
$gridRows += New-SpectreGridRow $columns
}

$renderable = $gridRows | Format-SpectreGrid
$renderable | Should -BeOfType [Spectre.Console.Grid]
$renderable | Out-SpectreHost
Assert-MockCalled -CommandName "Write-AnsiConsole" -Times 1 -Exactly
{ Assert-OutputMatchesSnapshot -SnapshotName "Format-SpectreGrid" -Output $testConsole.Output } | Should -Not -Throw
}
}
}
Loading

0 comments on commit 8f3e8ea

Please sign in to comment.