vexx32 / PSKoans

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

AboutArrays Bug\Issue #358

Closed ChaseFreeman17 closed 4 years ago

ChaseFreeman17 commented 4 years ago

Describe "Koan Bug, Issue"

..\Foundations\AboutArrays.Koans.ps1

Context "The Problematic Assertions"

It 'allows you to check if something is contained within it'

Context "Your Attempts"

Here is my current block of code as it stand without modifications, but filled in.

   It 'allows you to check if something is contained within it' {
        <#
            The available methods can be seen with Get-Member:

                Get-Member -InputObject @()

            For example, each array has a Contains method.
        #>

        $true -eq $Numbers.Contains(3) | Should -BeTrue

        # The Contains method is case sensitive for arrays containing strings.

        $true -eq $Strings.Contains('first') | Should -BeTrue
        $false -eq $Strings.Contains('First') | Should -BeTrue

        # PowerShell's -contains operator is not case sensitive.

        $Strings -contains 'THIRD' | Should -BeTrue
        $Strings -contains 'SeConD' | Should -BeTrue
    }

I am not 100% sure this is a bug but this block is referring to two variables that are instantiated in the It block above 'is usually of type Object[]'. Therefore these variables are null in this block.

If you are to add the following code to the It 'allows you to check if something is contained within it' then the assertions will pass `$Numbers = 1, 2, 3, 4

$Strings = 'first', 'second', 'third'`

Finally this passes:

    It 'allows you to check if something is contained within it' {
        <#
            The available methods can be seen with Get-Member:

                Get-Member -InputObject @()

            For example, each array has a Contains method.
        #>
        $Numbers = 1, 2, 3, 4
        $Strings = 'first', 'second', 'third'
        $true -eq $Numbers.Contains(3) | Should -BeTrue

        # The Contains method is case sensitive for arrays containing strings.

        $true -eq $Strings.Contains('first') | Should -BeTrue
        $false -eq $Strings.Contains('First') | Should -BeTrue

        # PowerShell's -contains operator is not case sensitive.

        $Strings -contains 'THIRD' | Should -BeTrue
        $Strings -contains 'SeConD' | Should -BeTrue
    }

Context "Additional Information"

Pester - 4.9.0 PSVersion - 5.1.18362.628

ChaseFreeman17 commented 4 years ago

Willing to make a PR if you agree this is what needs to be done.

vexx32 commented 4 years ago

Good catch! Yep, those should absolutely have been re-defined in that It block. Probably missed it during refactoring (that whole section used to be a single big It block).

That fix looks appropriate to me. 🙂