Skip to content

Commit

Permalink
adding more comment docs and fixed a ps2.0 bug
Browse files Browse the repository at this point in the history
  • Loading branch information
mwrock committed Oct 22, 2012
1 parent 875b0d5 commit fd0a9fe
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 6 deletions.
29 changes: 29 additions & 0 deletions Functions/Describe.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,35 @@ The actual test script. If you are following the AAA pattern (Arrange-Act-Assert
typically holds the arrange and act sections. The Asserts will also lie in this block but are
typically nested each in its own IT block.
.EXAMPLE
function Add-Numbers($a, $b) {
return $a + $b
}
Describe "Add-Numbers" {
It "adds positive numbers" {
$sum = Add-Numbers 2 3
$sum.should.be(5)
}
It "adds negative numbers" {
$sum = Add-Numbers (-2) (-2)
$sum.should.be((-4))
}
It "adds one negative number to positive number" {
$sum = Add-Numbers (-2) 2
$sum.should.be(0)
}
It "concatenates strings if given strings" {
$sum = Add-Numbers two three
$sum.should.be("twothree")
}
}
#>
param(
$name,
Expand Down
64 changes: 62 additions & 2 deletions Functions/It.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,65 @@
function It($name, [ScriptBlock] $test)
{
function It {
<#
.SYNOPSIS
Validates the results of a test inside of a Describe block.
.DESCRIPTION
The It function is intended to be used inside of a Describe
Block. If you are familiar with the AAA pattern
(Arrange-Act-Assert), this would be the appropriate location
for an assert. The convention is to assert a single
expectation for each It block. The code inside of the It block
should throw an exception if the expectation of the test is not
met and thus cause the test to fail. The name of the It block
should expressively state the expectation of the test.
In addition to using your own logic to test expectations and
throw exceptions, you may also use Pester's own helper functions
to assist in evaluating test results using the Should object.
.PARAMETER Name
An expressive phsae describing the expected test outcome.
.PARAMETER Test
The script block that should throw an exception if the
expectation of the test is not met.If you are following the
AAA pattern (Arrange-Act-Assert), this typically holds the
Assert.
.EXAMPLE
function Add-Numbers($a, $b) {
return $a + $b
}
Describe "Add-Numbers" {
It "adds positive numbers" {
$sum = Add-Numbers 2 3
$sum.should.be(5)
}
It "adds negative numbers" {
$sum = Add-Numbers (-2) (-2)
$sum.should.be((-4))
}
It "adds one negative number to positive number" {
$sum = Add-Numbers (-2) 2
$sum.should.be(0)
}
It "concatenates strings if given strings" {
$sum = Add-Numbers two three
$sum.should.be("twothree")
}
}
#>
param(
$name,
[ScriptBlock] $test
)
$results = Get-GlobalTestResults
$margin = " " * $results.TestDepth
$error_margin = $margin * 2
Expand Down
9 changes: 5 additions & 4 deletions Functions/Mock.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ param(
[ScriptBlock]$parameterFilter = {$True}
)

$origCommand = Validate-Command
$origCommand = Validate-Command $commandName
$filterTest=&($parameterFilter)
if($filterTest -ne $True -and $filterTest -ne $False){ throw "The Parameter Filter must return a boolean"}
$blocks = @{Mock=$mockWith; Filter=$parameterFilter; Verifiable=$verifiable}
Expand Down Expand Up @@ -258,10 +258,11 @@ param(
if(!$mock) { Throw "You did not declare a mock of the $commandName Command."}
Microsoft.PowerShell.Management\Set-Item Function:\Pester_TempParamTest -value "$($mock.CmdLet) `r`n param ( $($mock.Params) ) `r`n$parameterFilter"
$cmd=(Microsoft.PowerShell.Core\Get-Command Pester_TempParamTest)
$qualifiedCalls = ($global:mockCallHistory | ? {$_.CommandName -eq $commandName} | ? {$p=$_.params;&($cmd) @p} )
$qualifiedCalls = @()
$global:mockCallHistory | ? {$_.CommandName -eq $commandName} | ? {$p=$_.params;&($cmd) @p} | %{ $qualifiedCalls += $_}
Microsoft.PowerShell.Management\Remove-Item Function:\Pester_TempParamTest
if($qualifiedCalls.Length -ne $times -and ($Exactly -or ($times -eq 0))) {
throw "Expected $commandName to be called $times times exactly but was called $($qualifiedCalls.Length) times"
throw "Expected $commandName to be called $times times exactly but was called $($qualifiedCalls.Length.ToString()) times"
} elseif($qualifiedCalls.Length -lt $times) {
throw "Expected $commandName to be called at least $times times but was called $($qualifiedCalls.Length) times"
}
Expand All @@ -271,7 +272,7 @@ function Clear-Mocks {
if($mockTable){
$mockTable.Keys | % { Microsoft.PowerShell.Management\Remove-Item function:\$_ }
$mockTable.Clear()
if($mockCallHistory) {$mockCallHistory.Clear()}
$global:mockCallHistory = @()
Get-ChildItem Function: | ? { $_.Name.StartsWith("PesterIsMocking_") } | % {Rename-Item Function:\$_ "script:$($_.Name.Replace('PesterIsMocking_', ''))"}
}
}
Expand Down

0 comments on commit fd0a9fe

Please sign in to comment.