@@ -10,7 +10,7 @@ import JSON as J
1010import JSON.Array as JA
1111import JSON.Object as JO
1212import JSON.Path as Path
13- import Test.Assert (assertEqual , assertTrue )
13+ import Test.Assert (assertTrue )
1414
1515main :: Effect Unit
1616main = do
@@ -46,18 +46,50 @@ main = do
4646 assertTrue $ JA .fromArray (J .fromInt <$> [ 1 , 2 ]) <> JA .fromArray (J .fromInt <$> [ 2 , 3 ]) == JA .fromArray (J .fromInt <$> [ 1 , 2 , 2 , 3 ])
4747
4848 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- }
49+ assertTrue $ Path .print (Path.AtKey " data" (Path.AtIndex 0 (Path.AtKey " field" Path.Tip ))) == " $.data[0].field"
5350
5451 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 )
52+ assertTrue $ Path .get Path.Tip (J .fromString " hello" ) == Just (J .fromString " hello" )
53+ assertTrue $ Path .get Path.Tip (J .fromJArray (JA .fromArray [ J .fromInt 42 ])) == Just (J .fromJArray (JA .fromArray [ J .fromInt 42 ]))
54+ assertTrue $ Path .get (Path.AtIndex 0 Path.Tip ) (J .fromJArray (JA .fromArray [ J .fromInt 42 , J .fromString " X" , J .fromBoolean true ])) == Just (J .fromInt 42 )
55+ assertTrue $ Path .get (Path.AtIndex 1 Path.Tip ) (J .fromJArray (JA .fromArray [ J .fromInt 42 , J .fromString " X" , J .fromBoolean true ])) == Just (J .fromString " X" )
56+ assertTrue $ Path .get (Path.AtIndex 5 Path.Tip ) (J .fromJArray (JA .fromArray [ J .fromInt 42 , J .fromString " X" , J .fromBoolean true ])) == Nothing
57+ assertTrue $ Path .get (Path.AtKey " a" Path.Tip ) (J .fromJObject (JO .fromEntries [ Tuple " a" (J .fromInt 1 ), Tuple " x" (J .fromBoolean false ) ])) == Just (J .fromInt 1 )
58+ assertTrue $ Path .get (Path.AtKey " x" Path.Tip ) (J .fromJObject (JO .fromEntries [ Tuple " a" (J .fromInt 1 ), Tuple " x" (J .fromBoolean false ) ])) == Just (J .fromBoolean false )
59+ assertTrue $ Path .get (Path.AtKey " z" Path.Tip ) (J .fromJObject (JO .fromEntries [ Tuple " a" (J .fromInt 1 ), Tuple " x" (J .fromBoolean false ) ])) == Nothing
60+ assertTrue $ Path .get (Path.AtIndex 1 (Path.AtKey " x" Path.Tip )) (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 )
61+
62+ log " Check path extend"
63+ assertTrue do
64+ let p1 = Path.AtKey " data" $ Path.AtIndex 0 $ Path.Tip
65+ let p2 = Path.AtKey " info" $ Path.AtKey " title" $ Path.Tip
66+ let expected = Path.AtKey " data" $ Path.AtIndex 0 $ Path.AtKey " info" $ Path.AtKey " title" $ Path.Tip
67+ Path .extend p1 p2 == expected
68+
69+ log " Check path findCommonPrefix"
70+ assertTrue do
71+ let p1 = Path.AtKey " y" $ Path.AtKey " x" $ Path.AtIndex 1 $ Path.Tip
72+ let p2 = Path.AtKey " y" $ Path.AtKey " x" $ Path.AtIndex 0 $ Path.Tip
73+ let expected = Path.AtKey " y" $ Path.AtKey " x" $ Path.Tip
74+ Path .findCommonPrefix p1 p2 == expected
75+ assertTrue do
76+ let p1 = Path.AtKey " other" $ Path.Tip
77+ let p2 = Path.AtKey " y" $ Path.AtKey " x" $ Path.AtIndex 0 $ Path.Tip
78+ let expected = Path.Tip
79+ Path .findCommonPrefix p1 p2 == expected
80+
81+ log " Check path stripPrefix"
82+ assertTrue do
83+ let p1 = Path.AtKey " y" Path.Tip
84+ let p2 = Path.AtKey " y" $ Path.AtKey " x" $ Path.AtIndex 0 $ Path.Tip
85+ let expected = Path.AtKey " x" $ Path.AtIndex 0 Path.Tip
86+ Path .stripPrefix p1 p2 == Just expected
87+ assertTrue do
88+ let p1 = Path.AtKey " y" $ Path.AtKey " x" $ Path.Tip
89+ let p2 = Path.AtKey " y" $ Path.AtKey " x" $ Path.AtIndex 0 Path.Tip
90+ let expected = Path.AtIndex 0 Path.Tip
91+ Path .stripPrefix p1 p2 == Just expected
92+ assertTrue do
93+ let p1 = Path.AtKey " other" Path.Tip
94+ let p2 = Path.AtKey " y" $ Path.AtKey " x" $ Path.AtIndex 0 Path.Tip
95+ Path .stripPrefix p1 p2 == Nothing
0 commit comments