Skip to content

Commit f3bdf50

Browse files
committed
use quickcheck instead of strongcheck for tests
1 parent 9d4557d commit f3bdf50

File tree

2 files changed

+66
-57
lines changed

2 files changed

+66
-57
lines changed

bower.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@
2525
"purescript-argonaut-core": "^4.0.0",
2626
"purescript-integers": "^4.0.0",
2727
"purescript-maybe": "^4.0.0",
28+
"purescript-ordered-collections": "^1.0.0",
2829
"purescript-foreign-object": "^1.0.0"
2930
},
3031
"devDependencies": {
31-
"purescript-strongcheck": "#compiler/0.12"
32+
"purescript-quickcheck": "^5.0.0"
3233
}
33-
}
34+
}

test/Test/Main.purs

Lines changed: 63 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,106 +2,114 @@ module Test.Main where
22

33
import Prelude
44

5-
import Data.Argonaut.Core (Json, isObject, toObject)
5+
import Control.Monad.Gen.Common (genMaybe)
6+
import Data.Argonaut.Core (Json, isObject, stringify, toObject)
67
import Data.Argonaut.Decode (decodeJson)
7-
import Data.Argonaut.Encode (class EncodeJson, encodeJson, (:=), (:=?), (~>), (~>?))
8+
import Data.Argonaut.Encode (encodeJson, (:=), (:=?), (~>), (~>?))
89
import Data.Argonaut.Gen (genJson)
10+
import Data.Bifunctor (rmap)
911
import Data.Either (Either(..))
1012
import Data.Foldable (foldl)
1113
import Data.Maybe (Maybe(..), isJust, isNothing, maybe)
14+
import Data.String.Gen (genUnicodeString)
1215
import Data.Tuple (Tuple(..))
1316
import Effect (Effect)
1417
import Effect.Console (log)
1518
import Foreign.Object as FO
16-
import Test.StrongCheck (quickCheck, quickCheck', (<?>))
17-
import Test.StrongCheck.Arbitrary (class Arbitrary)
18-
import Test.StrongCheck.Gen (suchThat, resize)
19+
import Test.QuickCheck (Result(..), quickCheck, (<?>), (===))
20+
import Test.QuickCheck.Gen (Gen, resize, suchThat)
1921

2022
main :: Effect Unit
2123
main = do
2224
eitherCheck
2325
encodeDecodeCheck
2426
combinatorsCheck
2527

26-
newtype TestJson = TestJson Json
27-
28-
instance encodeJsonTestJson :: EncodeJson TestJson where
29-
encodeJson (TestJson x) = encodeJson x
30-
31-
instance arbitraryTestJson :: Arbitrary TestJson where
32-
arbitrary = TestJson <$> (resize 5 genJson)
28+
genTestJson :: Gen Json
29+
genTestJson = resize 5 genJson
3330

3431
encodeDecodeCheck :: Effect Unit
3532
encodeDecodeCheck = do
3633
log "Testing that any JSON can be encoded and then decoded"
37-
quickCheck' 20 prop_encode_then_decode
34+
quickCheck prop_encode_then_decode
3835

3936
log "Testing that any JSON can be decoded and then encoded"
40-
quickCheck' 20 (prop_decode_then_encode)
37+
quickCheck prop_decode_then_encode
4138

4239
where
4340

44-
prop_encode_then_decode :: TestJson -> Boolean
45-
prop_encode_then_decode (TestJson json) =
46-
Right json == decodeJson (encodeJson json)
47-
48-
prop_decode_then_encode :: TestJson -> Boolean
49-
prop_decode_then_encode (TestJson json) =
50-
let decoded = (decodeJson json) :: Either String Json in
51-
Right json == (decoded >>= (encodeJson >>> pure))
41+
prop_encode_then_decode :: Gen Result
42+
prop_encode_then_decode = do
43+
json <- genTestJson
44+
let redecoded = decodeJson (encodeJson json)
45+
pure $ Right json == redecoded <?> (show (rmap stringify redecoded) <> " /= Right " <> stringify json)
5246

53-
newtype Obj = Obj Json
54-
unObj :: Obj -> Json
55-
unObj (Obj j) = j
47+
prop_decode_then_encode :: Gen Result
48+
prop_decode_then_encode = do
49+
json <- genTestJson
50+
let (decoded :: Either String Json) = decodeJson json
51+
let reencoded = decoded >>= (encodeJson >>> pure)
52+
pure $ Right json == reencoded <?> (show (rmap stringify reencoded) <> " /= Right " <> stringify json)
5653

57-
instance arbitraryObj :: Arbitrary Obj where
58-
arbitrary = Obj <$> suchThat (resize 5 genJson) isObject
54+
genObj :: Gen Json
55+
genObj = suchThat (resize 5 genJson) isObject
5956

6057
combinatorsCheck :: Effect Unit
6158
combinatorsCheck = do
6259
log "Check assoc builder `:=`"
63-
quickCheck' 20 prop_assoc_builder_str
60+
quickCheck prop_assoc_builder_str
6461
log "Check assocOptional builder `:=?`"
65-
quickCheck' 20 prop_assoc_optional_builder_str
62+
quickCheck prop_assoc_optional_builder_str
6663
log "Check JAssoc append `~>`"
67-
quickCheck' 20 prop_assoc_append
64+
quickCheck prop_assoc_append
6865
log "Check JAssoc appendOptional `~>?`"
69-
quickCheck' 20 prop_assoc_append_optional
66+
quickCheck prop_assoc_append_optional
7067
log "Check get field `obj .? 'foo'`"
71-
quickCheck' 20 prop_get_jobject_field
68+
quickCheck prop_get_jobject_field
7269

7370
where
7471

75-
prop_assoc_builder_str :: Tuple String String -> Boolean
76-
prop_assoc_builder_str (Tuple key str) =
72+
prop_assoc_builder_str :: Gen Result
73+
prop_assoc_builder_str = do
74+
key <- genUnicodeString
75+
str <- genUnicodeString
7776
case (key := str) of
7877
Tuple k json ->
79-
(key == k) && (decodeJson json == Right str)
78+
pure $ Tuple key (decodeJson json) === Tuple k (Right str)
8079

81-
prop_assoc_optional_builder_str :: Tuple String (Maybe String) -> Boolean
82-
prop_assoc_optional_builder_str (Tuple key maybeStr) =
80+
prop_assoc_optional_builder_str :: Gen Result
81+
prop_assoc_optional_builder_str = do
82+
key <- genUnicodeString
83+
maybeStr <- genMaybe genUnicodeString
8384
case (key :=? maybeStr) of
8485
Just (Tuple k json) ->
85-
(key == k) && (decodeJson json == Right maybeStr)
86-
Nothing -> true
87-
88-
prop_assoc_append :: (Tuple (Tuple String TestJson) Obj) -> Boolean
89-
prop_assoc_append (Tuple (Tuple key (TestJson val)) (Obj obj)) =
86+
pure $ Tuple key (decodeJson json) === Tuple k (Right maybeStr)
87+
Nothing -> pure Success
88+
89+
prop_assoc_append :: Gen Result
90+
prop_assoc_append = do
91+
key <- genUnicodeString
92+
val <- genTestJson
93+
obj <- genObj
9094
let appended = (key := val) ~> obj
91-
in case toObject appended >>= FO.lookup key of
92-
Just value -> true
93-
_ -> false
94-
95-
prop_assoc_append_optional :: Tuple (Tuple String (Maybe TestJson)) Obj -> Boolean
96-
prop_assoc_append_optional (Tuple (Tuple key maybeVal) (Obj obj)) =
95+
case toObject appended >>= FO.lookup key of
96+
Just value -> pure Success
97+
_ -> pure (Failed "failed to lookup key")
98+
99+
prop_assoc_append_optional :: Gen Result
100+
prop_assoc_append_optional = do
101+
key <- genUnicodeString
102+
maybeVal <- genMaybe genTestJson
103+
obj <- genObj
97104
let appended = (key :=? maybeVal) ~>? obj
98-
in case toObject appended >>= FO.lookup key of
99-
Just value -> isJust maybeVal
100-
_ -> isNothing maybeVal
101-
102-
prop_get_jobject_field :: Obj -> Boolean
103-
prop_get_jobject_field (Obj obj) =
104-
maybe false go $ toObject obj
105+
pure case toObject appended >>= FO.lookup key of
106+
Just value -> isJust maybeVal === true
107+
_ -> isNothing maybeVal === true
108+
109+
prop_get_jobject_field :: Gen Result
110+
prop_get_jobject_field = do
111+
obj <- genObj
112+
pure (true === maybe false go (toObject obj))
105113
where
106114
go :: FO.Object Json -> Boolean
107115
go object =

0 commit comments

Comments
 (0)