@@ -3,17 +3,15 @@ module Test.Main where
3
3
import Prelude
4
4
5
5
import Control.Monad.Eff.Console (log )
6
-
7
6
import Data.Argonaut.Core (JObject , Json , isObject , toObject )
8
7
import Data.Argonaut.Decode (decodeJson )
9
- import Data.Argonaut.Encode (encodeJson , (:=), (~> ))
8
+ import Data.Argonaut.Encode (class EncodeJson , encodeJson , (:=), (:=?), (~>), (~>? ))
10
9
import Data.Argonaut.Gen (genJson )
11
10
import Data.Either (Either (..))
12
11
import Data.Foldable (foldl )
13
- import Data.Maybe (Maybe (..), maybe , isJust )
12
+ import Data.Maybe (Maybe (..), isJust , isNothing , maybe )
14
13
import Data.StrMap as SM
15
14
import Data.Tuple (Tuple (..))
16
-
17
15
import Test.StrongCheck (SC , quickCheck , quickCheck' , (<?>))
18
16
import Test.StrongCheck.Arbitrary (class Arbitrary )
19
17
import Test.StrongCheck.Gen (suchThat , resize )
@@ -26,6 +24,9 @@ main = do
26
24
27
25
newtype TestJson = TestJson Json
28
26
27
+ instance encodeJsonTestJson :: EncodeJson TestJson where
28
+ encodeJson (TestJson x) = encodeJson x
29
+
29
30
instance arbitraryTestJson :: Arbitrary TestJson where
30
31
arbitrary = TestJson <$> (resize 5 genJson)
31
32
@@ -59,8 +60,12 @@ combinatorsCheck :: SC () Unit
59
60
combinatorsCheck = do
60
61
log " Check assoc builder `:=`"
61
62
quickCheck' 20 prop_assoc_builder_str
63
+ log " Check assocOptional builder `:=?`"
64
+ quickCheck' 20 prop_assoc_optional_builder_str
62
65
log " Check JAssoc append `~>`"
63
66
quickCheck' 20 prop_assoc_append
67
+ log " Check JAssoc appendOptional `~>?`"
68
+ quickCheck' 20 prop_assoc_append_optional
64
69
log " Check get field `obj .? 'foo'`"
65
70
quickCheck' 20 prop_get_jobject_field
66
71
@@ -72,13 +77,27 @@ combinatorsCheck = do
72
77
Tuple k json ->
73
78
(key == k) && (decodeJson json == Right str)
74
79
80
+ prop_assoc_optional_builder_str :: Tuple String (Maybe String ) -> Boolean
81
+ prop_assoc_optional_builder_str (Tuple key maybeStr) =
82
+ case (key :=? maybeStr) of
83
+ Just (Tuple k json) ->
84
+ (key == k) && (decodeJson json == Right maybeStr)
85
+ Nothing -> true
86
+
75
87
prop_assoc_append :: (Tuple (Tuple String TestJson ) Obj ) -> Boolean
76
88
prop_assoc_append (Tuple (Tuple key (TestJson val)) (Obj obj)) =
77
89
let appended = (key := val) ~> obj
78
90
in case toObject appended >>= SM .lookup key of
79
91
Just value -> true
80
92
_ -> false
81
93
94
+ prop_assoc_append_optional :: Tuple (Tuple String (Maybe TestJson )) Obj -> Boolean
95
+ prop_assoc_append_optional (Tuple (Tuple key maybeVal) (Obj obj)) =
96
+ let appended = (key :=? maybeVal) ~>? obj
97
+ in case toObject appended >>= SM .lookup key of
98
+ Just value -> isJust maybeVal
99
+ _ -> isNothing maybeVal
100
+
82
101
prop_get_jobject_field :: Obj -> Boolean
83
102
prop_get_jobject_field (Obj obj) =
84
103
maybe false go $ toObject obj
0 commit comments