mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Added -CaseInsensitive switch parameter to Select-Object & Get-Unique cmdlets (#19683)
* Added Select-Object CaseInsensitive parameter and tests * Added Get-Unique CaseInsensitive parameter and tests * Fix codefactor issue * Add named arguements * Remove comments
This commit is contained in:
@@ -50,6 +50,13 @@ namespace Microsoft.PowerShell.Commands
|
||||
}
|
||||
|
||||
private bool _onType = false;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets case insensitive switch for string comparison.
|
||||
/// </summary>
|
||||
[Parameter]
|
||||
public SwitchParameter CaseInsensitive { get; set; }
|
||||
|
||||
#endregion Parameters
|
||||
|
||||
#region Overrides
|
||||
@@ -77,7 +84,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
if (string.Equals(
|
||||
inputString,
|
||||
_lastObjectAsString,
|
||||
StringComparison.CurrentCulture))
|
||||
CaseInsensitive.IsPresent ? StringComparison.CurrentCultureIgnoreCase : StringComparison.CurrentCulture))
|
||||
{
|
||||
isUnique = false;
|
||||
}
|
||||
@@ -89,9 +96,9 @@ namespace Microsoft.PowerShell.Commands
|
||||
else // compare as objects
|
||||
{
|
||||
_comparer ??= new ObjectCommandComparer(
|
||||
true, // ascending (doesn't matter)
|
||||
ascending: true,
|
||||
CultureInfo.CurrentCulture,
|
||||
true); // case-sensitive
|
||||
caseSensitive: !CaseInsensitive.IsPresent);
|
||||
|
||||
isUnique = (_comparer.Compare(InputObject, _lastObject) != 0);
|
||||
}
|
||||
|
||||
@@ -110,6 +110,13 @@ namespace Microsoft.PowerShell.Commands
|
||||
|
||||
private bool _unique;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets case insensitive switch for string comparison.
|
||||
/// Used in combination with Unique switch parameter.
|
||||
/// </summary>
|
||||
[Parameter]
|
||||
public SwitchParameter CaseInsensitive { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
@@ -632,7 +639,11 @@ namespace Microsoft.PowerShell.Commands
|
||||
bool isObjUnique = true;
|
||||
foreach (UniquePSObjectHelper uniqueObj in _uniques)
|
||||
{
|
||||
ObjectCommandComparer comparer = new(true, CultureInfo.CurrentCulture, true);
|
||||
ObjectCommandComparer comparer = new(
|
||||
ascending: true,
|
||||
CultureInfo.CurrentCulture,
|
||||
caseSensitive: !CaseInsensitive.IsPresent);
|
||||
|
||||
if ((comparer.Compare(obj.BaseObject, uniqueObj.WrittenObject.BaseObject) == 0) &&
|
||||
(uniqueObj.NotePropertyCount == addedNoteProperties.Count))
|
||||
{
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
# Copyright (c) Microsoft Corporation.
|
||||
# Licensed under the MIT License.
|
||||
Describe "Get-Unique DRT Unit Tests" -Tags "CI" {
|
||||
It "Command get-unique works with AsString switch" {
|
||||
|
||||
BeforeAll {
|
||||
$inputArray = "aa","aa","Aa","ba","BA","BA"
|
||||
}
|
||||
|
||||
It "Command get-unique works with AsString switch" {
|
||||
$results = $inputArray | Get-Unique -AsString
|
||||
|
||||
$results.Length | Should -Be 4
|
||||
@@ -17,6 +21,18 @@ Describe "Get-Unique DRT Unit Tests" -Tags "CI" {
|
||||
$results[2] | Should -BeOfType System.String
|
||||
$results[3] | Should -BeOfType System.String
|
||||
}
|
||||
|
||||
It "Command get-unique works with AsString and CaseInsensitive switches" {
|
||||
$results = $inputArray | Get-Unique -AsString -CaseInsensitive
|
||||
|
||||
$results.Length | Should -Be 2
|
||||
|
||||
$results[0] | Should -BeExactly "aa"
|
||||
$results[1] | Should -BeExactly "ba"
|
||||
|
||||
$results[0] | Should -BeOfType System.String
|
||||
$results[1] | Should -BeOfType System.String
|
||||
}
|
||||
}
|
||||
|
||||
Describe "Get-Unique" -Tags "CI" {
|
||||
@@ -26,6 +42,8 @@ Describe "Get-Unique" -Tags "CI" {
|
||||
$expectedOutput1 = 1,2,3,4,5
|
||||
$collection = "a", "b", "b", "d"
|
||||
$expectedOutput2 = "a", "b", "d"
|
||||
$collection2 = "a","A", "b", "B"
|
||||
$expectedOutput3 = "a", "b"
|
||||
}
|
||||
|
||||
It "Should be able to use the Get-Unique cmdlet without error with inputObject switch" {
|
||||
@@ -61,4 +79,9 @@ Describe "Get-Unique" -Tags "CI" {
|
||||
$actual = $collection | Get-Unique
|
||||
$(Compare-Object $actual $expectedOutput2 -SyncWindow 0).Length | Should -Be 0
|
||||
}
|
||||
|
||||
It "Should get the unique strings when CaseInsensitive switch is used" {
|
||||
$actual = $collection2 | Get-Unique -CaseInsensitive
|
||||
$(Compare-Object $actual $expectedOutput3 -SyncWindow 0).Length | Should -Be 0
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,6 +58,13 @@ Describe "Select-Object" -Tags "CI" {
|
||||
$result | Should -Be $expected
|
||||
}
|
||||
|
||||
It "Should work work correctly with Unique and CaseInsensitive parameter" {
|
||||
$result = "abc", "Abc" | Select-Object -Unique -CaseInsensitive
|
||||
|
||||
$result.Count | Should -Be 1
|
||||
$result | Should -Be "abc"
|
||||
}
|
||||
|
||||
It "Should return correct object with Skip parameter" {
|
||||
$result = $dirObject | Select-Object -Skip $TestLength
|
||||
|
||||
|
||||
Reference in New Issue
Block a user