コードの整理とプロジェクトのビルド Monad型クラス QuickCheckを使ったプロパティテスト プロジェクト
入門Haskellプログラミング (Will Kurt(著)、株式会社クイープ(監修、翻訳)、翔泳社)のUNIT6(コードの整理とプロジェクトのビルド)、LESSON36(QuickCheckを使ったプロパティテスト)、36.5(練習問題)Q36-1の解答を求めてみる。
package.yaml
name: palindrome-testing
version: 0.1.0.0
github: "githubuser/palindrome-testing"
license: BSD3
author: "Author name here"
maintainer: "example@example.com"
copyright: "2022 Author name here"
extra-source-files:
- README.md
- ChangeLog.md
# Metadata used when publishing your package
# synopsis: Short description of your package
# category: Web
# To avoid duplicated efforts in documentation and dealing with the
# complications of embedding Haddock markup inside cabal files, it is
# common to point users to the README.md file.
description: Please see the README on GitHub at <https://github.com/githubuser/palindrome-testing#readme>
dependencies:
- base >= 4.7 && < 5
- text
library:
source-dirs: src
extensions: OverloadedStrings
executables:
palindrome-testing-exe:
main: Main.hs
source-dirs: app
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N
dependencies:
- palindrome-testing
default-extensions: OverloadedStrings
tests:
palindrome-testing-test:
main: Spec.hs
source-dirs: test
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N
dependencies:
- palindrome-testing
- quickcheck-instances
- QuickCheck
- quickcheck-text
default-extensions: OverloadedStrings
palindrome-testing.cabal
cabal-version: 1.12
-- This file has been generated from package.yaml by hpack version 0.34.4.
--
-- see: https://github.com/sol/hpack
name: palindrome-testing
version: 0.1.0.0
description: Please see the README on GitHub at <https://github.com/githubuser/palindrome-testing#readme>
homepage: https://github.com/githubuser/palindrome-testing#readme
bug-reports: https://github.com/githubuser/palindrome-testing/issues
author: Author name here
maintainer: example@example.com
copyright: 2022 Author name here
license: BSD3
license-file: LICENSE
build-type: Simple
extra-source-files:
README.md
ChangeLog.md
source-repository head
type: git
location: https://github.com/githubuser/palindrome-testing
library
exposed-modules:
Lib
other-modules:
Paths_palindrome_testing
hs-source-dirs:
src
build-depends:
base >=4.7 && <5
, text
default-language: Haskell2010
executable palindrome-testing-exe
main-is: Main.hs
other-modules:
Paths_palindrome_testing
hs-source-dirs:
app
default-extensions:
OverloadedStrings
ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends:
base >=4.7 && <5
, palindrome-testing
, text
default-language: Haskell2010
test-suite palindrome-testing-test
type: exitcode-stdio-1.0
main-is: Spec.hs
other-modules:
Paths_palindrome_testing
hs-source-dirs:
test
default-extensions:
OverloadedStrings
ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends:
QuickCheck
, base >=4.7 && <5
, palindrome-testing
, quickcheck-instances
, quickcheck-text
, text
default-language: Haskell2010
コード
test/Spec.hs
import Data.Char (isPunctuation)
import Data.Text as T
import Lib (isPalindrome, preprocess)
import Test.QuickCheck
import Test.QuickCheck.Instances
prop_reverseInvariant text =
isPalindrome text == isPalindrome (T.reverse text)
prop_punctuationInvariant text =
let noPuncTest = T.filter (not . isPunctuation) text
in preprocess text == preprocess noPuncTest
main :: IO ()
main = do
quickCheck prop_reverseInvariant
quickCheckWith
stdArgs {maxSuccess = 1000}
prop_punctuationInvariant
putStrLn "done!"
コード
src/Lib.hs
module Lib
( isPalindrome,
preprocess,
)
where
import Data.Char (isPunctuation, isSpace)
import Data.Text as T (Text, filter, reverse, toLower)
isPalindrome :: T.Text -> Bool
isPalindrome text =
let cleanText = preprocess text
in cleanText == T.reverse cleanText
preprocess :: T.Text -> T.Text
preprocess = stripWhiteSpace . stripPunctuation . T.toLower
stripWhiteSpace :: T.Text -> T.Text
stripWhiteSpace = T.filter (not . isSpace)
stripPunctuation :: T.Text -> T.Text
stripPunctuation = T.filter $ not . isPunctuation
入出力結果(Terminal, Zsh)
% stack test
/Users/…/palindrome-testing/package.yaml: Ignoring unrecognized field $.library.extensions
palindrome-testing-0.1.0.0: unregistering (local file changes: test/Spec.hs)
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 executable 'palindrome-testing-exe' for palindrome-testing-0.1.0.0..
Building executable 'palindrome-testing-exe' 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
/Users/…/palindrome-testing/test/Spec.hs:15:3: error:
• No instance for (Arbitrary Text)
arising from a use of ‘quickCheck’
• In a stmt of a 'do' block: quickCheck prop_reverseInvariant
In the expression:
do quickCheck prop_reverseInvariant
quickCheckWith
stdArgs {maxSuccess = 1000} prop_punctuationInvariant
putStrLn "done!"
In an equation for ‘main’:
main
= do quickCheck prop_reverseInvariant
quickCheckWith
stdArgs {maxSuccess = 1000} prop_punctuationInvariant
putStrLn "done!"
|
15 | quickCheck prop_reverseInvariant
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Progress 1/2
-- While building package palindrome-testing-0.1.0.0 (scroll up to its section to see the error) using:
/Users/…/.stack/setup-exe-cache/x86_64-osx/Cabal-simple_mPHDZzAJ_3.2.1.0_ghc-8.10.7 --builddir=.stack-work/dist/x86_64-osx/Cabal-3.2.1.0 build lib:palindrome-testing exe:palindrome-testing-exe test:palindrome-testing-test --ghc-options " -fdiagnostics-color=always"
Process exited with code: ExitFailure 1
% stack test
/Users/…/palindrome-testing/package.yaml: Ignoring unrecognized field $.library.extensions
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 executable 'palindrome-testing-exe' for palindrome-testing-0.1.0.0..
Building executable 'palindrome-testing-exe' 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 ...
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-KHlFaoxsHbT2E4vDjysA3S
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)
+++ OK, passed 100 tests.
+++ OK, passed 1000 tests.
done!
palindrome-testing> Test suite palindrome-testing-test passed
Completed 2 action(s).
%