Pester icon indicating copy to clipboard operation
Pester copied to clipboard

Suggest to add `Should-Match` to Pester v6

Open johlju opened this issue 1 year ago • 6 comments

Checklist

Summary of the feature request

Currently the new assertions for Pester v6 does not support regular expression matching like Should -Match or Should -MatchExactly in v5. Suggest adding a command Should-Match and Should-NotMatch, or possibly add a parameter RegularExpression to the commands Should-BeLikeString and Should-NotBeLikeString.

How should it work?

The commands Should-Match and Should-NotMatch should have the same named and positional parameters as the command Should-NotBeLikeString.

It 'Should match regular expression` {
    '[value]' | Should-Match '^\[.+\]$'
}

It 'Should match regular expression` {
    'Should-Match '^\[.+\]$' '[value]'
}

It 'Should match regular expression` {
    'Should-Match -Expected '^\[.+\]$' -Actual '[value]' -CaseSensitive -Because 'must start and end with brackets' 
}

It 'Should match regular expression` {
    'Should-NotMatch -Expected '^\[.+\]$' -Actual 'value' -CaseSensitive -Because 'must not start and end with brackets' 
}

If we reuse existing commands Should-BeLikeString and Should-NotBeLikeString by adding a new switch parameter.

It 'Should match regular expression` {
    '[value]' | Should-BeLikeString '^\[.+\]$' -RegularExpression
}

It 'Should match regular expression` {
    'Should-BeLikeString '^\[.+\]$' '[value]' -RegularExpression
}

It 'Should match regular expression` {
    'Should-BeLikeString -RegularExpression -Expected '^\[.+\]$' -Actual '[value]' -CaseSensitive -Because 'must start and end with brackets' 
}

It 'Should match regular expression` {
    'Should-NotBeLikeString -Expected '^\[.+\]$' -Actual 'value' -CaseSensitive -Because 'must not start and end with brackets'  -RegularExpression
}

johlju avatar Jul 08 '24 07:07 johlju

Yes. This would be Should-MatchString and Should-NotMatchString, to follow the current naming convention.

nohwnd avatar Jul 08 '24 08:07 nohwnd

I will try to do some coding this week on this, but can't promise anything.

nohwnd avatar Jul 08 '24 08:07 nohwnd

In Pester v5 I really loved the simplicity of negating a statement:

.. | Should -Invoke Get-Process
.. | Should -Not -Invoke Get-Process

I guess the -Not switch will no longer be used in the future? Really loved that feature though..

DarkLite1 avatar Jul 26 '24 08:07 DarkLite1

The v6 style is currently

# invoke is not implemented yet
.. | Should-Invoke Get-Process
.. | Should-NotInvoke Get-Process

Is that worse? As we're using standalone functions to overcome parameter set max limitation the alternative would be:

.. | Should-Invoke Get-Process
.. | Should-Invoke -Not Get-Process
.. | Should-Invoke Get-Process -Not

The parameter doesn't flow/read as well imo.

fflaten avatar Jul 26 '24 09:07 fflaten

I hear what you say, it does read easier but for splatting purposes, the separate -Not argument would be easier. I can imagine TestCases or ForEach where a decision is made to either call the mocked function or not.

Also for inteliSense it would be easier to use one CmdLet name instead of having to type Should-Not.

DarkLite1 avatar Jul 26 '24 09:07 DarkLite1

Thanks for the explanation. Let's move this discussion to a separate issue (#2550)

fflaten avatar Jul 26 '24 11:07 fflaten