EliziumNet / Loopz

PowerShell iteration utilities with additional tools like the Parameter Set Tools
https://eliziumnet.github.io/Loopz/
MIT License
3 stars 0 forks source link

Get-PsObjectField doesnt work properly for boolean fields #113

Closed plastikfan closed 3 years ago

plastikfan commented 3 years ago

This has created a hidden bug inside Format-StructuredLine causing the following test failures:

When an initial attempt to fix the issue by setting $withLead:

[boolean]$withLead = $Options.psobject.properties.match('WithLead') ? $Options.WithLead : $false;

instead of using Get-PsObjectField

[πŸš€] ===================================================================================== [ Children of the Damned ] ===
.........................................................................................................................
[πŸš€] ====== [ The Number of the Beast (No Truncation) The Number of the Beast (No Truncation) The Number of the Beast (No Truncation) ! ] ===
.........................................................................................................................
[πŸš€]  [ Hallowed by thy Name Hallowed by thy Name Hallowed by thy Name Hallowed by thy Name Hallowed by thy Name H *** ]
[-] Format-StructuredLine.given: Message and Crumb.and: Large message.and: Truncate.and: Custom Ellipses.should: Create coloured line with Truncated message 14ms (14ms|1ms)
 Expected strings to be the same, but they were different.
 String lengths are both 181.
 Strings differ at index 24.
 Expected: '...[πŸš€] =====...'
 But was:  '...[πŸš€]  [ &[...'
 at $_structuredLine | Should -BeExactly $expected;, C:\Users\Plastikfan\dev\github\PoSh\Loopz\Elizium.Loopz\Tests\Public\ui\Format-StructuredLine.tests.ps1:119
 at <ScriptBlock>, C:\Users\Plastikfan\dev\github\PoSh\Loopz\Elizium.Loopz\Tests\Public\ui\Format-StructuredLine.tests.ps1:119
.........................................................................................................................
[πŸš€] ============ [ Hallowed by thy Name Hallowed by thy Name Hallowed by thy Name Hallowed by thy Name Hallowed by th ]
[-] Format-StructuredLine.given: Message and Crumb.and: Large message.and: Truncate.and: Custom MinimumFlexSize.should: Create coloured line with Truncated message 13ms (13ms|1ms)
 Expected strings to be the same, but they were different.
 String lengths are both 181.
 Strings differ at index 152.
 Expected: '...llowe ...&...'
 But was:  '...llowed by ...'
 at $_structuredLine | Should -BeExactly $expected;, C:\Users\Plastikfan\dev\github\PoSh\Loopz\Elizium.Loopz\Tests\Public\ui\Format-StructuredLine.tests.ps1:135
 at <ScriptBlock>, C:\Users\Plastikfan\dev\github\PoSh\Loopz\Elizium.Loopz\Tests\Public\ui\Format-StructuredLine.tests.ps1:135
.........................................................................................................................
[πŸš€] === [ Hallowed by thy Name Hallowed by thy Name Hallowed by thy Name Hallowed by thy Name Hallowed by thy Name Ha ]
[-] Format-StructuredLine.given: Message and Crumb.and: Large message.and: Truncate.and: Custom MinimumFlexSize.should: Create coloured line with Truncated message 13ms (12ms|1ms)
 Expected strings to be the same, but they were different.
 String lengths are both 181.
 Strings differ at index 152.
 Expected: '... thy  ...&...'
 But was:  '... thy Name ...'
 at $_structuredLine | Should -BeExactly $expected;, C:\Users\Plastikfan\dev\github\PoSh\Loopz\Elizium.Loopz\Tests\Public\ui\Format-StructuredLine.tests.ps1:149
 at <ScriptBlock>, C:\Users\Plastikfan\dev\github\PoSh\Loopz\Elizium.Loopz\Tests\Public\ui\Format-StructuredLine.tests.ps1:149
