-
-
Notifications
You must be signed in to change notification settings - Fork 476
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
Add Should-HaveParameter
, to replace Should -HaveParameter
#2618
Comments
Should-HaveParameter
to Pester 6 assertions to replace Should -HaveParameter
Should-HaveParameter
, to replace Should -HaveParameter
Yup. Thanks!
…-j
________________________________
Od: Johan Ljunggren ***@***.***>
Odesláno: Saturday, February 8, 2025 8:18:21 AM
Komu: pester/Pester ***@***.***>
Kopie: Subscribed ***@***.***>
Předmět: [pester/Pester] Add `Should-HaveParameter`, to replace `Should -HaveParameter` (Issue #2618)
Checklist
* Feature request has a meaningful title
* I have searched the existing issues. See all issues<https://github.com/pester/Pester/issues?q=is%3Aissue>
* I have tested using the latest version of Pester. See Installation and update guide<https://pester.dev/docs/introduction/installation>.
Summary of the feature request
Suggestions is to add Should-HaveParameter to Pester 6 assertions to replace Should -HaveParameter.
It was mentioned in the issue title<#2465> but there are no other mentions of it, so I thought we add a new issue to track it or discuss if it should be or not be.
How should it work?
No response
—
Reply to this email directly, view it on GitHub<#2618> or unsubscribe<https://github.com/notifications/unsubscribe-auth/ABLYLYIQSRSB6GAYWFYILND2OWVT3BFKMF2HI4TJMJ2XIZLTSSBKK5TBNR2WLJDUOJ2WLJDOMFWWLO3UNBZGKYLEL5YGC4TUNFRWS4DBNZ2F6YLDORUXM2LUPGBKK5TBNR2WLJLJONZXKZNENZQW2ZNLORUHEZLBMRPXI6LQMWBKK5TBNR2WLKJSGAYTQNRTGU4TPJDOMFWWLKLIMFZV63DBMJSWZAVFOZQWY5LFUR2HE5LFURXGC3LFVZ3WC5DDNBPWCY3UNF3GS5DZVRZXKYTKMVRXIX3UPFYGLJKJONZXKZNGORXXA2LDOOJYFJDUPFYGLKTSMVYG643JORXXE6NFOZQWY5LFU4YTEMJTGQ3TJAVEOR4XAZNFNFZXG5LFUV3GC3DVMWVDEOBTHE3DMNRQGEYYFJDUPFYGLJLMMFRGK3FFOZQWY5LFVEZDAMJYGYZTKOJXU52HE2LHM5SXFJTDOJSWC5DF>.
You are receiving this email because you are subscribed to this thread.
Triage notifications on the go with GitHub Mobile for iOS<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675> or Android<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
|
I'm not sure if this has already been talked about but I can see an argument for a general "Should-Have" assert/command vs. granular named asserts/commands. |
Thanks for the feedback!
Could you provide a few usecase examples? Only parameters have default values IIRC, so it would likely be We currently don't have a generic attribute assertion, so a separate |
Adding a reminder to |
@fflaten I know this is not where this should be, but if you tell me where I should put all of this, I can do that. thank you for fixing the code formatting. Actually, after spending time baking the idea, I think having a general Should-Have is not a good idea at all but being more explicit is the correct route. Here are the results from baking the idea longer: Should-HaveInitValue / Should-HaveDefault function Get-MyGreeting {
[CmdletBinding()]
[OutputType([string])]
param (
[Parameter(Mandatory, Position = 0, ValueFromPipeline, ValueFromPipelineByPropertyName, HelpMessage = 'Enter in the first name of the person you want a greeting for.')]
[ValidateNotNullOrEmpty()]
[string]
$FirstName,
[Parameter(Position = 1, ValueFromPipelineByPropertyName)]
[AllowEmptyString()]
[ValidateNotNull()]
[string]
$LastName = ([string]::Empty),
[Parameter(ValueFromPipelineByPropertyName, DontShow)]
[string]
$Greeting = 'Hello'
)
begin {
[string]$GreetingFormat = "{0} {1}$( ([string]::IsNullOrEmpty($LastName)) ? ([string]::Empty) : (', ')){2}"
[string]$Msg = ([string]::Empty)
}
process {
if (-not [string]::IsNullOrEmpty($LastName)) {
$Msg | Write-Output
}
$Msg = $GreetingFormat -f $Greeting, $LastName, $FirstName
$Msg = $Msg.Trim()
}
end {
$Msg | Write-Output
}
clean {
Remove-Variable -Name 'GreetingFormat', 'Msg' -Scope 'Local'
}
} Test file: Describe "Get-MyGreeting" {
Context "Inital setup of function state" {
It "Should have initial value for internal GreetingFormat as expected" {
Get-Command "Get-MyGreeting" | Should-HaveInitValue -Name 'GreetingFormt' -Type ([system.string]) -Pattern "{0} {1}$( ([string]::IsNullOrEmpty($LastName)) ? ([string]::Empty) : (', ')){2}" -Value " " -StringOnly
}
It "Should have a parameter named Greeting that contains the expected value" {
Get-Command "Get-MyGreeting" | Should-HaveInitValue -Name 'Greeting' -Type ([system.string]) -Value 'Hello'
}
} # end context "Initial setup of function state"
} # end describe "Get-MyGreeting" Should-HaveAttribute Describe "Get-MyGreeting" {
Context "Advance script function parameters" {
It "Should have ValidateNotNullOrEmptyAttribute applied to parameter 'Foo'" {
(Get-Command Get-MyGreeting).Parameters['FirstName'] | Should-HaveAttribute ([System.Management.Automation.ValidateNotNullOrEmptyAttribute])
}
It "Should have a mandatory pipeline enabled parameter 'foo'" {
(Get-Command Get-MyGreeting).Parameters['FirstName'] | Should-HaveAttribute ([System.Management.Automation.ParameterAttribute]) -Value @{Mandatory=$true; ValueFromPipeline=$true; ValueFromPipelineByPropertyName=$true;}
}
} # end context "Advance script function parameters"
Context "Output" {
It "Should return a greeting of type string" {
Get-MyGreeting -FirstName 'John' | Should-Be ([string])
}
It "Should return a greeting with a foobar attribute" {
Get-MyGreeting -FirstName 'John' | Should-HaveAttribute -Not [FoobarAttribute] @{Status='SNAFU'}
}
} # end context "Output"
} # end describe "Get-MyGreeting" Should-HaveMember Describe "Get-InterfaceObject" {
Context "Output" {
It "Should return an interface object" {
Get-InterfaceObject -ID 'Foo' | Should-Be -Type ([system.object])
}
It "Should have 10 properties" {
Get-InterfaceObject -ID 'Foo' | Should-HaveMember -MemberType 'Property' -Name '*' -Count 10
}
It "Should have a read only property named GUID of type GUID" {
Get-InterfaceObject -ID 'Foo' | Should-HaveMember -MemberType 'Property' -Name 'GUID' -ReturnType ([System.Guid]) -ReadOnly
}
It "Should have an expected value for the GUID on the 'Foo' interface" {
(Get-InterfaceObject -ID 'Foo').GUID | Should-Be '00000000-0000-0000-0000-000000000000'
}
It "Should have a Port([int], [int]) property" {
Get-InterfaceObject -ID 'Foo' | Should-HaveMember -MemberType 'Property' -Name 'Port' -PatameterTypes @( ([System.Int32]), ([System.Int32]) )
}
It "Should have 2 Send methods (SendNumber, SendText) and both return boolean values" {
Get-InterfaceObject -ID 'Foo' | Should-HaveMember -MemberType 'Method' -Name 'Send*' -Count 2 -ReturnType ([System.Boolean]) -Because "need a way to send numbers and text (converted to ascii codes internally) out the interface and have a return of true/false value to determine success"
}
It "Should have a write only property named OutBuffer of type Object" {
Get-InterfaceObject -ID 'Foo' | Should-HaveMember -MemberType 'Property' -Name 'OutBuffer' -Count 1 -ReturnType ([system.object]) -WriteOnly
}
} # end context "Output"
} # end describe "Get-InterfaceObject" |
Reading the comment above I have hard time understanding what the proposal actually is. I think keeping the assertions specific to one area is important, rather than having Should-Have that can act both on paramters and members. (And you seem to agree above). I am not opposed to specific Should-HaveMember assertion, but it seems quite easy to replace with separate calls to Should-Be, especially if we made Should-Be more strict by adding a switch to e.g. force the actual and expected to have the same type. Then you'd be able to check e.g. $myObject.GUID | Should-Be ([System.Guid]"00000000-0000-0000-0000-000000000000") -EnsureSameType And having a dedicated Should-HaveMember assertion falls apart if we would want to encode all the different value checks into it. e.g. checking if the value is a string, that starts with a given string. In that case we are better served by dedicated Should-BeLikeString. But I do still see some value in it, to check if members are present. Please make a new issue with this request if you'd like. |
Thank you @nohwnd, I agree 100% and I do realize that one could do the following for a 'member exists' assertion, which is probably how it should be left: (-Not $null -eq ($Contact | Get-Member 'Name')) | Should-Be $true Sorry about the confusion. To clear up confusion, it really was just a few things that I came up with while trying to make a concrete argument for a generalized Should-Have. After which I found that I couldn't argue for it but in the process found the listed commands candidates for possible value. I posted them here as I initially didn't know if @fflaten still wanted to "see" what I was thinking. I soon realized that it just made it more confusing and muddied the waters. Though I can only argue somewhat for a Should-HaveAttribute command, the others I have already realized are of no value that I could argue. Feel free to remove any comments to clean up the issue tracker. |
Brainstorming here is perfectly fine, thanks for your ideas. :) |
Checklist
Summary of the feature request
Suggestions is to add
Should-HaveParameter
to Pester 6 assertions to replaceShould -HaveParameter
.It was mentioned in the issue title but there are no other mentions of it, so I thought we add a new issue to track it or discuss if it should be or not be.
How should it work?
No response
The text was updated successfully, but these errors were encountered: