Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customize name and encoding #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/Test/Aeson/Internal/ADT/GoldenSpecs.hs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ goldenADTSpecs settings proxy = goldenADTSpecsWithNote settings proxy Nothing
goldenADTSpecsWithNote :: forall a. (ToADTArbitrary a, Eq a, Show a, ToJSON a, FromJSON a) =>
Settings -> Proxy a -> Maybe String -> Spec
goldenADTSpecsWithNote settings Proxy mNote = do
(moduleName,(typeName,constructors)) <- runIO $ fmap (adtModuleName &&& adtTypeName &&& adtCAPs) <$> generate $ toADTArbitrary (Proxy :: Proxy a)
describe ("JSON encoding of " ++ typeName ++ note) $
(moduleName,(typeName',constructors)) <- runIO $ fmap (adtModuleName &&& adtTypeName &&& adtCAPs) <$> generate $ toADTArbitrary (Proxy :: Proxy a)
let typeName = typeNameModifier settings typeName'
describe (encodingFormat settings ++ " encoding of " ++ typeName ++ note) $
mapM_ (testConstructor settings moduleName typeName) constructors
where
note = maybe "" (" " ++) mNote
Expand All @@ -73,7 +74,7 @@ goldenADTSpecsWithNote settings Proxy mNote = do
testConstructor :: forall a. (Eq a, Show a, FromJSON a, ToJSON a, ToADTArbitrary a) =>
Settings -> String -> String -> ConstructorArbitraryPair a -> SpecWith ( Arg (IO ()))
testConstructor Settings{..} moduleName typeName cap = do
it ("produces the same JSON as is found in " ++ goldenFile) $ do
it ("produces the same " ++ encodingFormat ++ " as is found in " ++ goldenFile) $ do
exists <- doesFileExist goldenFile
if exists
then compareWithGolden randomMismatchOption topDir mModuleName typeName cap goldenFile
Expand Down
4 changes: 2 additions & 2 deletions src/Test/Aeson/Internal/GoldenSpecs.hs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ goldenSpecsWithNotePlain settings@Settings{..} typeNameInfo@(TypeNameInfo{typeNa
let goldenFile = mkGoldenFile typeNameInfo
note = maybe "" (" " ++) mNote

describe ("JSON encoding of " ++ addBrackets (unTypeName typeNameTypeName) ++ note) $
it ("produces the same JSON as is found in " ++ goldenFile) $ do
describe (encodingFormat ++ " encoding of " ++ addBrackets (unTypeName typeNameTypeName) ++ note) $
it ("produces the same " ++ encodingFormat ++ " as is found in " ++ goldenFile) $ do
exists <- doesFileExist goldenFile
if exists
then compareWithGolden typeNameInfo proxy goldenFile comparisonFile
Expand Down
12 changes: 9 additions & 3 deletions src/Test/Aeson/Internal/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import Data.Proxy
import Data.Typeable

import Prelude
import Data.Char

import Test.Hspec
import Test.QuickCheck
Expand Down Expand Up @@ -53,14 +54,18 @@ data Settings = Settings
-- ^ Whether to create a separate comparison file or ovewrite the golden file.
, randomMismatchOption :: RandomMismatchOption
-- ^ Whether to output a warning or fail the test when the random seed produces different values than the values in the golden file.
, typeNameModifier :: String -> String
-- ^ How to construct the type name from the string version of a type
, encodingFormat :: String
-- ^ The encoding format that is being tested
}

-- | A custom directory name or a preselected directory name.
data GoldenDirectoryOption = CustomDirectoryName String | GoldenDirectory

-- | The default settings for general use cases.
defaultSettings :: Settings
defaultSettings = Settings GoldenDirectory False 5 FaultyFile RandomMismatchWarning
defaultSettings = Settings GoldenDirectory False 5 FaultyFile RandomMismatchWarning id "JSON"

-- | put brackets around a String.
addBrackets :: String -> String
Expand Down Expand Up @@ -135,13 +140,14 @@ data TypeNameInfo a =

mkTypeNameInfo :: forall a . Arbitrary a => Typeable a => Settings -> Proxy a -> IO (TypeNameInfo a)
mkTypeNameInfo (Settings { useModuleNameAsSubDirectory
, goldenDirectoryOption}) proxy = do
, goldenDirectoryOption
, typeNameModifier }) proxy = do
maybeModuleName <- maybeModuleNameIO
return $ TypeNameInfo (TopDir topDir )
(ModuleName <$> maybeModuleName )
(TypeName typeName)
where
typeName = show (typeRep proxy)
typeName = typeNameModifier (show (typeRep proxy))
maybeModuleNameIO =
if useModuleNameAsSubDirectory
then do
Expand Down