Skip to content

Commit ecce655

Browse files
committed
add tests
1 parent 01e1153 commit ecce655

File tree

1 file changed

+56
-34
lines changed

1 file changed

+56
-34
lines changed

src/parser/tests.rs

Lines changed: 56 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use pest::error::Error;
1515
use pest::iterators::Pair;
1616
use pest::Parser;
1717
use crate::{lit, q_segments, q_segment, singular_query, slice, test_fn, arg, test, segment, selector, atom, cmp, comparable, jq, filter, or, and};
18-
use crate::parser::parser2::{filter_atom, function_expr, jp_query, literal, singular_query, singular_query_segments, slice_selector, Rule};
18+
use crate::parser::parser2::{comp_expr, comparable, filter_atom, function_expr, jp_query, literal, singular_query, singular_query_segments, slice_selector, Rule};
1919
use std::panic;
2020

2121
struct TestPair<T> {
@@ -76,31 +76,8 @@ where T:PartialEq + Debug {
7676
}
7777

7878

79-
#[test]
80-
fn literals(){
81-
82-
TestPair::new(Rule::literal, literal)
83-
.assert("null", lit!())
84-
.assert("false", lit!(b false))
85-
.assert("true", lit!(b true))
86-
.assert("\"hello\"", lit!(s "\"hello\""))
87-
.assert("\'hello\'", lit!(s "\'hello\'"))
88-
.assert("\'hel\\'lo\'", lit!(s "\'hel\\'lo\'"))
89-
.assert("\'hel\"lo\'", lit!(s "\'hel\"lo\'"))
90-
.assert("\'hel\nlo\'", lit!(s "\'hel\nlo\'"))
91-
.assert("\'\"\'", lit!(s "\'\"\'"))
92-
.assert_fail("\'hel\\\"lo\'")
93-
.assert("1", lit!(i 1))
94-
.assert("0", lit!(i 0))
95-
.assert("-0", lit!(i 0))
96-
.assert("1.2", lit!(f 1.2))
97-
.assert("9007199254740990", lit!(i 9007199254740990))
98-
.assert_fail("9007199254740995")
99-
;
10079

10180

102-
}
103-
10481
#[test]
10582
fn singular_query_segment_test(){
10683
TestPair::new(Rule::singular_query_segments, singular_query_segments)
@@ -150,9 +127,54 @@ fn function_expr_test(){
150127
;
151128
}
152129

130+
131+
132+
#[test]
133+
fn jq_test(){
134+
let atom = Filter::Atom(atom!(comparable!(> singular_query!(@ a b)), ">", comparable!(lit!(i 1))));
135+
TestPair::new(Rule::jp_query,jp_query)
136+
.assert("$.a.b[[email protected] > 1]",jq!(
137+
segment!(selector!(a)),segment!(selector!(b)),
138+
segment!(selector!(? or!(and!(atom))))
139+
) )
140+
;
141+
}
142+
153143
#[test]
154-
fn atom_test(){
155-
TestPair::new(Rule::atom_expr,filter_atom)
144+
fn comp_expr_test() {
145+
TestPair::new(Rule::comp_expr, comp_expr)
146+
.assert("@.a.b.c == 1",
147+
cmp!(comparable!(> singular_query!(@ a b c)), "==", comparable!(lit!(i 1))))
148+
;
149+
}
150+
151+
#[test]
152+
fn literal_test(){
153+
154+
TestPair::new(Rule::literal, literal)
155+
.assert("null", lit!())
156+
.assert("false", lit!(b false))
157+
.assert("true", lit!(b true))
158+
.assert("\"hello\"", lit!(s "\"hello\""))
159+
.assert("\'hello\'", lit!(s "\'hello\'"))
160+
.assert("\'hel\\'lo\'", lit!(s "\'hel\\'lo\'"))
161+
.assert("\'hel\"lo\'", lit!(s "\'hel\"lo\'"))
162+
.assert("\'hel\nlo\'", lit!(s "\'hel\nlo\'"))
163+
.assert("\'\"\'", lit!(s "\'\"\'"))
164+
.assert_fail("\'hel\\\"lo\'")
165+
.assert("1", lit!(i 1))
166+
.assert("0", lit!(i 0))
167+
.assert("-0", lit!(i 0))
168+
.assert("1.2", lit!(f 1.2))
169+
.assert("9007199254740990", lit!(i 9007199254740990))
170+
.assert_fail("9007199254740995")
171+
;
172+
173+
174+
}
175+
#[test]
176+
fn filter_atom_test(){
177+
TestPair::new(Rule::atom_expr, filter_atom)
156178
.assert("1 > 2", atom!(comparable!(lit!(i 1)), ">" , comparable!(lit!(i 2))))
157179
.assert("!(@.a ==1 || @.b == 2)", atom!(! or!(
158180
and!( Filter::Atom(atom!(comparable!(> singular_query!(@ a)),"==", comparable!(lit!(i 1))))),
@@ -161,14 +183,14 @@ fn atom_test(){
161183
))
162184
;
163185
}
164-
165186
#[test]
166-
fn jq_test(){
167-
let atom = Filter::Atom(atom!(comparable!(> singular_query!(@ a b)), ">", comparable!(lit!(i 1))));
168-
TestPair::new(Rule::jp_query,jp_query)
169-
.assert("$.a.b[[email protected] > 1]",jq!(
170-
segment!(selector!(a)),segment!(selector!(b)),
171-
segment!(selector!(? or!(and!(atom))))
172-
) )
187+
fn comparable_test(){
188+
TestPair::new(Rule::comparable,comparable)
189+
.assert("1",comparable!(lit!(i 1)))
190+
.assert("\"a\"",comparable!(lit!(s "\"a\"")))
191+
.assert("@.a.b.c",comparable!(> singular_query!(@ a b c)))
192+
.assert("$.a.b.c",comparable!(> singular_query!(a b c)))
193+
.assert("$[1]",comparable!(> singular_query!([1])))
194+
.assert("length(1)",comparable!(f test_fn!(length arg!(lit!(i 1)))))
173195
;
174196
}

0 commit comments

Comments
 (0)