Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
Write-Host.Tests.ps195 linesDownload Raw Back to Microsoft.PowerShell.Utility
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3Describe "Write-Host with default Console Host" -Tags "Slow","Feature" {4 5    BeforeAll {6        $powershell = Join-Path -Path $PSHOME -ChildPath "pwsh"7 8        $testData = @(9            @{ Name = '-Separator';       Command = "Write-Host a,b,c -Separator '+'";                 returnCount = 1; returnValue = @("a+b+c") }10            @{ Name = '-NoNewline=true';  Command = "Write-Host a,b -NoNewline:`$true;Write-Host a,b"; returnCount = 1; returnValue = @("a ba b") }11            @{ Name = '-NoNewline=false'; Command = "Write-Host a1,b1;Write-Host a2,b2";               returnCount = 2; returnValue = @("a1 b1","a2 b2") }12        )13    }14 15    It "write-Host works with '<Name>' switch" -TestCases:$testData -Pending:$IsMacOS {16        param($Command, $returnCount, $returnValue)17 18        [array]$result = & $powershell -noprofile -c $Command19 20        $result.Count | Should -Be $returnCount21        foreach ($i in 0..($returnCount - 1))22        {23            $result[$i] | Should -Be $returnValue[$i]24        }25    }26}27 28Describe "Write-Host with wrong colors" -Tags "CI" {29 30    BeforeAll {31        $testWrongColor = @(32            @{ ForegroundColor = -1;    BackgroundColor = 'Red' }33            @{ ForegroundColor = 16;    BackgroundColor = 'Red' }34            @{ ForegroundColor = 'Red'; BackgroundColor = -1 }35            @{ ForegroundColor = 'Red'; BackgroundColor = 16 }36        )37    }38 39    It 'Should throw if color is invalid: ForegroundColor = <ForegroundColor>; BackgroundColor = <BackgroundColor>' -TestCases:$testWrongColor {40      param($ForegroundColor, $BackgroundColor)41      { Write-Host "No output from Write-Host" -ForegroundColor $ForegroundColor -BackgroundColor $BackgroundColor } | Should -Throw -ErrorId 'CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.WriteHostCommand'42    }43}44 45Describe "Write-Host with TestHostCS" -Tags "CI" {46 47    BeforeAll {48        $th = New-TestHost49        $rs = [runspacefactory]::Createrunspace($th)50        $rs.open()51        $ps = [powershell]::Create()52        $ps.Runspace = $rs53 54        $testHostCSData = @(55            @{ Name = 'defaults';                          Command = "Write-Host a,b,c";                                                                             returnCount = 1; returnValue = @("White:Black:a b c:NewLine"); returnInfo = @("a b c") }56            @{ Name = '-Object';                           Command = "Write-Host -Object a,b,c";                                                                     returnCount = 1; returnValue = @("White:Black:a b c:NewLine"); returnInfo = @("a b c") }57            @{ Name = '-Message';                          Command = "Write-Host -Message a,b,c";                                                                    returnCount = 1; returnValue = @("White:Black:a b c:NewLine"); returnInfo = @("a b c") }58            @{ Name = '-Msg';                              Command = "Write-Host -Msg a,b,c";                                                                        returnCount = 1; returnValue = @("White:Black:a b c:NewLine"); returnInfo = @("a b c") }59            @{ Name = '-Separator';                        Command = "Write-Host a,b,c -Separator '+'";                                                              returnCount = 1; returnValue = @("White:Black:a+b+c:NewLine"); returnInfo = @("a+b+c") }60            @{ 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") }61            @{ 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") }62            @{ 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") }63            @{ Name = 'XMLElement';                        Command = "Write-Host ([xml] '<OhElement>Where art thou?</OhElement>').DocumentElement";                  returnCount = 1; returnValue = @("White:Black:OhElement:NewLine"); returnInfo = @("OhElement") }64            @{ Name = 'XMLDocument';                        Command = "Write-Host ([system.xml.xmldocument] '<OhElement>Where art thou?</OhElement>').DocumentElement";                  returnCount = 1; returnValue = @("White:Black:OhElement:NewLine"); returnInfo = @("OhElement") }65        )66 67    }68 69    AfterAll {70        $rs.Close()71        $rs.Dispose()72        $ps.Dispose()73    }74 75    AfterEach {76        $ps.Commands.Clear()77        $th.ui.Streams.Clear()78    }79 80    It "Write-Host works with <Name>" -TestCases:$testHostCSData {81        param($Command, $returnCount, $returnValue, $returnInfo)82        $ps.AddScript($Command).Invoke()83 84        $result = $th.ui.Streams.ConsoleOutput85 86        $result.Count | Should -Be $returnCount87        (Compare-Object $result $returnValue -SyncWindow 0).length | Should -Be 088 89        $result = $th.ui.Streams.Information90 91        $result.Count | Should -Be $returnCount92        (Compare-Object $result $returnInfo -SyncWindow 0).length | Should -Be 093    }94}95