Public/New-PsFunction.tests.ps1
$here = Split-Path -Parent $MyInvocation.MyCommand.Path $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.' . "$here\$sut" . "$here\New-PsModule.ps1" . "$here\Get-PsModuleBuilderPath.ps1" . "$here\Set-PsModuleBuilderPath.ps1" function Get-ExpectedFiles { param( [string]$path, [string]$moduleName, [string]$functionType, [string]$functionName ) return @( "$path\$moduleName\$functionType\$functionName.ps1", "$path\$moduleName\$functionType\$functionName.Tests.ps1" ) } function Restore-TheTest { param([string]$path) Write-Host "Formatting the test with: $path" Remove-Item "TestDrive:\*" -Recurse -Force New-Item $path -ItemType Directory -Force Set-PsModuleBuilderPath $path } Describe New-PsFunction { $testModule = "TestModule" $testFuncType = "Public" $testFuncName = "Get-Stuff" $testPath = "TestDrive:\Modules" Context "Module exists..." { Restore-TheTest $testPath New-PsModule $testModule New-PsFunction $testModule $testFuncName $testFuncType $expectedFiles = (Get-ExpectedFiles $testPath $testModule $testFuncType $testFuncName) $expectedFiles | ForEach-Object { It "Should create file $_" { Test-Path $_ -PathType Leaf | Should -Be $true } } } Context "Module does not exist..." { Restore-TheTest $testPath New-PsModule $testModule Remove-Item "$testPath\$testModule" -Recurse -Force It "Should throw error with about module path not existing" { { New-PsFunction $testModule $testFuncName $testFuncType } | Should -Throw } } Context "Function name must look like Write-Stuff (aaa-bbb)" { Restore-TheTest $testPath New-PsModule $testModule It "Should not allow numbers in function name" { { New-PsFunction $testModule "not-c234orrect" $testFuncType } | Should -Throw } It "Should not allow a hyphen-less name" { { New-PsFunction $testModule "notcorrect" $testFuncType } | Should -Throw } } Context "Housekeeping..." { It "Requires -ModuleName parameter" { Get-Command New-PsFunction | Should -HaveParameter ModuleName Get-Command New-PsFunction | Should -HaveParameter ModuleName -Mandatory } It "Requires -FunctionName parameter" { Get-Command New-PsFunction | Should -HaveParameter FunctionName Get-Command New-PsFunction | Should -HaveParameter FunctionName -Mandatory } It "Accepts -FunctionType parameter" { Get-Command New-PsFunction | Should -HaveParameter FunctionType Get-Command New-PsFunction | Should -HaveParameter FunctionType -Not -Mandatory # Defaults to Public... } It "Accepts -WhatIf parameter" { Get-Command New-PsFunction | Should -HaveParameter WhatIf Get-Command New-PsFunction | Should -HaveParameter WhatIf -Not -Mandatory } } } |