aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVolpeon <github@volpeon.ink>2023-05-28 10:18:49 +0200
committerVolpeon <github@volpeon.ink>2023-05-28 10:18:49 +0200
commitb2c846b0daf9d6e26e1e2a07fecc848b4732baa0 (patch)
treed76c99df7a6e52301d41c59eac1736137062ceac
parentCompleted untested Flattening implementation (diff)
downloadhs-jsonld-b2c846b0daf9d6e26e1e2a07fecc848b4732baa0.tar.gz
hs-jsonld-b2c846b0daf9d6e26e1e2a07fecc848b4732baa0.tar.bz2
hs-jsonld-b2c846b0daf9d6e26e1e2a07fecc848b4732baa0.zip
Fixed flattening errors
-rw-r--r--jsonld.cabal2
-rw-r--r--package.yaml1
-rw-r--r--src/Data/JLD.hs40
-rw-r--r--src/Data/JLD/Flattening.hs2
-rw-r--r--src/Data/JLD/Flattening/NodeMap.hs78
-rw-r--r--test/Test/Common.hs35
-rw-r--r--test/Test/Expansion.hs29
-rw-r--r--test/Test/Flattening.hs36
8 files changed, 153 insertions, 70 deletions
diff --git a/jsonld.cabal b/jsonld.cabal
index 0efa3b2..376a647 100644
--- a/jsonld.cabal
+++ b/jsonld.cabal
@@ -75,6 +75,7 @@ library
75 , megaparsec 75 , megaparsec
76 , modern-uri 76 , modern-uri
77 , mtl 77 , mtl
78 , pretty-simple
78 , rdf4h 79 , rdf4h
79 , relude 80 , relude
80 , req 81 , req
@@ -120,6 +121,7 @@ test-suite jsonld-test
120 , megaparsec 121 , megaparsec
121 , modern-uri 122 , modern-uri
122 , mtl 123 , mtl
124 , pretty-simple
123 , rdf4h 125 , rdf4h
124 , relude 126 , relude
125 , req 127 , req
diff --git a/package.yaml b/package.yaml
index 60226fb..adc0acb 100644
--- a/package.yaml
+++ b/package.yaml
@@ -28,6 +28,7 @@ dependencies:
28 - megaparsec 28 - megaparsec
29 - modern-uri 29 - modern-uri
30 - mtl 30 - mtl
31 - pretty-simple
31 - rdf4h 32 - rdf4h
32 - relude 33 - relude
33 - req 34 - req
diff --git a/src/Data/JLD.hs b/src/Data/JLD.hs
index d7688d0..a7042dc 100644
--- a/src/Data/JLD.hs
+++ b/src/Data/JLD.hs
@@ -4,6 +4,7 @@ module Data.JLD (
4 module Data.JLD.Options, 4 module Data.JLD.Options,
5 JLDExpansionParams (..), 5 JLDExpansionParams (..),
6 JLDExpansionState (..), 6 JLDExpansionState (..),
7 JLDFlatteningParams (..),
7 expand, 8 expand,
8 flatten, 9 flatten,
9) where 10) where
@@ -100,9 +101,44 @@ expand document baseUrl paramsFn = do
100 101
101 pure (result', state') 102 pure (result', state')
102 103
103flatten :: Monad m => Value -> URI -> (JLDExpansionParams () m -> JLDExpansionParams e m) -> m (Either (JLDError e) Value, JLDExpansionState) 104data JLDFlatteningParams e m = JLDFlatteningParams
105 { jldFlatteningParamsDocumentLoader :: DocumentLoader e m
106 , jldFlatteningParamsProcessingMode :: JLDVersion
107 , jldFlatteningParamsMaxRemoteContexts :: Int
108 , jldFlatteningParamsExpandContext :: Maybe Value
109 , jldFlatteningParamsFrameExpansion :: Bool
110 , jldFlatteningParamsState :: JLDExpansionState
111 , jldFlatteningParamsCompactArrays :: Bool
112 }
113 deriving (Show)
114
115flatten :: Monad m => Value -> URI -> (JLDFlatteningParams () m -> JLDFlatteningParams e m) -> m (Either (JLDError e) Value, JLDExpansionState)
104flatten document baseUrl paramsFn = do 116flatten document baseUrl paramsFn = do
105 (result, state') <- expand document baseUrl paramsFn 117 let JLDFlatteningParams{..} =
118 paramsFn
119 JLDFlatteningParams
120 { jldFlatteningParamsDocumentLoader = DocumentLoader <. const <. pure <| Left ()
121 , jldFlatteningParamsProcessingMode = JLD1_1
122 , jldFlatteningParamsMaxRemoteContexts = 20
123 , jldFlatteningParamsExpandContext = Nothing
124 , jldFlatteningParamsFrameExpansion = False
125 , jldFlatteningParamsState =
126 JLDExpansionState
127 { jldExpansionStateContextCache = mempty
128 , jldExpansionStateDocumentCache = mempty
129 }
130 , jldFlatteningParamsCompactArrays = True
131 }
132 expansionParams =
133 JLDExpansionParams
134 { jldExpansionParamsDocumentLoader = jldFlatteningParamsDocumentLoader
135 , jldExpansionParamsProcessingMode = jldFlatteningParamsProcessingMode
136 , jldExpansionParamsMaxRemoteContexts = jldFlatteningParamsMaxRemoteContexts
137 , jldExpansionParamsExpandContext = jldFlatteningParamsExpandContext
138 , jldExpansionParamsFrameExpansion = jldFlatteningParamsFrameExpansion
139 , jldExpansionParamsState = jldFlatteningParamsState
140 }
141 (result, state') <- expand document baseUrl (const expansionParams)
106 case result of 142 case result of
107 Left err -> pure (Left err, state') 143 Left err -> pure (Left err, state')
108 Right expanded -> fmap (,state') <. runExceptT <| F.flatten expanded 144 Right expanded -> fmap (,state') <. runExceptT <| F.flatten expanded
diff --git a/src/Data/JLD/Flattening.hs b/src/Data/JLD/Flattening.hs
index 2bfd8dd..3a8c726 100644
--- a/src/Data/JLD/Flattening.hs
+++ b/src/Data/JLD/Flattening.hs
@@ -34,7 +34,7 @@ collectNodesStep ar node
34flatten :: Monad m => Value -> JLDFlatteningT e m Value 34flatten :: Monad m => Value -> JLDFlatteningT e m Value
35flatten element = do 35flatten element = do
36 -- 1. 2. 36 -- 1. 2.
37 nodeMap <- fst <$> buildNodeMap element id 37 (nodeMap, _, _, _) <- buildNodeMap element id
38 38
39 -- 3. 4. 39 -- 3. 4.
40 let defaultGraph = fromMaybe mempty <| M.lookup (show KeywordDefault) nodeMap 40 let defaultGraph = fromMaybe mempty <| M.lookup (show KeywordDefault) nodeMap
diff --git a/src/Data/JLD/Flattening/NodeMap.hs b/src/Data/JLD/Flattening/NodeMap.hs
index 65db9ab..6c35302 100644
--- a/src/Data/JLD/Flattening/NodeMap.hs
+++ b/src/Data/JLD/Flattening/NodeMap.hs
@@ -19,6 +19,7 @@ import Data.Aeson.KeyMap qualified as KM (filterWithKey, insert, lookup, member,
19import Data.Foldable.WithIndex (FoldableWithIndex (..), iforM_) 19import Data.Foldable.WithIndex (FoldableWithIndex (..), iforM_)
20import Data.Map.Strict qualified as M (insert, lookup) 20import Data.Map.Strict qualified as M (insert, lookup)
21import Data.Vector qualified as V (singleton, snoc, uniq) 21import Data.Vector qualified as V (singleton, snoc, uniq)
22import Debug.Pretty.Simple (pTraceShowM)
22 23
23type BNMT e m = REST BNMEnv (Either (JLDError e) ()) BNMState m 24type BNMT e m = REST BNMEnv (Either (JLDError e) ()) BNMState m
24 25
@@ -45,6 +46,8 @@ data BNMParams = BNMParams
45 , bnmParamsActiveProperty :: Maybe Text 46 , bnmParamsActiveProperty :: Maybe Text
46 , bnmParamsList :: Maybe Array 47 , bnmParamsList :: Maybe Array
47 , bnmParamsReferenceNode :: Maybe Object 48 , bnmParamsReferenceNode :: Maybe Object
49 , bnmParamsIdentifierCounter :: Int
50 , bnmParamsIdentifierMap :: Map Text Text
48 } 51 }
49 deriving (Show, Eq) 52 deriving (Show, Eq)
50 53
@@ -78,19 +81,26 @@ bnmBuildNodeMap :: Monad m => Value -> (BNMParams -> BNMParams) -> BNMT e m (May
78bnmBuildNodeMap value paramsFn = do 81bnmBuildNodeMap value paramsFn = do
79 BNMEnv{..} <- ask 82 BNMEnv{..} <- ask
80 nodeMap <- gets bnmStateNodeMap 83 nodeMap <- gets bnmStateNodeMap
84 identifierCounter <- gets bnmStateIdentifierCounter
85 identifierMap <- gets bnmStateIdentifierMap
81 let params p = 86 let params p =
82 paramsFn 87 paramsFn
83 p 88 p
84 { bnmParamsNodeMap = nodeMap 89 { bnmParamsNodeMap = nodeMap
85 , bnmParamsActiveGraph = bnmEnvActiveGraph 90 , bnmParamsActiveGraph = bnmEnvActiveGraph
86 , bnmParamsActiveSubject = bnmEnvActiveSubject 91 , bnmParamsIdentifierCounter = identifierCounter
87 , bnmParamsActiveProperty = bnmEnvActiveProperty 92 , bnmParamsIdentifierMap = identifierMap
88 } 93 }
89 (nodeMap', list) <- 94 (nodeMap', list, identifierCounter', identifierMap') <-
90 buildNodeMap value params |> runExceptT >=> \case 95 buildNodeMap value params |> runExceptT >=> \case
91 Left err -> throwError <| Left err 96 Left err -> throwError <| Left err
92 Right a -> pure a 97 Right a -> pure a
93 bnmModifyNodeMap <| const nodeMap' 98 modify \st ->
99 st
100 { bnmStateNodeMap = nodeMap'
101 , bnmStateIdentifierCounter = identifierCounter'
102 , bnmStateIdentifierMap = identifierMap'
103 }
94 pure list 104 pure list
95 105
96buildNodeMap' :: Monad m => Value -> BNMT e m () 106buildNodeMap' :: Monad m => Value -> BNMT e m ()
@@ -138,7 +148,14 @@ buildNodeMap' element = case element of
138 -- 5. 148 -- 5.
139 | Just elemList <- KM.lookup (show KeywordList) elementObject' -> do 149 | Just elemList <- KM.lookup (show KeywordList) elementObject' -> do
140 -- 5.1. 5.2. 150 -- 5.1. 5.2.
141 subList <- listToObject <$> bnmBuildNodeMap elemList id 151 subList <-
152 listToObject <$> bnmBuildNodeMap elemList \params ->
153 params
154 { bnmParamsActiveSubject = bnmEnvActiveSubject
155 , bnmParamsActiveProperty = bnmEnvActiveProperty
156 , bnmParamsList = Just mempty
157 , bnmParamsReferenceNode = bnmEnvReferenceNode
158 }
142 159
143 gets bnmStateList >>= \case 160 gets bnmStateList >>= \case
144 -- 5.3. 161 -- 5.3.
@@ -152,24 +169,25 @@ buildNodeMap' element = case element of
152 | isNodeObject (Object elementObject') -> do 169 | isNodeObject (Object elementObject') -> do
153 id' <- case KM.lookup (show KeywordId) elementObject' of 170 id' <- case KM.lookup (show KeywordId) elementObject' of
154 -- 6.1. 171 -- 6.1.
155 Just (String idValue) | isBlankIri idValue -> bnmCreateIdentifier <| Just idValue 172 Just (String idValue)
173 | isBlankIri idValue -> bnmCreateIdentifier (Just idValue)
174 | otherwise -> pure idValue
156 Just _ -> throwError <| Right () 175 Just _ -> throwError <| Right ()
157 -- 6.2. 176 -- 6.2.
158 Nothing -> bnmCreateIdentifier Nothing 177 Nothing -> bnmCreateIdentifier Nothing
159 178
160 -- 6.3. 179 -- 6.3.
161 nodeMap <- gets bnmStateNodeMap 180 bnmModifyNodeMap \nodeMap ->
162 when (N.hasKey2 bnmEnvActiveGraph (Just id') nodeMap) do 181 if N.hasKey2 bnmEnvActiveGraph (Just id') nodeMap
163 bnmModifyNodeMap <| N.insert bnmEnvActiveGraph (Just id') (Just <| show KeywordId) (String id') 182 then nodeMap
183 else N.insert bnmEnvActiveGraph (Just id') (Just <| show KeywordId) (String id') nodeMap
164 184
165 nodeMap' <- gets bnmStateNodeMap
166 if 185 if
167 -- 6.5. 186 -- 6.5.
168 | Just referenceNode <- bnmEnvReferenceNode -> 187 | Just referenceNode <- bnmEnvReferenceNode -> bnmModifyNodeMap \nodeMap ->
169 unless (N.memberArray bnmEnvActiveGraph bnmEnvActiveSubject bnmEnvActiveProperty (Object referenceNode) nodeMap') do 188 if N.memberArray bnmEnvActiveGraph bnmEnvActiveSubject bnmEnvActiveProperty (Object referenceNode) nodeMap
170 bnmModifyNodeMap 189 then nodeMap
171 <. N.modifyArray bnmEnvActiveGraph bnmEnvActiveSubject bnmEnvActiveProperty 190 else N.modifyArray bnmEnvActiveGraph bnmEnvActiveSubject bnmEnvActiveProperty (`V.snoc` Object referenceNode) nodeMap
172 <| flip V.snoc (Object referenceNode)
173 -- 6.6. 191 -- 6.6.
174 | isJust bnmEnvActiveProperty -> do 192 | isJust bnmEnvActiveProperty -> do
175 -- 6.6.1. 193 -- 6.6.1.
@@ -177,11 +195,10 @@ buildNodeMap' element = case element of
177 195
178 gets bnmStateList >>= \case 196 gets bnmStateList >>= \case
179 -- 6.6.2. 197 -- 6.6.2.
180 Nothing -> 198 Nothing -> bnmModifyNodeMap \nodeMap ->
181 unless (N.memberArray bnmEnvActiveGraph bnmEnvActiveSubject bnmEnvActiveProperty reference nodeMap') do 199 if N.memberArray bnmEnvActiveGraph bnmEnvActiveSubject bnmEnvActiveProperty reference nodeMap
182 bnmModifyNodeMap 200 then nodeMap
183 <. N.modifyArray bnmEnvActiveGraph bnmEnvActiveSubject bnmEnvActiveProperty 201 else N.modifyArray bnmEnvActiveGraph bnmEnvActiveSubject bnmEnvActiveProperty (`V.snoc` reference) nodeMap
184 <| flip V.snoc reference
185 -- 6.6.3. 202 -- 6.6.3.
186 Just stateList -> bnmModifyList <. const <. Just <| V.snoc stateList reference 203 Just stateList -> bnmModifyList <. const <. Just <| V.snoc stateList reference
187 -- 204 --
@@ -202,10 +219,10 @@ buildNodeMap' element = case element of
202 Nothing -> pure () 219 Nothing -> pure ()
203 220
204 -- 6.8. 221 -- 6.8.
205 nodeMap'' <- gets bnmStateNodeMap 222 nodeMap' <- gets bnmStateNodeMap
206 case KM.lookup (show KeywordIndex) elementObject' of 223 case KM.lookup (show KeywordIndex) elementObject' of
207 Just indexValue 224 Just indexValue
208 | N.hasKey3 bnmEnvActiveGraph (Just id') (Just <| show KeywordIndex) nodeMap'' -> throwError <| Left ConflictingIndexes 225 | N.hasKey3 bnmEnvActiveGraph (Just id') (Just <| show KeywordIndex) nodeMap' -> throwError <| Left ConflictingIndexes
209 | otherwise -> bnmModifyNodeMap <| N.insert bnmEnvActiveGraph (Just id') (Just <| show KeywordType) indexValue 226 | otherwise -> bnmModifyNodeMap <| N.insert bnmEnvActiveGraph (Just id') (Just <| show KeywordType) indexValue
210 -- 227 --
211 Nothing -> pure () 228 Nothing -> pure ()
@@ -257,9 +274,10 @@ buildNodeMap' element = case element of
257 else pure property 274 else pure property
258 275
259 -- 6.12.2. 276 -- 6.12.2.
260 gets (bnmStateNodeMap .> N.hasKey3 bnmEnvActiveGraph (Just id') (Just property')) >>= \case 277 bnmModifyNodeMap \nodeMap ->
261 True -> pure () 278 if N.hasKey3 bnmEnvActiveGraph (Just id') (Just property') nodeMap
262 False -> bnmModifyNodeMap <| N.insert bnmEnvActiveGraph (Just id') (Just property') (Array mempty) 279 then nodeMap
280 else N.insert bnmEnvActiveGraph (Just id') (Just property') (Array mempty) nodeMap
263 281
264 void <| bnmBuildNodeMap value \params -> 282 void <| bnmBuildNodeMap value \params ->
265 params 283 params
@@ -273,12 +291,12 @@ buildNodeMap' element = case element of
273 -- 291 --
274 _ -> pure () 292 _ -> pure ()
275 293
276buildNodeMap :: Monad m => Value -> (BNMParams -> BNMParams) -> JLDFlatteningT e m (NodeMap, Maybe Array) 294buildNodeMap :: Monad m => Value -> (BNMParams -> BNMParams) -> JLDFlatteningT e m (NodeMap, Maybe Array, Int, Map Text Text)
277buildNodeMap document paramsFn = do 295buildNodeMap document paramsFn = do
278 (result, BNMState{..}) <- buildNodeMap' document |> runREST env st 296 (result, BNMState{..}) <- buildNodeMap' document |> runREST env st
279 case result of 297 case result of
280 Left (Left err) -> throwError err 298 Left (Left err) -> throwError err
281 _ -> pure (bnmStateNodeMap, bnmStateList) 299 _ -> pure (bnmStateNodeMap, bnmStateList, bnmStateIdentifierCounter, bnmStateIdentifierMap)
282 where 300 where
283 BNMParams{..} = 301 BNMParams{..} =
284 paramsFn 302 paramsFn
@@ -289,6 +307,8 @@ buildNodeMap document paramsFn = do
289 , bnmParamsActiveProperty = Nothing 307 , bnmParamsActiveProperty = Nothing
290 , bnmParamsList = mempty 308 , bnmParamsList = mempty
291 , bnmParamsReferenceNode = Nothing 309 , bnmParamsReferenceNode = Nothing
310 , bnmParamsIdentifierCounter = 0
311 , bnmParamsIdentifierMap = mempty
292 } 312 }
293 313
294 env = 314 env =
@@ -303,8 +323,8 @@ buildNodeMap document paramsFn = do
303 BNMState 323 BNMState
304 { bnmStateNodeMap = bnmParamsNodeMap 324 { bnmStateNodeMap = bnmParamsNodeMap
305 , bnmStateList = bnmParamsList 325 , bnmStateList = bnmParamsList
306 , bnmStateIdentifierCounter = 1 326 , bnmStateIdentifierCounter = bnmParamsIdentifierCounter
307 , bnmStateIdentifierMap = mempty 327 , bnmStateIdentifierMap = bnmParamsIdentifierMap
308 } 328 }
309 329
310mergeNodeMaps :: NodeMap -> NodeMap 330mergeNodeMaps :: NodeMap -> NodeMap
diff --git a/test/Test/Common.hs b/test/Test/Common.hs
index ffc3264..e386551 100644
--- a/test/Test/Common.hs
+++ b/test/Test/Common.hs
@@ -1,24 +1,20 @@
1module Test.Common (W3CTestList (..), W3CTest (..), W3CTestOption (..), fetchTest, parseExpansionOptions) where 1module Test.Common (W3CTestList (..), W3CTest (..), W3CTestOption (..), documentLoader, fetchTest) where
2 2
3import Data.JLD.Prelude 3import Data.JLD.Prelude
4 4
5import Test.Tasty
6import Test.Tasty.ExpectedFailure (ignoreTestBecause)
7import Test.Tasty.HUnit (assertFailure, testCase, (@?=))
8
9import Data.Aeson (FromJSON (..), Value (..), (.:), (.:?)) 5import Data.Aeson (FromJSON (..), Value (..), (.:), (.:?))
10import Data.Aeson.Types (prependFailure, typeMismatch) 6import Data.Aeson.Types (prependFailure, typeMismatch)
11import Data.JLD (DocumentLoader (..), JLDExpansionParams (..), JLDVersion (..), mimeType, toJldErrorCode) 7import Data.JLD (DocumentLoader (..), mimeType)
12import Data.JLD.Model.URI (parseUri)
13import Data.Maybe (fromJust) 8import Data.Maybe (fromJust)
14import Network.HTTP.Req (GET (..), NoReqBody (..), defaultHttpConfig, header, jsonResponse, req, responseBody, runReq, useHttpsURI, useURI) 9import Network.HTTP.Req (GET (..), NoReqBody (..), defaultHttpConfig, header, jsonResponse, req, responseBody, runReq, useHttpsURI, useURI)
15import Text.URI (URI, mkURI, relativeTo) 10import Text.URI (URI)
16 11
17data W3CTestOption = W3CTestOption 12data W3CTestOption = W3CTestOption
18 { w3cTestOptionSpecVersion :: Maybe Text 13 { w3cTestOptionSpecVersion :: Maybe Text
19 , w3cTestOptionProcessingMode :: Maybe Text 14 , w3cTestOptionProcessingMode :: Maybe Text
20 , w3cTestOptionBase :: Maybe Text 15 , w3cTestOptionBase :: Maybe Text
21 , w3cTestOptionExpandContext :: Maybe Text 16 , w3cTestOptionExpandContext :: Maybe Text
17 , w3cTestOptionCompactArrays :: Maybe Bool
22 } 18 }
23 deriving (Show) 19 deriving (Show)
24 20
@@ -29,6 +25,7 @@ instance FromJSON W3CTestOption where
29 <*> (v .:? "processingMode") 25 <*> (v .:? "processingMode")
30 <*> (v .:? "base") 26 <*> (v .:? "base")
31 <*> (v .:? "expandContext") 27 <*> (v .:? "expandContext")
28 <*> (v .:? "compactArrays")
32 parseJSON invalid = prependFailure "parsing W3CTestOption failed, " (typeMismatch "Object" invalid) 29 parseJSON invalid = prependFailure "parsing W3CTestOption failed, " (typeMismatch "Object" invalid)
33 30
34data W3CTest = W3CTest 31data W3CTest = W3CTest
@@ -72,25 +69,3 @@ fetchTest url = do
72 runReq defaultHttpConfig do 69 runReq defaultHttpConfig do
73 res <- req GET reqUrl NoReqBody jsonResponse (reqOptions <> header "Accept" mimeType) 70 res <- req GET reqUrl NoReqBody jsonResponse (reqOptions <> header "Accept" mimeType)
74 pure <| responseBody res 71 pure <| responseBody res
75
76parseExpansionOptions :: URI -> URI -> Maybe W3CTestOption -> IO (URI, JLDExpansionParams () IO -> JLDExpansionParams Text IO)
77parseExpansionOptions baseUrl inputUrl maybeOptions = do
78 expandContext <- case maybeOptions >>= w3cTestOptionExpandContext of
79 Just rawUrl -> do
80 url <- fromJust <. (`relativeTo` baseUrl) <$> mkURI rawUrl
81 Just <$> fetchTest url
82 Nothing -> pure Nothing
83
84 let params p =
85 p
86 { jldExpansionParamsDocumentLoader = documentLoader
87 , jldExpansionParamsProcessingMode = case maybeOptions >>= w3cTestOptionProcessingMode of
88 Just "json-ld-1.0" -> JLD1_0
89 Just "json-ld-1.1" -> JLD1_1
90 _ -> jldExpansionParamsProcessingMode p
91 , jldExpansionParamsExpandContext = expandContext <|> jldExpansionParamsExpandContext p
92 }
93
94 pure (expandBaseUrl, params)
95 where
96 expandBaseUrl = fromMaybe inputUrl (parseUri =<< w3cTestOptionBase =<< maybeOptions)
diff --git a/test/Test/Expansion.hs b/test/Test/Expansion.hs
index b5b1e07..0d553a7 100644
--- a/test/Test/Expansion.hs
+++ b/test/Test/Expansion.hs
@@ -6,10 +6,33 @@ import Test.Tasty
6import Test.Tasty.ExpectedFailure (ignoreTestBecause) 6import Test.Tasty.ExpectedFailure (ignoreTestBecause)
7import Test.Tasty.HUnit 7import Test.Tasty.HUnit
8 8
9import Data.JLD (expand, toJldErrorCode) 9import Data.JLD (JLDExpansionParams (..), JLDVersion (..), expand, toJldErrorCode)
10import Data.JLD.Model.URI (parseUri)
10import Data.Maybe (fromJust) 11import Data.Maybe (fromJust)
11import Test.Common (W3CTest (..), W3CTestList (..), W3CTestOption (..), fetchTest, parseExpansionOptions) 12import Test.Common (W3CTest (..), W3CTestList (..), W3CTestOption (..), documentLoader, fetchTest)
12import Text.URI (mkURI, relativeTo) 13import Text.URI (URI, mkURI, relativeTo)
14
15parseExpansionOptions :: URI -> URI -> Maybe W3CTestOption -> IO (URI, JLDExpansionParams () IO -> JLDExpansionParams Text IO)
16parseExpansionOptions baseUrl inputUrl maybeOptions = do
17 expandContext <- case maybeOptions >>= w3cTestOptionExpandContext of
18 Just rawUrl -> do
19 url <- fromJust <. (`relativeTo` baseUrl) <$> mkURI rawUrl
20 Just <$> fetchTest url
21 Nothing -> pure Nothing
22
23 let params p =
24 p
25 { jldExpansionParamsDocumentLoader = documentLoader
26 , jldExpansionParamsProcessingMode = case maybeOptions >>= w3cTestOptionProcessingMode of
27 Just "json-ld-1.0" -> JLD1_0
28 Just "json-ld-1.1" -> JLD1_1
29 _ -> jldExpansionParamsProcessingMode p
30 , jldExpansionParamsExpandContext = expandContext <|> jldExpansionParamsExpandContext p
31 }
32
33 pure (expandBaseUrl, params)
34 where
35 expandBaseUrl = fromMaybe inputUrl (parseUri =<< w3cTestOptionBase =<< maybeOptions)
13 36
14expansionTests :: W3CTestList -> TestTree 37expansionTests :: W3CTestList -> TestTree
15expansionTests testList = testGroup "Expansion" <| uncurry expansionTest <$> zip (w3cSequence testList) [1 ..] 38expansionTests testList = testGroup "Expansion" <| uncurry expansionTest <$> zip (w3cSequence testList) [1 ..]
diff --git a/test/Test/Flattening.hs b/test/Test/Flattening.hs
index bc64b88..76f5434 100644
--- a/test/Test/Flattening.hs
+++ b/test/Test/Flattening.hs
@@ -6,10 +6,36 @@ import Test.Tasty
6import Test.Tasty.ExpectedFailure (ignoreTestBecause) 6import Test.Tasty.ExpectedFailure (ignoreTestBecause)
7import Test.Tasty.HUnit 7import Test.Tasty.HUnit
8 8
9import Data.JLD (expand, flatten, toJldErrorCode) 9import Data.JLD (JLDFlatteningParams (..), JLDVersion (..), flatten, toJldErrorCode)
10import Data.JLD.Model.URI (parseUri)
10import Data.Maybe (fromJust) 11import Data.Maybe (fromJust)
11import Test.Common (W3CTest (..), W3CTestList (..), W3CTestOption (..), fetchTest, parseExpansionOptions) 12import Test.Common (W3CTest (..), W3CTestList (..), W3CTestOption (..), documentLoader, fetchTest)
12import Text.URI (mkURI, relativeTo) 13import Text.URI (URI, mkURI, relativeTo)
14
15parseFlatteningOptions :: URI -> URI -> Maybe W3CTestOption -> IO (URI, JLDFlatteningParams () IO -> JLDFlatteningParams Text IO)
16parseFlatteningOptions baseUrl inputUrl maybeOptions = do
17 expandContext <- case maybeOptions >>= w3cTestOptionExpandContext of
18 Just rawUrl -> do
19 url <- fromJust <. (`relativeTo` baseUrl) <$> mkURI rawUrl
20 Just <$> fetchTest url
21 Nothing -> pure Nothing
22
23 let params p =
24 p
25 { jldFlatteningParamsDocumentLoader = documentLoader
26 , jldFlatteningParamsProcessingMode = case maybeOptions >>= w3cTestOptionProcessingMode of
27 Just "json-ld-1.0" -> JLD1_0
28 Just "json-ld-1.1" -> JLD1_1
29 _ -> jldFlatteningParamsProcessingMode p
30 , jldFlatteningParamsExpandContext = expandContext <|> jldFlatteningParamsExpandContext p
31 , jldFlatteningParamsCompactArrays = case maybeOptions >>= w3cTestOptionCompactArrays of
32 Just b -> b
33 _ -> jldFlatteningParamsCompactArrays p
34 }
35
36 pure (expandBaseUrl, params)
37 where
38 expandBaseUrl = fromMaybe inputUrl (parseUri =<< w3cTestOptionBase =<< maybeOptions)
13 39
14flatteningTests :: W3CTestList -> TestTree 40flatteningTests :: W3CTestList -> TestTree
15flatteningTests testList = testGroup "Flattening" <| uncurry flatteningTest <$> zip (w3cSequence testList) [1 ..] 41flatteningTests testList = testGroup "Flattening" <| uncurry flatteningTest <$> zip (w3cSequence testList) [1 ..]
@@ -29,7 +55,7 @@ flatteningTest W3CTest{..} (show .> (<> ". " <> toString w3cTestName) -> testNam
29 inputJld <- fetchTest inputUrl 55 inputJld <- fetchTest inputUrl
30 expectJld <- fetchTest expectUrl 56 expectJld <- fetchTest expectUrl
31 57
32 (expandBaseUrl, params) <- parseExpansionOptions baseUrl inputUrl w3cTestOption 58 (expandBaseUrl, params) <- parseFlatteningOptions baseUrl inputUrl w3cTestOption
33 (result, _) <- flatten inputJld expandBaseUrl params 59 (result, _) <- flatten inputJld expandBaseUrl params
34 60
35 result @?= Right expectJld 61 result @?= Right expectJld
@@ -41,7 +67,7 @@ flatteningTest W3CTest{..} (show .> (<> ". " <> toString w3cTestName) -> testNam
41 67
42 inputJld <- fetchTest inputUrl 68 inputJld <- fetchTest inputUrl
43 69
44 (expandBaseUrl, params) <- parseExpansionOptions baseUrl inputUrl w3cTestOption 70 (expandBaseUrl, params) <- parseFlatteningOptions baseUrl inputUrl w3cTestOption
45 (result, _) <- flatten inputJld expandBaseUrl params 71 (result, _) <- flatten inputJld expandBaseUrl params
46 72
47 (result |> first toJldErrorCode) @?= Left expectErrorRaw 73 (result |> first toJldErrorCode) @?= Left expectErrorRaw