Skip to content

Instantly share code, notes, and snippets.

@miladhub
Created August 24, 2019 06:57
Show Gist options
  • Save miladhub/e039b38d1dcb24f4234da79d8780fe58 to your computer and use it in GitHub Desktop.
Save miladhub/e039b38d1dcb24f4234da79d8780fe58 to your computer and use it in GitHub Desktop.
haskell_testing.hs
import qualified Prelude
import Prelude hiding(readFile)
import Control.Monad.State
class Monad m => FSMonad m where
readF :: FilePath -> m String
numCharactersInFile :: FSMonad m => FilePath -> m Int
numCharactersInFile fileName = do
contents <- readF fileName
return (length contents)
instance FSMonad IO where
readF = Prelude.readFile
data MockFS = SingleFile FilePath String
newtype StateMockFS = State MockFS
instance FSMonad StateMockFS where
readF pathRequested = do
(SingleFile pathExisting contents) <- get
if pathExisting == pathRequested
then return contents
else fail "file not found"
testNumCharactersInFile :: Bool
testNumCharactersInFile = evalState
(numCharactersInFile "test.txt")
(SingleFile "test.txt" "hello world")
== 11
@miladhub
Copy link
Author

miladhub commented Sep 4, 2019 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment