Skip to content

Commit

Permalink
Merging Describe / Context, updating output for new structure
Browse files Browse the repository at this point in the history
  • Loading branch information
dlwyatt committed Jun 29, 2016
1 parent 7b04ea4 commit 2b4149b
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 220 deletions.
79 changes: 0 additions & 79 deletions Functions/Context.Tests.ps1

This file was deleted.

105 changes: 6 additions & 99 deletions Functions/Context.ps1
Original file line number Diff line number Diff line change
@@ -1,108 +1,15 @@
function Context {
<#
.SYNOPSIS
Provides logical grouping of It blocks within a single Describe block. Any Mocks defined
inside a Context are removed at the end of the Context scope, as are any files or folders
added to the TestDrive during the Context block's execution. Any BeforeEach or AfterEach
blocks defined inside a Context also only apply to tests within that Context .
.PARAMETER Name
The name of the Context. This is a phrase describing a set of tests within a describe.
.PARAMETER Fixture
Script that is executed. This may include setup specific to the context and one or more It
blocks that validate the expected outcomes.
.EXAMPLE
function Add-Numbers($a, $b) {
return $a + $b
}
Describe "Add-Numbers" {
Context "when root does not exist" {
It "..." { ... }
}
Context "when root does exist" {
It "..." { ... }
It "..." { ... }
It "..." { ... }
}
}
.LINK
Describe
It
BeforeEach
AfterEach
about_Should
about_Mocking
about_TestDrive
#>
param(
[Parameter(Mandatory = $true)]
[Parameter(Mandatory = $true, Position = 0)]
[string] $Name,

[ValidateNotNull()]
[ScriptBlock] $Fixture = $(Throw "No test script block is provided. (Have you put the open curly brace on the next line?)")
)

ContextImpl @PSBoundParameters -Pester $Pester -ContextOutputBlock ${function:Write-Context} -TestOutputBlock ${function:Write-PesterResult}
}

function ContextImpl
{
param(
[Parameter(Mandatory = $true)]
[string] $Name,
[Alias('Tags')]
$Tag=@(),

[Parameter(Position = 1)]
[ValidateNotNull()]
[ScriptBlock] $Fixture = $(Throw "No test script block is provided. (Have you put the open curly brace on the next line?)"),

$Pester,
[scriptblock] $ContextOutputBlock,
[scriptblock] $TestOutputBlock
[ScriptBlock] $Fixture = $(Throw "No test script block is provided. (Have you put the open curly brace on the next line?)")
)

Assert-DescribeInProgress -CommandName Context

$Pester.EnterTestGroup($Name, 'Context')
$TestDriveContent = Get-TestDriveChildItem

if ($null -ne $ContextOutputBlock)
{
& $ContextOutputBlock $Name
}

try
{
Add-SetupAndTeardown -ScriptBlock $Fixture
Invoke-TestGroupSetupBlocks

do
{
$null = & $Fixture
} until ($true)
}
catch
{
$firstStackTraceLine = $_.InvocationInfo.PositionMessage.Trim() -split '\r?\n' | & $SafeCommands['Select-Object'] -First 1
$Pester.AddTestResult('Error occurred in Context block', "Failed", $null, $_.Exception.Message, $firstStackTraceLine, $null, $null, $_)

if ($null -ne $TestOutputBlock)
{
$Pester.TestResult[-1] | & $TestOutputBlock
}
}
finally
{
Invoke-TestGroupTeardownBlocks
}

Clear-TestDrive -Exclude ($TestDriveContent | & $SafeCommands['Select-Object'] -ExpandProperty FullName)
Exit-MockScope

$Pester.LeaveTestGroup($Name, 'Context')
Describe @PSBoundParameters -CommandUsed Context
}
18 changes: 9 additions & 9 deletions Functions/Describe.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ InModuleScope Pester {
}

It 'Does not rethrow terminating exceptions from the Fixture block' {
{ DescribeImpl -Pester $testState -Name 'A test' -Fixture $blockWithError } | Should Not Throw
{ DescribeImpl -Pester $testState -Name 'A test' -Fixture $blockWithError -NoTestDrive } | Should Not Throw
}

It 'Adds a failed test result when errors occur in the Describe block' {
Expand All @@ -57,7 +57,7 @@ InModuleScope Pester {
It 'Calls the Describe output block once, and does not call the test output block when no errors occur' {
$block = { $null = $null }

DescribeImpl -Pester $testState -Name 'A test' -Fixture $block -DescribeOutputBlock $describeOutput -TestOutputBlock $testOutput
DescribeImpl -Pester $testState -Name 'A test' -Fixture $block -DescribeOutputBlock $describeOutput -TestOutputBlock $testOutput -NoTestDrive

Assert-MockCalled MockMe -Exactly 0 -ParameterFilter { $Name -eq 'Test' } -Scope It
Assert-MockCalled MockMe -Exactly 1 -ParameterFilter { $Name -eq 'Describe' } -Scope It
Expand All @@ -66,7 +66,7 @@ InModuleScope Pester {
It 'Calls the Describe output block once, and the test output block once if an error occurs.' {
$block = { throw 'up' }

DescribeImpl -Pester $testState -Name 'A test' -Fixture $block -DescribeOutputBlock $describeOutput -TestOutputBlock $testOutput
DescribeImpl -Pester $testState -Name 'A test' -Fixture $block -DescribeOutputBlock $describeOutput -TestOutputBlock $testOutput -NoTestDrive

Assert-MockCalled MockMe -Exactly 1 -ParameterFilter { $Name -eq 'Test' } -Scope It
Assert-MockCalled MockMe -Exactly 1 -ParameterFilter { $Name -eq 'Describe' } -Scope It
Expand All @@ -86,14 +86,14 @@ InModuleScope Pester {

It -TestCases $cases 'Calls the test block when the test name <Description>' {
param ($Name)
DescribeImpl -Name $Name -Pester $testState -Fixture $testBlock
DescribeImpl -Name $Name -Pester $testState -Fixture $testBlock -NoTestDrive
Assert-MockCalled MockMe -Scope It -Exactly 1
}

It 'Does not call the test block when the test name doesn''t match a filter' {
DescribeImpl -Name 'Test On' -Pester $testState -Fixture $testBlock
DescribeImpl -Name 'Two' -Pester $testState -Fixture $testBlock
DescribeImpl -Name 'Bogus' -Pester $testState -Fixture $testBlock
DescribeImpl -Name 'Test On' -Pester $testState -Fixture $testBlock -NoTestDrive
DescribeImpl -Name 'Two' -Pester $testState -Fixture $testBlock -NoTestDrive
DescribeImpl -Name 'Bogus' -Pester $testState -Fixture $testBlock -NoTestDrive

Assert-MockCalled MockMe -Scope It -Exactly 0
}
Expand All @@ -114,13 +114,13 @@ InModuleScope Pester {
It -TestCases $cases 'Calls the test block when the tag filter <Description>' {
param ($Tags)

DescribeImpl -Name 'Blah' -Tags $Tags -Pester $testState -Fixture $testBlock
DescribeImpl -Name 'Blah' -Tags $Tags -Pester $testState -Fixture $testBlock -NoTestDrive
Assert-MockCalled MockMe -Scope It -Exactly 1
}

It 'Does not call the test block when the test tags don''t match the pester state''s tags.' {
# Unlike the test name filter, tags are literal matches and not interpreted as wildcards.
DescribeImpl -Name 'Blah' -Tags 'TestTwoTest' -Pester $testState -Fixture $testBlock
DescribeImpl -Name 'Blah' -Tags 'TestTwoTest' -Pester $testState -Fixture $testBlock -NoTestDrive

Assert-MockCalled MockMe -Scope It -Exactly 0
}
Expand Down
60 changes: 38 additions & 22 deletions Functions/Describe.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ about_TestDrive

[Parameter(Position = 1)]
[ValidateNotNull()]
[ScriptBlock] $Fixture = $(Throw "No test script block is provided. (Have you put the open curly brace on the next line?)")
[ScriptBlock] $Fixture = $(Throw "No test script block is provided. (Have you put the open curly brace on the next line?)"),

[string] $CommandUsed = 'Describe'
)

if ($null -eq (& $SafeCommands['Get-Variable'] -Name Pester -ValueOnly -ErrorAction $script:IgnoreErrorPreference))
Expand All @@ -90,13 +92,19 @@ function DescribeImpl {
[ValidateNotNull()]
[ScriptBlock] $Fixture = $(Throw "No test script block is provided. (Have you put the open curly brace on the next line?)"),

[string] $CommandUsed = 'Describe',

$Pester,

[scriptblock] $DescribeOutputBlock,

[scriptblock] $TestOutputBlock
[scriptblock] $TestOutputBlock,

[switch] $NoTestDrive
)

Assert-DescribeInProgress -CommandName $CommandUsed

if($Pester.TestNameFilter-and -not ($Pester.TestNameFilter | & $SafeCommands['Where-Object'] { $Name -like $_ }))
{
#skip this test
Expand All @@ -106,25 +114,28 @@ function DescribeImpl {
if($Pester.TagFilter -and @(& $SafeCommands['Compare-Object'] $Tag $Pester.TagFilter -IncludeEqual -ExcludeDifferent).count -eq 0) {return}
if($Pester.ExcludeTagFilter -and @(& $SafeCommands['Compare-Object'] $Tag $Pester.ExcludeTagFilter -IncludeEqual -ExcludeDifferent).count -gt 0) {return}

$Pester.EnterTestGroup($Name, 'Describe')
$Pester.EnterTestGroup($Name, $CommandUsed)

if ($null -ne $DescribeOutputBlock)
{
& $DescribeOutputBlock $Name
}

# If we're unit testing Describe, we have to restore the original PSDrive when we're done here;
# this doesn't affect normal client code, who can't nest Describes anyway.
$oldTestDrive = $null
if (Test-Path TestDrive:\)
{
$oldTestDrive = (Get-PSDrive TestDrive).Root
& $DescribeOutputBlock $Name $CommandUsed
}

$testDriveAdded = $false
try
{
New-TestDrive
$testDriveAdded = $true
if (-not $NoTestDrive)
{
if (-not (Test-Path TestDrive:\))
{
New-TestDrive
$testDriveAdded = $true
}
else
{
$TestDriveContent = Get-TestDriveChildItem
}
}

Add-SetupAndTeardown -ScriptBlock $Fixture
Invoke-TestGroupSetupBlocks
Expand All @@ -137,7 +148,7 @@ function DescribeImpl {
catch
{
$firstStackTraceLine = $_.InvocationInfo.PositionMessage.Trim() -split '\r?\n' | & $SafeCommands['Select-Object'] -First 1
$Pester.AddTestResult('Error occurred in Describe block', "Failed", $null, $_.Exception.Message, $firstStackTraceLine, $null, $null, $_)
$Pester.AddTestResult("Error occurred in $CommandUsed block", "Failed", $null, $_.Exception.Message, $firstStackTraceLine, $null, $null, $_)
if ($null -ne $TestOutputBlock)
{
& $TestOutputBlock $Pester.TestResult[-1]
Expand All @@ -146,17 +157,22 @@ function DescribeImpl {
finally
{
Invoke-TestGroupTeardownBlocks
if ($testDriveAdded) { Remove-TestDrive }
if (-not $NoTestDrive)
{
if ($testDriveAdded)
{
Remove-TestDrive
}
else
{
Clear-TestDrive -Exclude ($TestDriveContent | & $SafeCommands['Select-Object'] -ExpandProperty FullName)
}
}
}

Exit-MockScope

if ($oldTestDrive)
{
New-TestDrive -Path $oldTestDrive
}

$Pester.LeaveTestGroup($Name, 'Describe')
$Pester.LeaveTestGroup($Name, $CommandUsed)
}

# Name is now misleading; rename later. (Many files touched to change this.)
Expand Down
Loading

0 comments on commit 2b4149b

Please sign in to comment.