Skip to content

Commit

Permalink
Add snapshot tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaunLawrie committed Feb 18, 2024
1 parent 6a180d5 commit 3793ba1
Show file tree
Hide file tree
Showing 28 changed files with 456 additions and 214 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Test 1 █████████████████████ 10 
Test 2 █████████████████████████████████████████████ 20 
Test 3 █████████████████████████████████████████████████████████████████████ 30
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
████████████████████████████████████████████████████████████████████████████████
■ Test 1 10 ■ Test 2 20 ■ Test 3 30
50 changes: 25 additions & 25 deletions PwshSpectreConsole.Tests/@snapshots/Format-SpectreJson.snapshot.txt
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
╔═Test title═════════════════════════════════════════════════════════════════╗
║ {
"Name": "John",
"Age": 25,
"City": "New York",
"IsEmployed": true,
"Salary": 10,
"Hobbies": [
"Reading",
"Swimming"
],
"Address": {
"Street": "123 Main St",
"City": "New York",
"State": "NY",
"Deep": {
"Nested": "System.Collections.Hashtable"
},
"Zip": "10001"
}
║ }
╚════════════════════════════════════════════════════════════════════════════╝
╔═Test title═════════════════════════════════════════════════════════════════╗
║ { ║
║ "Name": "John", ║
║ "Age": 25, ║
║ "City": "New York", ║
║ "IsEmployed": true, ║
║ "Salary": 10, ║
║ "Hobbies": [ ║
║ "Reading", ║
║ "Swimming" ║
║ ], ║
║ "Address": { ║
║ "State": "NY", ║
║ "City": "New York", ║
║ "Street": "123 Main St", ║
║ "Deep": { ║
║ "Nested": "System.Collections.Hashtable" ║
║ }, ║
║ "Zip": "10001" ║
║ } ║
║ } ║
║ ║
║ ║
║ ║
╚════════════════════════════════════════════════════════════════════════════╝
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
╭─Test title───────────╮
This is a test panel
╰──────────────────────╯
╭─Test title───────────╮
│ This is a test panel │
╰──────────────────────╯
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
╭────────────┬────────┬───────╮
│ Color │ Name │ Value │
├────────────┼────────┼───────┤
│ Turquoise2 │ Test 1 │ 10 │
│ #ff0000 │ Test 2 │ 20 │
│ Turquoise2 │ Test 3 │ 30 │
╰────────────┴────────┴───────╯
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Root
┣━━ Child 1
┃ ┗━━ Grandchild 1
┃ ┣━━ Great Grandchild 1
┃ ┣━━ Great Grandchild 2
┃ ┗━━ Great Grandchild 3
┗━━ Child 2
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
July 2024
╭─────┬─────┬─────┬─────┬─────┬─────┬─────╮
│ Mon │ Tue │ Wed │ Thu │ Fri │ Sat │ Sun │
├─────┼─────┼─────┼─────┼─────┼─────┼─────┤
│ 1 │ 2 │ 3 │ 4 │ 5 │ 6 │ 7 │
│ 8 │ 9 │ 10 │ 11 │ 12 │ 13 │ 14 │
│ 15 │ 16 │ 17 │ 18 │ 19 │ 20 │ 21 │
│ 22 │ 23 │ 24 │ 25 │ 26 │ 27 │ 28 │
│ 29 │ 30 │ 31 │ │ │ │ │
│ │ │ │ │ │ │ │
╰─────┴─────┴─────┴─────┴─────┴─────┴─────╯
╭─────────────┬──────┬───────┬─────╮
│ Description │ Year │ Month │ Day │
├─────────────┼──────┼───────┼─────┤
│ Event 1 │ 2022 │ 3 │ 10 │
│ Event 2 │ 2022 │ 3 │ 20 │
╰─────────────┴──────┴───────┴─────╯
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
 __ _ _ _ _ 
 / _| (_) __ _ | | ___ | |_ | |_ 
 | |_ | | / _` | | | / _ \ | __| | __|
 | _| | | | (_| | | | | __/ | |_ | |_ 
 |_| |_| \__, | |_| \___| \__| \__|
 |___/ 
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello, World! 😎 Yay!
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
───────────────────────────────────────────────────────── yo, this is a test rule ──────────────────────────────────────────────────────────
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Test 1 █████████████████████ 10 
Test 2 █████████████████████████████████████████████ 20 
Test 3 █████████████████████████████████████████████████████████████████████ 30
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Describe "Format-SpectreBarChart" {

BeforeEach {
$testConsole = [Spectre.Console.Testing.TestConsole]::new()
$testConsole.EmitAnsiSequences = $true
$testWidth = Get-Random -Minimum 10 -Maximum 100
$testTitle = "Test Chart $([guid]::NewGuid())"
$testData = @()
Expand Down Expand Up @@ -69,5 +70,24 @@ Describe "Format-SpectreBarChart" {
Format-SpectreBarChart -Data $testData
Assert-MockCalled -CommandName "Write-AnsiConsole" -Times 1 -Exactly
}

It "Should match the snapshot" {
Mock Write-AnsiConsole {
$testConsole.Write($RenderableObject)
}
$testWidth = 120
Write-Debug "Setting test width to $testWidth"
$testData = @(
(New-SpectreChartItem -Label "Test 1" -Value 10 -Color "Turquoise2"),
(New-SpectreChartItem -Label "Test 2" -Value 20 -Color "#ff0000"),
(New-SpectreChartItem -Label "Test 3" -Value 30 -Color "Turquoise2")
)
Format-SpectreBarChart -Data $testData
$snapShotComparison = "$PSScriptRoot\..\@snapshots\Format-SpectreBarChart.snapshot.compare.txt"
Set-Content -Path $snapShotComparison -Value ($testConsole.Output -replace "`r", "") -NoNewline
$compare = Get-Content -Path $snapShotComparison -AsByteStream
$snapshot = Get-Content -Path "$PSScriptRoot\..\@snapshots\Format-SpectreBarChart.snapshot.txt" -AsByteStream
$snapshot | Should -Be $compare
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Describe "Format-SpectreBreakdownChart" {
InModuleScope "PwshSpectreConsole" {
BeforeEach {
$testConsole = [Spectre.Console.Testing.TestConsole]::new()
$testConsole.EmitAnsiSequences = $true
$testWidth = Get-Random -Minimum 10 -Maximum 100
$testData = @()
for($i = 0; $i -lt (Get-Random -Minimum 3 -Maximum 10); $i++) {
Expand Down Expand Up @@ -53,5 +54,31 @@ Describe "Format-SpectreBreakdownChart" {
Format-SpectreBreakdownChart -Data $testData
Assert-MockCalled -CommandName "Write-AnsiConsole" -Times 1 -Exactly
}

It "Should match the snapshot" {
Mock Write-AnsiConsole {
$testConsole.Write($RenderableObject)
}
$testWidth = 120
Write-Debug "Setting test width to $testWidth"
$testData = @(
(New-SpectreChartItem -Label "Test 1" -Value 10 -Color "Turquoise2"),
(New-SpectreChartItem -Label "Test 2" -Value 20 -Color "Turquoise2"),
(New-SpectreChartItem -Label "Test 3" -Value 30 -Color "Turquoise2")
)
Format-SpectreBreakdownChart -Data $testData
$snapShotComparison = "$PSScriptRoot\..\@snapshots\Format-SpectreBreakdownChart.snapshot.compare.txt"
Set-Content -Path $snapShotComparison -Value ($testConsole.Output -replace "`r", "") -NoNewline
$compare = Get-Content -Path $snapShotComparison -AsByteStream
$snapShot = Get-Content -Path "$PSScriptRoot\..\@snapshots\Format-SpectreBreakdownChart.snapshot.txt" -AsByteStream
try {
$snapshot | Should -Be $compare
} catch {
# byte array to string
Write-Host "Expected:`n`n$([System.Text.Encoding]::UTF8.GetString($snapshot))"
Write-Host "Got:`n`n$([System.Text.Encoding]::UTF8.GetString($compare))"
throw
}
}
}
}
Loading

0 comments on commit 3793ba1

Please sign in to comment.