vexx32 / PSKoans

A simple, fun, and interactive way to learn the PowerShell language through Pester unit testing.
GNU General Public License v3.0
1.73k stars 176 forks source link

AboutNumbers wrong types #474

Open magoerlich opened 1 year ago

magoerlich commented 1 year ago

https://github.com/vexx32/PSKoans/blob/1317701a7d2bf6ca7efff0eb21d3a3d0478740e9/PSKoans/Koans/Introduction/AboutNumbers.Koans.ps1#L94-L99

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 commented 1 year ago

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 commented 1 year ago

It's a legitimate issue.

 ([int]::MaxValue + 200) -is [long]

is basically what Pester is doing. Both win PS and pwsh cause addition to yield a Double. Please may I ask what version of PS you're testing in Justin? And if you get True for the expression above (I get False because it does indeed yield a Double).

On Thu, 2 Mar 2023 at 15:47, Justin Warwick @.***> wrote:

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.

— Reply to this email directly, view it on GitHub https://github.com/vexx32/PSKoans/issues/474#issuecomment-1452087016, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADB3UDVB7MMYMNZQGK6FCA3W2C6JRANCNFSM6AAAAAATFZOL2M . You are receiving this because you are subscribed to this thread.Message ID: @.***>