Skip to content

Commit 51fc238

Browse files
oakmaccburgmer
authored andcommitted
add test cases for filter expressions with boolean
1 parent 8eeb586 commit 51fc238

File tree

4 files changed

+59
-7
lines changed

4 files changed

+59
-7
lines changed

src/json_path/parser.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,4 @@
7070
[:path pth]))))))
7171

7272
(defn parse-path [path]
73-
(parse (re-seq #"<=|>=|\.\.|[.*$@\[\]\(\)\"=<>]|\d+|[\w-\/]+|\?\(|!=|&&|true|false" path)))
73+
(parse (re-seq #"<=|>=|\.\.|[.*$@\[\]\(\)\"=<>]|\d+|[\w-\/]+|\?\(|!=|&&|\|\||true|false" path)))

src/json_path/walker.clj

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@
77
(and (map? m)
88
(contains? m :value)))
99

10-
(defn eval-bool-expr [op-form context operands]
11-
(boolean (apply op-form (map #(eval-expr % context) operands))))
10+
;; NOTE: this function produces a runtime error when comparing things of different types
11+
;; ie: (< 3 "foo")
12+
;; Will need to coerce the types in order to get the "Filter expression with boolean or operator and value false"
13+
;; test case to pass
14+
(defn eval-bool-expr [comp-fn context operands]
15+
(boolean (apply comp-fn (map #(eval-expr % context) operands))))
1216

1317
(def boolean-ops
1418
"expression operands that result in a boolean result"

test/json_path/test/json_path_test.clj

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
(ns json-path.test.json-path-test
2-
[:use [json-path]
2+
[:use
3+
[json-path]
34
[midje.sweet]])
45

56
(unfinished)
@@ -24,9 +25,28 @@
2425
{:bar "baz" :hello "world"}]}) => ["world"]
2526
(at-path "$.foo[?(@.id=$.id)].text"
2627
{:id 45, :foo [{:id 12, :text "bar"},
27-
{:id 45, :text "hello"}]}) => ["hello"]
28+
{:id 45, :text "hello"}]}) => ["hello"]
2829
(at-path "$.foo[*].bar[*].baz"
29-
{:foo [{:bar [{:baz "hello"}]}]}) => ["hello"])
30+
{:foo [{:bar [{:baz "hello"}]}]}) => ["hello"]
31+
(at-path "$[?(@.key>42 && @.key<44)]"
32+
[{:key 42}, {:key 43}, {:key 44}]) => [{:key 43}]
33+
(at-path "$[?(@.key>43 || @.key<43)]"
34+
[{:key 42}, {:key 43}, {:key 44}]) => [{:key 42}, {:key 44}])
35+
36+
;; TODO: add this case, comparator is blowing up comparing against different types
37+
;; "Filter expression with boolean or operator and value false"
38+
; (at-path "$[?(@.key>0 && false)]"
39+
; [{:key 1}
40+
; {:key 3}
41+
; {:key "nice"}
42+
; {:key true}
43+
; {:key nil}
44+
; {:key false}
45+
; {:key {}}
46+
; {:key []}
47+
; {:key -1}
48+
; {:key 0}
49+
; {:key ""}]) => []
3050

3151
(facts
3252
(-> (query "$.hello"
@@ -55,4 +75,10 @@
5575
(->> (query "$.foo[?(@.bar=\"baz\")].hello"
5676
{:foo [{:bar "wrong" :hello "goodbye"}
5777
{:bar "baz" :hello "world"}]})
58-
(map :path)) => '([:foo 1 :hello]))
78+
(map :path)) => '([:foo 1 :hello])
79+
(->> (query "$[?(@.key>42 && @.key<44)]"
80+
[{:key 42}, {:key 43}, {:key 44}])
81+
(map :value)) => [{:key 43}]
82+
(->> (query "$[?(@.key>43 || @.key<43)]"
83+
[{:key 42}, {:key 43}, {:key 44}])
84+
(map :value)) => [{:key 42}, {:key 44}])

test/json_path/test/parser_test.clj

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,28 @@
7373
[:child]
7474
[:key "bar"]]]
7575
[:val 44]]]]]]
76+
(parse-path "$[?(@.bar>42 && true)]") => [:path [[:root]]
77+
[:selector [:filter
78+
[:and
79+
[:gt
80+
[:path [[:current]
81+
[:child]
82+
[:key "bar"]]]
83+
[:val 42]]
84+
[:bool [:val true]]]]]]
85+
(parse-path "$[?(@.bar>42 || @.bar<44)]") => [:path [[:root]]
86+
[:selector [:filter
87+
[:or
88+
[:gt
89+
[:path [[:current]
90+
[:child]
91+
[:key "bar"]]]
92+
[:val 42]]
93+
[:lt
94+
[:path [[:current]
95+
[:child]
96+
[:key "bar"]]]
97+
[:val 44]]]]]]
7698
(parse-path "$.foo[?(@.bar=\"baz\")].hello") => [:path [[:root] [:child] [:key "foo"]]
7799
[:selector [:filter [:eq [:path [[:current]
78100
[:child]

0 commit comments

Comments
 (0)