.........................................................................................................................
[πŸš€] ______ [ Hallowed by thy Fame Hallowed by thy Fame Hallowed by thy ... ] ___
.........................................................................................................................
================================================================================================ [ 22 Acacia Avenue ] ===
.........................................................................................................................
====== [ Stranger in a Strange Land (No Truncation) Stranger in a Strange Land (No Truncation) Stranger in a Strange Land (No Truncation) ! ] ===
.................................................................................
====== [ Heaven Can Wait Heaven Can Wait Heaven Can Wait Heaven Can Wai ... ] ===
.................................................................................
=== [ Heaven Can Wait Heaven Can Wait Heaven Can Wait Heaven Can Wait Heaven C ]
[-] Format-StructuredLine.given: Message Only.and: Truncate.and: Custom MinimumFlexSize.should: Create coloured line with Truncated message 12ms (12ms|1ms)
 Expected strings to be the same, but they were different.
 String lengths are both 141.
 Strings differ at index 112.
 Expected: '...ait H ...&...'
 But was:  '...ait Heaven...'
 at $_structuredLine | Should -BeExactly $expected;, C:\Users\Plastikfan\dev\github\PoSh\Loopz\Elizium.Loopz\Tests\Public\ui\Format-StructuredLine.tests.ps1:257
 at <ScriptBlock>, C:\Users\Plastikfan\dev\github\PoSh\Loopz\Elizium.Loopz\Tests\Public\ui\Format-StructuredLine.tests.ps1:257
.................................................................................
========= [ Heaven Can Wait Heaven Can Wait Heaven Can Wait Heaven Can Wait He ]
[-] Format-StructuredLine.given: Message Only.and: Truncate.and: Custom MinimumFlexSize.should: Create coloured line with Truncated message 12ms (12ms|1ms)
 Expected strings to be the same, but they were different.
 String lengths are both 141.
 Strings differ at index 112.
 Expected: '... Can  ...&...'
 But was:  '... Can Wait ...'
 at $_structuredLine | Should -BeExactly $expected;, C:\Users\Plastikfan\dev\github\PoSh\Loopz\Elizium.Loopz\Tests\Public\ui\Format-StructuredLine.tests.ps1:272
 at <ScriptBlock>, C:\Users\Plastikfan\dev\github\PoSh\Loopz\Elizium.Loopz\Tests\Public\ui\Format-StructuredLine.tests.ps1:272
.................................................................................
______ [ Heaven Can Bait Heaven Can Bait Heaven Can Bait Heaven Can Bai ... ] ___
.........................................................................................................................
[πŸ”₯] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
plastikfan commented 3 years ago

Some temp test code:


    Context 'test psobject props' {
      It '*** does it work?' -Skip {
        [PSCustomObject]$testObject = [PSCustomObject]@{
          Activate    = $false;
        }

        [string]$field = 'Activate'
        if ($testObject.psobject.properties.match($field)) {
          Write-Host "Activate is present: $($testObject.$field)";
        }
        else {
          Write-Host "Activate is NOT present";
        }

        # $Object.psobject.properties.match($Field)

        if ($testObject.psobject.properties.match($field)) {
          Write-Host "Activate is present: $($testObject.$field)";
        }
        else {
          Write-Host "Activate is NOT present";
        }

        # if (Get-PsObjectField -Object $testObject -Field $field) {
        #   Write-Host "Activate is present: $($testObject.$field)";
        # }
        # else {
        #   # false negative
        #   Write-Host "Activate is NOT present";
        # }

        # [string]$field = 'Activate'
        # if ($testObject.psobject.properties.match('Activate').Count) {
        #   Write-Host "$field is present: $($testObject.$field)";
        # }
        # else {
        #   Write-Host "$field is NOT present";
        # }
      }
    }
plastikfan commented 3 years ago

Maybe this is caused by the fact that you need to use the Count property

$object.psobject.properties.match('WithLead').Count

plastikfan commented 3 years ago

Had to fix a bug in Rename-Many (from working on bootstrap, issue #112), where Except was mistyped as Exclude

plastikfan commented 3 years ago

commit 9d23c9ec6b563d4f1019a6bddb4bce2b8ab7d3dd, was checked in without the corresponding issue number inserted