From 94080b596f1c9abf03774f7bd630e9c495e51554 Mon Sep 17 00:00:00 2001 From: kvprasoon Date: Sun, 1 Jul 2018 18:33:37 +0530 Subject: [PATCH 1/8] Adding -AllStats Switch parameter for Measure-Object cmdlet Adding -AllStats Switch parameter for Measure-Object cmdlet, Issue #6278 --- .../commands/utility/Measure-Object.cs | 22 +++++++++++++++++++ .../Measure-Object.Tests.ps1 | 12 ++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Measure-Object.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Measure-Object.cs index 26540f05856..492122541bc 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Measure-Object.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Measure-Object.cs @@ -310,6 +310,24 @@ public SwitchParameter Sum } private bool _measureSum; + /// + /// Set to true is all the statitics is to be returned + /// + /// + [Parameter(ParameterSetName = GenericParameterSet)] + public SwitchParameter AllStats + { + get + { + return _allStats; + } + set + { + _allStats = value; + } + } + private bool _allStats; + /// /// Set to true is Average is to be returned /// @@ -462,6 +480,10 @@ protected override void ProcessRecord() return; } + if( _allStats ){ + _measureSum = _measureStandardDeviation = _measureAverage = _measureMax = _measureMin = true; + } + _totalRecordCount++; if (Property == null) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Measure-Object.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Measure-Object.Tests.ps1 index b0155a79344..1555225b830 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Measure-Object.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Measure-Object.Tests.ps1 @@ -174,6 +174,18 @@ Describe "Measure-Object" -Tags "CI" { $actual.Maximum | Should -Be $expected } + + It "Should be able to return all the statitics for given values" { + $testNumbers = 1,1,2,4,5,6 + $actual = $testNumbers | Measure-Object -AllStats + + $actual.Average | Should -Not -BeNullOrEmpty + $actual.Count | Should -Not -BeNullOrEmpty + $actual.Sum | Should -Not -BeNullOrEmpty + $actual.Maximum | Should -Not -BeNullOrEmpty + $actual.Average | Should -Not -BeNullOrEmpty + $actual.StandardDeviation | Should -Not -BeNullOrEmpty + } } Context "String tests" { From d804ae9bebf9abb177b93271aee109f563bd826b Mon Sep 17 00:00:00 2001 From: kvprasoon Date: Sun, 1 Jul 2018 18:58:40 +0530 Subject: [PATCH 2/8] Code factor fixes Code factor fixes --- .../commands/utility/Measure-Object.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Measure-Object.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Measure-Object.cs index 492122541bc..ed607c919e1 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Measure-Object.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Measure-Object.cs @@ -311,7 +311,7 @@ public SwitchParameter Sum private bool _measureSum; /// - /// Set to true is all the statitics is to be returned + /// Set to true is all the statitics is to be returned. /// /// [Parameter(ParameterSetName = GenericParameterSet)] @@ -321,11 +321,13 @@ public SwitchParameter AllStats { return _allStats; } + set { _allStats = value; } } + private bool _allStats; /// @@ -480,7 +482,8 @@ protected override void ProcessRecord() return; } - if( _allStats ){ + if (_allStats) + { _measureSum = _measureStandardDeviation = _measureAverage = _measureMax = _measureMin = true; } From bde3fe4e059cea53b3258b2a3e056b46c467ea50 Mon Sep 17 00:00:00 2001 From: kvprasoon Date: Sun, 1 Jul 2018 22:21:53 +0530 Subject: [PATCH 3/8] Code factor fix Code factor fix --- .../commands/utility/Measure-Object.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Measure-Object.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Measure-Object.cs index ed607c919e1..595b5f4854f 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Measure-Object.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Measure-Object.cs @@ -311,7 +311,7 @@ public SwitchParameter Sum private bool _measureSum; /// - /// Set to true is all the statitics is to be returned. + /// Sets all generic parameters to true and returns all the statitics. /// /// [Parameter(ParameterSetName = GenericParameterSet)] From 218f5ad3dd7143c6a4eeb9663611b8bda7b95578 Mon Sep 17 00:00:00 2001 From: kvprasoon Date: Tue, 3 Jul 2018 00:24:32 +0530 Subject: [PATCH 4/8] Changes as per review comments. Changes as per review comments. --- .../commands/utility/Measure-Object.cs | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Measure-Object.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Measure-Object.cs index 595b5f4854f..5384c081763 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Measure-Object.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Measure-Object.cs @@ -311,7 +311,7 @@ public SwitchParameter Sum private bool _measureSum; /// - /// Sets all generic parameters to true and returns all the statitics. + /// Sets all generic parameters to true and returns all the statistics. /// /// [Parameter(ParameterSetName = GenericParameterSet)] @@ -471,6 +471,19 @@ private bool IsMeasuringGeneric } } + /// + /// Does the begin part of the cmdlet. + /// + protected override void BeginProcessing() + { + // Sets all other generic parameters to true to get all statistics. + if (_allStats) + { + _measureSum = _measureStandardDeviation = _measureAverage = _measureMax = _measureMin = true; + } + + } + /// /// Collect data about each record that comes in. /// Side effects: Updates totalRecordCount. @@ -482,11 +495,6 @@ protected override void ProcessRecord() return; } - if (_allStats) - { - _measureSum = _measureStandardDeviation = _measureAverage = _measureMax = _measureMin = true; - } - _totalRecordCount++; if (Property == null) From bf55d1522b623841c9f36dcd556ea874aabd2945 Mon Sep 17 00:00:00 2001 From: kvprasoon Date: Tue, 3 Jul 2018 00:27:05 +0530 Subject: [PATCH 5/8] Code factor fix. Code factor fix. --- .../commands/utility/Measure-Object.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Measure-Object.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Measure-Object.cs index 5384c081763..d712940dd9b 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Measure-Object.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Measure-Object.cs @@ -481,7 +481,6 @@ protected override void BeginProcessing() { _measureSum = _measureStandardDeviation = _measureAverage = _measureMax = _measureMin = true; } - } /// From 1d02d79148f73f130b44def58518d11ab196a371 Mon Sep 17 00:00:00 2001 From: kvprasoon Date: Wed, 4 Jul 2018 01:04:57 +0530 Subject: [PATCH 6/8] Review comment changes Review comment changes --- .../commands/utility/Measure-Object.cs | 3 +++ .../Measure-Object.Tests.ps1 | 15 ++++++--------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Measure-Object.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Measure-Object.cs index d712940dd9b..89a615557a6 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Measure-Object.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Measure-Object.cs @@ -481,6 +481,9 @@ protected override void BeginProcessing() { _measureSum = _measureStandardDeviation = _measureAverage = _measureMax = _measureMin = true; } + + // finally call the base class. + base.BeginProcessing(); } /// diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Measure-Object.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Measure-Object.Tests.ps1 index 1555225b830..d942afa6f70 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Measure-Object.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Measure-Object.Tests.ps1 @@ -176,15 +176,12 @@ Describe "Measure-Object" -Tags "CI" { } It "Should be able to return all the statitics for given values" { - $testNumbers = 1,1,2,4,5,6 - $actual = $testNumbers | Measure-Object -AllStats - - $actual.Average | Should -Not -BeNullOrEmpty - $actual.Count | Should -Not -BeNullOrEmpty - $actual.Sum | Should -Not -BeNullOrEmpty - $actual.Maximum | Should -Not -BeNullOrEmpty - $actual.Average | Should -Not -BeNullOrEmpty - $actual.StandardDeviation | Should -Not -BeNullOrEmpty + $result = 1..10 | Measure-Object -AllStats + $result.Count | Should -Be 10 + $result.Average | Should -Be 5.5 + $result.Sum | Should -Be 55 + $result.Minimum | Should -Be 1 + $result.Maximum | Should -Be 10 } } From 0391972fc0e507ba022a33b1d806b77c867d5ef7 Mon Sep 17 00:00:00 2001 From: kvprasoon Date: Thu, 5 Jul 2018 23:48:51 +0530 Subject: [PATCH 7/8] Adding assertion for StandardDeviationin unitMeasure-Object test Adding assertion for StandardDeviationin unitMeasure-Object test --- .../Microsoft.PowerShell.Utility/Measure-Object.Tests.ps1 | 1 + 1 file changed, 1 insertion(+) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Measure-Object.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Measure-Object.Tests.ps1 index d942afa6f70..6c24ec0dcb5 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Measure-Object.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Measure-Object.Tests.ps1 @@ -182,6 +182,7 @@ Describe "Measure-Object" -Tags "CI" { $result.Sum | Should -Be 55 $result.Minimum | Should -Be 1 $result.Maximum | Should -Be 10 + ($result.StandardDeviation).ToString() | Should -Be '3.02765035409749' } } From 30a649fb6914931fa332a1bfac80708934605a92 Mon Sep 17 00:00:00 2001 From: kvprasoon Date: Sat, 7 Jul 2018 02:23:55 +0530 Subject: [PATCH 8/8] Code factor issue fix. Code factor issue fix. --- .../commands/utility/Measure-Object.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Measure-Object.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Measure-Object.cs index 89a615557a6..9befe0c608e 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Measure-Object.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Measure-Object.cs @@ -311,7 +311,7 @@ public SwitchParameter Sum private bool _measureSum; /// - /// Sets all generic parameters to true and returns all the statistics. + /// Gets or sets the value indicating if all statistics should be returned. /// /// [Parameter(ParameterSetName = GenericParameterSet)]