Skip to content

Commit bb59b72

Browse files
committed
test: add scenarios for parsing basic schemata
1 parent e57a16d commit bb59b72

File tree

2 files changed

+78
-7
lines changed

2 files changed

+78
-7
lines changed

test/unit/Test/Spec/JsonSchema/Codec.purs

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,19 @@ import Data.Argonaut.Gen as AGen
1212
import Data.Either (Either(..))
1313
import Data.Either.Nested (type (\/))
1414
import Data.Foldable (notElem)
15+
import Data.Map as Map
16+
import Data.Maybe (Maybe(..))
1517
import Data.String (Pattern(..))
1618
import Data.String as String
1719
import Data.String.Gen as StringGen
1820
import Data.Tuple.Nested ((/\))
1921
import Foreign.Object as Object
20-
import JsonSchema (JsonSchema)
22+
import JsonSchema (JsonSchema(..), ObjectFormJsonSchemaSpec(..))
2123
import JsonSchema as Schema
2224
import JsonSchema.Codec as Codec
2325
import Test.QuickCheck (Result(..), (===))
24-
import Test.Spec (describe)
26+
import Test.Spec (describe, it)
27+
import Test.Spec.Assertions (fail, shouldEqual)
2528
import Test.Types (TestSpec)
2629
import Test.Utils (TestLength(..), generativeTestCase)
2730

@@ -99,6 +102,79 @@ spec = describe "Codec" do
99102
, pattern: "type"
100103
}
101104

105+
positiveParseTestCase
106+
{ description: "an empty object schema"
107+
, expectedSchema: ObjectFormJsonSchema JsonEmptySchema
108+
, json: A.jsonEmptyObject
109+
}
110+
111+
positiveParseTestCase
112+
{ description: "a false boolean schema"
113+
, expectedSchema: BooleanFormJsonSchema false
114+
, json: A.fromBoolean false
115+
}
116+
117+
positiveParseTestCase
118+
{ description: "a true boolean schema"
119+
, expectedSchema: BooleanFormJsonSchema true
120+
, json: A.fromBoolean true
121+
}
122+
123+
positiveParseTestCase
124+
{ description: "a schema for an array"
125+
, expectedSchema: ObjectFormJsonSchema
126+
$ JsonArraySchema { itemsSchema: Nothing, uniqueItems: false }
127+
, json: A.jsonSingletonObject "type" $ A.fromString "array"
128+
}
129+
130+
positiveParseTestCase
131+
{ description: "a schema for a boolean"
132+
, expectedSchema: ObjectFormJsonSchema JsonBooleanSchema
133+
, json: A.jsonSingletonObject "type" $ A.fromString "boolean"
134+
}
135+
136+
positiveParseTestCase
137+
{ description: "a schema for integer"
138+
, expectedSchema: ObjectFormJsonSchema $ JsonIntegerSchema {}
139+
, json: A.jsonSingletonObject "type" $ A.fromString "integer"
140+
}
141+
142+
positiveParseTestCase
143+
{ description: "a schema for a null"
144+
, expectedSchema: ObjectFormJsonSchema JsonNullSchema
145+
, json: A.jsonSingletonObject "type" $ A.fromString "null"
146+
}
147+
148+
positiveParseTestCase
149+
{ description: "a schema for a number"
150+
, expectedSchema: ObjectFormJsonSchema $ JsonNumberSchema {}
151+
, json: A.jsonSingletonObject "type" $ A.fromString "number"
152+
}
153+
154+
positiveParseTestCase
155+
{ description: "a schema for an object"
156+
, expectedSchema: ObjectFormJsonSchema
157+
$ JsonObjectSchema { properties: Map.empty }
158+
, json: A.jsonSingletonObject "type" $ A.fromString "object"
159+
}
160+
161+
positiveParseTestCase
162+
{ description: "a schema for a string"
163+
, expectedSchema: ObjectFormJsonSchema $ JsonStringSchema {}
164+
, json: A.jsonSingletonObject "type" $ A.fromString "string"
165+
}
166+
167+
positiveParseTestCase
168+
{ description String, expectedSchema JsonSchema, json Json }
169+
TestSpec
170+
positiveParseTestCase { description, expectedSchema, json } = it
171+
("Should parse " <> description)
172+
case Codec.parseSchema json of
173+
Left parseErrorMessage →
174+
fail $ "Could not parse JSON schema: " <> parseErrorMessage
175+
Right parsedSchema →
176+
parsedSchema `shouldEqual` expectedSchema
177+
102178
errorMessageShouldMention
103179
{ description String
104180
, parsingResult String \/ JsonSchema

test/unit/Test/Utils.purs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,12 @@ module Test.Utils
55

66
import Prelude
77

8-
import Data.Array as Array
9-
import Data.Foldable (class Foldable, foldM)
108
import Data.Maybe (maybe)
11-
import Data.String (Pattern(..), Replacement(..))
12-
import Data.String as String
139
import Effect (Effect)
1410
import Effect.Class (liftEffect)
1511
import Node.Process as Process
1612
import Test.QuickCheck (Result, quickCheckGen')
1713
import Test.QuickCheck.Gen (Gen)
18-
import Test.QuickCheck.Gen as Gen
1914
import Test.Spec (it)
2015
import Test.Types (TestSpec)
2116

0 commit comments

Comments
 (0)