pester / Pester

Pester is the ubiquitous test and mock framework for PowerShell.
https://pester.dev/
Other
3.11k stars 473 forks source link

Suggestion: Provide PowerShell operator syntax (Obsolete Should?) #334

Closed Jaykul closed 5 years ago

Jaykul commented 9 years ago

I think it would be awesome if we could provide a new syntax word which would map roughly to the syntax of Where-Object such that all of the PowerShell operators would work for assertions.

The logical thing would probably be to keep "Should" for the sake of pluggable assertions (but perhaps mark as obsolete the assertions which map to operators and would thus overlap), but perhaps we could find a way to make the new command work for both, and just mark "Should" as entirely obsolete (that is, obviously, leaving it in for now, but warning people to migrate to a new more PowerShell-specific syntax).

As a suggestion, we could use the keyword "must" with an alias "mustbe" ... so we could write things like:

12 | Must -Eq 12
"This","And","That" | Must -contain "And"
14 | MustBe -gt 10
dlwyatt commented 9 years ago

@ghostsquad had a good suggestion for this in #279 . By using dynamic parameter sets, we can support both the new and old syntax in calls to Should, and we can also add aliases (where -Be could also be -Eq, if that's your preference.)

Jaykul commented 9 years ago

Yeah, I had one called Assert-That from PSAINT which I was migrating over: https://github.com/Jaykul/Pester/blob/Assertive/Functions/Assertions/Must.ps1

It basically just proxies Where-Object with support for -Not and adding -BeNullOrEmpty as well as -All and -Any for working with arrays, so you can do:

1 | Must -Not -Equal 2 
1,2,2 | Must -Not -All -Equal 2

But because it's proxying Where-Object, you can also test properties, and I added some aliases:

$result = @(1,2),@(1,3,5),@(2,4,6)
$result | Must -All Length -GE 2
$result | Must -All Length -NotBeLessThan 2

However, I haven't finished the tests, and as you can see here, currently it just returns true or false, instead of throwing a descriptive error:

https://github.com/Jaykul/Pester/blob/Assertive/Functions/Assertions/Must.Tests.ps1

nohwnd commented 7 years ago

At least we have a new form of Assertions for Pester 6 :D

it-praktyk commented 7 years ago

So maybe should we change the label from 'Feature to ' 'Futurology'?

nohwnd commented 5 years ago

The operator syntax is already there provided by the aliases. Better assertions with better collection support are planned.