-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Implement Get-Random -Count without specifying an InputObject list #9111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -59,14 +59,29 @@ Describe "Get-Random DRT Unit Tests" -Tags "CI" { | |
| ) | ||
|
|
||
| # minimum is always set to the actual low end of the range, details refer to closed issue #887. | ||
| It "get a correct random number for '<Name>'" -TestCases $testData { | ||
| It "Should return a correct random number for '<Name>'" -TestCases $testData { | ||
| param($maximum, $minimum, $greaterThan, $lessThan, $type) | ||
|
|
||
| $result = Get-Random -Maximum $maximum -Minimum $minimum | ||
| $result | Should -BeGreaterThan $greaterThan | ||
| $result | Should -BeLessThan $lessThan | ||
| $result | Should -BeOfType $type | ||
| } | ||
|
|
||
| It "Should return correct random numbers for '<Name>' with Count specified" -TestCases $testData { | ||
| param($maximum, $minimum, $greaterThan, $lessThan, $type) | ||
|
|
||
| $result = Get-Random -Maximum $maximum -Minimum $minimum -Count 1 | ||
| $result | Should -BeGreaterThan $greaterThan | ||
| $result | Should -BeLessThan $lessThan | ||
| $result | Should -BeOfType $type | ||
|
|
||
| $result = Get-Random -Maximum $maximum -Minimum $minimum -Count 3 | ||
iSazonov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| foreach ($randomNumber in $result) { | ||
| $randomNumber | Should -BeGreaterThan $greaterThan | ||
| $randomNumber | Should -BeLessThan $lessThan | ||
| $randomNumber | Should -BeOfType $type | ||
| } | ||
| } | ||
|
|
||
| It "Should be able to throw error when '<Name>'" -TestCases $testDataForError { | ||
|
|
@@ -111,7 +126,7 @@ Describe "Get-Random" -Tags "CI" { | |
|
|
||
| It "Should return a number from 1,2,3,5,8,13 " { | ||
| $randomNumber = Get-Random -InputObject 1, 2, 3, 5, 8, 13 | ||
| $randomNumber | Should -Be (1 -or 2 -or 3 -or 5 -or 8 -or 13) | ||
| $randomNumber | Should -BeIn 1, 2, 3, 5, 8, 13 | ||
| } | ||
|
|
||
| It "Should return an array " { | ||
|
|
@@ -123,21 +138,21 @@ Describe "Get-Random" -Tags "CI" { | |
| It "Should return three random numbers for array of 1,2,3,5,8,13 " { | ||
| $randomNumber = Get-Random -InputObject 1, 2, 3, 5, 8, 13 -Count 3 | ||
| $randomNumber.Count | Should -Be 3 | ||
| $randomNumber[0] | Should -Be (1 -or 2 -or 3 -or 5 -or 8 -or 13) | ||
| $randomNumber[1] | Should -Be (1 -or 2 -or 3 -or 5 -or 8 -or 13) | ||
| $randomNumber[2] | Should -Be (1 -or 2 -or 3 -or 5 -or 8 -or 13) | ||
| $randomNumber[0] | Should -BeIn 1, 2, 3, 5, 8, 13 | ||
| $randomNumber[1] | Should -BeIn 1, 2, 3, 5, 8, 13 | ||
| $randomNumber[2] | Should -BeIn 1, 2, 3, 5, 8, 13 | ||
| $randomNumber[3] | Should -BeNullOrEmpty | ||
| } | ||
|
|
||
| It "Should return all the numbers for array of 1,2,3,5,8,13 in no particular order" { | ||
| $randomNumber = Get-Random -InputObject 1, 2, 3, 5, 8, 13 -Count ([int]::MaxValue) | ||
| $randomNumber.Count | Should -Be 6 | ||
| $randomNumber[0] | Should -Be (1 -or 2 -or 3 -or 5 -or 8 -or 13) | ||
| $randomNumber[1] | Should -Be (1 -or 2 -or 3 -or 5 -or 8 -or 13) | ||
| $randomNumber[2] | Should -Be (1 -or 2 -or 3 -or 5 -or 8 -or 13) | ||
| $randomNumber[3] | Should -Be (1 -or 2 -or 3 -or 5 -or 8 -or 13) | ||
| $randomNumber[4] | Should -Be (1 -or 2 -or 3 -or 5 -or 8 -or 13) | ||
| $randomNumber[5] | Should -Be (1 -or 2 -or 3 -or 5 -or 8 -or 13) | ||
| $randomNumber[0] | Should -BeIn 1, 2, 3, 5, 8, 13 | ||
| $randomNumber[1] | Should -BeIn 1, 2, 3, 5, 8, 13 | ||
| $randomNumber[2] | Should -BeIn 1, 2, 3, 5, 8, 13 | ||
| $randomNumber[3] | Should -BeIn 1, 2, 3, 5, 8, 13 | ||
| $randomNumber[4] | Should -BeIn 1, 2, 3, 5, 8, 13 | ||
| $randomNumber[5] | Should -BeIn 1, 2, 3, 5, 8, 13 | ||
| $randomNumber[6] | Should -BeNullOrEmpty | ||
| } | ||
|
|
||
|
|
@@ -163,6 +178,7 @@ Describe "Get-Random" -Tags "CI" { | |
| $secondRandomNumber = Get-Random 34359738367 -SetSeed 20 | ||
| $firstRandomNumber | Should -Be @secondRandomNumber | ||
| } | ||
|
|
||
| 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" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I haven't dug deeper, but the full error message is self-contradictory (Int32 vs. UInt32):
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I actually fixed this back when I was toying with the tokenizer, and then it was requested I re-break this for consistency with prior implementations. This can be fixed, but the fix would be in the tokenizer and how hex numbers get parsed. This number is an extra digit beyond the widest permitted for uint64, and a hex number with a leading zero is forced to be an unsigned numeral when using the conversion paths we have in the tokenizer (which are still done with I remember I originally did some fun things in my tokenizer PRs where I would automatically upcast between uint/int64 if needed, but it differed too much from the previous implementation I think and we decided to keep the behaviour consistent. I think my solution was to have it upcast up to decimal (or even biginteger) if needed, which worked well. I think due to the refactoring in #7993 this error message might actually change, but it's been a little while and I don't quite recall.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see: if the pre-parameter-binding typing can't convert a "number-looking" argument to an actual number, it passes a string, and line Again, not in the scope of this PR, but I wonder if the error message could be made friendlier (and it's still
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops, @vexx32 - wires crossed - reading your comment now. |
||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking asserts above it seems it should be BeLessOrEqual in all tests. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with you. I've made the modifications.