Skip to content

Improve Should -BeLike for arrays #1323

Closed

Description

@DarkLite1

It would be nice to be able to test an element in an array for matching a specific string.

For example:

$Collection = @('xxx disabled xxx', 'xxx incorrect')

Describe 'test' {
    it 'should be green' {
        $Collection | Should -BeLike '*disabled*'
    }
    it 'should be green' {
        $Collection | Should -Match '*disabled*'
    }
    it 'suggestion' {
        $Collection | Should -BeInLike '*disabled*'
    }
    it 'working workaround' {
        $Collection.where( { $_ -match 'disabled' }) | Should -BeTrue
    }
}

It would be great if this would be supported by the Should command.

Activity

  1. martin9700 commented on Aug 13, 2019

    @martin9700

    To match an element in an array you use -Contain:

    $Collection = @('xxx disabled xxx', 'xxx incorrect')
    
    Describe 'test' {
        It "Using Contain" {
            $Collection | Should -Contain "xxx disabled xxx"
        }
    }
  2. DarkLite1 commented on Aug 14, 2019

    @DarkLite1
    ContributorAuthor

    Thank you @martin9700 but -Contain looks for an exact match in the collection while the intention here is to find a partial match in the collection.

  3. martin9700 commented on Aug 14, 2019

    @martin9700

    I see now. Basically asking to have -BeLike act more like -Like. Another workaround, a little more elegant:

    it 'should be green too' {
       $Collection -like "*disabled*"
    }

    All the It function is really looking for is $true or $false so you can feed that information to it directly without using Should.

  4. DarkLite1 commented on Aug 14, 2019

    @DarkLite1
    ContributorAuthor

    True! Didn't think about that as I was in the Pester mindset. But your workaround is indeed very elegant. I'll consider using that in the meantime,. Thank you for your insights.

  5. DarkLite1 commented on Aug 14, 2019

    @DarkLite1
    ContributorAuthor

    Hmmm... After trying the following code it's returning success while it should fail:

    $Collection = @('apples', 'kiwi')
    
    Describe 'test' {
        it 'should not be green but it is' {
           $Collection -like "*disabled*"
        }
    }

    Maybe I made an error somewhere.... But otherwise, this is not a valid workaround. I'm afraid

  6. martin9700 commented on Aug 14, 2019

    @martin9700

    Bummer, that's why contains exists is to look at each element. I think your Where solution may be the only way, we're now talking about functionality that stands outside what PowerShell can do itself.

  7. DarkLite1 commented on Aug 14, 2019

    @DarkLite1
    ContributorAuthor

    Let's see if it's possible to implement my feature request in the Pester suit. Thanks anyhow for the help Maritn, much appreciated.

  8. nohwnd commented on Aug 20, 2019

    @nohwnd
    Member

    @DarkLite1 you did not make an error, test won't fail unless something throws an exception. Your expression just returns false, which the framework ignores.

    I think you are looking for the functionality of Assert-Any or Assert-All from my assert module.

  9. Veretax commented on Dec 23, 2019

    @Veretax

    I ran into a similar issue where I had a function that was returning two lines of string instead of 1. I wanted to use -BeLike with a matcher like 'msg to match' But it kept throwing an error. I found the Where method up above actually worked for this case as well

  10. DarkLite1 commented on Jan 6, 2020

    @DarkLite1
    ContributorAuthor

    @nohwnd assert-any and assert-all are not actually the same as this request. What I would like Pester to do is to match a piece of an item in an array (with wildcards for example or simply with -match.

    $Collection = @('Banana', 'Grape')

    The test would pass on $Collection -BeLike '*pe*' and would fail on $Collection -BeLike '*nnn*'.

  11. nohwnd commented on Jan 6, 2020

    @nohwnd
    Member

    @DarkLite1 I am not sure I follow what you want. In your code examples it looks like you are adding your own operators. Or should those be parameters to a function (like with the modern where syntax)? How is that different from the Assert-All / Assert-Any in Assert? That allows you to set a predicate that runs on each item right?

    It would be (writing from memory) :

    $Collection | Assert-Any { $_ -like '*nnn*' }

    And that would pass if at least one item would pass the predicate.

  12. DarkLite1 commented on Jan 6, 2020

    @DarkLite1
    ContributorAuthor

    You are correct, I was wrong about assert-any as I assumed that one item in the array needs to be equal to the requested item. Maybe -ContainsLike would've been a better suggestion for the Pester code base. It would avoid the need for using the .where method or for installing an extra module.

  13. added this to the Better Should milestone on May 23, 2021
  14. added
    Assertions
    on Jun 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Assertions

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions