Skip to content

Commit

Permalink
Merge pull request #40 from purescript-contrib/compiler/0.12
Browse files Browse the repository at this point in the history
Compiler/0.12
  • Loading branch information
kritzcreek authored Jun 9, 2018
2 parents 448d1dd + cb7c83e commit c7c1161
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 63 deletions.
10 changes: 5 additions & 5 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
},
"license": "MIT",
"dependencies": {
"purescript-argonaut-codecs": "^3.0.0",
"purescript-argonaut-core": "^3.1.0",
"purescript-argonaut-traversals": "^3.0.0"
"purescript-argonaut-codecs": "^4.0.0",
"purescript-argonaut-core": "^4.0.0",
"purescript-argonaut-traversals": "^4.0.0"
},
"devDependencies": {
"purescript-console": "^3.0.0",
"purescript-strongcheck": "^3.1.0"
"purescript-console": "^4.0.0",
"purescript-quickcheck": "^5.0.0"
}
}
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
"test": "pulp build --include examples -- --censor-lib --strict && pulp test"
},
"devDependencies": {
"pulp": "^11.0.2",
"purescript-psa": "^0.5.1",
"purescript": "^0.11.6",
"pulp": "^12.0.0",
"purescript-psa": "^0.6.0",
"purescript": "^0.12.0",
"rimraf": "^2.6.2"
}
}
2 changes: 1 addition & 1 deletion src/Data/Argonaut.purs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Data.Argonaut
, module Data.Argonaut.Traversals
) where

import Data.Argonaut.Core (JArray, JAssoc, JBoolean, JNull, JNumber, JObject, JString, Json, foldJson, foldJsonArray, foldJsonBoolean, foldJsonNull, foldJsonNumber, foldJsonObject, foldJsonString, fromArray, fromBoolean, fromNull, fromNumber, fromObject, fromString, isArray, isBoolean, isNull, isNumber, isObject, isString, jNull, jsonEmptyArray, jsonEmptyObject, jsonEmptyString, jsonFalse, jsonNull, jsonSingletonArray, jsonSingletonObject, jsonTrue, jsonZero, stringify, toArray, toBoolean, toNull, toNumber, toObject, toString)
import Data.Argonaut.Core (Json, caseJson, caseJsonArray, caseJsonBoolean, caseJsonNull, caseJsonNumber, caseJsonObject, caseJsonString, fromArray, fromBoolean, fromNumber, fromObject, fromString, isArray, isBoolean, isNull, isNumber, isObject, isString, jsonEmptyArray, jsonEmptyObject, jsonEmptyString, jsonFalse, jsonNull, jsonSingletonArray, jsonSingletonObject, jsonTrue, jsonZero, stringify, toArray, toBoolean, toNull, toNumber, toObject, toString)
import Data.Argonaut.Decode (class DecodeJson, decodeJson, defaultField, getField, getFieldOptional, (.?), (.?=), (.??))
import Data.Argonaut.Encode (class EncodeJson, assoc, encodeJson, extend, (:=), (~>))
import Data.Argonaut.JCursor (JCursor(..), JsonPrim(..), cursorGet, cursorSet, downField, downIndex, fail, fromPrims, inferEmpty, insideOut, primBool, primNull, primNum, primStr, primToJson, runJsonPrim, toPrims)
Expand Down
83 changes: 29 additions & 54 deletions test/Test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,68 +2,43 @@ module Test.Main where

import Prelude

import Control.Monad.Eff.Console (log)

import Data.Argonaut (Json, decodeJson, encodeJson, fromString, (.?))
import Data.Argonaut.JCursor (JCursor(..), toPrims, fromPrims)
import Data.Argonaut (Json, decodeJson, encodeJson, stringify)
import Data.Argonaut.Gen (genJson)
import Data.Argonaut.JCursor (JCursor(..), toPrims, fromPrims)
import Data.Either (Either(..))
import Data.Foldable (foldMap)
import Data.Maybe (Maybe(..))
import Data.StrMap as M

import Test.StrongCheck (SC, Result, assert, quickCheck', (<?>))
import Test.StrongCheck.Arbitrary (class Arbitrary, arbitrary)
import Test.StrongCheck.Gen (chooseInt, resize)

newtype TestJson = TestJson Json

instance arbitraryJson :: Arbitrary TestJson where
arbitrary = TestJson <$> (resize 5 genJson)

prop_encode_then_decode :: TestJson -> Boolean
prop_encode_then_decode (TestJson json) =
Right json == (decodeJson $ encodeJson $ json)

prop_decode_then_encode :: TestJson -> Boolean
prop_decode_then_encode (TestJson json) =
let decoded = (decodeJson json) :: Either String Json in
Right json == (decoded >>= (encodeJson >>> pure))

prop_toPrims_fromPrims :: TestJson -> Result
prop_toPrims_fromPrims (TestJson j) =
Just j == fromPrims (toPrims j) <?> "fromPrims.toPrims: " <> show (toPrims j) <> "\n\n" <> show (fromPrims (toPrims j))

newtype TestJCursor = TestJCursor JCursor

runTestJCursor :: TestJCursor -> JCursor
runTestJCursor (TestJCursor j) = j

instance arbJCursor :: Arbitrary TestJCursor where
arbitrary = do
import Data.String.Gen (genUnicodeString)
import Effect (Effect)
import Effect.Console (log)
import Test.QuickCheck (Result, quickCheck, (<?>))
import Test.QuickCheck.Gen (Gen, chooseInt, resize)

genTestJson :: Gen Json
genTestJson = resize 5 genJson

prop_toPrims_fromPrims :: Gen Result
prop_toPrims_fromPrims = do
j <- genTestJson
pure $ Just j == fromPrims (toPrims j) <?> "fromPrims.toPrims: " <> show (toPrims j) <> "\n\n" <> foldMap stringify (fromPrims (toPrims j))

genTestJCursor :: Gen JCursor
genTestJCursor = do
i <- chooseInt 0 2
r <- if i == 0 then pure JCursorTop
else if i == 1 then JField <$> arbitrary <*> (runTestJCursor <$> arbitrary)
else JIndex <$> arbitrary <*> (runTestJCursor <$> arbitrary)
pure $ TestJCursor r
else if i == 1 then JField <$> genUnicodeString <*> genTestJCursor
else JIndex <$> chooseInt bottom top <*> genTestJCursor
pure r

prop_jcursor_serialization :: TestJCursor -> Result
prop_jcursor_serialization (TestJCursor c) =
(decodeJson (encodeJson c) == Right c) <?> "JCursor: " <> show c
prop_jcursor_serialization :: Gen Result
prop_jcursor_serialization = do
c <- genTestJCursor
pure $ (decodeJson (encodeJson c) == Right c) <?> "JCursor: " <> show c

main :: SC () Unit
main :: Effect Unit
main = do
log "Testing that any JSON can be encoded and then decoded"
quickCheck' 20 prop_encode_then_decode

log "Testing that any JSON can be decoded and then encoded"
quickCheck' 20 prop_decode_then_encode

log "Testing that toPrims / fromPrims inverses"
quickCheck' 20 prop_toPrims_fromPrims
quickCheck prop_toPrims_fromPrims

log "Testing that JCursor can be encoded / decoded from JSON"
quickCheck' 20 prop_jcursor_serialization

log "Testing .? combinator"
assert $ let bar = fromString "bar"
in (M.singleton "foo" bar) .? "foo" == Right bar
quickCheck prop_jcursor_serialization

0 comments on commit c7c1161

Please sign in to comment.