Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PowerShellスクリプトにユニットテストを埋め込むい
param(
[parameter(Mandatory, Position=0, ParameterSetName="Normal")]
[Int]$Number,
[parameter(ParameterSetName="Normal")]
[String]$OutputPath = "out.txt",
[parameter(ParameterSetName="Test")]
[switch]$Test = $false
)
function Get-Double([Int]$Number) {
$Number * 2
}
function Write-Result([String]$Path) {
$input > $Path
}
function Main {
Get-Double $Number | Write-Result $OutputPath
}
function Test {
Invoke-Pester -Script $MyInvocation.MyCommand.Path # 自分をテストスクリプトと見做してPesterを実行する
}
if ($Test) {
Test
} else {
Main
}
# ----- ここからテスト -----
Describe "Get-Double" {
It "2なら4" {
Get-Double 2 | Should Be 4
}
It "3なら6" {
Get-Double 3 | Should Be 6
}
}
Describe "Write-Result" {
It "2をoutに" {
echo 2 | Write-Result TestDrive:\out
Get-Content TestDrive:\out | Should Be 2
}
It "helloをworldに" {
echo hello | Write-Result TestDrive:\world
Get-Content TestDrive:\world | Should Be "hello"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment