nohwnd / Assert

A set of advanced assertions for Pester to simplify how you write tests.
MIT License
101 stars 12 forks source link

Pass on values from the pipeline to Assert-Equivalent #51

Closed ronaldborman closed 3 months ago

ronaldborman commented 3 months ago

Most assertions seem to accept values from the pipeline (based on the examples in the Readme.md of this project).

Using Pester 5.5 and Assert 0.9.6 I tried to change the Assert-Equivalent example (which instead uses a parameter for the actual value) so that the actual value is passed from the pipeline:

Describe 'Assert-Equivalent' {
  It 'should use the input from the pipeline' {
    $expected = [PSCustomObject]@{
      Name      = 'Jakub'
      Age       = 28
      Languages = 'Czech', 'English'
    }

    $actual = [PSCustomObject]@{
      Name      = 'Jkb'
      Languages = 'Czech', 'English', 'German'
    }

    #Assert-Equivalent -Actual $actual -Expected $expected
    $actual | Assert-Equivalent -Expected $expected
  }
}

But instead of the differences in the expected and actual objects I get the following error:

Assert-Equivalent : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the par ameters that take pipeline input.

This also happens when I additionally pass the expected value as a positional parameter: $actual | Assert-Equivalent $expected

As the example passes the expected value as a named parameter, it suggests it is not possible to pass the actual value from the pipeline. If so, would this be a feature you're willing to consider (preferably with a positional parameter as the expected value)?

nohwnd commented 3 months ago

You are correct, and even though this module is now maintenance only this feature is super easy to implement so if you’d like to make a PR please go ahead. Otherwise, please check out the port of the module to Pester 6-alpha, where I’ve already fixed this issue for should-beequivalent assertion

Odesláno z aplikace Outlook pro iOShttps://aka.ms/o0ukef


Od: Ronald Borman @.> Odesláno: Saturday, June 15, 2024 12:21:41 PM Komu: nohwnd/Assert @.> Kopie: Subscribed @.***> Předmět: [nohwnd/Assert] Pass on values from the pipeline to Assert-Equivalent (Issue #51)

Most assertions seem to accept values from the pipeline (based on the examples in the Readme.md of this project).

Using Pester 5.5 and Assert 0.9.6 I tried to change the Assert-Equivalent example (which instead uses a parameter for the actual value) so that the actual value is passed from the pipeline:

Describe 'Assert-Equivalent' { It 'should use the input from the pipeline' { $expected = [PSCustomObject]@{ Name = 'Jakub' Age = 28 Languages = 'Czech', 'English' }

$actual = [PSCustomObject]@{
  Name      = 'Jkb'
  Languages = 'Czech', 'English', 'German'
}

#$actual | Assert-Equivalent -Actual $actual -Expected $expected
$actual | Assert-Equivalent -Expected $expected

} }

But instead of the differences in the expected and actual objects I get the following error:

Assert-Equivalent : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the par ameters that take pipeline input.

This also happens when I additionally pass the expected value as a positional parameter: $actual | Assert-Equivalent $expected

As the example passes the expected value as a named parameter, it suggests it is not possible to pass the actual value from the pipeline. If so, would this be a feature you're willing to consider (preferably with a positional parameter as the expected value)?

— Reply to this email directly, view it on GitHubhttps://github.com/nohwnd/Assert/issues/51 or unsubscribehttps://github.com/notifications/unsubscribe-auth/ABLYLYORSNKEOHOFZVTSSHLZHQITNBFKMF2HI4TJMJ2XIZLTSOBKK5TBNR2WLJDUOJ2WLJDOMFWWLO3UNBZGKYLEL5YGC4TUNFRWS4DBNZ2F6YLDORUXM2LUPGBKK5TBNR2WLJLJONZXKZNENZQW2ZNLORUHEZLBMRPXI6LQMWBKK5TBNR2WLJDUOJ2WLJDOMFWWLLTXMF2GG2C7MFRXI2LWNF2HTLDTOVRGUZLDORPXI6LQMWSUS43TOVS2M5DPOBUWG44SQKSHI6LQMWVHEZLQN5ZWS5DPOJ42K5TBNR2WLKBXHEYDEMZRHA4IFJDUPFYGLJLJONZXKZNFOZQWY5LFVIZDGNJUG4ZDINJWGGTXI4TJM5TWK4VGMNZGKYLUMU. You are receiving this email because you are subscribed to this thread.

Triage notifications on the go with GitHub Mobile for iOShttps://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Androidhttps://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

ronaldborman commented 3 months ago

Although I'm more of an occasional user of PowerShell and don't know all the ins and outs (this would actually be my first PowerShell related PR) I will give it a try....

Looking at the source code of this repository and the should-beequivalent assertion in the Pester repository, will it be enough to change the parameter declaration of the Assert-Equivalent function to the following?

    [CmdletBinding()]
    param(
        [Parameter(Position = 1, ValueFromPipeline = $true)]
        $Actual,
        [Parameter(Position = 0, Mandatory)]
        $Expected,
        $Options = (Get-EquivalencyOption),
        [Switch] $StrictOrder
   )

And then add a couple of tests to Assert-Equivalent.Tests.ps1?

nohwnd commented 3 months ago

Yeah pretty much, I am not sure right now how we collect the stuff from pipeline and if the equivalency function is using the standard unified way, but shoot me a PR and I can help you fix it.

And dont worry, we all had to start somewhere :)


