From 47a2875caf5a926207f0ada7b876fa2b94a06052 Mon Sep 17 00:00:00 2001 From: Gabriel Adrian Samfira Date: Mon, 16 Dec 2024 15:18:03 +0200 Subject: [PATCH] Fix broken if comparison PowerShell considers the integer 0 as a false value when used in an if statement. We need to explicitly compare the result to $null. Signed-off-by: Gabriel Adrian Samfira --- Tests/powershell-yaml.Tests.ps1 | 8 ++++++++ powershell-yaml.psm1 | 4 ++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Tests/powershell-yaml.Tests.ps1 b/Tests/powershell-yaml.Tests.ps1 index 9682382..631c940 100644 --- a/Tests/powershell-yaml.Tests.ps1 +++ b/Tests/powershell-yaml.Tests.ps1 @@ -254,6 +254,8 @@ wishlist: scientificNotationInt: 1e+3 scientificNotationBigInt: 1e+40 intWithTag: !!int "42" + zeroIntWithTag: !!int "0" + zeroIntWithoutTag: 0 scientificNotationIntWithTag: !!int "1e+3" price : 55.34 total: 4443.52 @@ -302,6 +304,8 @@ bools: scientificNotationInt = [int32]1000 scientificNotationBigInt = [System.Numerics.BigInteger]::Parse("10000000000000000000000000000000000000000") intWithTag = 42 + zeroIntWithTag = 0 + zeroIntWithoutTag = 0 scientificNotationIntWithTag = 1000 } ); @@ -351,6 +355,10 @@ bools: $product['aStringTatLooksLikeAFloat'] | Should -BeOfType ([string]) $product['aStringThatLooksLikeAnInt'] | Should -Be $expectedProduct['aStringThatLooksLikeAnInt'] $product['aStringThatLooksLikeAnInt'] | Should -BeOfType ([string]) + $product['zeroIntWithTag'] | Should -Be $expectedProduct['zeroIntWithTag'] + $product['zeroIntWithTag'] | Should -BeOfType ([int32]) + $product['zeroIntWithoutTag'] | Should -Be $expectedProduct['zeroIntWithoutTag'] + $product['zeroIntWithoutTag'] | Should -BeOfType ([int32]) $product['scientificNotationInt'] | Should -Be $expectedProduct['scientificNotationInt'] $product['scientificNotationInt'] | Should -BeOfType ([int32]) $product['scientificNotationBigInt'] | Should -Be $expectedProduct['scientificNotationBigInt'] diff --git a/powershell-yaml.psm1 b/powershell-yaml.psm1 index 0dabc31..fc2e9e1 100644 --- a/powershell-yaml.psm1 +++ b/powershell-yaml.psm1 @@ -161,7 +161,7 @@ function Convert-ValueToProperType { } foreach ($i in $intTypes) { $asIntType = $parsedValue -as $i - if($asIntType) { + if($null -ne $asIntType) { return $asIntType } } @@ -212,7 +212,7 @@ function Convert-ValueToProperType { $types = @([int], [long]) foreach($i in $types){ $asType = $parsedValue -as $i - if($asType) { + if($null -ne $asType) { return $asType } }