2020-03-24 11:08:37 -07:00
|
|
|
# Copyright (c) Microsoft Corporation.
|
2018-02-13 09:23:53 -08:00
|
|
|
# Licensed under the MIT License.
|
2016-07-25 14:01:41 -07:00
|
|
|
Describe "Write-Output DRT Unit Tests" -Tags "CI" {
|
2016-04-27 01:08:53 -07:00
|
|
|
It "Simple Write Object Test" {
|
|
|
|
|
$objectWritten = 1, 2.2, @("John", "Smith", 10), "abc"
|
|
|
|
|
$results = Write-Output $objectWritten
|
2018-03-23 10:26:23 -07:00
|
|
|
$results.Length | Should -Be $objectWritten.Length
|
2016-04-27 01:08:53 -07:00
|
|
|
|
2018-03-23 10:26:23 -07:00
|
|
|
$results[0] | Should -Be $objectWritten[0]
|
|
|
|
|
$results[1] | Should -Be $objectWritten[1]
|
2016-04-27 01:08:53 -07:00
|
|
|
|
2018-03-23 10:26:23 -07:00
|
|
|
$results[2] | Should -Be $objectWritten[2]
|
|
|
|
|
$results[2] -is [System.Array] | Should -BeTrue
|
2016-04-27 01:08:53 -07:00
|
|
|
|
2018-03-23 10:26:23 -07:00
|
|
|
$results[3] | Should -Be $objectWritten[3]
|
|
|
|
|
$results[3] -is [System.String] | Should -BeTrue
|
2016-04-27 01:08:53 -07:00
|
|
|
}
|
2016-06-02 23:31:12 -07:00
|
|
|
|
|
|
|
|
It "Works with NoEnumerate switch" {
|
|
|
|
|
$objectWritten = 1, 2.2, @("John", "Smith", 10), "abc"
|
2019-03-13 04:54:12 -04:00
|
|
|
[string]$s = Write-Output $objectWritten -NoEnumerate 6>&1
|
2018-03-23 10:26:23 -07:00
|
|
|
$s | Should -Be '1 2.2 System.Object[] abc'
|
2016-06-02 23:31:12 -07:00
|
|
|
}
|
2019-03-13 04:54:12 -04:00
|
|
|
|
|
|
|
|
It "Preserves the input collection type and contents with -NoEnumerate" {
|
|
|
|
|
$List = [System.Collections.Generic.List[string]]::new( [string[]]@("test", "one", "two") )
|
|
|
|
|
$ObjectWritten = Write-Output $List -NoEnumerate
|
|
|
|
|
|
|
|
|
|
$ObjectWritten.GetType() | Should -Be ([System.Collections.Generic.List[string]])
|
|
|
|
|
$ObjectWritten[0] | Should -BeExactly "test"
|
|
|
|
|
$ObjectWritten[1] | Should -BeExactly "one"
|
|
|
|
|
$ObjectWritten[2] | Should -BeExactly "two"
|
|
|
|
|
}
|
2016-04-27 01:08:53 -07:00
|
|
|
}
|
|
|
|
|
|
2016-07-25 14:01:41 -07:00
|
|
|
Describe "Write-Output" -Tags "CI" {
|
2016-03-04 14:48:30 -08:00
|
|
|
$testString = $testString
|
|
|
|
|
Context "Input Tests" {
|
2019-03-13 04:54:12 -04:00
|
|
|
It "Should allow piped input" {
|
|
|
|
|
{ $testString | Write-Output } | Should -Not -Throw
|
|
|
|
|
}
|
2016-03-04 14:48:30 -08:00
|
|
|
|
2019-03-13 04:54:12 -04:00
|
|
|
It "Should write output to the output stream when using piped input" {
|
|
|
|
|
$testString | Write-Output | Should -Be $testString
|
|
|
|
|
}
|
2016-03-04 14:48:30 -08:00
|
|
|
|
2019-03-13 04:54:12 -04:00
|
|
|
It "Should use inputobject switch" {
|
|
|
|
|
{ Write-Output -InputObject $testString } | Should -Not -Throw
|
|
|
|
|
}
|
2016-03-04 14:48:30 -08:00
|
|
|
|
2019-03-13 04:54:12 -04:00
|
|
|
It "Should write output to the output stream when using inputobject switch" {
|
|
|
|
|
Write-Output -InputObject $testString | Should -Be $testString
|
|
|
|
|
}
|
2016-03-04 14:48:30 -08:00
|
|
|
|
2019-03-13 04:54:12 -04:00
|
|
|
It "Should be able to write to a variable" {
|
|
|
|
|
Write-Output -InputObject $testString -OutVariable var
|
|
|
|
|
$var | Should -Be $testString
|
|
|
|
|
}
|
2016-03-04 14:48:30 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Context "Pipeline Command Tests" {
|
2019-03-13 04:54:12 -04:00
|
|
|
It "Should send object to the next command in the pipeline" {
|
|
|
|
|
Write-Output -InputObject (1 + 1) | Should -Be 2
|
|
|
|
|
}
|
2016-03-04 14:48:30 -08:00
|
|
|
|
2019-03-13 04:54:12 -04:00
|
|
|
It "Should have the same result between inputobject switch and piped input" {
|
|
|
|
|
Write-Output -InputObject (1 + 1) | Should -Be 2
|
2016-03-04 14:48:30 -08:00
|
|
|
|
2019-03-13 04:54:12 -04:00
|
|
|
1 + 1 | Write-Output | Should -Be 2
|
|
|
|
|
}
|
2016-03-04 14:48:30 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Context "Enumerate Objects" {
|
2019-03-13 04:54:12 -04:00
|
|
|
$enumerationObject = @(1, 2, 3)
|
|
|
|
|
It "Should see individual objects when not using the NoEnumerate switch" {
|
|
|
|
|
$singleCollection = $(Write-Output $enumerationObject| Measure-Object).Count
|
2016-03-04 14:48:30 -08:00
|
|
|
|
2019-03-13 04:54:12 -04:00
|
|
|
$singleCollection | Should -Be $enumerationObject.length
|
|
|
|
|
}
|
2016-03-04 14:48:30 -08:00
|
|
|
|
2019-03-13 04:54:12 -04:00
|
|
|
It "Should be able to treat a collection as a single object using the NoEnumerate switch" {
|
|
|
|
|
$singleCollection = $(Write-Output $enumerationObject -NoEnumerate | Measure-Object).Count
|
2016-03-04 14:48:30 -08:00
|
|
|
|
2019-03-13 04:54:12 -04:00
|
|
|
$singleCollection | Should -Be 1
|
|
|
|
|
}
|
2016-03-04 14:48:30 -08:00
|
|
|
}
|
|
|
|
|
}
|