From 7d5d874d11ddc0340a137cff713fa5ef8e196c49 Mon Sep 17 00:00:00 2001 From: Zachary Folwick Date: Fri, 13 Nov 2015 10:57:25 -0800 Subject: [PATCH 1/5] added Measure-Object tests. There are considerable differences between different versions of PowerShell --- src/pester-tests/Measure-Object.Tests.ps1 | 33 +++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/pester-tests/Measure-Object.Tests.ps1 diff --git a/src/pester-tests/Measure-Object.Tests.ps1 b/src/pester-tests/Measure-Object.Tests.ps1 new file mode 100644 index 0000000000..e805bfc799 --- /dev/null +++ b/src/pester-tests/Measure-Object.Tests.ps1 @@ -0,0 +1,33 @@ +Describe "Measure-Object" { + $testObject = 1,3,4 + + It "Should be able to be called without error" { + { Measure-Object | Out-Null } | Should Not Throw + } + + It "Should be able to call on piped input" { + { $testObject | Measure-Object } | Should Not Throw + } + + It "Should be able to count the number of objects input to it" { + $($testObject | Measure-Object).Count | Should Be $testObject.Length + } + + It "Should be able to count using the Property switch" { + $expected = $(Get-ChildItem).Length + $actual = $(Get-ChildItem | Measure-Object -Property Length).Count + + $actual | Should Be $expected + } + + It "Should be able to get additional stats" { + $actual = Get-Process | Measure-Object -Property workingset64 -Minimum -Maximum -Average + + $actual.Average | Should BeGreaterThan 0 + $actual.Characters | Should BeNullOrEmpty + $actual.Lines | Should BeNullOrEmpty + $actual.Maximum | Should Not BeNullOrEmpty + $actual.Minimum | Should Not BeNullOrEmpty + $actual.Sum | Should BeNullOrEmpty + } +} From 1cd02ed5a1e9342c4d3c02c0827c8c0618011750 Mon Sep 17 00:00:00 2001 From: Zachary Folwick Date: Mon, 16 Nov 2015 12:03:58 -0800 Subject: [PATCH 2/5] bring in changes from monad to fix measure-object counting issue --- src/monad | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/monad b/src/monad index 6f5afbe569..0720e033d9 160000 --- a/src/monad +++ b/src/monad @@ -1 +1 @@ -Subproject commit 6f5afbe569d4bedfc58c8682d9b0058a68e289e9 +Subproject commit 0720e033d9f241571d6d5d7378836773a50cc1cd From f6b512cafb758625f7646c65543404e5fce0c550 Mon Sep 17 00:00:00 2001 From: Zachary Folwick Date: Mon, 16 Nov 2015 16:45:20 -0800 Subject: [PATCH 3/5] added tests per code Review --- src/pester-tests/Measure-Object.Tests.ps1 | 35 ++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/pester-tests/Measure-Object.Tests.ps1 b/src/pester-tests/Measure-Object.Tests.ps1 index e805bfc799..f031fa0b93 100644 --- a/src/pester-tests/Measure-Object.Tests.ps1 +++ b/src/pester-tests/Measure-Object.Tests.ps1 @@ -16,7 +16,7 @@ It "Should be able to count using the Property switch" { $expected = $(Get-ChildItem).Length $actual = $(Get-ChildItem | Measure-Object -Property Length).Count - + $actual | Should Be $expected } @@ -30,4 +30,37 @@ $actual.Minimum | Should Not BeNullOrEmpty $actual.Sum | Should BeNullOrEmpty } + + Context "String tests" { + if ($env:TEMP -eq "/tmp") + { + $nl = "`n" + } + else { + $nl = "`r`n" + } + + $testString = "HAD I the heavens’ embroidered cloths,$nl Enwrought with golden and silver light,$nl The blue and the dim and the dark cloths$nl Of night and light and the half light,$nl I would spread the cloths under your feet:$nl But I, being poor, have only my dreams;$nl I have spread my dreams under your feet;$nl Tread softly because you tread on my dreams." + + It "Should be able to count the number of words in a string" { + $expectedLength = $testString.Replace($nl,"").Split().length + $actualLength = $testString | Measure-Object -Word + + $actualLength.Words | Should Be $expectedLength + } + + It "Should be able to count the number of characters in a string" { + $expectedLength = $testString.length + $actualLength = $testString | Measure-Object -Character + + $actualLength.Characters | Should Be $expectedLength + } + + It "Should be able to count the number of lines in a string" { + $expectedLength = $testString.Split($nl).length + $actualLength = $testString | Measure-Object -Line + + $actualLength.Lines | Should Be $expectedLength + } + } } From 29baeac0da96ac4767cc6dc020a26b25a2ac102f Mon Sep 17 00:00:00 2001 From: Zachary Folwick Date: Tue, 17 Nov 2015 09:34:58 -0800 Subject: [PATCH 4/5] added numeric tests to verify number capabilities --- src/pester-tests/Measure-Object.Tests.ps1 | 94 +++++++++++++++++++++++ 1 file changed, 94 insertions(+) diff --git a/src/pester-tests/Measure-Object.Tests.ps1 b/src/pester-tests/Measure-Object.Tests.ps1 index f031fa0b93..41aeb9b77e 100644 --- a/src/pester-tests/Measure-Object.Tests.ps1 +++ b/src/pester-tests/Measure-Object.Tests.ps1 @@ -31,6 +31,100 @@ $actual.Sum | Should BeNullOrEmpty } + Context "Numeric tests" { + It "Should be able to sum" { + $actual = $testObject | Measure-Object -Sum + $expected = 0 + + foreach ( $obj in $testObject ) + { + $expected += $obj + } + + $actual.Sum | Should Be $expected + } + + It "Should be able to average" { + $actual = $testObject | Measure-Object -Average + $expected = 0 + + foreach ( $obj in $testObject ) + { + $expected += $obj + } + + $expected /= $testObject.length + + $actual.Average | Should Be $expected + } + + It "Should be able to return a minimum" { + $actual = $testObject | Measure-Object -Minimum + $expected = $testObject[0] + + for ($i=0; $i -lt $testObject.length; $i++) + { + if ( $testObject[$i] -lt $expected ) + { + + $expected = $testObject[$i] + } + } + + $actual.Minimum | Should Be $expected + } + + It "Should be able to return a minimum when multiple objects are the minimum" { + $testMinimum = 1,1,2,4 + $actual = $testMinimum | Measure-Object -Minimum + $expected = $testMinimum[0] + + for ($i=0; $i -lt $testMinimum.length; $i++) + { + if ( $testMinimum[$i] -lt $expected ) + { + + $expected = $testMinimum[$i] + } + } + + $actual.Minimum | Should Be $expected + } + + It "Should be able to return a maximum" { + $actual = $testObject | Measure-Object -Maximum + $expected = 0 + + for ($i=0; $i -lt $testObject.length; $i++) + { + if ( $testObject[$i] -gt $expected ) + { + + $expected = $testObject[$i] + } + } + + $actual.Maximum | Should Be $expected + } + + It "Should be able to return a maximum when multiple objects are the maximum" { + $testMaximum = 1,3,5,5 + $actual = $testMaximum | Measure-Object -Maximum + $expected = 0 + + for ($i=0; $i -lt $testMaximum.length; $i++) + { + if ( $testMaximum[$i] -gt $expected ) + { + + $expected = $testMaximum[$i] + } + } + + $actual.Maximum | Should Be $expected + } + } + Context "String tests" { if ($env:TEMP -eq "/tmp") { From 51cd991ffe0b94ea8dad8b1c895d8e273cc2ebfe Mon Sep 17 00:00:00 2001 From: Zachary Folwick Date: Tue, 17 Nov 2015 09:46:40 -0800 Subject: [PATCH 5/5] corrected bad test assumptions --- src/pester-tests/Measure-Object.Tests.ps1 | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/pester-tests/Measure-Object.Tests.ps1 b/src/pester-tests/Measure-Object.Tests.ps1 index 41aeb9b77e..d4d70ad257 100644 --- a/src/pester-tests/Measure-Object.Tests.ps1 +++ b/src/pester-tests/Measure-Object.Tests.ps1 @@ -34,7 +34,7 @@ Context "Numeric tests" { It "Should be able to sum" { $actual = $testObject | Measure-Object -Sum - $expected = 0 + $expected = $testObject[0] foreach ( $obj in $testObject ) { @@ -46,7 +46,7 @@ It "Should be able to average" { $actual = $testObject | Measure-Object -Average - $expected = 0 + $expected = $testObject[0] foreach ( $obj in $testObject ) { @@ -93,7 +93,7 @@ It "Should be able to return a maximum" { $actual = $testObject | Measure-Object -Maximum - $expected = 0 + $expected = $testObject for ($i=0; $i -lt $testObject.length; $i++) { @@ -110,7 +110,7 @@ It "Should be able to return a maximum when multiple objects are the maximum" { $testMaximum = 1,3,5,5 $actual = $testMaximum | Measure-Object -Maximum - $expected = 0 + $expected = $testMaximum for ($i=0; $i -lt $testMaximum.length; $i++) {