Skip to content
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

AboutNumbers wrong types #474

Open
magoerlich opened this issue Dec 21, 2022 · 2 comments
Open

AboutNumbers wrong types #474

magoerlich opened this issue Dec 21, 2022 · 2 comments

Comments

@magoerlich
Copy link

$BigValue | Should -BeOfType [long]
$BigValue | Should -BeGreaterThan $MaxValue
$SmallValue = __
$SmallValue | Should -BeOfType [long]
$SmallValue | Should -BeLessThan $MinValue

Is this part correct about numbers getting converted to longs?

Because no matter, what I try for me it's always a double

Code:

$BigValue = [int]::MaxValue + 200
$BigValue | Should -BeOfType [long]
$BigValue | Should -BeGreaterThan $MaxValue

Output:

Expected the value to have type [long] or any of its subtypes, but got 2147483847 with type [double].

Starting with a short and increasing it to >MaxValue behaves the same:

~\PSKoans
❯ $int = [int]::MaxValue

~\PSKoans
❯ $int.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Int32                                    System.ValueType


~\PSKoans
❯ $int += 100

~\PSKoans
❯ $int.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Double                                   System.ValueType
@jrwarwick
Copy link

Although your observation is correct, there is some obfuscation involved. Essentially "Long" is an alias for the underlying actual datatype.
Refer to this page on "Numeric Type Accelerators": https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_numeric_literals?view=powershell-7.3#numeric-type-accelerators
I would expect the -BeOfType parameter to handle this, and indeed it seems to on my install of PSKoans.

@indented-automation
Copy link
Contributor

indented-automation commented Mar 2, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants