Created
August 24, 2020 04:02
-
-
Save macrat/722f705118402507e024ec4dcf90425a to your computer and use it in GitHub Desktop.
PowerShellスクリプトにユニットテストを埋め込むい
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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