pa_rs/
lib.rs

1pub mod parser;
2pub mod slow_parser;
3
4#[cfg(test)]
5mod tests {
6    use super::slow_parser::*;
7    #[test]
8    fn test_parse_char() {
9        assert_eq!(parse_char('a').run("abcd"), Ok('a'));
10        assert_ne!(parse_char('a').run("bcd"), Ok('a'));
11    }
12
13    #[test]
14    fn test_parse_str() {
15        assert_eq!(parse_str("null").run("null"), Ok("null".into()));
16        assert_ne!(parse_str("true").run("false"), Ok("true".into()));
17
18        let mut p = parse_one_of([parse_str("true"), parse_str("truck")]);
19        assert_eq!(p.run("truck"), Ok("truck".into()));
20        assert_eq!(p.run("true"), Ok("true".into()));
21    }
22
23    #[test]
24    fn test_parse_bool() {
25        assert_eq!(parse_bool().run("true"), Ok(true));
26        assert_eq!(parse_bool().run("false"), Ok(false));
27        assert_ne!(parse_bool().run("False"), Ok(false));
28    }
29
30    #[test]
31    fn test_parse_float() {
32        assert_eq!(parse_float().run("123"), Ok(123f64));
33        assert_eq!(parse_float().run("-123.43"), Ok(-123.43));
34        assert_eq!(parse_float().run("0.23"), Ok(0.23));
35        assert_eq!(parse_float().run("23."), Ok(23.));
36    }
37
38    #[test]
39    fn test_parse_list_of() {
40        let parse_list_of_float = || parse_list_of(|| parse_float());
41
42        assert_eq!(
43            parse_list_of_float().run("[1.2, 2.3, 3.4]"),
44            Ok(vec![1.2, 2.3, 3.4])
45        );
46        assert_eq!(parse_list_of_float().run("[1.2]"), Ok(vec![1.2]));
47        assert_eq!(parse_list_of_float().run("[]"), Ok(vec![]));
48
49        let parse_list_of_list_of_float = || {
50            parse_list_of(|| parse_list_of_float())
51                .run("[[1.2 , 2.2 ], [ 2.3], [3.4,1.0,2.0 ,3.0]]")
52        };
53        assert_eq!(
54            parse_list_of_list_of_float(),
55            Ok(vec![vec![1.2, 2.2], vec![2.3], vec![3.4, 1.0, 2.0, 3.0]])
56        );
57    }
58
59    use super::parser::*;
60
61    #[test]
62    fn test_char_p() {
63        let cp = char_p('a');
64        assert_eq!(cp.run("abcd"), Ok('a'));
65        assert_ne!(cp.run("bcd"), Ok('a'));
66    }
67
68    #[test]
69    fn test_str_p() {
70        assert_eq!(str_p("null").run("null"), Ok("null".to_string()));
71        assert_ne!(str_p("true").run("false"), Ok("true".to_string()));
72
73        let p = one_of_p([str_p("true"), str_p("truck")]);
74        assert_eq!(p.run("truck"), Ok("truck".into()));
75        assert_eq!(p.run("true"), Ok("true".into()));
76    }
77
78    #[test]
79    fn test_bool_p() {
80        let p = bool_p();
81        assert_eq!(p.run("true"), Ok(true));
82        assert_eq!(p.run("false"), Ok(false));
83        assert_ne!(p.run("False"), Ok(false));
84    }
85
86    #[test]
87    fn test_float_p() {
88        assert_eq!(float_p().run("123"), Ok(123f64));
89        assert_eq!(float_p().run("-123.43"), Ok(-123.43));
90        assert_eq!(float_p().run("0.23"), Ok(0.23));
91        assert_eq!(float_p().run("23."), Ok(23.));
92    }
93
94    #[test]
95    fn test_list_of_p() {
96        let parse_list_of_float = list_square_p(float_p());
97
98        assert_eq!(
99            parse_list_of_float.run("[1.2, 2.3, 3.4]"),
100            Ok(vec![1.2, 2.3, 3.4])
101        );
102        assert_eq!(parse_list_of_float.run("[1.2]"), Ok(vec![1.2]));
103        assert_eq!(parse_list_of_float.run("[]"), Ok(vec![]));
104
105        let parse_list_of_list_of_float = list_square_p(parse_list_of_float);
106        assert_eq!(
107            parse_list_of_list_of_float.run("[[1.2 , 2.2 ], [ 2.3], [3.4,1.0,2.0 ,3.0]]"),
108            Ok(vec![vec![1.2, 2.2], vec![2.3], vec![3.4, 1.0, 2.0, 3.0]])
109        );
110    }
111
112    #[test]
113    fn test_quoted_str_p() {
114        assert_eq!(dq_str_p().run("\"abcd\""), Ok(format!("abcd")));
115        assert_eq!(dq_str_p().run("\"abcd\"123"), Ok(format!("abcd")));
116        assert_eq!(
117            dq_str_p().run("\"ab\n 'c' \td\""),
118            Ok(format!("ab\n 'c' \td"))
119        );
120
121        assert_eq!(sq_str_p().run("\'abcd\'"), Ok(format!("abcd")));
122        assert_eq!(sq_str_p().run("\'abcd\'123"), Ok(format!("abcd")));
123        assert_eq!(
124            sq_str_p().run("\'ab\n \"c\" \td\'"),
125            Ok(format!("ab\n \"c\" \td"))
126        );
127    }
128}