forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject.tests.ps1
More file actions
100 lines (85 loc) · 4.99 KB
/
Copy pathobject.tests.ps1
File metadata and controls
100 lines (85 loc) · 4.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
Describe "Object cmdlets" -Tags "CI" {
Context "Group-Object" {
It "AsHashtable returns a hashtable" {
$result = Get-Process | Group-Object -Property ProcessName -AsHashTable
$result["powershell"].Count | Should BeGreaterThan 0
}
It "AsString returns a string" {
$processes = Get-Process | Group-Object -Property ProcessName -AsHashTable -AsString
$result = $processes.Keys | ForEach-Object {$_.GetType()}
$result[0].Name | Should Be "String"
}
}
Context "Tee-Object" {
It "with literal path" {
$path = "TestDrive:\[TeeObjectLiteralPathShouldWorkForSpecialFilename].txt"
Write-Output "Test" | Tee-Object -LiteralPath $path | Tee-Object -Variable TeeObjectLiteralPathShouldWorkForSpecialFilename
$TeeObjectLiteralPathShouldWorkForSpecialFilename | Should Be (Get-Content -LiteralPath $path)
}
}
}
Describe "Object cmdlets" -Tags "CI" {
Context "Measure-Object" {
BeforeAll {
## Powershell language prefers , as an array seperator without "".
## If a number has comma in them it considers it to be the 1000 seperator like "1,000".
## In de-DE language the comma is used as decimal point, but powershell still uses it as a 1000 seperator.
## In case the number has a comma, it is ignored. So, "99,1" becomes 991.
## To work around that behavior, we use ToString() on the expected answer and . for decimal in the input.
$firstValue = "9995788.71"
$expectedFirstValue = $null
$null = [System.Management.Automation.LanguagePrimitives]::TryConvertTo($firstValue, [double], [cultureinfo]::InvariantCulture, [ref] $expectedFirstValue)
$firstObject = new-object psobject
$firstObject | Add-Member -NotePropertyName Header -NotePropertyValue $firstValue
$secondValue = "15847577.7"
$expectedSecondValue = $null
$null = [System.Management.Automation.LanguagePrimitives]::TryConvertTo($secondValue, [double], [cultureinfo]::InvariantCulture, [ref] $expectedSecondValue)
$secondObject = new-object psobject
$secondObject | Add-Member -NotePropertyName Header -NotePropertyValue $secondValue
$testCases = @(
@{ data = @("abc","ABC","Def"); min = "abc"; max = "Def"},
@{ data = @([datetime]::Today, [datetime]::Today.AddDays(-1)); min = ([datetime]::Today.AddDays(-1)).ToString() ; max = [datetime]::Today.ToString() }
@{ data = @(1,2,3,"ABC"); min = 1; max = "ABC"},
@{ data = @(4,2,3,"ABC",1); min = 1; max = "ABC"},
@{ data = @(4,2,3,"ABC",1,"DEF"); min = 1; max = "DEF"},
@{ data = @("111 Test","19"); min = "111 Test"; max = "19"},
@{ data = @("19", "111 Test"); min = "111 Test"; max = "19"},
@{ data = @("111 Test",19); min = "111 Test"; max = 19},
@{ data = @(19, "111 Test"); min = "111 Test"; max = 19},
@{ data = @(100,2,3, "A", 1); min = 1; max = "A"},
@{ data = @(4,2,3, "ABC", 1, "DEF"); min = 1; max = "DEF"},
@{ data = @("abc",[Datetime]::Today,"def"); min = [Datetime]::Today.ToString(); max = "def"}
)
}
It "can compare string representation for minimum" {
$minResult = $firstObject, $secondObject | Measure-Object Header -Minimum
$minResult.Minimum.ToString() | Should Be $expectedFirstValue.ToString()
}
It "can compare string representation for maximum" {
$maxResult = $firstObject, $secondObject | Measure-Object Header -Maximum
$maxResult.Maximum.ToString() | Should Be $expectedSecondValue.ToString()
}
It 'correctly find minimum of (<data>)' -TestCases $testCases {
param($data, $min, $max)
$output = $data | Measure-Object -Minimum
$output.Minimum.ToString() | Should Be $min
}
It 'correctly find maximum of (<data>)' -TestCases $testCases {
param($data, $min, $max)
$output = $data | Measure-Object -Maximum
$output.Maximum.ToString() | Should Be $max
}
It 'returns a GenericMeasureInfoObject' {
$gmi = 1,2,3 | measure-object -max -min
$gmi.GetType().FullName | Should Be 'Microsoft.PowerShell.Commands.GenericMeasureInfo'
}
It 'should return correct error for non-numeric input' {
$gmi = "abc",[Datetime]::Now | measure -sum -max -ev err -ea silentlycontinue
$err | % { $_.FullyQualifiedErrorId | Should Be 'NonNumericInputObject,Microsoft.PowerShell.Commands.MeasureObjectCommand' }
}
It 'should have the correct count' {
$gmi = "abc",[Datetime]::Now | measure -sum -max -ev err -ea silentlycontinue
$gmi.Count | Should Be 2
}
}
}