MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3Describe "Object cmdlets" -Tags "CI" {4 Context "Group-Object" {5 It "AsHashtable returns a hashtable" {6 $result = Get-Process | Group-Object -Property ProcessName -AsHashTable7 $result["pwsh"].Count | Should -BeGreaterThan 08 }9 10 It "AsString returns a string" {11 $processes = Get-Process | Group-Object -Property ProcessName -AsHashTable -AsString12 $result = $processes.Keys | ForEach-Object {$_.GetType()}13 $result[0].Name | Should -Be "String"14 }15 }16 17 Context "Tee-Object" {18 It "with literal path" {19 $path = "TestDrive:\[TeeObjectLiteralPathShouldWorkForSpecialFilename].txt"20 Write-Output "Test" | Tee-Object -LiteralPath $path | Tee-Object -Variable TeeObjectLiteralPathShouldWorkForSpecialFilename21 $TeeObjectLiteralPathShouldWorkForSpecialFilename | Should -Be (Get-Content -LiteralPath $path)22 }23 }24}25 26Describe "Object cmdlets" -Tags "CI" {27 Context "Measure-Object" {28 BeforeAll {29 ## Powershell language prefers , as an array separator without "".30 ## If a number has comma in them it considers it to be the 1000 separator like "1,000".31 ## In de-DE language the comma is used as decimal point, but powershell still uses it as a 1000 separator.32 ## In case the number has a comma, it is ignored. So, "99,1" becomes 991.33 ## To work around that behavior, we use ToString() on the expected answer and . for decimal in the input.34 35 $firstValue = "9995788.71"36 $expectedFirstValue = $null37 $null = [System.Management.Automation.LanguagePrimitives]::TryConvertTo($firstValue, [double], [cultureinfo]::InvariantCulture, [ref] $expectedFirstValue)38 $firstObject = New-Object psobject39 $firstObject | Add-Member -NotePropertyName Header -NotePropertyValue $firstValue40 41 $secondValue = "15847577.7"42 $expectedSecondValue = $null43 $null = [System.Management.Automation.LanguagePrimitives]::TryConvertTo($secondValue, [double], [cultureinfo]::InvariantCulture, [ref] $expectedSecondValue)44 $secondObject = New-Object psobject45 $secondObject | Add-Member -NotePropertyName Header -NotePropertyValue $secondValue46 47 $testCases = @(48 @{ data = @("abc","ABC","Def"); min = "abc"; max = "Def"},49 @{ data = @([datetime]::Today, [datetime]::Today.AddDays(-1)); min = ([datetime]::Today.AddDays(-1)).ToString() ; max = [datetime]::Today.ToString() }50 @{ data = @(1,2,3,"ABC"); min = 1; max = "ABC"},51 @{ data = @(4,2,3,"ABC",1); min = 1; max = "ABC"},52 @{ data = @(4,2,3,"ABC",1,"DEF"); min = 1; max = "DEF"},53 @{ data = @("111 Test","19"); min = "111 Test"; max = "19"},54 @{ data = @("19", "111 Test"); min = "111 Test"; max = "19"},55 @{ data = @("111 Test",19); min = "111 Test"; max = 19},56 @{ data = @(19, "111 Test"); min = "111 Test"; max = 19},57 @{ data = @(100,2,3, "A", 1); min = 1; max = "A"},58 @{ data = @(4,2,3, "ABC", 1, "DEF"); min = 1; max = "DEF"},59 @{ data = @("abc",[Datetime]::Today,"def"); min = [Datetime]::Today.ToString(); max = "def"}60 )61 }62 63 It "can compare string representation for minimum" {64 $minResult = $firstObject, $secondObject | Measure-Object Header -Minimum65 $minResult.Minimum.ToString() | Should -Be $expectedFirstValue.ToString()66 }67 68 It "can compare string representation for maximum" {69 $maxResult = $firstObject, $secondObject | Measure-Object Header -Maximum70 $maxResult.Maximum.ToString() | Should -Be $expectedSecondValue.ToString()71 }72 73 It 'correctly find minimum of (<data>)' -TestCases $testCases {74 param($data, $min, $max)75 76 $output = $data | Measure-Object -Minimum77 $output.Minimum.ToString() | Should -Be $min78 }79 80 It 'correctly find maximum of (<data>)' -TestCases $testCases {81 param($data, $min, $max)82 83 $output = $data | Measure-Object -Maximum84 $output.Maximum.ToString() | Should -Be $max85 }86 87 It 'returns a GenericMeasureInfoObject' {88 $gmi = 1,2,3 | Measure-Object -max -min89 $gmi | Should -BeOfType Microsoft.PowerShell.Commands.GenericMeasureInfo90 }91 92 It 'should return correct error for non-numeric input' {93 $gmi = "abc",[Datetime]::Now | Measure-Object -Sum -max -ErrorVariable err -ErrorAction silentlycontinue94 $err | ForEach-Object { $_.FullyQualifiedErrorId | Should -Be 'NonNumericInputObject,Microsoft.PowerShell.Commands.MeasureObjectCommand' }95 }96 97 It 'should have the correct count' {98 $gmi = "abc",[Datetime]::Now | Measure-Object -Sum -max -ErrorVariable err -ErrorAction silentlycontinue99 $gmi.Count | Should -Be 2100 }101 }102}103 