Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_jdat/tests/decode_faults.rs

47.9 KiB, 3 runs

created by r1870400018:67311, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1//! `Dat::decode_string` on text that is wrong. It was lenient where leniency hid a fault, found
2//! while building the Oxegen settings reader (D-20261005-12): a map left open returned its inner
3//! map, text after the value was ignored, and digits parted by a space read as one number. Each
4//! of those is an error now that names the line and the column, and no key or value. What the
5//! format allows by design (unquoted words, single quotes, kind annotations, trailing commas, comments that
6//! run to the end of the line) is checked to stay.
7
8use oxedyne_fe2o3_core::prelude::*;
9use oxedyne_fe2o3_jdat::{
10 prelude::*,
11 bdat::DecodeLimits,
12 string::dec::DecoderConfig,
13 usr::{
14 UsrKindId,
15 UsrKinds,
16 },
17};
18use oxedyne_fe2o3_num::float::Float64;
19
20use std::collections::BTreeMap;
21
22
23fn refused(s: &str, words: &[&str]) {
24 refused_without(s, words, &[]);
25}
26
27// As `refused`, and the refusal must hold none of `absent`, a value or a key of `s`.
28fn refused_without(s: &str, words: &[&str], absent: &[&str]) {
29 match Dat::decode_string(s) {
30 Ok(d) => panic!("{:?} should be refused, and became {}", s, d),
31 Err(e) => {
32 let msg = e.plain();
33 for w in words {
34 assert!(msg.contains(w), "the refusal of {:?} should hold {:?}, and reads: {}",
35 s, w, msg);
36 }
37 for w in absent {
38 assert!(!msg.contains(w), "the refusal of {:?} should not hold {:?}, and reads: {}",
39 s, w, msg);
40 }
41 },
42 }
43}
44
45fn read(s: &str) -> Dat {
46 match Dat::decode_string(s) {
47 Ok(d) => d,
48 Err(e) => panic!("{:?} should read, and was refused: {}", s, e.plain()),
49 }
50}
51
52#[test]
53fn test_map_left_open_is_refused_00() {
54 // The inner map used to come back, and the key "a" was dropped.
55 refused("{\"a\": {\"size\": 1}", &["closure", "line 1"]);
56 refused("{\"a\": [1]", &["closure"]);
57 refused("[[1]", &["closure"]);
58 refused("[(u8|5)", &["closure"]);
59 refused("{\"a\": (u8|5)", &["closure"]);
60 refused("{\"a\": 1,", &["closure"]);
61 refused("[1, 2", &["closure"]);
62}
63
64#[test]
65fn test_kind_annotation_left_open_is_refused_00() {
66 refused("(u8|5", &["closure", "')'"]);
67 refused("{\"n\": (u8|1", &["closure"]);
68 refused("{\"n\": (u8|1}", &[]);
69}
70
71#[test]
72fn test_text_after_the_value_is_refused_00() {
73 refused("{\"a\": 1} x", &["after the end", "line 1"]);
74 refused("{\"a\": 1} {\"b\": 2}", &["after the end"]);
75 refused("{\"a\": 1}\n}", &["after the end", "line 2"]);
76 refused("[1] 2", &["after the end"]);
77 refused("(u8|5) 6", &["after the end"]);
78 // Whitespace and comments may follow.
79 read("{\"a\": 1} \n\t ");
80 read("{\"a\": 1} ! end !");
81 read("{\"a\": 1} # to the end of the line");
82 read("[1] ! end ! \n # again");
83 read("(u8|5) ! end !");
84}
85
86#[test]
87fn test_values_need_a_separator_00() {
88 // These read as the number 12, and as "ab".
89 refused("[1 2]", &["no ',' or ':'"]);
90 refused("{\"a\": 1 2}", &["no ',' or ':'"]);
91 refused("{\"a\": \"x\" \"y\"}", &["no ',' or ':'"]);
92 refused("[\"a\" \"b\"]", &["no ',' or ':'"]);
93 refused("{\"a\": 1 \"b\": 2}", &["no ',' or ':'"]);
94 refused("{\"a\": 1\n\"b\": 2}", &["line 2"]);
95 // A space beside the separators, and around a value, is fine.
96 assert_eq!(read("[ 1 , 2 ]").to_string(), read("[1,2]").to_string());
97 read("{ \"a\" : 1 , \"b\" : \"x\" }");
98}
99
100#[test]
101fn test_lines_are_counted_by_newlines_00() {
102 // A backslash used to count as a line, so a file with escapes named the wrong line.
103 refused("{\n\"a\": \"x\\\\y\\\"z\",\n\"b\": 1 2}", &["line 3"]);
104 refused("{\"a\": \"\\n\\n\\n\", \"b\": 1 2}", &["line 1"]);
105}
106
107#[test]
108fn test_refusal_names_line_and_column_not_the_key_00() {
109 // A key may be personal data (an email, a huser code), so a refusal places itself by line and
110 // column and names none (lead ruling, U0-13).
111 refused_without("{\n\t\"roles\": {\n\t\t\"qr\": {\n\t\t\t\"size\": 1 2,\n\t\t},\n\t},\n}",
112 &["line 4", "column 14"], &["roles", "qr", "size"]);
113 refused_without("{\"screens\": {\"cs\": [\"a\",\n \"b\" \"c\"]}}", &["line 2"], &["screens", "cs"]);
114 // A duplicate key, said where the second one is closed.
115 refused_without("{\n\"roles\": {\n\"qr\": 1,\n\"qr\": 2}}", &["line 4", "duplicate key", "already exists"],
116 &["roles", "qr\""]);
117 // Column counts characters, a tab as one, and not bytes.
118 refused("{\n\t\"é日\": 1 2}", &["line 2", "column 10"]);
119}
120
121#[test]
122fn test_located_gives_the_place_without_the_text_00() {
123 let cfg = DecoderConfig::<BTreeMap<_, _>, BTreeMap<_, _>>::default();
124 match Dat::decode_string_located("{\n \"k\": {\"s3cret\": 1 2}}", &cfg) {
125 Ok(d) => panic!("should be refused, and became {}", d),
126 Err(at) => {
127 assert_eq!((at.line, at.col), (2, 21));
128 // The place is a line and a column; neither key nor value is in the words.
129 let said = at.error.msgs().join(" ");
130 assert!(!said.contains("s3cret") && !said.contains("\"k\""), "{}", said);
131 },
132 }
133}
134
135#[test]
136fn test_comments_are_free_text_00() {
137 // A quote or a brace in a comment is not one outside it.
138 let d = read("{ ! was \"logo\": { [ 1, 2 ] } # not a close ! \"a\": 1, # it's \"fine\"\n \"b\": 2 }");
139 let s = d.to_string();
140 assert!(s.contains("\"a\"") && s.contains("\"b\""), "read as {}", s);
141 // A marker in a string is not a comment.
142 assert_eq!(read(r##"{"a": "# not ! a comment #"}"##).to_string(), r##"{ "a": "# not ! a comment #"}"##);
143}
144
145#[test]
146fn test_the_format_is_kept_00() {
147 // Features, by design: an unquoted word is a string, a single quote makes a string, a kind
148 // annotation types a value, a trailing comma is allowed, 0x is hex, and a comment runs to the
149 // end of its line.
150 assert_eq!(read("{\"a\": yes}").to_string(), "{ \"a\": \"yes\"}");
151 assert_eq!(read("{\"a\": 29x.4}").to_string(), "{ \"a\": \"29x.4\"}");
152 assert_eq!(read("{\"a\": 'x'}").to_string(), "{ \"a\": \"x\"}");
153 assert_eq!(read("{a: 1}").to_string(), "{ \"a\": (u8|1)}");
154 assert_eq!(read("{\"a\": (u8|1), \"b\": 0x10,}").to_string(), "{ \"a\": (u8|1), \"b\": (u8|16)}");
155 assert_eq!(read("[1, 2,]").to_string(), "[ (u8|1), (u8|2)]");
156 assert!(Dat::decode_string("{\"n\": (u8|300)}").is_err());
157 assert!(Dat::decode_string("{\"n\": (zzz|1)}").is_err());
158 read("{\"a\": 1, # a note\n \"b\": 2}");
159}
160
161#[test]
162fn test_a_user_kind_payload_keeps_its_closing_paren_00() {
163 // `(node|{...})` ends its frame at the payload's brace, and the root takes the `)` itself.
164 let mut uks = UsrKinds::new(BTreeMap::new(), BTreeMap::new());
165 let ukind = UsrKindId::new(1, Some("node"), None);
166 uks.add(ukind.clone()).unwrap_or_default();
167 let cfg = DecoderConfig::<_, _>::jdat(Some(uks));
168 let at = |s: &str| match Dat::decode_string_located(s, &cfg) {
169 Ok(d) => format!("read {}", d),
170 Err(at) => format!("refused line {} col {}: {}", at.line, at.col, at.error.plain()),
171 };
172 assert!(at("(node|{\"a\":1})").starts_with("read"), "{}", at("(node|{\"a\":1})"));
173 assert!(at("(node|[1, 2])").starts_with("read"));
174 assert!(at("(node|{\"a\":1} ! note ! \n ) # end").starts_with("read"));
175 assert!(at("(node|{\"a\":1}").starts_with("refused"), "open: {}", at("(node|{\"a\":1}"));
176 assert!(at("(node|{\"a\":1} x)").starts_with("refused line 1 col 15"), "{}", at("(node|{\"a\":1} x)"));
177 assert!(at("(node|{\"a\":1}) x").starts_with("refused line 1 col 16"), "{}", at("(node|{\"a\":1}) x"));
178 assert!(at("(node|[1] ").starts_with("refused"));
179}
180
181#[test]
182fn test_a_user_kind_payload_in_a_container_00() {
183 let mut uks = UsrKinds::new(BTreeMap::new(), BTreeMap::new());
184 uks.add(UsrKindId::new(1, Some("node"), None)).unwrap_or_default();
185 let cfg = DecoderConfig::<_, _>::jdat(Some(uks));
186 let at = |s: &str| match Dat::decode_string_located(s, &cfg) {
187 Ok(d) => format!("read {}", d),
188 Err(at) => format!("refused line {} col {}", at.line, at.col),
189 };
190 for s in [
191 "[(node|{\"a\":1}), 2]",
192 "{\"k\": (node|[1, 2]), \"j\": 3}",
193 "{\"k\": (node|{\"a\":(node|{\"b\":1})}) ! after ! , \"j\": 3}",
194 "(node|(node|{\"x\":1}))",
195 ] {
196 assert!(at(s).starts_with("read"), "{} became {}", s, at(s));
197 }
198 // A closing paren too many, or one missing, is a fault wherever it falls.
199 for s in [
200 "{\"k\": (node|{\"a\":1})) }",
201 "{\"k\": (node|{\"a\":1} }",
202 "[(node|{\"a\":1}), 2))",
203 "(node|(node|{\"x\":1})",
204 ] {
205 assert!(at(s).starts_with("refused"), "{} became {}", s, at(s));
206 }
207}
208
209#[test]
210fn test_a_whole_number_kind_takes_no_fraction_00() {
211 // Only the digits before the point were parsed, so these read as 1 and 2. A whole value
212 // written with a point or an exponent is read (lead ruling, U0-5 L3); a fraction is not.
213 refused_without("(u8|1.5)", &["whole number", "has a fraction"], &["1.5"]);
214 refused("(i32|-2.5)", &["whole number"]);
215 assert_eq!(read("(u16|1e2)"), Dat::U16(100));
216 assert_eq!(read("{\"n\": (u64|3.0)}"), one_key("n", Dat::U64(3)));
217 refused_without("{\"nKeyQ\": (u64|3.5)}", &["whole number", "line 1"], &["nKeyQ"]);
218 // A float kind keeps its fraction, and hex digits are not an exponent.
219 assert_eq!(read("(f64|1.5)"), Dat::F64(Float64(1.5)));
220 assert_eq!(read("(u8|0xff)"), Dat::U8(255));
221 assert_eq!(read("(u16|0x1e5)"), Dat::U16(485));
222}
223
224#[test]
225fn test_a_number_with_no_digit_is_refused_00() {
226 // A number with no digit in its significand (`e2`, `.`, `0x`) read as 0, and an exponent mark
227 // with no digit after it (`1e`) read as 1 (U0-13 M1; the root is `NumberString::validate`).
228 for s in [
229 "(u8|e2)", "(u8|e)", "(u8|.)", "(u8|.e5)", "(u64|E9)", "(i32|-e2)", "(u8|1e)", "(u8|1e-)",
230 "(u8|1e+)", "(adec|e2)", "(adec|1e)", "(u8|0x)", "(aint|e2)", "(i64|-.)", "(f64|e2)",
231 "(f64|1e)", "(f32|.)",
232 ] {
233 refused_without(s, &["line 1", "column"], &["e2", "E9"]);
234 }
235 refused("{\n\"k\": (u8|e2)}", &["line 2", "column"]);
236 // The kinds read what they read.
237 assert_eq!(read("(u8|0e5)"), Dat::U8(0));
238 assert_eq!(read("(u8|1e2)"), Dat::U8(100));
239 assert_eq!(read("(u8|0x1f)"), Dat::U8(31));
240 assert_eq!(read("(f64|1e2)"), Dat::F64(Float64(100.0)));
241 // With no kind, text that is no number is a string, as it always was.
242 assert_eq!(read("e2"), Dat::Str("e2".to_string()));
243 assert_eq!(read("."), Dat::Str(".".to_string()));
244 assert_eq!(read("1e"), Dat::Str("1e".to_string()));
245 assert_eq!(read("[e2, ., 1e-]"), Dat::List(vec![
246 Dat::Str("e2".to_string()), Dat::Str(".".to_string()), Dat::Str("1e-".to_string()),
247 ]));
248 // One rule in a list, a map and a nest of both: the atom no number takes is a string.
249 assert_eq!(read("{\"k\": e2}"), one_key("k", Dat::Str("e2".to_string())));
250 assert_eq!(read("{\"k\": .}"), one_key("k", Dat::Str(".".to_string())));
251 assert_eq!(read("{\"k\": 1e-}"), one_key("k", Dat::Str("1e-".to_string())));
252 assert_eq!(read("{\"k\": [e2, 1e]}"), one_key("k", Dat::List(vec![
253 Dat::Str("e2".to_string()), Dat::Str("1e".to_string()),
254 ])));
255 assert_eq!(read("[{\"k\": e2}, 3]"), Dat::List(vec![
256 one_key("k", Dat::Str("e2".to_string())), Dat::U8(3),
257 ]));
258 // A typed atom still refuses it.
259 refused("[(u8|e2)]", &["line 1", "column"]);
260}
261
262#[test]
263fn test_a_negative_zero_reads_as_zero_in_every_whole_number_kind_00() {
264 // `(u8|-0.0)` read and `(u8|-0)` was refused: one rule for both (U0-13 L1), a zero has no sign.
265 for (s, want) in [
266 ("(u8|-0)", Dat::U8(0)), ("(u8|-0.0)", Dat::U8(0)), ("(u8|+0)", Dat::U8(0)),
267 ("(u16|-0e3)", Dat::U16(0)), ("(u64|-0)", Dat::U64(0)), ("(i8|-0)", Dat::I8(0)),
268 ("(i32|-0.0)", Dat::I32(0)),
269 ] {
270 assert_eq!(read(s), want, "{}", s);
271 }
272 // A number below zero is still refused in an unsigned kind.
273 refused("(u8|-1)", &["cannot be negative"]);
274 refused("(u8|-0.5)", &["whole number"]);
275 refused("(u8|-1e0)", &["is negative"]);
276}
277
278fn bx(d: Dat) -> Dat {
279 Dat::Box(Box::new(d))
280}
281
282fn sm(d: Dat) -> Dat {
283 Dat::Opt(Box::new(Some(d)))
284}
285
286#[test]
287fn test_a_wrapper_round_a_dataless_value_reads_its_own_paren_00() {
288 // The encoder writes these, and the wrapper's frame ended at the inner ')' and left the
289 // outer one over: "Found text after the end of the value".
290 assert_eq!(read("(box|(true))"), bx(Dat::Bool(true)));
291 assert_eq!(read("(box|(false))"), bx(Dat::Bool(false)));
292 assert_eq!(read("(some|(none))"), sm(Dat::Opt(Box::new(None))));
293 assert_eq!(read("(some|(empty))"), sm(Dat::Empty));
294 assert_eq!(read("(some|(some|(none)))"), sm(sm(Dat::Opt(Box::new(None)))));
295 assert_eq!(read("(box|(box|(box|(empty))))"), bx(bx(bx(Dat::Empty))));
296 // The same in a map, where the stray ')' once emptied the map, and in a list.
297 let d = read("{\"k\": (box|(none)), \"j\": (some|(true)), \"i\": 1}");
298 assert_eq!(d.to_string(), "{ \"i\": (u8|1), \"j\": (some|(true)), \"k\": (box|(none))}");
299 assert_eq!(read("[(some|(empty)), (box|(true)), 3]").to_string(),
300 "[ (some|(empty)), (box|(true)), (u8|3)]");
301 // Whitespace and a note may stand inside the wrapper, round its payload.
302 assert_eq!(read("(box| ( true ) )"), bx(Dat::Bool(true)));
303 assert_eq!(read("(box|(true) ! n ! )"), bx(Dat::Bool(true)));
304 // A paren too many, or too few, is a fault wherever it falls.
305 refused("(box|(true)))", &["after the end"]);
306 refused("(box|(true)", &["closure"]);
307 refused("{\"k\": (box|(none)))}", &[]);
308 refused("{\"k\": (box|(none)}", &[]);
309 refused("[(some|(empty))), 3]", &[]);
310}
311
312#[test]
313fn test_a_finished_value_takes_no_other_before_a_separator_00() {
314 // A value of any kind, once finished, refuses the next until a ',' or a ':'. Each of these
315 // read without a fault: the first value was dropped, or the second joined the wrong key.
316 for s in [
317 "{\n \"server\": {\"port\": 80}\n \"db\": {\"path\": \"x.db\"}\n}\n",
318 "{\"roles\": {\"qr\": {\"size\": 29}\n \"logo\": {\"size\": 4}}}",
319 "{\"list\": [1, 2]\n \"name\": \"x\"}",
320 "{\n \"a\": (u8|1)\n \"b\": 2\n}",
321 "[{\"a\":1} {\"b\":2}]",
322 "[1 (u8|2)]",
323 "[(u8|1) (u8|2)]",
324 "[[1] [2]]",
325 "[[1] \"x\"]",
326 "[[1] x]",
327 "[{} {}]",
328 "[{} [1]]",
329 "{\"a\": [1] 2}",
330 "{\"a\": 1 [2]}",
331 "{\"a\": 1 {\"b\": 2}}",
332 "{\"a\": \"x\" (u8|1)}",
333 "{\"a\": 'x' [1]}",
334 "{\"a\": (box|(true)) (false)}",
335 "(box|(true) x)",
336 "(box|(true) \"x\")",
337 "x{\"a\":1}",
338 "x[1]",
339 "[x(u8|1)]",
340 "[1(u8|2)]",
341 "[\"a\"[1]]",
342 "(u 8|1)",
343 ] {
344 refused(s, &["no ',' or ':'"]);
345 }
346 refused("{\n \"server\": {\"port\": 80}\n \"db\": 1\n}", &["line 3"]);
347 // With the commas, and with a note between, they read. A quote glued to a word is one string.
348 assert_eq!(read("{\"server\": {\"port\": 80},\n \"db\": {\"path\": \"x.db\"}}").to_string(),
349 "{ \"db\": { \"path\": \"x.db\"}, \"server\": { \"port\": (u8|80)}}");
350 assert_eq!(read("[{\"a\":1}, {\"b\":2}]").to_string(), "[ { \"a\": (u8|1)}, { \"b\": (u8|2)}]");
351 assert_eq!(read("[ [1] ! n ! , [2] ]").to_string(), "[ [ (u8|1)] !n !, [ (u8|2)]]");
352 assert_eq!(read("[he\"(]\"o, (u8|1)]"), Dat::List(vec![Dat::Str(fmt!("he(]o")), Dat::U8(1)]));
353 assert_eq!(read("(u8 |1)"), Dat::U8(1));
354}
355
356fn map_of(k: &str, v: Dat) -> Dat {
357 let mut m = BTreeMap::new();
358 m.insert(Dat::Str(k.to_string()), v);
359 Dat::Map(m)
360}
361
362#[test]
363fn test_a_kind_takes_only_the_opener_that_fits_it_00() {
364 // A box, a some and a user kind wrap a list or a map; an atom or the other molecule's kind
365 // takes neither. These all read as the bare molecule, wrapper lost, or were refused.
366 assert_eq!(read("(box|{})"), Dat::Box(Box::new(Dat::Map(BTreeMap::new()))));
367 assert_eq!(read("(box|{\"k\": (u8|1)})"), Dat::Box(Box::new(map_of("k", Dat::U8(1)))));
368 assert_eq!(read("(some|{\"k\": (u8|1)})"), Dat::Opt(Box::new(Some(map_of("k", Dat::U8(1))))));
369 assert_eq!(read("(box|[])"), Dat::Box(Box::new(Dat::List(Vec::new()))));
370 assert_eq!(read("(box|[1])"), Dat::Box(Box::new(Dat::List(vec![Dat::U8(1)]))));
371 assert_eq!(read("(some|[1, 2])"),
372 Dat::Opt(Box::new(Some(Dat::List(vec![Dat::U8(1), Dat::U8(2)])))));
373 assert_eq!(read("(box|(some|{\"a\": (box|[1])}))"), Dat::Box(Box::new(Dat::Opt(Box::new(Some(
374 map_of("a", Dat::Box(Box::new(Dat::List(vec![Dat::U8(1)])))))))))
375 );
376 assert_eq!(read("[(box|{}), (some|[])]"), Dat::List(vec![
377 Dat::Box(Box::new(Dat::Map(BTreeMap::new()))),
378 Dat::Opt(Box::new(Some(Dat::List(Vec::new())))),
379 ]));
380 for s in [
381 "(u8|{\"a\":1})", "(str|{\"a\":1})", "(f64|{})", "(bool|{})", "(u8|[1])", "(str|[])",
382 "(map|[1])", "(omap|[1])", "(list|{\"a\":1})", "(t2|{\"a\":1})", "(vek|{})", "(bu8|{})",
383 "{\"x\": (u8|{\"a\":1})}", "{\"x\": (map|[1])}", "[(list|{}), 1]", "{\"x\": (str|[1])}",
384 ] {
385 refused(s, &["line 1"]);
386 }
387 refused("(box|[1] [2])", &["no ',' or ':'"]);
388 refused("(box|{} {})", &["no ',' or ':'"]);
389 refused("(box|{} [1])", &["no ',' or ':'"]);
390 refused("(box|{\"a\":1}", &["closure"]);
391}
392
393#[test]
394fn test_a_user_kind_wraps_its_list_or_map_payload_00() {
395 let mut uks = UsrKinds::new(BTreeMap::new(), BTreeMap::new());
396 let ukid = UsrKindId::new(1, Some("node"), None);
397 uks.add(ukid.clone()).unwrap_or_default();
398 let cfg = DecoderConfig::<_, _>::jdat(Some(uks));
399 let at = |s: &str| match Dat::decode_string_with_config(s, &cfg) {
400 Ok(d) => d,
401 Err(e) => panic!("{:?} should read, and was refused: {}", s, e.plain()),
402 };
403 assert_eq!(at("(node|{\"a\": (u8|1)})"), Dat::Usr(ukid.clone(), Some(Box::new(map_of("a", Dat::U8(1))))));
404 assert_eq!(at("(node|[(u8|1)])"), Dat::Usr(ukid.clone(), Some(Box::new(Dat::List(vec![Dat::U8(1)])))));
405 assert_eq!(at("[(node|[]), (node)]"), Dat::List(vec![
406 Dat::Usr(ukid.clone(), Some(Box::new(Dat::List(Vec::new())))),
407 Dat::Usr(ukid.clone(), None),
408 ]));
409}
410
411#[test]
412fn test_a_plain_list_inside_a_tuple_is_a_list_00() {
413 // The inner list took its parent's kind: `(t2|[ [1, 2], 3 ])` read the inner list as a t2.
414 let l12 = Dat::List(vec![Dat::U8(1), Dat::U8(2)]);
415 assert_eq!(read("(t2|[ [1, 2], 3 ])"), Dat::Tup2(Box::new([l12.clone(), Dat::U8(3)])));
416 assert_eq!(read("(t2|[ 3, [1, 2] ])"), Dat::Tup2(Box::new([Dat::U8(3), l12.clone()])));
417 assert_eq!(read("(t2|[ [], [[1, 2]] ])"), Dat::Tup2(Box::new([
418 Dat::List(Vec::new()), Dat::List(vec![l12.clone()]),
419 ])));
420 assert_eq!(read("(t3|[ [1], (t2|[ [1, 2], 3 ]), 4 ])"), Dat::Tup3(Box::new([
421 Dat::List(vec![Dat::U8(1)]),
422 Dat::Tup2(Box::new([l12.clone(), Dat::U8(3)])),
423 Dat::U8(4),
424 ])));
425 assert_eq!(read("(list|[ [1, 2], [] ])"), Dat::List(vec![l12.clone(), Dat::List(Vec::new())]));
426 assert_eq!(read("(box|(t2|[ [1, 2], 3 ]))"),
427 Dat::Box(Box::new(Dat::Tup2(Box::new([l12.clone(), Dat::U8(3)])))));
428 assert_eq!(read("(t2|[ {\"k\": [1, 2]}, 3 ])"),
429 Dat::Tup2(Box::new([map_of("k", l12.clone()), Dat::U8(3)])));
430}
431
432#[test]
433fn test_a_kind_or_quote_left_open_at_the_top_is_refused_00() {
434 // Each of these read: `(` as (empty), `(true` as true, `(none` as none, `(u8` as the string
435 // "u8", `"abc` and `'abc` as "abc".
436 for s in ["(", "(true", "(false", "(none", "(empty", "(u8", "(box", "(my_kind", " ( "] {
437 refused(s, &["closure", "')'"]);
438 }
439 for s in ["\"abc", "'abc", "\"", "'", "\"a\\\"", "he\"llo", "{\"a\": \"x}", "[\"x, 1]", "{\"a\": 'x}"] {
440 refused(s, &["closure", "quote"]);
441 }
442 // A quote or a kind that is closed reads, wherever it sits.
443 assert_eq!(read("(true)"), Dat::Bool(true));
444 assert_eq!(read("\"a'b\""), Dat::Str(fmt!("a'b")));
445 assert_eq!(read("'a\"b'"), Dat::Str(fmt!("a\"b")));
446}
447
448#[test]
449fn test_a_stray_bar_or_colon_is_refused_00() {
450 // `(u8|1|2)` read as 12 and `{"a": 1:2}` as {"a": 2}; `{:1}` gave an (empty) key.
451 for s in [
452 "(u8|1|2)", "{\"a\": 1|2}", "[1|2]", "|", "||", "[|]", "{\"a\": |}", "(u8||1)", "a|b",
453 "{a|b: 1}", "(box|(u8|1)|)", "[(u8|1)|]",
454 ] {
455 refused(s, &["'|'"]);
456 }
457 for s in [
458 "{\"a\": 1:2}", "{\"a\"::1}", "{\"a\": 1, \"b\": : 2}", "{\"a\": \"b\": 1}",
459 ] {
460 refused(s, &["':'"]);
461 }
462 // The separators in their places, and inside strings and notes, read.
463 assert_eq!(read("(u8|1)"), Dat::U8(1));
464 assert_eq!(read("{\"a|b\": \"c:d|e\", ! n|: ! \"f\": (u8|2)}").to_string(),
465 "{ \"a|b\": \"c:d|e\", \"f\" !n|: !: (u8|2)}");
466 read("{(u8|1): 2, [3]: 4}");
467}
468
469#[test]
470fn test_an_empty_quoted_key_is_a_key_00() {
471 // A `""` key before a comma was replaced by a random u32 key, the one before the closing
472 // brace was kept, so `{"": 1}` and `{"": 1, "b": 2}` disagreed and the key was lost.
473 let one = |ks: &[&str]| {
474 let mut m = BTreeMap::new();
475 for (i, k) in ks.iter().enumerate() {
476 m.insert(Dat::Str(k.to_string()), Dat::U8(i as u8 + 1));
477 }
478 Dat::Map(m)
479 };
480 assert_eq!(read("{\"\": 1}"), one(&[""]));
481 assert_eq!(read("{\"\": 1, \"b\": 2}"), one(&["", "b"]));
482 assert_eq!(read("{\"b\": 1, \"\": 2}"), one(&["b", ""]));
483 assert_eq!(read("{'': 1, \"c\": 2}"), one(&["", "c"]));
484 // Two empty keys are a duplicate, as any other key is.
485 refused("{\"\": 1, \"\": 2}", &["already exists"]);
486 // The encoder's own text reads back.
487 let d = one(&["", "b"]);
488 assert_eq!(read(&d.jdat().unwrap_or_default()), d);
489}
490
491#[test]
492fn test_a_comma_with_nothing_before_it_is_refused_00() {
493 // `[1,,2]` read as [1, (empty), 2] and `{"a": 1,, "b": 2}` was refused only at the colon
494 // after it; a ',' that ends no item is a missing item, in a list as in a map.
495 for s in [
496 "[,]", "[,1]", "[1,,2]", "[1, ,2]", "[1,2,,]", "[ , ]", "(list|[,])", "[[,]]",
497 "{,}", "{,\"a\": 1}", "{\"a\": 1,, \"b\": 2}", "{\"a\": 1, , \"b\": 2}", "{\"a\": {,}}",
498 "{\"a\": [1,,2]}", "(box|[1,,2])", "(t2|[1,,2])",
499 ] {
500 refused(s, &["','", "nothing"]);
501 }
502 // A member that has a key and no value is the same fault as one cut off by the brace.
503 for s in ["{\"a\":,}", "{\"a\": 1, \"b\":, \"c\": 2}", "{\"a\": ,}", "{:,}"] {
504 refused(s, &["no value"]);
505 }
506 // An item, a string, an explicit empty or a note before the comma is something.
507 assert_eq!(read("[1,]"), Dat::List(vec![Dat::U8(1)]));
508 assert_eq!(read("[\"\", 1]"), Dat::List(vec![Dat::Str(fmt!("")), Dat::U8(1)]));
509 assert_eq!(read("[(empty), 1]"), Dat::List(vec![Dat::Empty, Dat::U8(1)]));
510 assert_eq!(read("{\"a\": 1,}"), one_key("a", Dat::U8(1)));
511 assert_eq!(read("{\"a\": (empty), \"b\": \"\"}").to_string(), "{ \"a\": (empty), \"b\": \"\"}");
512 read("[ ! a note ! , 1]");
513 read("{\"a\": ! a note !, \"b\": 1}");
514}
515
516#[test]
517fn test_a_comma_where_a_colon_belongs_is_refused_00() {
518 // `{"a", 1}` read as {"a": 1}, the comma setting the key; the format's colon is the only
519 // separator of a key from its value (lead ruling, consistent with H2).
520 for s in [
521 "{\"a\", 1}", "{\"a\", 1, \"b\", 2}", "{\"a\": 1, \"b\", 2}", "{1, 2}", "{\"a\" ! c !, 1}",
522 "{! c !, \"a\": 1}", "{\"a\": {\"b\", 1}}", "{\"a\",}", "(map|{\"a\", 1})",
523 ] {
524 refused(s, &["','", "':'"]);
525 }
526 read("{\"a\": 1, \"b\": 2}");
527}
528
529#[test]
530fn test_whitespace_in_a_kind_label_reads_as_a_space_00() {
531 // A tab, a CR and a LF are whitespace as a space is, around a label; a gap inside one is not.
532 for s in ["(u8 |1)", "(u8\t|1)", "(u8\r|1)", "(u8\n|1)", "(u8\r\n|1)", "(\r\nu8|1)", "( \tu8 \t|1)"] {
533 assert_eq!(read(s), Dat::U8(1), "{:?}", s);
534 }
535 assert_eq!(read("{\"a\": (u8\r\n|1)}"), one_key("a", Dat::U8(1)));
536 for s in ["(u 8|1)", "(u\t8|1)", "(u\r\n8|1)"] {
537 refused(s, &["gap"]);
538 }
539}
540
541fn one_key(k: &str, v: Dat) -> Dat {
542 map_of(k, v)
543}
544
545#[test]
546fn test_an_empty_key_is_refused_unless_it_keys_a_note_00() {
547 // `{:1}` read as an (empty) key; the format keys a note to the empty dat, `{:! note !,}`.
548 for s in ["{:1}", "{\"a\": 1, :2}", "{ : \"x\"}", "{: ! n ! 5}", "{:[1]}", "{:{}}"] {
549 refused(s, &["':'", "key"]);
550 }
551 refused("{:}", &["key"]);
552 read("{:! a note !}");
553 read("{\"a\": 1, :! a note !, \"b\": 2}");
554 read("{1:2, :! a singleton comment !, ! a self-keyed comment ! 3:4}");
555 // An explicit (empty) is a key, as the encoder writes it.
556 let mut m = BTreeMap::new();
557 m.insert(Dat::Empty, Dat::U8(1));
558 assert_eq!(read("{(empty): 1}"), Dat::Map(m));
559}
560
561// A note is no part of what a value is, so these compare what was read with the notes taken off.
562fn strip(d: &Dat) -> Dat {
563 match d {
564 Dat::ABox(_, inner, _) => strip(inner),
565 Dat::Map(m) => Dat::Map(m.iter().map(|(k, v)| (strip(k), strip(v))).collect()),
566 Dat::List(v) => Dat::List(v.iter().map(strip).collect()),
567 other => other.clone(),
568 }
569}
570
571fn big_map(n: usize) -> String {
572 let mut s = String::from("{");
573 for i in 0..n {
574 s.push_str(&format!("\"k{}\": 0, ", i));
575 }
576 s
577}
578
579#[test]
580fn test_a_duplicate_key_is_refused_in_few_words_00() {
581 // The refusal held a debug print of the whole map being read, megabytes for a large one.
582 let s = format!("{}\"k0\": 1}}", big_map(500));
583 refused_without(&s, &["A duplicate key, which already exists in the map"], &["k0"]);
584 match Dat::decode_string(&s) {
585 Err(e) => assert!(e.plain().len() < 400, "the refusal is {} bytes", e.plain().len()),
586 Ok(d) => panic!("should be refused, and became {}", d),
587 }
588 // A key behind a note is the same key.
589 refused("{\"a\": 1, ! n ! \"a\": 2}", &["already exists"]);
590 refused("{\"a\": 1, \"b\": 2, # n\n \"a\":! c\n}", &["already exists"]);
591 // As it is in an ordered map.
592 let mut cfg = DecoderConfig::<BTreeMap<_, _>, BTreeMap<_, _>>::default();
593 cfg.use_ordmaps = true;
594 for s in [format!("{}\"k0\": 1}}", big_map(500)), "{\"a\": 1, ! n ! \"a\": 2}".to_string()] {
595 match Dat::decode_string_with_config(s.as_str(), &cfg) {
596 Err(e) => assert!(e.plain().contains("already exists") && e.plain().len() < 400,
597 "the refusal of an ordered map is {}", e.plain()),
598 Ok(d) => panic!("should be refused, and became {}", d),
599 }
600 }
601}
602
603#[test]
604fn test_an_unpaired_member_is_refused_without_the_map_00() {
605 let s = format!("{}\"z\"}}", big_map(500));
606 refused(&s, &["value with no key at the end of a map"]);
607 let s = format!("{}\"z\":}}", big_map(500));
608 refused_without(&s, &["A key has no value at the end of a map"], &["\"z\""]);
609 for s in [format!("{}\"z\"}}", big_map(500)), format!("{}\"z\":}}", big_map(500))] {
610 match Dat::decode_string(&s) {
611 Err(e) => assert!(e.plain().len() < 400, "the refusal is {} bytes", e.plain().len()),
612 Ok(d) => panic!("should be refused, and became {}", d),
613 }
614 }
615}
616
617#[test]
618fn test_a_byte_order_mark_is_not_text_00() {
619 let want = read("{\"a\":1}");
620 assert_eq!(read("\u{feff}{\"a\":1}"), want);
621 assert_eq!(read("\u{feff}\n{\"a\":1}"), want);
622 refused("\u{feff}\u{feff}{\"a\":1}", &[]);
623 refused("{\"a\":1}\u{feff}", &[]);
624 // Inside a string it is a character like another.
625 assert_eq!(read("{\"a\":\"\u{feff}x\"}"), one_key("a", Dat::Str("\u{feff}x".to_string())));
626}
627
628#[test]
629fn test_a_comment_before_the_root_value_is_dropped_00() {
630 for (s, want) in [
631 ("! one !\n{\"a\": 1}", "{\"a\": 1}"),
632 ("# one\n{\"a\": 1}", "{\"a\": 1}"),
633 ("# one\n# two\n{\"a\": 1}", "{\"a\": 1}"),
634 ("! one !\n[1, 2]", "[1, 2]"),
635 ("# one\n[1, 2]", "[1, 2]"),
636 ("# one\n(u8|1)", "(u8|1)"),
637 ("# one\n\"x\"", "\"x\""),
638 ("# one\n42", "42"),
639 ] {
640 assert_eq!(read(s), read(want), "{:?}", s);
641 }
642 // And one after it, or between a value's marks, stays as it was.
643 assert_eq!(read("{\"a\": 1} # end"), read("{\"a\": 1}"));
644 assert_eq!(read("{\"a\": 1} ! end !"), read("{\"a\": 1}"));
645 // Outside a list or a map a comment has no item to be the note of, and the refusal says so
646 // in the decoder's words and not the old "only allowed in molecules".
647 refused("42 # c\n43", &["comment", "outside any list or map", "line "]);
648}
649
650#[test]
651fn test_a_whole_number_kind_takes_a_whole_number_in_any_form_00() {
652 assert_eq!(read("(u8|1.0)"), Dat::U8(1));
653 assert_eq!(read("(u8|10e-1)"), Dat::U8(1));
654 assert_eq!(read("(u64|1e3)"), Dat::U64(1000));
655 assert_eq!(read("(i8|-2.0)"), Dat::I8(-2));
656 assert_eq!(read("(i32|-1.5e1)"), Dat::I32(-15));
657 assert_eq!(read("{\"n\": (u16|65535.0)}"), one_key("n", Dat::U16(65535)));
658 let t0 = std::time::Instant::now();
659 for s in [
660 "(u8|1.5)", "(u8|256.0)", "(u8|-1.0)", "(u8|1e-1)", "(u8|1e999999)", "(u64|1e40)",
661 "(u8|1e9999999999999999999)", "(i8|128.0)", "(u8|0.5e1e1)",
662 "(f64|1e400)", "(f32|1e39)", "(f64|-1e400)",
663 ] {
664 refused(s, &[]);
665 }
666 assert!(t0.elapsed().as_secs() < 2, "the refusals took {:?}", t0.elapsed());
667 // A bare number is as it was.
668 assert!(Dat::decode_string("{\"n\": 1e400}").is_ok());
669 assert!(matches!(read("(f64|1e300)"), Dat::F64(_)));
670}
671
672#[test]
673fn test_a_comment_is_allowed_where_whitespace_is_00() {
674 let two = read("{\"a\": 1, \"b\": 2}");
675 for s in [
676 "{\"a\": 1 # c\n, \"b\": 2}",
677 "{\"a\": 1 ! c !, \"b\": 2}",
678 "{\"a\" # c\n: 1, \"b\": 2}",
679 "{\"a\": 1, \"b\" # c\n: 2}",
680 "{\"a\" ! c ! : 1, \"b\": 2}",
681 "{\"a\": 1, \"b\": 2 # c\n}",
682 ] {
683 assert_eq!(strip(&read(s)), strip(&two), "{:?}", s);
684 }
685 let two = read("[1, 2]");
686 for s in ["[1 # c\n, 2]", "[1, 2 # c\n]", "[1 ! c !, 2]", "[1 # a\n\n# b\n, 2]"] {
687 assert_eq!(strip(&read(s)), strip(&two), "{:?}", s);
688 }
689 assert_eq!(strip(&read("[[1] # c\n, 2]")), strip(&read("[[1], 2]")));
690 assert_eq!(strip(&read("{\"a\": [1 # c\n, 2] # d\n, \"b\": {\"x\": 1 # e\n} # f\n}")),
691 strip(&read("{\"a\": [1, 2], \"b\": {\"x\": 1}}")));
692 // The note is kept: it is on the value, and the key comes out bare.
693 assert!(read("{\"a\": 1 # c\n, \"b\": 2}").to_string().contains("#c#"));
694 // A comment is not a separator, nor does it supply a key or a value.
695 for s in [
696 "{\"a\": 1 # c\n \"b\": 2}", "[1 # c\n 2]", "{\"a\" # c\n, \"b\": 2}", "{\"a\": 1 # c\n : 2}",
697 "{\"a\": 1 # c\n,, \"b\": 2}", "{# c\n, \"a\": 1}", "[1, # c\n, 2]", "{\"a\": 1 # c\n\"a\": 2}",
698 ] {
699 refused(s, &[]);
700 }
701 // A comment on a line of its own, or after a ':', is the note it was; one after a ',' is an
702 // entry of its own.
703 assert!(Dat::decode_string("{\"a\": 1 # c\n, # d\n \"b\": 2}").is_ok());
704 assert!(Dat::decode_string("{\"a\": 1,\n # c\n \"b\": 2}").is_ok());
705 assert!(Dat::decode_string("{\"a\":! c\n \"b\": 2}").is_ok());
706}
707
708#[test]
709fn test_a_refusal_at_a_line_end_names_that_line_00() {
710 // The newline that ends a comment closes the entry before it, and the refusal is of that entry.
711 let cfg = DecoderConfig::<BTreeMap<_, _>, BTreeMap<_, _>>::default();
712 match Dat::decode_string_located("{\"a\": 1, \"b\": 2, \"b\":! c\n}", &cfg) {
713 Ok(d) => panic!("should be refused, and became {}", d),
714 Err(at) => assert_eq!((at.line, at.col), (1, 25)),
715 }
716 match Dat::decode_string_located("{\n\"a\": 1,\n\"a\":! c\n}", &cfg) {
717 Ok(d) => panic!("should be refused, and became {}", d),
718 Err(at) => assert_eq!((at.line, at.col), (3, 8)),
719 }
720}
721
722#[test]
723fn test_a_refusal_holds_no_cursor_dump_and_no_internal_names_00() {
724 for s in [
725 "[1,,2]", "{\"a\", 1}", "{\"a\": 1 \"b\"}", "{:1}", "[1, 2", "{\"a\": 1", "(u8|1", "(u8|1|2)",
726 "{\"a\": 1, \"a\": 2}", "(u8 1)", "[1 2]", "{\"a\"}", "{\"a\":}", "(map|[1])", "(list|{\"a\": 1})",
727 "(u8|(u8|1))", "(tup2|[1])", "(tup2|[1, 2, 3])", "(abox|)", "]", "}", ")", ":", "|",
728 ] {
729 match Dat::decode_string(s) {
730 Ok(d) => panic!("{:?} should be refused, and became {}", s, d),
731 Err(e) => {
732 let msg = e.plain();
733 for bad in ["char '", " pos ", "store.", "DaticleMap", "OrdDaticleMap", "Map({", "List(["] {
734 assert!(!msg.contains(bad), "the refusal of {:?} holds {:?}: {}", s, bad, msg);
735 }
736 },
737 }
738 }
739 refused("[1, 2", &["list"]);
740 refused("{\"a\": 1", &["map"]);
741}
742
743#[test]
744fn test_a_key_with_a_line_break_is_not_named_00() {
745 let cfg = DecoderConfig::<BTreeMap<_, _>, BTreeMap<_, _>>::default();
746 for (s, name) in [("{\"a\\nb\": 1 2}", "a\\nb"), ("{\"a\\rb\": 1 2}", "a\\rb"), ("{\"a\\\"b\": 1 2}", "a\\\"b")] {
747 match Dat::decode_string_located(s, &cfg) {
748 Ok(d) => panic!("{:?} should be refused, and became {}", s, d),
749 Err(at) => assert!(!at.error.msgs().join(" ").contains(name), "{:?}", s),
750 }
751 match Dat::decode_string(s) {
752 Ok(d) => panic!("{:?} should be refused, and became {}", s, d),
753 Err(e) => {
754 let msg = e.plain();
755 assert!(!msg.contains('\n') && !msg.contains('\r'), "{:?} refused with a break: {:?}", s, msg);
756 },
757 }
758 }
759}
760
761#[test]
762fn test_a_refusal_has_a_class_00() {
763 // A store scan counts refusals by class and prints no text; the label comes from the
764 // decoder's words, so a reworded refusal fails here and not silently in a count.
765 use oxedyne_fe2o3_jdat::string::dec::Located;
766 let cfg = DecoderConfig::<BTreeMap<_, _>, BTreeMap<_, _>>::default();
767 let class = |s: &str| match Dat::decode_string_located(s, &cfg) {
768 Ok(d) => format!("READ {}", d),
769 Err(at) => at.class(s).to_string(),
770 };
771 for (text, want) in [
772 // A comma-less comment, whichever mark and wherever it falls, and not one in a string.
773 ("[1 # c\n 2]", "no separator after a comment"),
774 ("[1 ! c ! 2]", "no separator after a comment"),
775 ("{\"a\":1 # c\n \"b\":2}", "no separator after a comment"),
776 ("[\"a # b\" 2]", "no separator"),
777 ("[1 2]", "no separator"),
778 ("{\"a\":1 \"b\":2}", "no separator"),
779 ("[1 # c\n, 2 3]", "no separator"),
780 // A whole-number kind with a fraction.
781 ("(u8|1.5)", "whole-number kind with a fraction"),
782 ("{\"n\": (i32|-2.5)}", "whole-number kind with a fraction"),
783 ("[,]", "comma follows nothing"),
784 ("{\"a\", 1}", "comma where a colon belongs"),
785 ("{:1}", "colon with no key"),
786 ("{\"a\"::1}", "second colon"),
787 ("{\"a\":,}", "key without value"),
788 ("{\"a\":1,\"a\":2}", "duplicate key"),
789 ("[1, 2", "unclosed"),
790 ("[1] x", "text after the value"),
791 ("[1|2]", "kind label"),
792 ] {
793 assert_eq!(class(text), want, "the class of {:?}", text);
794 assert!(Located::CLASSES.contains(&want), "{:?} is not a listed class", want);
795 }
796 // A text that reads has no class to give.
797 assert!(class("[1, # c\n 2]").starts_with("READ"));
798}
799
800
801// A refusal names the kind, the position and the class, never the value's text (U0-13 M2). The
802// messages reach a browser through `jdat_to_json` and an app's log, and a value in a refusal is a
803// value in a log. A key is no safer, a stored map is keyed by an email or a huser code (lead
804// ruling, U0-13). Four marks are planted: a word and a run of digits in the values, a word in the
805// keys, and a letter outside ASCII for the refusals that name one character.
806const WORD: &str = "Zq9xW";
807const DIGITS: &str = "7351846";
808const KEYW: &str = "Qv7kXp";
809const LETTER: char = 'Ж';
810
811// What a refusal says, in every form a caller can print.
812fn said_by(at: &oxedyne_fe2o3_jdat::string::dec::Located) -> String {
813 fmt!("{} | {} | {} | {:?}", at.error.plain(), at.error, at.error.msgs().join(" "), at.error)
814 .to_lowercase()
815}
816
817// Is a mark in the words? A mark of three characters or more is looked for in every window of
818// three, so that a fragment of the value fails as well as the whole.
819fn leaked(said: &str) -> Option<String> {
820 // The debug form of an error carries the decoder's own source lines, `dec.rs:1735`, which
821 // move with every edit and can hold any run of digits, so they are not the value's text.
822 let mut plain = String::new();
823 let mut rest = said;
824 while let Some(i) = rest.find(".rs:") {
825 plain.push_str(&rest[..i + 4]);
826 rest = rest[i + 4..].trim_start_matches(|c: char| c.is_ascii_digit());
827 }
828 plain.push_str(rest);
829 let said = plain.as_str();
830 for mark in [WORD, DIGITS, KEYW] {
831 let m: Vec<char> = mark.to_lowercase().chars().collect();
832 for w in m.windows(3) {
833 let w: String = w.iter().collect();
834 if said.contains(&w) {
835 return Some(fmt!("{:?} in the refusal: {}", w, said));
836 }
837 }
838 }
839 if said.contains(LETTER.to_lowercase().next().unwrap_or(LETTER)) {
840 return Some(fmt!("{:?} in the refusal: {}", LETTER, said));
841 }
842 None
843}
844
845fn node_cfg() -> DecoderConfig<BTreeMap<u16, oxedyne_fe2o3_jdat::usr::UsrKind>, BTreeMap<String, UsrKindId>> {
846 let mut uks = UsrKinds::new(BTreeMap::new(), BTreeMap::new());
847 uks.add(UsrKindId::new(1, Some("node"), None)).unwrap_or_default();
848 DecoderConfig::<_, _>::jdat(Some(uks))
849}
850
851#[test]
852fn test_no_refusal_holds_the_values_text_00() {
853 use oxedyne_fe2o3_jdat::string::dec::Located;
854 let cfg = node_cfg();
855 // One text for each class `Located::class` gives, and the several ways into the 'other'
856 // class, each with the marks where a value stands.
857 let by_class: Vec<(&str, Vec<String>)> = vec![
858 (Located::CLASSES[0], vec![
859 fmt!("(u8|{}.{})", DIGITS, DIGITS), fmt!("{{\"{}\": (i32|-{}.5)}}", KEYW, DIGITS),
860 fmt!("(u32|4294967295.{})", DIGITS),
861 ]),
862 (Located::CLASSES[1], vec![
863 fmt!("(u8|{}e0)", DIGITS), fmt!("(i8|{}.0)", DIGITS), fmt!("(u16|{}e1)", DIGITS),
864 fmt!("{{\"{}\": (u8|{}e0)}}", KEYW, DIGITS),
865 ]),
866 (Located::CLASSES[2], vec![fmt!("[{} # c\n {}]", WORD, WORD), fmt!("{{\"{}\": {} # c\n \"{}\": 1}}", KEYW, WORD, KEYW)]),
867 (Located::CLASSES[3], vec![
868 fmt!("[{} {}]", WORD, WORD), fmt!("[\"{}\" \"{}\"]", WORD, WORD),
869 fmt!("{{\"{}\": {} \"{}\": 1}}", KEYW, WORD, KEYW), fmt!("{} {}", WORD, DIGITS),
870 fmt!("(u8|1 {})", LETTER), fmt!("(node|{}{})", WORD, "{"),
871 ]),
872 (Located::CLASSES[4], vec![fmt!("[,{}]", WORD), fmt!("{{\"{}\": [,{}]}}", KEYW, WORD)]),
873 (Located::CLASSES[5], vec![
874 fmt!("{{\"k\": {}, \"{}\", {}}}", WORD, KEYW, WORD), fmt!("{{\"{}\", {}}}", KEYW, WORD),
875 ]),
876 (Located::CLASSES[6], vec![fmt!("{{:{}}}", WORD), fmt!("{{\"{}\": 1, :{}}}", KEYW, WORD)]),
877 (Located::CLASSES[7], vec![fmt!("{{\"k\":: {}}}", WORD), fmt!("{{\"{}\":: {}}}", KEYW, WORD)]),
878 (Located::CLASSES[8], vec![
879 fmt!("{{\"k\": {}, \"j\":,}}", WORD), fmt!("{{\"{}\": {}, \"{}2\":,}}", KEYW, WORD, KEYW),
880 fmt!("{{\"{}\": {}, \"{}2\":}}", KEYW, WORD, KEYW),
881 ]),
882 (Located::CLASSES[9], vec![
883 fmt!("{{\"k\": {}, \"k\": {}}}", WORD, WORD), fmt!("{{\"{}\": {}, \"{}\": {}}}", KEYW, WORD, KEYW, WORD),
884 fmt!("{{\"{}\": 1, ! {} ! \"{}\": 2}}", KEYW, WORD, KEYW),
885 ]),
886 (Located::CLASSES[10], vec![
887 fmt!("[{}, {}", WORD, DIGITS), fmt!("(u8|{}", DIGITS), fmt!("{{\"{}\": {}", KEYW, WORD),
888 ]),
889 (Located::CLASSES[11], vec![
890 fmt!("[{}] {}", WORD, WORD), fmt!("[{}] {}", WORD, DIGITS), fmt!("{{\"{}\": {}}} {}", KEYW, WORD, KEYW),
891 ]),
892 (Located::CLASSES[12], vec![
893 fmt!("[{}|{}]", WORD, WORD), fmt!("(u8 {}|1)", LETTER), fmt!("(u8 x{}|1)", WORD),
894 fmt!("({}|1)", LETTER), fmt!("({}|1)", WORD), fmt!("(str|[1, {}])", WORD),
895 fmt!("{{\"{}\": (zzz|{})}}", KEYW, WORD),
896 ]),
897 (Located::CLASSES[13], vec![
898 fmt!("(u8|-{})", DIGITS), fmt!("(u16|-{})", DIGITS), fmt!("(u32|-{})", DIGITS),
899 fmt!("(u64|-{})", DIGITS), fmt!("(u128|-{})", DIGITS), fmt!("(c64|-{})", DIGITS),
900 fmt!("(f32|{}e38)", DIGITS), fmt!("(f64|{}e999)", DIGITS), fmt!("(f32|3.{}e38)", DIGITS),
901 fmt!("{{\"{}\": (u8|-{})}}", KEYW, DIGITS),
902 ]),
903 (Located::CLASSES[15], vec![
904 // A number with no digit, or an exponent mark with no digit after it (M1).
905 fmt!("(u8|e{})", DIGITS), fmt!("(u8|{}e)", DIGITS), fmt!("(i32|-e{})", DIGITS),
906 fmt!("{{\"{}\": (u8|e{})}}", KEYW, DIGITS), fmt!("(adec|{}e-)", DIGITS),
907 // The rest.
908 fmt!("(u8|\"{}\")", WORD), fmt!("(u8|{})", WORD), fmt!("{{\"k\": [1, {}]}}", "(u8|\"Zq9xW\")"),
909 fmt!("(bu8|[1, \"{}\"])", WORD), fmt!("(f64|{})", WORD),
910 fmt!("(f64|e{})", DIGITS), fmt!("(i8|{}{})", DIGITS, WORD),
911 fmt!("(aint|{}.5)", DIGITS), fmt!("(aint|{}{})", DIGITS, WORD), fmt!("(adec|{}x)", DIGITS),
912 fmt!("(b32|\"{}\")", WORD), fmt!("(b2|\"{}\")", WORD),
913 fmt!("(u8|0x{})", WORD), fmt!("(u8|{}_{})", DIGITS, DIGITS), fmt!("(u8|+{})", WORD),
914 fmt!("\"a\\{}b\"", LETTER), fmt!("\"\\u{}\"", LETTER),
915 fmt!("\"\\uD800\\u{}\"", LETTER),
916 fmt!("(u8|{}){}", DIGITS, WORD),
917 ]),
918 ];
919 // Every text that is refused under another class than its own, listed together.
920 let mut astray: Vec<String> = Vec::new();
921 for (class, texts) in &by_class {
922 for text in texts {
923 match Dat::decode_string_located(text, &cfg) {
924 Ok(d) => astray.push(fmt!("{:?} is read as {}, not refused", text, d)),
925 Err(at) => if at.class(text) != *class {
926 astray.push(fmt!("{:?} is {:?}, not {:?}", text, at.class(text), class));
927 },
928 }
929 }
930 }
931 assert!(astray.is_empty(), "texts of another class than listed:\n{}", astray.join("\n"));
932 let mut n = 0;
933 for (class, texts) in &by_class {
934 for text in texts {
935 for (name, c) in [("jdat", &cfg)] {
936 let at = match Dat::decode_string_located(text, c) {
937 Ok(d) => panic!("{} {:?} should be refused, and read as {}", name, text, d),
938 Err(at) => at,
939 };
940 let said = said_by(&at);
941 assert_eq!(at.class(text), *class, "the class of {:?}: {}", text, said);
942 assert!(leaked(&said).is_none(), "{:?} is refused with: {}", text, leaked(&said).unwrap_or_default());
943 // A refusal still says where (rc condition): a line and a column, nonzero.
944 assert!(at.line >= 1 && at.col >= 1, "{:?} is refused at line {}, column {}", text, at.line, at.col);
945 let place = fmt!("line {}, column {}", at.line, at.col);
946 // The refusal `decode_string` hands a caller says the same, and says where.
947 match Dat::decode_string_with_config(text, c) {
948 Ok(_) => panic!("{:?} should be refused by decode_string", text),
949 Err(e) => {
950 let said = fmt!("{} | {} | {:?}", e.plain(), e, e).to_lowercase();
951 assert!(leaked(&said).is_none(), "{:?} is refused with: {}", text, leaked(&said).unwrap_or_default());
952 assert!(e.plain().contains(&place), "{:?}: the refusal should say {:?}, and reads: {}",
953 text, place, e.plain());
954 },
955 }
956 // As does the browser's reader, which refuses what the decoder does (the one
957 // `(node|..)` text is an unknown kind to it, and refused as well).
958 match Dat::jdat_to_json(text, &DecodeLimits::text(), true) {
959 Ok(j) => panic!("{:?} should be refused by jdat_to_json, and became {}", text, j),
960 Err(e) => {
961 let said = fmt!("{} | {} | {:?}", e.plain(), e, e).to_lowercase();
962 assert!(leaked(&said).is_none(), "{:?} is refused by jdat_to_json with: {}", text, leaked(&said).unwrap_or_default());
963 assert!(e.plain().contains("line ") && e.plain().contains("column "),
964 "{:?}: jdat_to_json should say where, and reads: {}", text, e.plain());
965 },
966 }
967 n += 1;
968 }
969 }
970 }
971 assert!(n >= 60, "only {} refusals were driven", n);
972}
973
974#[test]
975fn test_no_refusal_to_a_mutated_text_holds_the_values_text_00() {
976 // Truncations, deletions and insertions at every position of documents whose values hold the
977 // marks: every text that is refused is refused without them, under both configurations.
978 let ucfg = node_cfg();
979 let jcfg = DecoderConfig::<BTreeMap<u16, oxedyne_fe2o3_jdat::usr::UsrKind>, BTreeMap<String, UsrKindId>>::json(None);
980 let docs = [
981 fmt!("{{\"{k}\": (u8|{d}), \"j\": [{w}, \"{w}\", (f32|{d}e38)], \"m{k}\": (bu8|[1, \"{w}\"]), \"n\": (u16|-{d})}}", d = DIGITS, w = WORD, k = KEYW),
982 fmt!("[(node|\"{w}\"), (node|{d}), (node|{{\"a\": \"{w}\"}}), (t2|[{w}, {d}]), (box|(u8|{d})), (some|\"{w}\")]", d = DIGITS, w = WORD),
983 fmt!("{{\"a\": (adec|{d}.{d}e5), \"b\": (b32|\"{w}\"), \"c\": (c64|{d}), \"e\": (i8|-{d}.0), \"f\": \"x\\u00e9\\n{w}\"}}", d = DIGITS, w = WORD),
984 fmt!("{{\"{k}\": 1, \"{k}\": 2, \"{k}x\": [{w} {w}], {k}y: {d}, \"{k}z\"}}", d = DIGITS, w = WORD, k = KEYW),
985 fmt!("{{\"k\": {d}, \"j\": [\"{w}\", {w}, {d}.{d}, -{d}e-{d}, true, null], \"q\": \"{w}\"}}", d = DIGITS, w = WORD),
986 ];
987 let junk = ['(', ')', '|', '[', ']', '{', '}', ',', ':', '"', '\'', '#', '!', '\\', 'x', '-', '.', 'e', ' ', '\n', LETTER];
988 let mut refused = 0;
989 let mut bad = Vec::new();
990 let mut unplaced = Vec::new();
991 for doc in &docs {
992 let cs: Vec<char> = doc.chars().collect();
993 let mut texts: Vec<String> = Vec::new();
994 for i in 0..=cs.len() {
995 texts.push(cs[..i].iter().collect());
996 if i < cs.len() {
997 let mut d = cs.clone();
998 d.remove(i);
999 texts.push(d.iter().collect());
1000 }
1001 for j in junk {
1002 let mut d = cs.clone();
1003 d.insert(i, j);
1004 texts.push(d.iter().collect());
1005 }
1006 }
1007 for text in &texts {
1008 for c in [&ucfg] {
1009 if let Err(at) = Dat::decode_string_located(text, c) {
1010 refused += 1;
1011 let said = said_by(&at);
1012 if let Some(l) = leaked(&said) {
1013 bad.push(fmt!("{:?} -> {}", text, l));
1014 }
1015 if let Err(e) = Dat::decode_string_with_config(text, c) {
1016 if !e.plain().contains(&fmt!("line {}, column {}", at.line, at.col)) {
1017 unplaced.push(fmt!("{:?} -> {}", text, e.plain()));
1018 }
1019 }
1020 }
1021 }
1022 if let Err(at) = Dat::decode_string_located(text, &jcfg) {
1023 refused += 1;
1024 let said = said_by(&at);
1025 if let Some(l) = leaked(&said) {
1026 bad.push(fmt!("{:?} -> {}", text, l));
1027 }
1028 if let Err(e) = Dat::decode_string_with_config(text, &jcfg) {
1029 if !e.plain().contains(&fmt!("line {}, column {}", at.line, at.col)) {
1030 unplaced.push(fmt!("{:?} -> {}", text, e.plain()));
1031 }
1032 }
1033 }
1034 }
1035 }
1036 bad.sort_by_key(|b| b.len());
1037 for b in bad.iter().take(10) {
1038 println!("{}", b.chars().take(500).collect::<String>());
1039 }
1040 assert!(refused >= 3000, "only {} refusals were driven", refused);
1041 assert!(unplaced.is_empty(), "{} refusals do not say where, the first being {:?}", unplaced.len(), unplaced.first());
1042 assert!(bad.is_empty(), "{} refusals hold a value's text, the shortest being {}", bad.len(),
1043 bad.first().map(|b| b.chars().take(500).collect::<String>()).unwrap_or_default());
1044}