Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaunLawrie committed Jan 3, 2024
1 parent b3f1513 commit a9b04ca
Show file tree
Hide file tree
Showing 18 changed files with 280 additions and 15 deletions.
82 changes: 82 additions & 0 deletions PwshSpectreConsole.Tests/formatting/Format-SpectreJson.tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
Remove-Module PwshSpectreConsole -Force -ErrorAction SilentlyContinue
Import-Module "$PSScriptRoot\..\..\PwshSpectreConsole\PwshSpectreConsole.psd1" -Force
Import-Module "$PSScriptRoot\..\TestHelpers.psm1" -Force

Describe "Format-SpectreJson" {
InModuleScope "PwshSpectreConsole" {

$data = @(
[pscustomobject]@{
Name = "John"
Age = 25
City = "New York"
IsEmployed = $true
Salary = 10
Hobbies = @("Reading", "Swimming")
Address = @{
Street = "123 Main St"
City = "New York"
Deep = @{
Nested = @{
Value = @{
That = @{
Is = @{
Nested = @{
Again = "Hello"
}
}
}
}
}
}
State = "NY"
Zip = "10001"
}
}
)

BeforeEach {
$testBorder = Get-RandomBoxBorder
$testColor = Get-RandomColor
$testTitle = Get-RandomString
$testExpand = Get-RandomBool
$testWidth = Get-Random -Minimum 5 -Maximum 100
$testHeight = Get-Random -Minimum 5 -Maximum 100
$testBorder | Out-Null
$testColor | Out-Null
$testTitle | Out-Null
$testExpand | Out-Null
$testWidth | Out-Null
$testHeight | Out-Null

Mock Get-HostWidth { return 100 }
Mock Get-HostHeight { return 100 }
}

It "tries to render a panel which somewhat implies that the json parsing worked" {
Mock Write-AnsiConsole -Verifiable -ParameterFilter {
$RenderableObject -is [Spectre.Console.Panel] `
-and ($null -eq $testTitle -or $RenderableObject.Header.Text -eq $testTitle) `
-and ($null -eq $testBorder -or $RenderableObject.Border.GetType().Name -like "*$testBorder*") `
-and ($null -eq $testColor -or $RenderableObject.BorderStyle.Foreground.ToMarkup() -eq $testColor) `
-and ($null -eq $testWidth -or $RenderableObject.Width -eq $testWidth) `
-and ($null -eq $testHeight -or $RenderableObject.Height -eq $testHeight) `
-and ($null -eq $testExpand -or $RenderableObject.Expand -eq $testExpand)
}

Format-SpectreJson -Title $testTitle -Border $testBorder -Color $testColor -Height $testHeight -Width $testWidth -Expand:$testExpand -Data $data
Assert-MockCalled -CommandName "Write-AnsiConsole" -Times 1 -Exactly
Should -InvokeVerifiable
}

It "tries to render json when noborder is specified" {
Mock Write-AnsiConsole -Verifiable -ParameterFilter {
$RenderableObject -is [Spectre.Console.Json.JsonText]
}

Format-SpectreJson -NoBorder -Data $data
Assert-MockCalled -CommandName "Write-AnsiConsole" -Times 1 -Exactly
Should -InvokeVerifiable
}
}
}
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 "Invoke-SpectreCommandWithProgress" -Tag "integration" {
InModuleScope "PwshSpectreConsole" {

It "executes the scriptblock for the basic case" {
Invoke-SpectreCommandWithProgress -ScriptBlock {
param (
$Context
)
$task1 = $Context.AddTask("Completing a single stage process")
Start-Sleep -Milliseconds 500
$task1.Increment(100)
return 1
} | Should -Be 1
}

It "executes the scriptblock with background jobs" {
Invoke-SpectreCommandWithProgress -ScriptBlock {
param (
$Context
)
$jobs = @()
$jobs += Add-SpectreJob -Context $Context -JobName "job 1" -Job (Start-Job { Start-Sleep -Seconds 1 })
$jobs += Add-SpectreJob -Context $Context -JobName "job 2" -Job (Start-Job { Start-Sleep -Seconds 1 })
Wait-SpectreJobs -Context $Context -Jobs $jobs
return 1
} | Should -Be 1
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
Remove-Module PwshSpectreConsole -Force -ErrorAction SilentlyContinue
Import-Module "$PSScriptRoot\..\..\PwshSpectreConsole\PwshSpectreConsole.psd1" -Force
Import-Module "$PSScriptRoot\..\TestHelpers.psm1" -Force

Describe "Invoke-SpectreCommandWithStatus" -Tag "integration" {
InModuleScope "PwshSpectreConsole" {

BeforeEach {
$testTitle = Get-RandomString
$testSpinner = Get-RandomSpinner
$testColor = Get-RandomColor
$testTitle | Out-Null
$testSpinner | Out-Null
$testColor | Out-Null
}

It "executes the scriptblock for the basic case" {
Mock Start-AnsiConsoleStatus -Verifiable -ParameterFilter {
$Title -eq $testTitle `
-and $Spinner.GetType().Name -like "*$testSpinner*" `
-and $SpinnerStyle.Foreground.ToMarkup() -eq $testColor `
-and $ScriptBlock -is [scriptblock]
} -MockWith {
& $ScriptBlock
}
Invoke-SpectreCommandWithStatus -Title $testTitle -Spinner $testSpinner -Color $testColor -ScriptBlock {
return 1
} | Should -Be 1
Assert-MockCalled -CommandName "Start-AnsiConsoleStatus" -Times 1 -Exactly
Should -InvokeVerifiable
}

It "executes the scriptblock without mocking" {
Invoke-SpectreCommandWithStatus -Title $testTitle -Spinner $testSpinner -Color $testColor -ScriptBlock {
Start-Sleep -Seconds 1
return 1
} | Should -Be 1
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Import-Module "$PSScriptRoot\..\TestHelpers.psm1" -Force
Describe "Write-SpectreFigletText" {
InModuleScope "PwshSpectreConsole" {
BeforeEach {
$color = Get-RandomColor
$justification = Get-RandomJustify
Mock Write-AnsiConsole -Verifiable -ParameterFilter {
$RenderableObject -is [Spectre.Console.FigletText] `
Expand Down
44 changes: 44 additions & 0 deletions PwshSpectreConsole.Tests/writing/Write-SpectreHost.tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
Remove-Module PwshSpectreConsole -Force -ErrorAction SilentlyContinue
Import-Module "$PSScriptRoot\..\..\PwshSpectreConsole\PwshSpectreConsole.psd1" -Force
Import-Module "$PSScriptRoot\..\TestHelpers.psm1" -Force

Describe "Write-SpectreHost" {
InModuleScope "PwshSpectreConsole" {
BeforeEach {
$testMessage = Get-RandomString
$testMessage | Out-Null
Mock Write-SpectreHostInternalMarkup
Mock Write-SpectreHostInternalMarkupLine
}

It "writes a message" {
Mock Write-SpectreHostInternalMarkupLine -Verifiable -ParameterFilter {
$Message -eq $testMessage
}
Write-SpectreHost -Message $testMessage
Assert-MockCalled -CommandName "Write-SpectreHostInternalMarkupLine" -Times 1 -Exactly
Assert-MockCalled -CommandName "Write-SpectreHostInternalMarkup" -Times 0 -Exactly
Should -InvokeVerifiable
}

It "accepts pipeline input" {
Mock Write-SpectreHostInternalMarkupLine -Verifiable -ParameterFilter {
$Message -eq $testMessage
}
$testMessage | Write-SpectreHost
Assert-MockCalled -CommandName "Write-SpectreHostInternalMarkupLine" -Times 1 -Exactly
Assert-MockCalled -CommandName "Write-SpectreHostInternalMarkup" -Times 0 -Exactly
Should -InvokeVerifiable
}

It "handles nonewline" {
Mock Write-SpectreHostInternalMarkup -Verifiable -ParameterFilter {
$Message -eq $testMessage
}
Write-SpectreHost -Message $testMessage -NoNewline
Assert-MockCalled -CommandName "Write-SpectreHostInternalMarkup" -Times 1 -Exactly
Assert-MockCalled -CommandName "Write-SpectreHostInternalMarkupLine" -Times 0 -Exactly
Should -InvokeVerifiable
}
}
}
23 changes: 23 additions & 0 deletions PwshSpectreConsole.Tests/writing/Write-SpectreRule.tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Remove-Module PwshSpectreConsole -Force -ErrorAction SilentlyContinue
Import-Module "$PSScriptRoot\..\..\PwshSpectreConsole\PwshSpectreConsole.psd1" -Force
Import-Module "$PSScriptRoot\..\TestHelpers.psm1" -Force

Describe "Write-SpectreRule" {
InModuleScope "PwshSpectreConsole" {
BeforeEach {
$color = Get-RandomColor
$color | Out-Null
$justification = Get-RandomJustify
Mock Write-AnsiConsole -Verifiable -ParameterFilter {
$RenderableObject -is [Spectre.Console.Rule] `
-and $RenderableObject.Justification -eq $justification
}
}

It "writes a rule" {
Write-SpectreRule -Title (Get-RandomString) -Alignment $justification -Color $color
Assert-MockCalled -CommandName "Write-AnsiConsole" -Times 1 -Exactly
Should -InvokeVerifiable
}
}
}
4 changes: 4 additions & 0 deletions PwshSpectreConsole/private/Get-HostHeight.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Required for unit test mocking
function Get-HostHeight {
return $Host.UI.RawUI.WindowSize.Height
}
16 changes: 16 additions & 0 deletions PwshSpectreConsole/private/Write-SpectreHostInternal.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Functions required for unit testing write-spectrehost
function Write-SpectreHostInternalMarkup {
param (
[Parameter(Mandatory)]
[string] $Message
)
[Spectre.Console.AnsiConsole]::Markup($Message)
}

function Write-SpectreHostInternalMarkupLine {
param (
[Parameter(Mandatory)]
[string] $Message
)
[Spectre.Console.AnsiConsole]::MarkupLine($Message)
}
14 changes: 12 additions & 2 deletions PwshSpectreConsole/public/formatting/Format-SpectreJson.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ function Format-SpectreJson {
.PARAMETER Data
The array of objects to be formatted into Json.
.PARAMETER NoBorder
If specified, the Json will not be surrounded by a border.
.PARAMETER Border
The border style of the Json. Default is "Rounded".
Expand Down Expand Up @@ -65,14 +68,15 @@ function Format-SpectreJson {
[object] $Data,
[int] $Depth,
[string] $Title,
[switch] $NoBorder,
[ValidateSet([SpectreConsoleBoxBorder], ErrorMessage = "Value '{0}' is invalid. Try one of: {1}")]
[string] $Border = "Rounded",
[ValidateSpectreColor()]
[ArgumentCompletionsSpectreColors()]
[string] $Color = $script:AccentColor.ToMarkup(),
[ValidateScript({ $_ -gt 0 -and $_ -le [console]::BufferWidth }, ErrorMessage = "Value '{0}' is invalid. Cannot be negative or exceed console width.")]
[ValidateScript({ $_ -gt 0 -and $_ -le (Get-HostWidth) }, ErrorMessage = "Value '{0}' is invalid. Cannot be negative or exceed console width.")]
[int] $Width,
[ValidateScript({ $_ -gt 0 -and $_ -le [console]::WindowHeight }, ErrorMessage = "Value '{0}' is invalid. Cannot be negative or exceed console height.")]
[ValidateScript({ $_ -gt 0 -and $_ -le (Get-HostHeight) }, ErrorMessage = "Value '{0}' is invalid. Cannot be negative or exceed console height.")]
[int] $Height,
[switch] $Expand
)
Expand All @@ -98,6 +102,12 @@ function Format-SpectreJson {
$json.NumberStyle = [Spectre.Console.Style]::new([Spectre.Console.Color]::Cyan2)
$json.BooleanStyle = [Spectre.Console.Style]::new([Spectre.Console.Color]::Teal)
$json.NullStyle = [Spectre.Console.Style]::new([Spectre.Console.Color]::Plum1)

if($NoBorder) {
Write-AnsiConsole $json
return
}

$panel = [Spectre.Console.Panel]::new($json)
$panel.Border = [Spectre.Console.BoxBorder]::$Border
$panel.BorderStyle = [Spectre.Console.Style]::new(($Color | Convert-ToSpectreColor))
Expand Down
6 changes: 3 additions & 3 deletions PwshSpectreConsole/public/formatting/Format-SpectrePanel.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@ function Format-SpectrePanel {
[Reflection.AssemblyMetadata("title", "Format-SpectrePanel")]
param (
[Parameter(ValueFromPipeline, Mandatory)]
[string] $Data,
[object] $Data,
[string] $Title,
[ValidateSet([SpectreConsoleBoxBorder], ErrorMessage = "Value '{0}' is invalid. Try one of: {1}")]
[string] $Border = "Rounded",
[switch] $Expand,
[ValidateSpectreColor()]
[ArgumentCompletionsSpectreColors()]
[string] $Color = $script:AccentColor.ToMarkup(),
[ValidateScript({ $_ -gt 0 -and $_ -le [console]::BufferWidth }, ErrorMessage = "Value '{0}' is invalid. Cannot be negative or exceed console width.")]
[ValidateScript({ $_ -gt 0 -and $_ -le (Get-HostWidth) }, ErrorMessage = "Value '{0}' is invalid. Cannot be negative or exceed console width.")]
[int]$Width,
[ValidateScript({ $_ -gt 0 -and $_ -le [console]::WindowHeight }, ErrorMessage = "Value '{0}' is invalid. Cannot be negative or exceed console height.")]
[ValidateScript({ $_ -gt 0 -and $_ -le (Get-HostHeight) }, ErrorMessage = "Value '{0}' is invalid. Cannot be negative or exceed console height.")]
[int]$Height
)
$panel = [Spectre.Console.Panel]::new($Data)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function Format-SpectreTable {
[ValidateSpectreColor()]
[ArgumentCompletionsSpectreColors()]
[string] $Color = $script:AccentColor.ToMarkup(),
[ValidateScript({ $_ -gt 0 -and $_ -le [console]::BufferWidth }, ErrorMessage = "Value '{0}' is invalid. Cannot be negative or exceed console width.")]
[ValidateScript({ $_ -gt 0 -and $_ -le (Get-HostWidth) }, ErrorMessage = "Value '{0}' is invalid. Cannot be negative or exceed console width.")]
[int]$Width,
[switch]$HideHeaders,
[String]$Title,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function Format-SpectreTree {
The hashtable to format as a tree.
.PARAMETER Border
The type of border to use for the tree. Valid values are 'Rounded', 'Heavy', 'Light', 'Double', 'Solid', 'Ascii', and 'None'. Default is 'Rounded'.
The type of border to use for the tree.
.PARAMETER Color
The color to use for the tree. This can be a Spectre Console color name or a hex color code. Default is the accent color defined in the script.
Expand Down
4 changes: 2 additions & 2 deletions PwshSpectreConsole/public/progress/Add-SpectreJob.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function Add-SpectreJob {
This function adds a Spectre job to the list of jobs you want to wait for with Wait-SpectreJobs.
.PARAMETER Context
The Spectre context to add the job to. The context object is only available inside Wait-SpectreJobs.
The Spectre context to add the job to. The context object is only available inside Invoke-SpectreCommandWithProgress.
[https://spectreconsole.net/api/spectre.console/progresscontext/](https://spectreconsole.net/api/spectre.console/progresscontext/)
.PARAMETER JobName
Expand All @@ -34,7 +34,7 @@ function Add-SpectreJob {
[Reflection.AssemblyMetadata("title", "Add-SpectreJob")]
param (
[Parameter(Mandatory)]
[object] $Context,
[Spectre.Console.ProgressContext] $Context,
[Parameter(Mandatory)]
[string] $JobName,
[Parameter(Mandatory)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ function Invoke-SpectreCommandWithProgress {
$task1.Increment(25)
Start-Sleep -Seconds 1
}
.EXAMPLE
# This example will display a progress bar while multiple background jobs are running.
Invoke-SpectreCommandWithProgress -ScriptBlock {
param (
$Context
)
$jobs = @()
$jobs += Add-SpectreJob -Context $Context -JobName "job 1" -Job (Start-Job { Start-Sleep -Seconds 5 })
$jobs += Add-SpectreJob -Context $Context -JobName "job 2" -Job (Start-Job { Start-Sleep -Seconds 10 })
Wait-SpectreJobs -Context $Context -Jobs $jobs
}
#>
[Reflection.AssemblyMetadata("title", "Invoke-SpectreCommandWithProgress")]
param (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function Invoke-SpectreCommandWithStatus {
The script block to invoke.
.PARAMETER Spinner
The type of spinner to display. Valid values are "dots", "dots2", "dots3", "dots4", "dots5", "dots6", "dots7", "dots8", "dots9", "dots10", "dots11", "dots12", "line", "line2", "pipe", "simpleDots", "simpleDotsScrolling", "star", "star2", "flip", "hamburger", "growVertical", "growHorizontal", "balloon", "balloon2", "noise", "bounce", "boxBounce", "boxBounce2", "triangle", "arc", "circle", "squareCorners", "circleQuarters", "circleHalves", "squish", "toggle", "toggle2", "toggle3", "toggle4", "toggle5", "toggle6", "toggle7", "toggle8", "toggle9", "toggle10", "toggle11", "toggle12", "toggle13", "arrow", "arrow2", "arrow3", "bouncingBar", "bouncingBall", "smiley", "monkey", "hearts", "clock", "earth", "moon", "runner", "pong", "shark", "dqpb", "weather", "christmas", "grenade", "point", "layer", "betaWave", "pulse", "noise2", "gradient", "christmasTree", "santa", "box", "simpleDotsDown", "ballotBox", "checkbox", "radioButton", "spinner", "lineSpinner", "lineSpinner2", "pipeSpinner", "simpleDotsSpinner", "ballSpinner", "balloonSpinner", "noiseSpinner", "bouncingBarSpinner", "smileySpinner", "monkeySpinner", "heartsSpinner", "clockSpinner", "earthSpinner", "moonSpinner", "auto", "random".
The type of spinner to display.
.PARAMETER Title
The title to display above the spinner.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function Write-SpectreFigletText {
The text to display in the Figlet format.
.PARAMETER Alignment
The alignment of the text. Valid values are "Left", "Right", and "Center". The default value is "Left".
The alignment of the text. The default value is "Left".
.PARAMETER Color
The color of the text. The default value is the accent color of the script.
Expand Down
Loading

0 comments on commit a9b04ca

Please sign in to comment.