Created
June 5, 2019 11:23
-
-
Save etorreborre/fe6f0a647b205ab013b610a2ce97b4fd to your computer and use it in GitHub Desktop.
component mock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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