コードの整理とプロジェクトのビルド Monad型クラス QuickCheckを使ったプロパティテスト QuickCheckによるプロパティテスト プロパティのテスト
入門Haskellプログラミング (Will Kurt(著)、株式会社クイープ(監修、翻訳)、翔泳社)のUNIT6(コードの整理とプロジェクトのビルド)、LESSON36(QuickCheckを使ったプロパティテスト)、36.3(QuickCheckによるプロパティテスト)、プロパティのテスト、クイックチェック 37-4の解答を求めてみる。
コード
test/Spec.hs
import Lib (isPalindrome)
assert :: Bool -> String -> String -> IO ()
assert test passStatement failStatement =
putStrLn $
if test
then passStatement
else failStatement
prop_reverseInvariant text =
isPalindrome text == isPalindrome (reverse text)
main :: IO ()
main = do
putStrLn "Running tests..."
mapM_
( \(func, word) ->
assert
(func word)
(mconcat ["paased '", word, "'"])
(mconcat ["FAIL: '", word, "'"])
)
[ (isPalindrome, "racecar"),
(isPalindrome, "racecar!"),
(not . isPalindrome, "cat"),
(isPalindrome, ":racecar:")
]
putStrLn "done!"
入出力結果(Terminal, Zsh)
% stack test
palindrome-testing> build (lib + exe + test)
Preprocessing library for palindrome-testing-0.1.0.0..
Building library for palindrome-testing-0.1.0.0..
Preprocessing test suite 'palindrome-testing-test' for palindrome-testing-0.1.0.0..
Building test suite 'palindrome-testing-test' for palindrome-testing-0.1.0.0..
[2 of 2] Compiling Main
Linking .stack-work/dist/x86_64-osx/Cabal-3.2.1.0/build/palindrome-testing-test/palindrome-testing-test ...
Preprocessing executable 'palindrome-testing-exe' for palindrome-testing-0.1.0.0..
Building executable 'palindrome-testing-exe' for palindrome-testing-0.1.0.0..
palindrome-testing> copy/register
Installing library in /Users/…/palindrome-testing/.stack-work/install/x86_64-osx/160203b3c1b5851ee38cefff1898f2ea31f9dd27504c585eee0da67dd7dc8bb2/8.10.7/lib/x86_64-osx-ghc-8.10.7/palindrome-testing-0.1.0.0-ArexdrWdtZoJlI5LW2VA53
Installing executable palindrome-testing-exe in /Users/…/palindrome-testing/.stack-work/install/x86_64-osx/160203b3c1b5851ee38cefff1898f2ea31f9dd27504c585eee0da67dd7dc8bb2/8.10.7/bin
Registering library for palindrome-testing-0.1.0.0..
palindrome-testing> test (suite: palindrome-testing-test)
Running tests...
paased 'racecar'
paased 'racecar!'
paased 'cat'
paased ':racecar:'
done!
palindrome-testing> Test suite palindrome-testing-test passed
Completed 2 action(s).
%