vexx32 / PSKoans

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

Issue with the AboutArrays.Koans.ps1 file #131

Closed Alektorophobia closed 5 years ago

Alektorophobia commented 5 years ago

Describe "Koan Bug, Issue, or Help Request"

I noticed that the last test 'does not throw exceptions with invalid indexes' in the Foundations\AboutArrays.Koans.ps1 file passes without any user modifications.

Context "The Problematic Assertions"

https://github.com/vexx32/PSKoans/blob/622f2739bc4d70596ffe522876dc1735d7d148e3/PSKoans/Koans/Foundations/AboutArrays.Koans.ps1#L94-L103

Both sides of the Pipeline are $null. The left side has the alias named __, and that calls the function Get-Blank that returns $null. On the right side the Should -Be values are outside of the range of the array, and that also returns $null.

Context "Your Attempts"

I had attempted to change the __ to 0, which now fails and the following is displayed when running Measure-Karma.

The answers you seek...

Expected $null, but got 0.

    Please meditate on the following code:

[It] does not throw exceptions with invalid indexes
at <ScriptBlock>, C:\Users\Username\PSKoans\Foundations\AboutArrays.Koans.ps1: line 99
99:         0 | Should -Be $Array[4]

Context "Additional Information"

I'm running the following versions: PSversion: 5.1.17763.134 pskoans: 0.42.2 pester: 4.6.0

vexx32 commented 5 years ago

Aha, that's a fun one.

🤔 I guess the best thing I can do is to make Get-Blank return a custom type that always results in $false when compared with anything. Pretty sure this is relatively doable, something like...

class Blank {
    [string] ToString() {
        return "__"
    }

    [bool] op_Equals([object] $other) {
        return $false
    }
}

Probably needs some tests of some sort in the proper implementation, but that's a start if anyone wants to PR that before I get to it. 😄