Od: Ronald Borman @.> Odesláno: Saturday, June 15, 2024 1:29:31 PM Komu: nohwnd/Assert @.> Kopie: Comment @.>; Subscribed @.> Předmět: Re: [nohwnd/Assert] Pass on values from the pipeline to Assert-Equivalent (Issue #51)

Although I'm more of an occasional user of PowerShell and don't know all the ins and outs (this would actually be my first PowerShell related PR) I will give it a try....

Looking at the source code of this repository and the should-beequivalent assertion in the Pester repository, will it be enough to change the parameter declaration of the Assert-Equivalent function to the following?

[CmdletBinding()]
param(
    [Parameter(Position = 1, ValueFromPipeline = $true)]
    $Actual,
    [Parameter(Position = 0, Mandatory)]
    $Expected,
    $Options = (Get-EquivalencyOption),
    [Switch] $StrictOrder

)

And then add a couple of tests to Assert-Equivalent.Tests.ps1?

— Reply to this email directly, view it on GitHubhttps://github.com/nohwnd/Assert/issues/51#issuecomment-2169373557 or unsubscribehttps://github.com/notifications/unsubscribe-auth/ABLYLYJXZ3SFDU5P4Z7YTELZHQQRZBFKMF2HI4TJMJ2XIZLTSOBKK5TBNR2WLJDUOJ2WLJDOMFWWLO3UNBZGKYLEL5YGC4TUNFRWS4DBNZ2F6YLDORUXM2LUPGBKK5TBNR2WLJDUOJ2WLJDOMFWWLLTXMF2GG2C7MFRXI2LWNF2HTAVFOZQWY5LFUVUXG43VMWSG4YLNMWVXI2DSMVQWIX3UPFYGLLDTOVRGUZLDORPXI6LQMWWES43TOVSUG33NNVSW45FGORXXA2LDOOJIFJDUPFYGLKTSMVYG643JORXXE6NFOZQWY5LFVA3TSMBSGMYTQOECUR2HS4DFUVUXG43VMWSXMYLMOVS2UMRTGU2DOMRUGU3DDJ3UOJUWOZ3FOKTGG4TFMF2GK. You are receiving this email because you commented on the thread.

Triage notifications on the go with GitHub Mobile for iOShttps://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Androidhttps://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

ronaldborman commented 3 months ago

I've made the PR, but the AppVeyor build seems to have failed. Not sure if I did something wrong.

nohwnd commented 3 months ago

You did not, the build upgraded to Pester 6-alpha1 where I used the same names for assertions and so the tests were testing that rather than the assertions coming from Assert.

ronaldborman commented 3 months ago

PR merged, I will close this issue. Thanks for the learning opportunity.

nohwnd commented 3 months ago

Thanks for you contribution :)

nohwnd commented 3 months ago

Your further contributions would be welcome on Pester repo :)