Skip to content

Commit

Permalink
Added the BeIn assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
adbertram committed Nov 18, 2016
1 parent dc550d2 commit 64755d1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Functions/Assertions/BeIn.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Set-StrictMode -Version Latest

InModuleScope Pester {
Describe "PesterBeIn" {
It "passes if value is in the collection" {
Test-PositiveAssertion (PesterBeIn 1 (@(1,2,3)))
Test-PositiveAssertion (PesterBeIn 'a' (@(1,'a',3)))
1 | Should BeIn @(1,2,3)
'a' | Should BeIn @(1,'a',3)
}
It "fails if value is not in the collection" {
Test-NegativeAssertion (PesterBeIn 4 (@(1,2,3)))
Test-NegativeAssertion (PesterBeIn 'b' (@(1,'a',3)))
4 | Should Not BeIn @(1,2,3)
'b' | Should Not BeIn @(1,'a',3)
}
}
}
16 changes: 16 additions & 0 deletions Functions/Assertions/BeIn.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

function PesterBeIn($value, $expectedArrayOfValues) {
return [bool]($expectedArrayOfValues -contains $value)
}

function PesterBeInFailureMessage($value, $expectedArrayOfValues) {
if(-not ([bool]($expectedArrayOfValues -contains $value))) {
return "Expected: ${value} to be in collection [$($expectedArrayOfValues -join ',')] but was not found."
}
}

function NotPesterBeInFailureMessage($value, $expectedArrayOfValues) {
if([bool]($expectedArrayOfValues -contains $value)) {
return "Expected: ${value} to not be in collection [$($expectedArrayOfValues -join ',')] but was found."
}
}
5 changes: 5 additions & 0 deletions en-US/about_Should.help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ LONG DESCRIPTION

$Error.Count | Should BeGreaterThan 0

BeIn
Asserts that a collection of values contain a specific value. Uses PowerShell's -contains operator to confirm.

1 | Should BeIn @(1,2,3,'a','b','c')

BeLessThan
Asserts that a number is less than an expected value. Uses PowerShell's -gt operator to compare the two values.

Expand Down

0 comments on commit 64755d1

Please sign in to comment.