git.iko.soy/typeable/hjsonschema
on master help sign in
Break tests into local and remote; fix haddock.
commit79fc9f1c18d8a64cd0cd280a216c692617b1ba75 authorIan Grant Jeffries <ian@housejeffries.com> dateSun 8 Feb 2015, 00:42 (12 years ago) parent14fbe45
5 files changed+84−24
hjsonschema.cabal+26−4 src/Data/JsonSchema/JsonReference.hs+2−2 tests/Test.hs → tests/Lib.hs+20−18 tests/Local.hs+18 tests/Remote.hs+18
hjsonschema.cabal+26−4
@@ -1,5 +1,5 @@
11name: hjsonschema
2−version: 0.1.0.0
2+version: 0.1.1.0
33synopsis: Haskell implementation of JSON Schema v4.
44homepage: https://github.com/seagreen/hjsonschema
55license: MIT
@@ -8,8 +8,10 @@ author: Ian Grant Jeffries
88maintainer: ian@housejeffries.com
99category: Data
1010build-type: Simple
11−extra-source-files: README.md
1211cabal-version: >=1.10
12+extra-source-files: README.md
13+ JSON-Schema-Test-Suite/tests/draft4/*.json
14+ tests/Lib.hs
1315
1416library
1517 hs-source-dirs: src
@@ -33,10 +35,30 @@ library
3335 , vector >= 0.10 && < 0.11
3436 , wreq >= 0.3 && < 0.4
3537
36−test-suite official
38+test-suite local
39+ type: exitcode-stdio-1.0
40+ hs-source-dirs: tests
41+ main-is: Local.hs
42+ default-language: Haskell2010
43+ ghc-options: -Wall
44+ default-extensions: OverloadedStrings
45+ build-depends: aeson
46+ , base
47+ , bytestring
48+ , hjsonschema
49+ , unordered-containers
50+ , text
51+ , vector
52+ , directory >= 1.2 && < 1.3
53+ , filepath >= 1.3 && < 1.4
54+ , HUnit >= 1.2 && < 1.3
55+ , test-framework >= 0.8 && < 0.9
56+ , test-framework-hunit >= 0.3 && < 0.4
57+
58+test-suite remote
3759 type: exitcode-stdio-1.0
3860 hs-source-dirs: tests
39− main-is: Test.hs
61+ main-is: Remote.hs
4062 default-language: Haskell2010
4163 ghc-options: -Wall
4264 default-extensions: OverloadedStrings
src/Data/JsonSchema/JsonReference.hs+2−2
@@ -1,7 +1,7 @@
1−module Data.JsonSchema.JsonReference where
2−
31-- | There should be a JSON Reference library for haskell.
42
3+module Data.JsonSchema.JsonReference where
4+
55import Control.Exception
66import Control.Lens
77import Control.Monad
tests/Test.hs → tests/Lib.hsrenamed+20−18
@@ -1,6 +1,6 @@
11{-# LANGUAGE TemplateHaskell #-}
22
3−module Main where
3+module Lib where
44
55import Control.Applicative
66import Control.Monad
@@ -10,22 +10,15 @@ import qualified Data.ByteString.Lazy as LBS
1010import Data.Char (toLower)
1111import qualified Data.HashMap.Strict as H
1212import Data.JsonSchema
13−import Data.List (isSuffixOf)
1413import Data.Monoid
1514import Data.Text (Text)
1615import qualified Data.Text as T
1716import qualified Data.Vector as V
18−import System.Directory (getDirectoryContents)
1917import System.FilePath ((</>))
2018import Test.Framework
2119import Test.Framework.Providers.HUnit
2220import qualified Test.HUnit as HU
2321
24−main :: IO ()
25−main = do
26− ts <- readSchemaTests
27− defaultMain (toTest <$> ts)
28−
2922data SchemaTest = SchemaTest
3023 { _stDescription :: Text
3124 , _stSchema :: RawSchema
@@ -48,22 +41,31 @@ instance FromJSON SchemaTest where
4841 <*> o .: "schema"
4942 <*> o .: "tests" -- I wish this were "cases"
5043
51−readSchemaTests :: IO [SchemaTest]
52−readSchemaTests = do
53− jsonFiles <- filter (".json" `isSuffixOf`) <$> getDirectoryContents dir
54− fmap concat $ forM jsonFiles $ \file -> do
55− let fullPath = dir </> file
56− jsonBS <- LBS.readFile fullPath
57− case eitherDecode jsonBS of
58− Left err -> fail $ "couldn't parse file '" <> fullPath <> "': " <> err
59− Right schemas -> return $ prependFileName file <$> schemas
44+isLocal :: String -> Bool
45+isLocal file = (file /= "definitions.json")
46+ && (file /= "ref.json")
47+ && (file /= "refRemote.json")
48+
49+readSchemaTests :: String -> [String] -> IO [SchemaTest]
50+readSchemaTests dir jsonFiles = concatMapM fileToCases jsonFiles
6051 where
61− dir = "JSON-Schema-Test-Suite/tests/draft4"
52+ -- Each file contains an array of SchemaTests, not just one.
53+ fileToCases :: String -> IO [SchemaTest]
54+ fileToCases name = do
55+ let fullPath = dir </> name
56+ jsonBS <- LBS.readFile fullPath
57+ case eitherDecode jsonBS of
58+ Left e -> fail $ "couldn't parse file '" <> fullPath <> "': " <> e
59+ Right schemaTests -> return $ prependFileName name <$> schemaTests
60+
6261 prependFileName :: String -> SchemaTest -> SchemaTest
6362 prependFileName fileName s = s
6463 { _stDescription = T.pack fileName <> ": " <> _stDescription s
6564 }
6665
66+ concatMapM :: (Monad m) => (a -> m [b]) -> [a] -> m [b]
67+ concatMapM f xs = liftM concat (mapM f xs)
68+
6769toTest :: SchemaTest -> Test
6870toTest st = testGroup groupName (mkCase <$> _stCases st)
6971 where
tests/Local.hsnew file+18
@@ -0,0 +1,18 @@
1+module Main where
2+
3+import Control.Applicative
4+import Data.List (isSuffixOf)
5+import Lib
6+import System.Directory (getDirectoryContents)
7+import Test.Framework
8+
9+main :: IO ()
10+main = do
11+ files <- filter isLocal . filter (".json" `isSuffixOf`)
12+ <$> getDirectoryContents dir
13+ ts <- readSchemaTests dir files
14+ defaultMain (toTest <$> ts)
15+
16+ where
17+ dir :: String
18+ dir = "JSON-Schema-Test-Suite/tests/draft4"
tests/Remote.hsnew file+18
@@ -0,0 +1,18 @@
1+module Main where
2+
3+import Control.Applicative
4+import Data.List (isSuffixOf)
5+import Lib
6+import System.Directory (getDirectoryContents)
7+import Test.Framework
8+
9+main :: IO ()
10+main = do
11+ files <- filter (not . isLocal) . filter (".json" `isSuffixOf`)
12+ <$> getDirectoryContents dir
13+ ts <- readSchemaTests dir files
14+ defaultMain (toTest <$> ts)
15+
16+ where
17+ dir :: String
18+ dir = "JSON-Schema-Test-Suite/tests/draft4"