-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6127031
commit 2d8ed8e
Showing
6 changed files
with
253 additions
and
10 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
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,95 @@ | ||
{-| | ||
Module : Test.Aeson.Internal.RoundtripFromFile | ||
Description : Golden tests for Arbitrary | ||
Copyright : (c) Plow Technologies, 2022 | ||
License : BSD3 | ||
Maintainer : [email protected] | ||
Stability : Beta | ||
Internal module, use at your own risk. | ||
-} | ||
|
||
{-# LANGUAGE NamedFieldPuns #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE RecordWildCards #-} | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
{-# LANGUAGE TypeApplications #-} | ||
{-# LANGUAGE RankNTypes #-} | ||
module Test.Aeson.Internal.ADT.RoundtripFromFile where | ||
|
||
import Control.Exception | ||
import Control.Monad | ||
|
||
import Data.Aeson | ||
import Data.ByteString.Lazy hiding (putStrLn) | ||
import Data.Maybe (isJust) | ||
import Data.Proxy | ||
import Data.Typeable | ||
|
||
import Prelude hiding (readFile, writeFile) | ||
|
||
import System.Directory | ||
import System.Environment (lookupEnv) | ||
|
||
import Test.Aeson.Internal.Utils | ||
import Test.Hspec | ||
import Test.HUnit.Lang (HUnitFailure) | ||
import Test.Aeson.Internal.RandomSamples | ||
import Test.Aeson.Internal.ADT.GoldenSpecs | ||
import Test.QuickCheck.Arbitrary.ADT | ||
|
||
|
||
|
||
roundtripADTFromFile :: forall a. | ||
(Typeable a, ToADTArbitrary a, Eq a, Show a, ToJSON a, FromJSON a) | ||
=> Settings -> Proxy a -> Spec | ||
roundtripADTFromFile settings proxy = do | ||
(moduleName,typeName,constructors) <- runIO $ generateInfoFromADT proxy | ||
describe ("JSON encoding of " ++ typeName) $ | ||
mapM_ (testConstructorRoundtrip settings moduleName typeName) constructors | ||
|
||
testConstructorRoundtrip :: forall a. (Eq a, Show a, FromJSON a, ToJSON a, ToADTArbitrary a) => | ||
Settings -> String -> String -> ConstructorArbitraryPair a -> SpecWith ( Arg (IO ())) | ||
testConstructorRoundtrip Settings{..} moduleName typeName cap = do | ||
it ("produces the same JSON as is found in " ++ goldenFile) $ do | ||
createMissing <- isJust <$> lookupEnv "CREATE_MISSING_GOLDEN" | ||
fileExist <- doesFileExist goldenFile | ||
if fileExist then | ||
runTestFromFile (Proxy @a) goldenFile | ||
`catches` | ||
[ | ||
Handler (\(err :: HUnitFailure) -> fixIfFlag err), | ||
Handler (\(err :: AesonDecodeError) -> fixIfFlag err) | ||
] | ||
else if createMissing then | ||
createGoldenFile sampleSize cap goldenFile | ||
else | ||
expectationFailure $ "Missing golden file: " ++ goldenFile | ||
|
||
where | ||
goldenFile = mkGoldenFilePath topDir mModuleName typeName cap | ||
topDir = case goldenDirectoryOption of | ||
GoldenDirectory -> "golden" | ||
CustomDirectoryName d -> d | ||
mModuleName = case useModuleNameAsSubDirectory of | ||
True -> Just moduleName | ||
False -> Nothing | ||
|
||
fixIfFlag err = do | ||
doFix <- isJust <$> lookupEnv "RECREATE_BROKEN_GOLDEN" | ||
if doFix then do | ||
createGoldenFile sampleSize cap goldenFile | ||
else | ||
throwIO err | ||
|
||
runTestFromFile :: forall a . (Eq a, Show a, ToJSON a, FromJSON a) => Proxy a -> String -> IO () | ||
runTestFromFile _proxy goldenFile = do | ||
bytes <- readFile goldenFile | ||
case decode bytes of | ||
Just (randomSamples :: RandomSamples Value) -> | ||
let jsonList = samples randomSamples in | ||
case traverse fromJSON jsonList of | ||
Data.Aeson.Success (decodedData :: [a]) -> | ||
fmap toJSON decodedData `shouldBe` jsonList | ||
Data.Aeson.Error err -> expectationFailure err | ||
Nothing -> expectationFailure "Cannot decode JSON" |
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,83 @@ | ||
{-| | ||
Module : Test.Aeson.Internal.RoundtripFromFile | ||
Description : Golden tests for Arbitrary | ||
Copyright : (c) Plow Technologies, 2022 | ||
License : BSD3 | ||
Maintainer : [email protected] | ||
Stability : Beta | ||
Internal module, use at your own risk. | ||
-} | ||
|
||
{-# LANGUAGE NamedFieldPuns #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE RecordWildCards #-} | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
{-# LANGUAGE TypeApplications #-} | ||
{-# LANGUAGE RankNTypes #-} | ||
module Test.Aeson.Internal.RoundtripFromFile where | ||
|
||
import Control.Exception | ||
import Control.Monad | ||
|
||
import Data.Aeson | ||
import Data.ByteString.Lazy hiding (putStrLn) | ||
import Data.Maybe (isJust) | ||
import Data.Proxy | ||
import Data.Typeable | ||
|
||
import Prelude hiding (readFile, writeFile) | ||
|
||
import System.Directory | ||
import System.Environment (lookupEnv) | ||
import System.FilePath | ||
|
||
import Test.Aeson.Internal.Utils | ||
import Test.Hspec | ||
import Test.HUnit.Lang (HUnitFailure) | ||
import Test.QuickCheck | ||
import Test.Aeson.Internal.RandomSamples | ||
import Test.Aeson.Internal.GoldenSpecs | ||
|
||
|
||
roundtripFromFile :: forall a. | ||
(Typeable a, Arbitrary a, Eq a, Show a, ToJSON a, FromJSON a) | ||
=> Settings -> Proxy a -> Spec | ||
roundtripFromFile settings proxy = do | ||
typeNameInfo <- runIO $ mkTypeNameInfo settings proxy | ||
let goldenFile = mkGoldenFile typeNameInfo | ||
|
||
createMissing <- runIO $ isJust <$> lookupEnv "CREATE_MISSING_GOLDEN" | ||
let fixIfFlag err = do | ||
doFix <- isJust <$> lookupEnv "RECREATE_BROKEN_GOLDEN" | ||
if doFix then do | ||
createGoldenfile settings proxy goldenFile | ||
else | ||
throwIO err | ||
describe ("JSON encoding of " ++ addBrackets (unTypeName $ typeNameTypeName typeNameInfo)) $ | ||
it ("produces the same JSON as is found in " ++ goldenFile) $ do | ||
|
||
fileExist <- doesFileExist goldenFile | ||
if fileExist then | ||
runTestFromFile proxy goldenFile | ||
`catches` | ||
[ | ||
Handler (\(err :: HUnitFailure) -> fixIfFlag err), | ||
Handler (\(err :: AesonDecodeError) -> fixIfFlag err) | ||
] | ||
else if createMissing then | ||
createGoldenfile settings proxy goldenFile | ||
else | ||
expectationFailure $ "Missing golden file: " ++ goldenFile | ||
|
||
runTestFromFile :: forall a . (Eq a, Show a, ToJSON a, FromJSON a) => Proxy a -> String -> IO () | ||
runTestFromFile _proxy goldenFile = do | ||
bytes <- readFile goldenFile | ||
case decode bytes of | ||
Just (randomSamples :: RandomSamples Value) -> | ||
let jsonList = samples randomSamples in | ||
case traverse fromJSON jsonList of | ||
Data.Aeson.Success (decodedData :: [a]) -> | ||
fmap toJSON decodedData `shouldBe` jsonList | ||
Data.Aeson.Error err -> expectationFailure err | ||
Nothing -> expectationFailure "Cannot decode JSON" |
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