1
1
mirror of https://github.com/github/semantic.git synced 2024-12-22 22:31:36 +03:00
semantic/test/JSONTestCase.hs

70 lines
3.2 KiB
Haskell
Raw Normal View History

{-# LANGUAGE DeriveAnyClass, OverloadedStrings #-}
2016-10-06 02:17:46 +03:00
module JSONTestCase where
2016-10-27 01:01:15 +03:00
import Data.Aeson
import Data.Aeson.Types
2016-10-06 02:17:46 +03:00
import Data.Map.Strict as Map
2016-10-28 04:23:31 +03:00
import Data.HashMap.Strict as HM
2016-10-06 02:17:46 +03:00
import Prelude
import Prologue
2016-11-01 18:06:36 +03:00
data JSONMetaRepo = JSONMetaRepo { repoUrl :: !String
2016-10-06 02:17:46 +03:00
, language :: !String
, fileExt :: !String
2016-10-06 02:17:46 +03:00
, syntaxes :: ![JSONMetaSyntax]
2016-10-18 23:10:22 +03:00
, templateText :: !(Maybe String)
2016-10-06 02:17:46 +03:00
} deriving (Show, Generic, FromJSON)
data JSONMetaSyntax = JSONMetaSyntax { template :: !(Maybe String)
, syntax :: !String
2016-10-06 02:17:46 +03:00
, insert :: !String
, replacement :: !String
} deriving (Show, Generic, FromJSON)
data JSONTestCase = JSONTestCase { gitDir :: !String
, testCaseDescription :: !String
, filePaths :: ![String]
, shas :: !String
, patch :: ![String]
2016-11-01 18:06:36 +03:00
, expectedResult :: !ExpectedResult
2016-10-06 02:17:46 +03:00
} deriving (Show, Generic, FromJSON)
data ExpectedResult = SummaryResult (Map Text (Map Text [Value]))
| JSONResult (Map Text Value)
deriving (Show, Generic, Eq)
-- | These replace the defaultOptions normally used by genericToEncoding.
-- | All options are default except for `sumEncoding`, which uses the `UntaggedValue`
-- | option to prevent the sum type `ExpectedResult` from encoding with a `tag` and `contents`
-- | fields when a JSONTestCase is encoded.
jsonTestCaseOptions :: Options
jsonTestCaseOptions = Options { fieldLabelModifier = id
, constructorTagModifier = id
, allNullaryToStringTag = False
, omitNothingFields = True
, sumEncoding = UntaggedValue
, unwrapUnaryRecords = False
}
2016-10-06 02:17:46 +03:00
instance ToJSON JSONTestCase where
toJSON = genericToJSON jsonTestCaseOptions
toEncoding = genericToEncoding jsonTestCaseOptions
instance ToJSON ExpectedResult where
toJSON = genericToJSON jsonTestCaseOptions
toEncoding = genericToEncoding jsonTestCaseOptions
-- | We have to parse the specific formats of the ExpectedResults based on their keys.
-- | This is how we determine which ExpectedResult constructor to use.
instance FromJSON ExpectedResult where
parseJSON = Data.Aeson.withObject "ExpectedResult" $ \o ->
SummaryResult <$> summaryResultValues o <|>
JSONResult <$> jsonResultValues o
where
jsonResultValues :: Object -> Parser (Map Text Value)
jsonResultValues o = Map.fromList <$> (fromKey "oids" <> fromKey "rows" <> fromKey "paths")
where fromKey k = (\a -> [(k, a)]) <$> o .: k
summaryResultValues :: Object -> Parser (Map Text (Map Text [Value]))
summaryResultValues o = Map.fromList <$> (fromKey "changes" <> fromKey "errors")
where fromKey k = (\a -> [(k :: Text, Map.fromList . HM.toList $ a )] ) <$> o .: k