@@ -2,13 +2,15 @@ module Test.Main where
22
33import Prelude
44
5+ import Data.Maybe (Maybe (..))
56import Data.Tuple (Tuple (..))
67import Effect (Effect )
78import Effect.Console (log )
89import JSON as J
910import JSON.Array as JA
1011import JSON.Object as JO
11- import Test.Assert (assertTrue )
12+ import JSON.Path as Path
13+ import Test.Assert (assertEqual , assertTrue )
1214
1315main :: Effect Unit
1416main = do
@@ -25,10 +27,37 @@ main = do
2527
2628 log " Check array comparisons"
2729 assertTrue $ J .fromJArray (JA .fromArray [] ) == J .fromJArray (JA .fromArray [] )
28- assertTrue $ J .fromJArray (JA .fromArray [J .fromInt 1 ]) == J .fromJArray (JA .fromArray [J .fromInt 1 ])
29- assertTrue $ J .fromJArray (JA .fromArray [J .fromInt 1 ]) < J .fromJArray (JA .fromArray [J .fromInt 2 ])
30+ assertTrue $ J .fromJArray (JA .fromArray [ J .fromInt 1 ]) == J .fromJArray (JA .fromArray [ J .fromInt 1 ])
31+ assertTrue $ J .fromJArray (JA .fromArray [ J .fromInt 1 ]) < J .fromJArray (JA .fromArray [ J .fromInt 2 ])
3032
3133 log " Check object comparisons"
3234 assertTrue $ JO .empty == JO .empty
33- assertTrue $ J .fromJObject (JO .fromEntries [Tuple " a" (J .fromInt 1 )] ) == J .fromJObject (JO .fromEntries [Tuple " a" (J .fromInt 1 )] )
34- assertTrue $ J .fromJObject (JO .fromEntries [Tuple " a" (J .fromInt 1 )] ) < J .fromJObject (JO .fromEntries [Tuple " a" (J .fromInt 2 )] )
35+ assertTrue $ J .fromJObject (JO .fromEntries [ Tuple " a" (J .fromInt 1 ) ]) == J .fromJObject (JO .fromEntries [ Tuple " a" (J .fromInt 1 ) ])
36+ assertTrue $ J .fromJObject (JO .fromEntries [ Tuple " a" (J .fromInt 1 ) ]) < J .fromJObject (JO .fromEntries [ Tuple " a" (J .fromInt 2 ) ])
37+
38+ log " Check array index"
39+ assertTrue $ JA .index (-1 ) (JA .fromArray (J .fromInt <$> [ 0 , 2 , 4 ])) == Nothing
40+ assertTrue $ JA .index 0 (JA .fromArray (J .fromInt <$> [ 0 , 2 , 4 ])) == Just (J .fromInt 0 )
41+ assertTrue $ JA .index 1 (JA .fromArray (J .fromInt <$> [ 0 , 2 , 4 ])) == Just (J .fromInt 2 )
42+ assertTrue $ JA .index 2 (JA .fromArray (J .fromInt <$> [ 0 , 2 , 4 ])) == Just (J .fromInt 4 )
43+ assertTrue $ JA .index 3 (JA .fromArray (J .fromInt <$> [ 0 , 2 , 4 ])) == Nothing
44+
45+ log " Check array concat"
46+ assertTrue $ JA .fromArray (J .fromInt <$> [ 1 , 2 ]) <> JA .fromArray (J .fromInt <$> [ 2 , 3 ]) == JA .fromArray (J .fromInt <$> [ 1 , 2 , 2 , 3 ])
47+
48+ log " Check path printing"
49+ assertEqual
50+ { expected: " $.data[0].field"
51+ , actual: Path .print (Path.AtKey " field" (Path.AtIndex 0 (Path.AtKey " data" Path.Top )))
52+ }
53+
54+ log " Check path get"
55+ assertTrue $ Path .get Path.Top (J .fromString " hello" ) == Just (J .fromString " hello" )
56+ assertTrue $ Path .get Path.Top (J .fromJArray (JA .fromArray [ J .fromInt 42 ])) == Just (J .fromJArray (JA .fromArray [ J .fromInt 42 ]))
57+ assertTrue $ Path .get (Path.AtIndex 0 Path.Top ) (J .fromJArray (JA .fromArray [ J .fromInt 42 , J .fromString " X" , J .fromBoolean true ])) == Just (J .fromInt 42 )
58+ assertTrue $ Path .get (Path.AtIndex 1 Path.Top ) (J .fromJArray (JA .fromArray [ J .fromInt 42 , J .fromString " X" , J .fromBoolean true ])) == Just (J .fromString " X" )
59+ assertTrue $ Path .get (Path.AtIndex 5 Path.Top ) (J .fromJArray (JA .fromArray [ J .fromInt 42 , J .fromString " X" , J .fromBoolean true ])) == Nothing
60+ assertTrue $ Path .get (Path.AtKey " a" Path.Top ) (J .fromJObject (JO .fromEntries [ Tuple " a" (J .fromInt 1 ), Tuple " x" (J .fromBoolean false ) ])) == Just (J .fromInt 1 )
61+ assertTrue $ Path .get (Path.AtKey " x" Path.Top ) (J .fromJObject (JO .fromEntries [ Tuple " a" (J .fromInt 1 ), Tuple " x" (J .fromBoolean false ) ])) == Just (J .fromBoolean false )
62+ assertTrue $ Path .get (Path.AtKey " z" Path.Top ) (J .fromJObject (JO .fromEntries [ Tuple " a" (J .fromInt 1 ), Tuple " x" (J .fromBoolean false ) ])) == Nothing
63+ assertTrue $ Path .get (Path.AtKey " x" (Path.AtIndex 1 Path.Top )) (J .fromJArray (JA .fromArray [ J .fromString " skip" , (J .fromJObject (JO .fromEntries [ Tuple " a" (J .fromInt 1 ), Tuple " x" (J .fromBoolean false ) ])) ])) == Just (J .fromBoolean false )
0 commit comments