我有一个程序,它从 getLine
获取用户输入,然后验证它是否全是数字。如果通过,它运行 String -> String
函数并将结果打印到屏幕。如果不是,它会重复 getLine
。
module Main where
import Control.Monad.Loops (untilJust)
import Data.Char (isDigit)
main = do
let validateInput s = if all isDigit s then Just s else Nothing
putStrLn =<< myFunc <$> untilJust (validateInput <$> (putStr "Number : " >> getLine))
myFunc = id -- to do
我如何使用 Hspec
之类的东西测试这个主要功能,以检查它对数字输入与其他输入(字母、空等)的正确处理
即
test1 rejects a non-numeric input of "abc"
test2 returns 123 as 123
最佳答案
由于您想测试逻辑而不是用户交互,我建议您只考虑输入验证。
关于haskell - 使用 Hspec 测试用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45384808/