-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from himura/fixture-th
Loading the all fixtures as variables by using TH
- Loading branch information
Showing
7 changed files
with
33 additions
and
34 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,43 @@ | ||
{-# LANGUAGE QuasiQuotes #-} | ||
{-# LANGUAGE TemplateHaskell #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE CPP #-} | ||
|
||
module Fixtures where | ||
|
||
import Language.Haskell.TH | ||
import Data.Aeson | ||
import Data.Attoparsec.ByteString | ||
import qualified Data.Text as T | ||
import qualified Data.Text.Encoding as T | ||
import qualified Data.Text.IO as T | ||
import qualified Data.ByteString as S | ||
import Data.Maybe | ||
import Text.Shakespeare.Text | ||
import System.Directory | ||
import System.Environment | ||
import System.FilePath | ||
import System.IO.Unsafe (unsafePerformIO) | ||
import Control.Applicative | ||
|
||
fj :: T.Text -> Value | ||
fj = fromJust . maybeResult . parse json . T.encodeUtf8 | ||
parseJSONValue :: S.ByteString -> Value | ||
parseJSONValue = fromJust . maybeResult . parse json | ||
|
||
fixturePath :: String | ||
fixturePath = unsafePerformIO $ do | ||
fromMaybe defaultPath <$> lookupEnv "TWITTER_FIXTURE_PATH" | ||
where | ||
defaultPath = takeDirectory __FILE__ </> "fixtures" | ||
|
||
loadFixture :: String -> IO Value | ||
loadFixture filename = fj <$> T.readFile (fixturePath </> filename) | ||
loadFixture :: (S.ByteString -> a) -> String -> IO a | ||
loadFixture conv filename = conv <$> S.readFile (fixturePath </> filename) | ||
|
||
fixture :: String -> Value | ||
fixture = unsafePerformIO . loadFixture | ||
fixture :: (S.ByteString -> a) -> String -> a | ||
fixture conv = unsafePerformIO . loadFixture conv | ||
|
||
errorMsgJson :: Value | ||
errorMsgJson = fj [st|{"request":"\/1\/statuses\/user_timeline.json","error":"Not authorized"}|] | ||
|
||
statusJson :: Value | ||
statusJson = fixture "status_object.json" | ||
|
||
statusEntityJson :: Value | ||
statusEntityJson = fixture "status_object_with_entity.json" | ||
|
||
mediaEntityJson :: Value | ||
mediaEntityJson = fixture "media_entity.json" | ||
|
||
mediaExtendedEntityJson :: Value | ||
mediaExtendedEntityJson = fixture "media_extended_entity.json" | ||
loadFixturesTH :: Name -> Q [Dec] | ||
loadFixturesTH convFn = do | ||
files <- runIO $ filter (\fn -> takeExtension fn == ".json") <$> getDirectoryContents fixturePath | ||
concat <$> mapM genEachDefs files | ||
where | ||
genEachDefs filename = do | ||
let funN = mkName $ "fixture_" ++ dropExtension filename | ||
sigdef <- sigD funN (conT ''Value) | ||
bind <- valD (varP funN) (normalB [|fixture $(varE convFn) $(litE (stringL filename))|]) [] | ||
return [ sigdef, bind ] |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"symbols":[],"urls":[{"indices":[32,52], "url":"http://t.co/IOwBrTZR", "display_url":"youtube.com/watch?v=oHg5SJ\u2026", "expanded_url":"http://www.youtube.com/watch?v=oHg5SJYRHA0"}],"user_mentions":[{"name":"Twitter API", "indices":[4,15], "screen_name":"twitterapi", "id":6253282, "id_str":"6253282"}],"hashtags":[{"indices":[32,36],"text":"lol"}]} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"request":"\/1\/statuses\/user_timeline.json","error":"Not authorized"} |
File renamed without changes.
File renamed without changes.
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