簡體   English   中英

使用Hspec測試用戶輸入

[英]Testing user input with Hspec

我有一個程序,該程序從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類的Hspec來測試這個主要功能,以檢查它與數字輸入相對於其他輸入(字母,空等)是否正確

test1 rejects a non-numeric input of "abc"
test2 returns 123 as 123

由於您要測試邏輯而不是用戶交互,因此建議您僅考慮輸入驗證。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM