2016-03-04 23:48:30 +01:00
Describe " Measure-Object " {
2015-11-17 18:34:58 +01:00
$testObject = 1 , 3 , 4
It " Should be able to be called without error " {
2016-03-04 23:52:27 +01:00
{ Measure-Object | Out-Null } | Should Not Throw
2015-11-17 18:34:58 +01:00
}
It " Should be able to call on piped input " {
2016-03-04 23:52:27 +01:00
{ $testObject | Measure-Object } | Should Not Throw
2015-11-17 18:34:58 +01:00
}
It " Should be able to count the number of objects input to it " {
2016-03-04 23:52:27 +01:00
$ ( $testObject | Measure-Object ) . Count | Should Be $testObject . Length
2015-11-17 18:34:58 +01:00
}
It " Should be able to count using the Property switch " {
2016-03-04 23:52:27 +01:00
$expected = $ ( Get-ChildItem ) . Length
$actual = $ ( Get-ChildItem | Measure-Object -Property Length ) . Count
2015-11-17 18:34:58 +01:00
2016-03-04 23:52:27 +01:00
$actual | Should Be $expected
2015-11-17 18:34:58 +01:00
}
It " Should be able to get additional stats " {
2016-03-04 23:52:27 +01:00
$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
2015-11-17 18:34:58 +01:00
}
Context " Numeric tests " {
2016-03-04 23:52:27 +01:00
It " Should be able to sum " {
$actual = $testObject | Measure-Object -Sum
$expected = 0
2015-11-17 18:34:58 +01:00
2016-03-04 23:52:27 +01:00
foreach ( $obj in $testObject )
{
$expected + = $obj
}
2015-11-17 18:34:58 +01:00
2016-03-04 23:52:27 +01:00
$actual . Sum | Should Be $expected
}
2015-11-17 18:34:58 +01:00
2016-03-04 23:52:27 +01:00
It " Should be able to average " {
$actual = $testObject | Measure-Object -Average
$expected = 0
2015-11-17 18:34:58 +01:00
2016-03-04 23:52:27 +01:00
foreach ( $obj in $testObject )
{
$expected + = $obj
}
2015-11-17 18:34:58 +01:00
2016-03-04 23:52:27 +01:00
$expected / = $testObject . length
2015-11-17 18:34:58 +01:00
2016-03-04 23:52:27 +01:00
$actual . Average | Should Be $expected
}
2015-11-17 18:34:58 +01:00
2016-03-04 23:52:27 +01:00
It " Should be able to return a minimum " {
$actual = $testObject | Measure-Object -Minimum
$expected = $testObject [ 0 ]
2015-11-17 18:34:58 +01:00
2016-03-04 23:52:27 +01:00
for ( $i = 0 ; $i -lt $testObject . length ; $i + + )
{
if ( $testObject [ $i ] -lt $expected )
{
2015-11-17 18:34:58 +01:00
2016-03-04 23:52:27 +01:00
$expected = $testObject [ $i ]
}
}
2015-11-17 18:34:58 +01:00
2016-03-04 23:52:27 +01:00
$actual . Minimum | Should Be $expected
}
2015-11-17 18:34:58 +01:00
2016-03-04 23:52:27 +01:00
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 ]
2015-11-17 18:34:58 +01:00
2016-03-04 23:52:27 +01:00
for ( $i = 1 ; $i -lt $testMinimum . length ; $i + + )
{
if ( $testMinimum [ $i ] -lt $expected )
{
2015-11-17 18:34:58 +01:00
2016-03-04 23:52:27 +01:00
$expected = $testMinimum [ $i ]
}
}
2015-11-17 18:34:58 +01:00
2016-03-04 23:52:27 +01:00
$actual . Minimum | Should Be $expected
}
2015-11-17 18:34:58 +01:00
2016-03-04 23:52:27 +01:00
It " Should be able to return a maximum " {
$actual = $testObject | Measure-Object -Maximum
$expected = $testObject [ 0 ]
2015-11-17 18:34:58 +01:00
2016-03-04 23:52:27 +01:00
for ( $i = 1 ; $i -lt $testObject . length ; $i + + )
{
if ( $testObject [ $i ] -gt $expected )
{
2015-11-17 18:34:58 +01:00
2016-03-04 23:52:27 +01:00
$expected = $testObject [ $i ]
}
}
2015-11-17 18:34:58 +01:00
2016-03-04 23:52:27 +01:00
$actual . Maximum | Should Be $expected
}
2015-11-17 18:34:58 +01:00
2016-03-04 23:52:27 +01:00
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 = $testMaximum [ 0 ]
2015-11-17 18:34:58 +01:00
2016-03-04 23:52:27 +01:00
for ( $i = 1 ; $i -lt $testMaximum . length ; $i + + )
{
if ( $testMaximum [ $i ] -gt $expected )
{
2015-11-17 18:34:58 +01:00
2016-03-04 23:52:27 +01:00
$expected = $testMaximum [ $i ]
}
}
2015-11-17 18:34:58 +01:00
2016-03-04 23:52:27 +01:00
$actual . Maximum | Should Be $expected
}
2015-11-17 18:34:58 +01:00
}
Context " String tests " {
2016-03-04 23:52:27 +01:00
$nl = [ Environment ] :: NewLine
2015-11-17 18:34:58 +01:00
2016-03-04 23:52:27 +01:00
$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. "
2015-11-17 18:34:58 +01:00
2016-03-04 23:52:27 +01:00
It " Should be able to count the number of words in a string " {
$expectedLength = $testString . Replace ( $nl , " " ) . Split ( ) . length
$actualLength = $testString | Measure-Object -Word
2015-11-17 18:34:58 +01:00
2016-03-04 23:52:27 +01:00
$actualLength . Words | Should Be $expectedLength
}
2015-11-17 18:34:58 +01:00
2016-03-04 23:52:27 +01:00
It " Should be able to count the number of characters in a string " {
$expectedLength = $testString . length
$actualLength = $testString | Measure-Object -Character
2015-11-17 18:34:58 +01:00
2016-03-04 23:52:27 +01:00
$actualLength . Characters | Should Be $expectedLength
}
2015-11-17 18:34:58 +01:00
2016-03-04 23:52:27 +01:00
It " Should be able to count the number of lines in a string " {
$expectedLength = $testString . Split ( $nl , [ System.StringSplitOptions ] :: RemoveEmptyEntries ) . length
$actualLength = $testString | Measure-Object -Line
2015-11-17 18:34:58 +01:00
2016-03-04 23:52:27 +01:00
$actualLength . Lines | Should Be $expectedLength
}
2015-11-17 18:34:58 +01:00
}
}