2

我想将一个整数作为 CLI 参数传递给使用 QuickCheck / 的 Haskell 程序monadicIO。该整数将在内部使用assert以使测试可定制。问题是,一旦我解析 中的整数值main,我不知道如何在monadicIO调用内部传递它而不使用像IORef. 我认为一个优雅的解决方案可能是Readermonad,但我找不到让它工作的解决方案,因为quickCheck它的论点很僵化。有任何想法吗?

后来编辑1:根据要求,我附上了我正在尝试的实际代码,但失败了。注释掉的行代表我失败的尝试。背景:测试套件旨在运行一个非常简单的远程端点,该端点计算由 QuickCheck 生成的随机输入的 SHA512。远程端点是基于 Python/Flask 的。

稍后编辑 2 以响应 @user2407038:我可以propHasExpectedLengthCeiling采用 Int 类型的附加参数,但quickCheck会为其生成随机值,这不是我想要发生的。我的目标是使用maxSegmentLengthCeiling我从命令行参数中获取的,并在块let testPassed = actualMaxSegmentLength <= maxSegmentLengthCeiling内部使用它monadicIO。现在maxSegmentLengthCeiling被指定为顶级值,这意味着每次更改值时都必须重新编译代码。我还没有任何涉及的代码,IORef因为这是最后的手段,我的问题的本质是如何避免走这IORef条路。

import qualified Data.ByteString.Lazy.Char8 as LC

import Control.Applicative     ( (<$>) )
import Data.Function           ( on )
import Data.List               ( groupBy )
import Data.Char               ( isDigit )
--import Safe                    ( headMay
--                               , readMay
--                               )
--import System.Environment      ( getArgs )
import Network.HTTP.Conduit    ( simpleHttp )
import Test.QuickCheck         ( Arbitrary
                               , Property
                               , arbitrary
                               , choose
                               , frequency
                               , quickCheckWith
                               , stdArgs
                               , vectorOf
                               )
import Test.QuickCheck.Test    ( Args
                               , maxSuccess
                               )
import Test.QuickCheck.Monadic ( assert
                               , monadicIO
                               , run
                               )

newtype CustomInput = MkCustomInput String deriving Show

instance Arbitrary CustomInput where
  arbitrary =
    let
      genCustomInput = vectorOf 20
                       $ frequency [ (26, choose ('0','9'))
                                   , (10, choose ('a','z'))
                                   ]
    in
      MkCustomInput <$> genCustomInput

maxSegmentLengthCeiling :: Int
maxSegmentLengthCeiling = 22

urlPrefix :: String
urlPrefix = "http://192.168.2.3:5000/sha512sum/"

propHasExpectedLengthCeiling :: CustomInput -> Property
propHasExpectedLengthCeiling (MkCustomInput input) = monadicIO $ do
  testPassed <- run $ do
    response <- simpleHttp $ urlPrefix ++ input
    let stringResponse = LC.unpack response
    let brokenDownStringResponse = groupBy ( (==) `on` isDigit ) stringResponse
    let actualMaxSegmentLength = maximum $ map length brokenDownStringResponse
    let testPassed = actualMaxSegmentLength <= maxSegmentLengthCeiling
    putStrLn ""
    putStrLn ""
    putStrLn $ "Input:       " ++ input
    putStrLn $ "Control sum: " ++ stringResponse
    putStrLn $ "Breakdown:   " ++ show brokenDownStringResponse
    putStrLn $ "Max. length: " ++ show actualMaxSegmentLength
    putStrLn $ "Ceiling:     " ++ show maxSegmentLengthCeiling
    putStrLn $ "Test result: " ++ if testPassed then "Pass" else "Fail"
    putStrLn ""
    putStrLn ""
    return testPassed
  assert $ testPassed

customArgs :: Args
customArgs = stdArgs { maxSuccess = 1000000 }

--readMayAsInt :: String -> Maybe Int
--readMayAsInt = readMay

main :: IO ()
main =
--main = do
--  cliArgs <- getArgs
--  let ceilingInputMay = headMay cliArgs >>= readMayAsInt
--  maxSegmentLengthCeiling <- case ceilingInputMay of
--                               (Just lengthCeiling) -> return lengthCeiling
--                               Nothing              -> error "No valid number given"
  quickCheckWith
    customArgs
    propHasExpectedLengthCeiling
4

1 回答 1

1

maxSegmentLengthCeiling参数设置为propHasExpectedLengthCeiling

propHasExpectedLengthCeiling :: Int -> CustomInput -> Property

并将其调用为

main = do 
  [n] <- getArgs
  quickCheckWith customArgs (propHasExpectedLengthCeiling (read n))
于 2014-07-31T17:29:46.923 回答