haskelltestinghspec

Testing user input with Hspec


I have a program that takes a user input from getLine then validates that it is all numbers. If it passes it runs a function of String -> String and prints the result to screen. If not it repeats the 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

How do I test the this main function with something like Hspec to check that it does the right thing with a number input vs other inputs (letters, empty etc)

ie

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

Solution

  • Since you want to test the logic rather than the user interaction, I'd suggest that you simply factor out the input validation.