Pester icon indicating copy to clipboard operation
Pester copied to clipboard

Should -HaveParameter should check DefaultParameterType

Open nohwnd opened this issue 4 years ago • 4 comments

Default parameter type is figured out in the code, but parameter for it is not surfaced to Should which is unfortunate when you check $Path = (Get-DefaultPath) because our assertion will pass for both $Path = (Get-DefaultPath) and $Path = "(Get-DefaultPath)".

But if the DefaultValueType was surfaced then we could say the the value should be Expression, not a string, and we would be able to distinguish those two.

nohwnd avatar Apr 01 '21 20:04 nohwnd

@lipkau ^^^ would you consider PRing it? 🙂

nohwnd avatar Apr 01 '21 20:04 nohwnd

I'll pick this up.

indented-automation avatar Jun 08 '21 18:06 indented-automation

To clarify, do we want to require -DefaultValue <scriptblock> when the DefaultValueType is Expression? Like this?

Describe 'd' {
    It 'i' {
        function myDefault { 'my\default' }
        function myFunc {
            param(
                [string]$MyParam = '(myDefault)'
            )
        }

        # This should work
        Get-Command myFunc | Should -HaveParameter MyParam -DefaultValue {(myDefault)}

        # This shouldn't
        Get-Command myFunc | Should -HaveParameter MyParam -DefaultValue '(myDefault)'
    }
}

We still end up comparing them as strings, so it would mostly be for the optics right? If so, is it worth the breaking change? Am I missing something?

Would likely have to trim the scriptblock-text since formatter in VSCode likes to add whitespace between curly-braces and content in {content}

fflaten avatar Aug 03 '22 20:08 fflaten

I think I just meant that we will always compare them as strings, but we will have additional -DefaultValueType parameter which can would be the actual type of the default value:

$Path = (Get-DefaultPath) == -DefaultValue "(Get-DefaultPath)" and -DefaultValueType ScriptBlock $Path = (Get-DefaultPath) != -DefaultValue "(Get-DefaultPath)" and -DefaultValueType String

(or whatever is the name of the type on the default value, is that Expression?)

nohwnd avatar Oct 03 '22 08:10 nohwnd