Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class GetRandomCommand : PSCmdlet

Comment thread
SteveL-MSFT marked this conversation as resolved.
private const string RandomNumberParameterSet = "RandomNumberParameterSet";
private const string RandomListItemParameterSet = "RandomListItemParameterSet";
private static readonly object[] _nullInArray = new object[] { null };

private enum MyParameterSet
{
Expand Down Expand Up @@ -275,7 +276,7 @@ private double ConvertToDouble(object o, double defaultIfNull)
/// List from which random elements are chosen.
/// </summary>
[Parameter(ParameterSetName = RandomListItemParameterSet, ValueFromPipeline = true, Position = 0, Mandatory = true)]
[ValidateNotNullOrEmpty]
Comment thread
SteveL-MSFT marked this conversation as resolved.
[System.Management.Automation.AllowNull]
Comment thread
iSazonov marked this conversation as resolved.
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
public object[] InputObject { get; set; }

Expand Down Expand Up @@ -491,19 +492,24 @@ protected override void ProcessRecord()
{
if (EffectiveParameterSet == MyParameterSet.RandomListItem)
{
foreach (object item in InputObject)
// this allows for $null to be in an array passed to InputObject
foreach (object item in InputObject ?? _nullInArray)
Comment thread
SteveL-MSFT marked this conversation as resolved.
{
if (_numberOfProcessedListItems < Count) // (3)
// (3)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the comment for?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codefactor was complaining about the trailing comment so I moved it above. If you look at the large comment above, it references (1), (2), (3) explaining the code so I left it.

if (_numberOfProcessedListItems < Count)
{
Debug.Assert(_chosenListItems.Count == _numberOfProcessedListItems, "Initial K elements should all be included in chosenListItems");
_chosenListItems.Add(item);
}
else
{
Debug.Assert(_chosenListItems.Count == Count, "After processing K initial elements, the length of chosenItems should stay equal to K");
if (Generator.Next(_numberOfProcessedListItems + 1) < Count) // (1)

// (1)
if (Generator.Next(_numberOfProcessedListItems + 1) < Count)
{
int indexToReplace = Generator.Next(_chosenListItems.Count); // (2)
// (2)
int indexToReplace = Generator.Next(_chosenListItems.Count);
_chosenListItems[indexToReplace] = item;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,16 @@ Describe "Get-Random" -Tags "CI" {
It "Should throw an error because the hexadecimal number is to large " {
{ Get-Random 0x07FFFFFFFFFFFFFFFF } | Should -Throw "Value was either too large or too small for a UInt32"
}

It "Should accept collection containing empty string for -InputObject" {
1..10 | ForEach-Object {
Get-Random -InputObject @('a','b','') | Should -BeIn 'a','b',''
}
}

It "Should accept `$null in collection for -InputObject" {
1..10 | ForEach-Object {
Get-Random -InputObject @('a','b',$null) | Should -BeIn 'a','b',$null
}
}
}