Debian Codegraph @

src/test.rs

raw symbols SCIP
1 // (c) 2016-2017 Productize SPRL <[email protected]>
2
3 use ser;
4 use formatter;
5 use parser;
6
7 fn check_parse_res(s: &str, o: &str) {
8 let e = parser::parse_str(s).unwrap();
9 let t = ser::to_string(&e).unwrap();
10 assert_eq!(o, t)
11 }
12
13 fn check_parse(s: &str) {
14 let e = parser::parse_str(s).unwrap();
15 let t = ser::to_string(&e).unwrap();
16 assert_eq!(s, t)
17 }
18
19 fn check_parse_kicad(s: &str) {
20 let e = parser::parse_str(s).unwrap();
21 let t = ser::to_string_with_rules(&e, kicad_test_rules()).unwrap();
22 assert_eq!(s, t)
23 }
24
25 fn check_parse_rules(s: &str, rules: formatter::Rules) {
26 let e = parser::parse_str(s).unwrap();
27 let t = ser::to_string_with_rules(&e, rules).unwrap();
28 assert_eq!(s, t)
29 }
30
31
32 fn parse_fail(s: &str) {
33 parser::parse_str(s).unwrap();
34 }
35
36 fn kicad_test_rules() -> formatter::Rules {
37 let mut rf = formatter::Rules::new();
38 rf.insert("layer", 1);
39 rf.insert("desc", 1);
40 rf.insert("fp_text", 1);
41 rf.insert("fp_poly", 1);
42 rf.insert("fp_line", 1);
43 rf.insert("pad", 1);
44 rf.insert("general", 1);
45 rf
46 }
47
48 #[test]
49 fn test_empty() {
50 check_parse("")
51 }
52
53 #[test]
54 fn test_empty_qstring() {
55 check_parse("(hello \"\")")
56 }
57
58 #[test]
59 fn test_minimal() {
60 check_parse("()")
61 }
62
63 #[test]
64 fn test_string() {
65 check_parse("hello")
66 }
67
68 #[test]
69 fn test_qstring_a() {
70 check_parse_res("\"hello\"", "hello")
71 }
72
73 #[test]
74 fn test_qstring_a2() {
75 check_parse("\"hello world\"")
76 }
77
78 #[test]
79 fn test_qstring_a3() {
80 check_parse("\"hello(world)\"")
81 }
82
83 #[test]
84 fn test_number() {
85 check_parse("1.3")
86 }
87
88 #[test]
89 fn test_float_vs_int() {
90 check_parse("2.0")
91 }
92
93 #[test]
94 fn test_double() {
95 check_parse("(())")
96 }
97
98 #[test]
99 fn test_br_string() {
100 check_parse("(world)")
101 }
102
103 #[test]
104 fn test_br_qstring() {
105 check_parse_res("(\"world\")", "(world)")
106 }
107
108 #[test]
109 fn test_br_int() {
110 check_parse("(42)")
111 }
112
113 #[test]
114 fn test_br_float() {
115 check_parse("(12.7)")
116 }
117
118 #[test]
119 fn test_br_qbrstring() {
120 check_parse("(\"(()\")")
121 }
122
123 #[test]
124 fn test_number_string() {
125 check_parse("567A_WZ")
126 }
127
128 #[test]
129 #[should_panic(expected = "End of file reached")]
130 fn test_invalid1() {
131 parse_fail("(")
132 }
133
134 #[test]
135 #[should_panic(expected = "Unexpected )")]
136 fn test_invalid2() {
137 parse_fail(")")
138 }
139
140 #[test]
141 #[should_panic(expected = "End of file reached")]
142 fn test_invalid3() {
143 parse_fail("\"hello")
144 }
145
146 #[test]
147 #[should_panic(expected = "line: 4, col: 5")]
148 fn test_invalid_check_position() {
149 parse_fail(
150 "\"hello
151
152
153 ",
154 )
155 }
156
157 #[test]
158 fn test_complex() {
159 check_parse(
160 "(module SWITCH_3W_SIDE_MMP221-R (layer F.Cu) (descr \"\") (pad 1 thru_hole rect \
161 (size 1.2 1.2) (at -2.5 -1.6 0) (layers *.Cu *.Mask) (drill 0.8)) (pad 2 \
162 thru_hole rect (size 1.2 1.2) (at 0.0 -1.6 0) (layers *.Cu *.Mask) (drill 0.8)) \
163 (pad 3 thru_hole rect (size 1.2 1.2) (at 2.5 -1.6 0) (layers *.Cu *.Mask) (drill \
164 0.8)) (pad 5 thru_hole rect (size 1.2 1.2) (at 0.0 1.6 0) (layers *.Cu *.Mask) \
165 (drill 0.8)) (pad 6 thru_hole rect (size 1.2 1.2) (at -2.5 1.6 0) (layers *.Cu \
166 *.Mask) (drill 0.8)) (pad 4 thru_hole rect (size 1.2 1.2) (at 2.5 1.6 0) (layers \
167 *.Cu *.Mask) (drill 0.8)) (fp_line (start -4.5 -1.75) (end 4.5 -1.75) (layer \
168 F.SilkS) (width 0.127)) (fp_line (start 4.5 -1.75) (end 4.5 1.75) (layer \
169 F.SilkS) (width 0.127)) (fp_line (start 4.5 1.75) (end -4.5 1.75) (layer \
170 F.SilkS) (width 0.127)) (fp_line (start -4.5 1.75) (end -4.5 -1.75) (layer \
171 F.SilkS) (width 0.127)))",
172 )
173 }
174
175 #[test]
176 fn test_kicad_1() {
177 check_parse_kicad(
178 "(module SILABS_EFM32_QFM24
179 (layer F.Cu))",
180 )
181 }
182
183 #[test]
184 fn test_multiline() {
185 let mut rules = formatter::Rules::new();
186 rules.insert("foo", 1);
187 rules.insert("mars", 1);
188 check_parse_rules(
189 "\
190 (hello \"test it\"
191 (foo bar)
192 (mars venus))",
193 rules,
194 )
195 }
196
197 #[test]
198 fn test_multiline_two_empty() {
199 check_parse_res(
200 "\
201 (hello
202
203 world)",
204 "(hello world)",
205 )
206 }
207
208 #[test]
209 fn test_fail_pcb() {
210 check_parse_kicad(
211 "\
212 (kicad_pcb (version 4) (host pcbnew \"(2015-05-31 BZR 5692)-product\")
213 \
214 (general))",
215 )
216 }
217
218 #[test]
219 fn test_quoted_escape() {
220 check_parse_kicad("(hello \"21\\\" (inch test)\")")
221 }