@biacããã®ããã°ãPSExpect - PowerShell 用ユニット テスト ライブラリー: biac の それさえもおそらくは幸せな日々@niftyãã§ç´¹ä»ããã¦ãã
psexpect
ã使ç¨ãã¦FizzFuzzãã£ã¦ã¿ã¾ããã
ãã¡ã¤ã«ã®é ç½®
ãã¡ã¤ã«ã¯ãããªæãã
\PowerShell +-PSExpect ⦠PSExpectæ¬ä½ +-FizzBuzz.ps1 ⦠FizzBuzzæ¬ä½ +-FizzBuzzTest.ps1 ⦠ãã¹ãã³ã¼ã
FizzBuzzæ¬ä½ï¼FizzBuzz.ps1ï¼
ã¾ãã¯FizzBuzzæ¬ä½ã
function global:FizzBuzz([int]$x) { if (($x % 3 -eq 0) -and ($x % 5 -eq 0)){ return "FizzBuzz" } if ($x % 3 -eq 0) { return "Fizz" } if ($x % 5 -eq 0) { return "Buzz" } return $x }
ã¹ã¯ãªãããã¡ã¤ã«å ã§å®£è¨ãããé¢æ°ãå¥ã®ã¹ã¯ãªãããã¡ã¤ã«ããåç §ãããã®ã§ããglobal:ãã¨ããã¹ã³ã¼ã修飾åãä»ãã¦ã¾ãã
ãã¹ãã³ã¼ãï¼FizzBuzzTest.ps1ï¼
ã§ããã¹ãã³ã¼ãã
if (!(Test-Path variable:_TESTLIB)) { .\PSExpect\TestLib.ps1 } .\FizzBuzz.ps1 function Check([int]$x, $expected) { $result = FizzBuzz($x) AssertEqual $expected $result ("arg:" + $x) RaiseAssertions } Check 1 1 Check 2 2 Check 3 "Fizz" Check 5 "Buzz" Check 6 "Fizz" Check 10 "Buzz" Check 15 "FizzBuzz"
1è¡ç®ã§PSExpectãèªã¿è¾¼ãã§ï¼è¦ãã¾ãã¾ã§ããï½ï¼ã2è¡ç®ã§FizzBuzzæ¬ä½ã®ã¹ã¯ãªãããèªã¿è¾¼ã¾ãã¦ã¾ãã
PSExpectã®ä½¿ãæ¹ã¨ãã¦ã¯ãAssertEqualçã®ãã§ãã¯ãæ¸ãã¦ããã¦ãRaiseAssertionsã§ã³ã³ã½ã¼ã«ã«Assertionã®çµæã表示ãããããªæãã§ãã
å®è¡ããã¨ã以ä¸ã®æ§ãªçµæãã³ã³ã½ã¼ã«ã«åºåããã¾ãã
2010/10/19 22:06:54,SHOULDPASS,PASSED,arg:1 2010/10/19 22:06:54,SHOULDPASS,PASSED,arg:2 2010/10/19 22:06:54,SHOULDPASS,PASSED,arg:3 2010/10/19 22:06:54,SHOULDPASS,PASSED,arg:5 2010/10/19 22:06:54,SHOULDPASS,PASSED,arg:6 2010/10/19 22:06:54,SHOULDPASS,PASSED,arg:10 2010/10/19 22:06:54,SHOULDPASS,PASSED,arg:15
ãã®ä»ããããæ©è½ã¯ãããããªã®ã§ãããä»åã¯ãããã¨ãããã¨ã§ãã®è¾ºã§ï½