-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b3f1513
commit a9b04ca
Showing
18 changed files
with
280 additions
and
15 deletions.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
PwshSpectreConsole.Tests/formatting/Format-SpectreJson.tests.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
PwshSpectreConsole.Tests/progress/Invoke-SpectreCommandWithProgress.tests.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
PwshSpectreConsole.Tests/progress/Invoke-SpectreCommandWithStatus.tests.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
PwshSpectreConsole.Tests/writing/Write-SpectreHost.tests.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
PwshSpectreConsole.Tests/writing/Write-SpectreRule.tests.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.