Skip to content

Instantly share code, notes, and snippets.

@etorreborre
Created June 5, 2019 11:23
Show Gist options
  • Save etorreborre/fe6f0a647b205ab013b610a2ce97b4fd to your computer and use it in GitHub Desktop.
Save etorreborre/fe6f0a647b205ab013b610a2ce97b4fd to your computer and use it in GitHub Desktop.
component mock
-- a component
data FileSystem m = FileSystem {
exists :: Path -> m (Either Text Bool)
, readFile :: Path -> m (Either Text Text)
}
-- a constructor for a mock file system
newFileSystemWith :: (FileSystemValues -> FileSystemValues) -> FileSystem
newFileSystemWith f = FileSystem {
exists = \_ -> pure (_exists $ f fileSystemValues)
, readFile = \_ -> pure (_readFile $ f fileSystemValues)
}
-- mocked returned values
data FileSystemValues = FileSystemValues {
_exists :: Either Text Bool
, _read :: Either Text Text
}
fileSystemValues = FileSystemValues
(Left "todo - exists")
(Left "todo - readFile")
-- set mock values
existingFiles v = v { _exists = Right True }
someText t v = v { _read = Right t }
-- create a mock and compose functions to set return values
myMock = newFileSystemWith (existingFiles . someText "hello world")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment