Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should -HaveParameter should check DefaultParameterType #1888

Open
nohwnd opened this issue Apr 1, 2021 · 4 comments
Open

Should -HaveParameter should check DefaultParameterType #1888

nohwnd opened this issue Apr 1, 2021 · 4 comments
Labels

Comments

@nohwnd
Copy link
Member

nohwnd commented Apr 1, 2021

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 nohwnd added the Feature label Apr 1, 2021
@nohwnd
Copy link
Member Author

nohwnd commented Apr 1, 2021

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

@indented-automation
Copy link
Contributor

I'll pick this up.

@fflaten
Copy link
Collaborator

fflaten commented Aug 3, 2022

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}

@nohwnd
Copy link
Member Author

nohwnd commented Oct 3, 2022

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?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants