mirror of
https://github.com/aphrody-labs/PowerShell.git
synced 2026-10-17 20:56:02 +00:00
Related #3238 1. Add autoload for test modules 2. Move TestHostCS.psm1 to 'test\tools\Modules\' folder 3. Remove explicit load TestHostCS.psm1 from test files
94 lines
4.4 KiB
PowerShell
94 lines
4.4 KiB
PowerShell
Describe "Write-Host with default Console Host" -Tags "Slow","Feature" {
|
|
|
|
BeforeAll {
|
|
$powershell = Join-Path -Path $PsHome -ChildPath "powershell"
|
|
|
|
$testData = @(
|
|
@{ Name = '-Separator'; Command = "Write-Host a,b,c -Separator '+'"; returnCount = 1; returnValue = @("a+b+c") }
|
|
@{ Name = '-NoNewline=true'; Command = "Write-Host a,b -NoNewline:`$true;Write-Host a,b"; returnCount = 1; returnValue = @("a ba b") }
|
|
@{ Name = '-NoNewline=false'; Command = "Write-Host a1,b1;Write-Host a2,b2"; returnCount = 2; returnValue = @("a1 b1","a2 b2") }
|
|
)
|
|
}
|
|
|
|
It "write-Host works with '<Name>' switch" -TestCases:$testData -Pending:$IsOSX {
|
|
param($Command, $returnCount, $returnValue)
|
|
|
|
[array]$result = & $powershell -noprofile $Command
|
|
|
|
$result.Count | Should Be $returnCount
|
|
foreach ($i in 0..($returnCount - 1))
|
|
{
|
|
$result[$i] | Should Be $returnValue[$i]
|
|
}
|
|
}
|
|
}
|
|
|
|
Describe "Write-Host with wrong colors" -Tags "CI" {
|
|
|
|
BeforeAll {
|
|
$testWrongColor = @(
|
|
@{ ForegroundColor = -1; BackgroundColor = 'Red' }
|
|
@{ ForegroundColor = 16; BackgroundColor = 'Red' }
|
|
@{ ForegroundColor = 'Red'; BackgroundColor = -1 }
|
|
@{ ForegroundColor = 'Red'; BackgroundColor = 16 }
|
|
)
|
|
}
|
|
|
|
It 'Should throw if color is invalid: ForegroundColor = <ForegroundColor>; BackgroundColor = <BackgroundColor>' -TestCases:$testWrongColor {
|
|
param($ForegroundColor, $BackgroundColor)
|
|
try
|
|
{
|
|
Write-Host "No output from Write-Host" -ForegroundColor $ForegroundColor -BackgroundColor $BackgroundColor
|
|
throw "No Exception!"
|
|
}
|
|
catch { $_.FullyQualifiedErrorId | Should Be 'CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.WriteHostCommand' }
|
|
}
|
|
}
|
|
|
|
Describe "Write-Host with TestHostCS" -Tags "CI" {
|
|
|
|
BeforeAll {
|
|
$th = New-TestHost
|
|
$rs = [runspacefactory]::Createrunspace($th)
|
|
$rs.open()
|
|
$ps = [powershell]::Create()
|
|
$ps.Runspace = $rs
|
|
|
|
$testHostCSData = @(
|
|
@{ Name = 'defaults'; Command = "Write-Host a,b,c"; returnCount = 1; returnValue = @("White:Black:a b c:NewLine"); returnInfo = @("a b c") }
|
|
@{ Name = '-Object'; Command = "Write-Host -Object a,b,c"; returnCount = 1; returnValue = @("White:Black:a b c:NewLine"); returnInfo = @("a b c") }
|
|
@{ Name = '-Separator'; Command = "Write-Host a,b,c -Separator '+'"; returnCount = 1; returnValue = @("White:Black:a+b+c:NewLine"); returnInfo = @("a+b+c") }
|
|
@{ Name = '-Separator, colors and -NoNewLine'; Command = "Write-Host a,b,c -Separator ',' -ForegroundColor Yellow -BackgroundColor DarkBlue -NoNewline"; returnCount = 1; returnValue = @("Yellow:DarkBlue:a,b,c:NoNewLine"); returnInfo = @("a,b,c") }
|
|
@{ Name = '-NoNewline:$true and colors'; Command = "Write-Host a,b -NoNewline:`$true -ForegroundColor Red -BackgroundColor Green;Write-Host a,b"; returnCount = 2; returnValue = @("Red:Green:a b:NoNewLine", "White:Black:a b:NewLine"); returnInfo = @("a b", "a b") }
|
|
@{ Name = '-NoNewline:$false and colors'; Command = "Write-Host a,b -NoNewline:`$false -ForegroundColor Red -BackgroundColor Green;Write-Host a,b"; returnCount = 2; returnValue = @("Red:Green:a b:NewLine","White:Black:a b:NewLine"); returnInfo = @("a b", "a b") }
|
|
)
|
|
|
|
}
|
|
|
|
AfterAll {
|
|
$rs.Close()
|
|
$rs.Dispose()
|
|
$ps.Dispose()
|
|
}
|
|
|
|
AfterEach {
|
|
$ps.Commands.Clear()
|
|
$th.ui.Streams.Clear()
|
|
}
|
|
|
|
It "Write-Host works with <Name>" -TestCases:$testHostCSData {
|
|
param($Command, $returnCount, $returnValue, $returnInfo)
|
|
$ps.AddScript($Command).Invoke()
|
|
|
|
$result = $th.ui.Streams.ConsoleOutput
|
|
|
|
$result.Count | Should Be $returnCount
|
|
(Compare-Object $result $returnValue -SyncWindow 0).length -eq 0 | Should Be $true
|
|
|
|
$result = $th.ui.Streams.Information
|
|
|
|
$result.Count | Should Be $returnCount
|
|
(Compare-Object $result $returnInfo -SyncWindow 0).length -eq 0 | Should Be $true
|
|
}
|
|
}
|