From 37643c26fd82d46b94b9276d2f6304f7d443db95 Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Fri, 12 Jan 2024 13:50:46 +0700 Subject: [PATCH 01/43] feat: example files --- hermes/crates/cbork/cddl-parser/src/lib.rs | 3 -- hermes/crates/cbork/cddl-parser/tests/cddl.rs | 23 +++++++++++ .../tests/correct_cddl/example_7071.cddl | 38 +++++++++++++++++++ .../correct_cddl/example_7071_compact.cddl | 17 +++++++++ 4 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 hermes/crates/cbork/cddl-parser/tests/cddl.rs create mode 100644 hermes/crates/cbork/cddl-parser/tests/correct_cddl/example_7071.cddl create mode 100644 hermes/crates/cbork/cddl-parser/tests/correct_cddl/example_7071_compact.cddl diff --git a/hermes/crates/cbork/cddl-parser/src/lib.rs b/hermes/crates/cbork/cddl-parser/src/lib.rs index 241283f85..acb522456 100644 --- a/hermes/crates/cbork/cddl-parser/src/lib.rs +++ b/hermes/crates/cbork/cddl-parser/src/lib.rs @@ -139,9 +139,6 @@ pub fn parse_cddl<'a>( }; result.map(Box::new).map_err(|e| { - println!("{e:?}"); - println!("{e}"); - Box::new(CDDLError::from(e)) }) } diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl.rs b/hermes/crates/cbork/cddl-parser/tests/cddl.rs new file mode 100644 index 000000000..d03d8076e --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/cddl.rs @@ -0,0 +1,23 @@ +use std::{fs, io::Result}; + +use cddl_parser::{parse_cddl, Extension}; + +#[test] +fn parse_cddl_files() -> Result<()> { + let entries = fs::read_dir("tests/correct_cddl")?; + + for entry in entries { + let file_path = entry?.path(); + + if !file_path.is_file() { + continue; + } + + let mut content = fs::read_to_string(&file_path)?; + + parse_cddl(&mut content, &Extension::RFC8610Parser) + .unwrap_or_else(|err| panic!("{file_path:?} {err}")); + } + + Ok(()) +} diff --git a/hermes/crates/cbork/cddl-parser/tests/correct_cddl/example_7071.cddl b/hermes/crates/cbork/cddl-parser/tests/correct_cddl/example_7071.cddl new file mode 100644 index 000000000..3da6a1b7f --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/correct_cddl/example_7071.cddl @@ -0,0 +1,38 @@ +reputation-object = { + reputation-context, + reputon-list +} + +reputation-context = ( + application: text +) + +reputon-list = ( + reputons: reputon-array +) + +reputon-array = [* reputon] + +reputon = { + rater-value, + assertion-value, + rated-value, + rating-value, + ? conf-value, + ? normal-value, + ? sample-value, + ? gen-value, + ? expire-value, + * ext-value, +} + +rater-value = ( rater: text ) +assertion-value = ( assertion: text ) +rated-value = ( rated: text ) +rating-value = ( rating: float16 ) +conf-value = ( confidence: float16 ) +normal-value = ( normal-rating: float16 ) +sample-value = ( sample-size: uint ) +gen-value = ( generated: uint ) +expire-value = ( expires: uint ) +ext-value = ( text => any ) diff --git a/hermes/crates/cbork/cddl-parser/tests/correct_cddl/example_7071_compact.cddl b/hermes/crates/cbork/cddl-parser/tests/correct_cddl/example_7071_compact.cddl new file mode 100644 index 000000000..856c517df --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/correct_cddl/example_7071_compact.cddl @@ -0,0 +1,17 @@ +reputation-object = { + application: text + reputons: [* reputon] +} + +reputon = { + rater: text + assertion: text + rated: text + rating: float16 + ? confidence: float16 + ? normal-rating: float16 + ? sample-size: uint + ? generated: uint + ? expires: uint + * text => any +} \ No newline at end of file From 217c16fddbfc51166958b1bd333d229ccba0cc6c Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Mon, 15 Jan 2024 16:24:20 +0700 Subject: [PATCH 02/43] refactor: rename test files --- hermes/crates/cbork/cddl-parser/tests/cddl.rs | 2 +- .../valid_rfc8610_example_7071.cddl} | 0 .../valid_rfc8610_example_7071_compact.cddl} | 0 .../valid_rfc8610_example_array_object.cddl | 10 +++++++++ .../valid_rfc8610_example_nested_object.cddl | 22 +++++++++++++++++++ ...rfc8610_example_nested_object_compact.cddl | 12 ++++++++++ 6 files changed, 45 insertions(+), 1 deletion(-) rename hermes/crates/cbork/cddl-parser/tests/{correct_cddl/example_7071.cddl => cddl/valid_rfc8610_example_7071.cddl} (100%) rename hermes/crates/cbork/cddl-parser/tests/{correct_cddl/example_7071_compact.cddl => cddl/valid_rfc8610_example_7071_compact.cddl} (100%) create mode 100644 hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_example_array_object.cddl create mode 100644 hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_example_nested_object.cddl create mode 100644 hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_example_nested_object_compact.cddl diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl.rs b/hermes/crates/cbork/cddl-parser/tests/cddl.rs index d03d8076e..f54a9481f 100644 --- a/hermes/crates/cbork/cddl-parser/tests/cddl.rs +++ b/hermes/crates/cbork/cddl-parser/tests/cddl.rs @@ -4,7 +4,7 @@ use cddl_parser::{parse_cddl, Extension}; #[test] fn parse_cddl_files() -> Result<()> { - let entries = fs::read_dir("tests/correct_cddl")?; + let entries = fs::read_dir("tests/cddl")?; for entry in entries { let file_path = entry?.path(); diff --git a/hermes/crates/cbork/cddl-parser/tests/correct_cddl/example_7071.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_example_7071.cddl similarity index 100% rename from hermes/crates/cbork/cddl-parser/tests/correct_cddl/example_7071.cddl rename to hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_example_7071.cddl diff --git a/hermes/crates/cbork/cddl-parser/tests/correct_cddl/example_7071_compact.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_example_7071_compact.cddl similarity index 100% rename from hermes/crates/cbork/cddl-parser/tests/correct_cddl/example_7071_compact.cddl rename to hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_example_7071_compact.cddl diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_example_array_object.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_example_array_object.cddl new file mode 100644 index 000000000..0590259b7 --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_example_array_object.cddl @@ -0,0 +1,10 @@ +root = [2*2 { + precision: text, + Latitude: float, + Longitude: float, + Address: text, + City: text, + State: text, + Zip: text, + Country: text +}] \ No newline at end of file diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_example_nested_object.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_example_nested_object.cddl new file mode 100644 index 000000000..6f36f7942 --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_example_nested_object.cddl @@ -0,0 +1,22 @@ +root = { image } + +image = ( + Image: { + size, + Title: text, + thumbnail, + IDs: [* int] + } +) + +size = ( + Width: 0..1280 + Height: 0..1024 +) + +thumbnail = ( + Thumbnail: { + size, + Url: ~uri + } +) \ No newline at end of file diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_example_nested_object_compact.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_example_nested_object_compact.cddl new file mode 100644 index 000000000..1b00b80bb --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_example_nested_object_compact.cddl @@ -0,0 +1,12 @@ +root = { + Image: { + size, Title: text, + Thumbnail: { size, Url: ~uri }, + IDs: [* int] + } +} + +size = ( + Width: 0..1280, + Height: 0..1024, +) \ No newline at end of file From 40bda27f6d823489b7bfdf433a348dca2cee9b56 Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Mon, 15 Jan 2024 16:26:39 +0700 Subject: [PATCH 03/43] refactor: rename from rfc9615 to rfc9165 --- .../grammar/{rfc_9615.pest => rfc_9165.pest} | 0 hermes/crates/cbork/cddl-parser/src/lib.rs | 28 +++++++++---------- 2 files changed, 14 insertions(+), 14 deletions(-) rename hermes/crates/cbork/cddl-parser/src/grammar/{rfc_9615.pest => rfc_9165.pest} (100%) diff --git a/hermes/crates/cbork/cddl-parser/src/grammar/rfc_9615.pest b/hermes/crates/cbork/cddl-parser/src/grammar/rfc_9165.pest similarity index 100% rename from hermes/crates/cbork/cddl-parser/src/grammar/rfc_9615.pest rename to hermes/crates/cbork/cddl-parser/src/grammar/rfc_9165.pest diff --git a/hermes/crates/cbork/cddl-parser/src/lib.rs b/hermes/crates/cbork/cddl-parser/src/lib.rs index acb522456..6ff3c2184 100644 --- a/hermes/crates/cbork/cddl-parser/src/lib.rs +++ b/hermes/crates/cbork/cddl-parser/src/lib.rs @@ -20,12 +20,12 @@ pub mod rfc_8610 { pub struct RFC8610Parser; } -pub mod rfc_9615 { +pub mod rfc_9165 { pub use pest::Parser; #[derive(pest_derive::Parser)] #[grammar = "grammar/rfc_8610.pest"] - #[grammar = "grammar/rfc_9615.pest"] + #[grammar = "grammar/rfc_9165.pest"] pub struct RFC8610Parser; } @@ -34,7 +34,7 @@ pub mod cddl { #[derive(pest_derive::Parser)] #[grammar = "grammar/rfc_8610.pest"] - #[grammar = "grammar/rfc_9615.pest"] + #[grammar = "grammar/rfc_9165.pest"] #[grammar = "grammar/cddl_modules.pest"] pub struct RFC8610Parser; } @@ -45,7 +45,7 @@ pub mod cddl_test { // Parser with DEBUG rules. These rules are only used in tests. #[derive(pest_derive::Parser)] #[grammar = "grammar/rfc_8610.pest"] - #[grammar = "grammar/rfc_9615.pest"] + #[grammar = "grammar/rfc_9165.pest"] #[grammar = "grammar/cddl_modules.pest"] #[grammar = "grammar/cddl_test.pest"] // Ideally this would only be used in tests. pub struct CDDLTestParser; @@ -55,9 +55,9 @@ pub mod cddl_test { pub enum Extension { /// RFC8610 ONLY limited parser. RFC8610Parser, - /// RFC8610 and RFC9615 limited parser. - RFC9615Parser, - /// RFC8610, RFC9615, and CDDL modules. + /// RFC8610 and RFC9165 limited parser. + RFC9165Parser, + /// RFC8610, RFC9165, and CDDL modules. CDDLParser, } @@ -68,7 +68,7 @@ pub const POSTLUDE: &str = include_str!("grammar/postlude.cddl"); #[derive(Debug)] pub enum AST<'a> { RFC8610(Pairs<'a, rfc_8610::Rule>), - RFC9615(Pairs<'a, rfc_9615::Rule>), + RFC9165(Pairs<'a, rfc_9165::Rule>), CDDL(Pairs<'a, cddl::Rule>), } @@ -77,8 +77,8 @@ pub enum AST<'a> { pub enum CDDLErrorType { /// An error related to RFC 8610 extension. RFC8610(Error), - /// An error related to RFC 9615 extension. - RFC9615(Error), + /// An error related to RFC 9165 extension. + RFC9165(Error), /// An error related to CDDL modules extension. CDDL(Error), } @@ -126,10 +126,10 @@ pub fn parse_cddl<'a>( .map(AST::RFC8610) .map_err(CDDLErrorType::RFC8610) }, - Extension::RFC9615Parser => { - rfc_9615::RFC8610Parser::parse(rfc_9615::Rule::cddl, input) - .map(AST::RFC9615) - .map_err(CDDLErrorType::RFC9615) + Extension::RFC9165Parser => { + rfc_9165::RFC8610Parser::parse(rfc_9165::Rule::cddl, input) + .map(AST::RFC9165) + .map_err(CDDLErrorType::RFC9165) }, Extension::CDDLParser => { cddl::RFC8610Parser::parse(cddl::Rule::cddl, input) From 45dfa8f57cd372fa76b200284e43205bbe800d3f Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Mon, 15 Jan 2024 16:53:22 +0700 Subject: [PATCH 04/43] feat: error display for testing multiple files --- hermes/crates/cbork/cddl-parser/tests/cddl.rs | 12 ++++++++++-- .../tests/cddl/valid_rfc8610_simple_1.cddl | 7 +++++++ .../tests/cddl/valid_rfc8610_simple_2.cddl | 7 +++++++ .../tests/cddl/valid_rfc8610_simple_3.cddl | 11 +++++++++++ .../tests/cddl/valid_rfc8610_simple_4.cddl | 7 +++++++ .../tests/cddl/valid_rfc8610_simple_5.cddl | 13 +++++++++++++ .../tests/cddl/valid_rfc8610_simple_6.cddl | 16 ++++++++++++++++ 7 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_1.cddl create mode 100644 hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_2.cddl create mode 100644 hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_3.cddl create mode 100644 hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_4.cddl create mode 100644 hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_5.cddl create mode 100644 hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_6.cddl diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl.rs b/hermes/crates/cbork/cddl-parser/tests/cddl.rs index f54a9481f..096406327 100644 --- a/hermes/crates/cbork/cddl-parser/tests/cddl.rs +++ b/hermes/crates/cbork/cddl-parser/tests/cddl.rs @@ -6,6 +6,8 @@ use cddl_parser::{parse_cddl, Extension}; fn parse_cddl_files() -> Result<()> { let entries = fs::read_dir("tests/cddl")?; + let mut error_results = vec![]; + for entry in entries { let file_path = entry?.path(); @@ -15,8 +17,14 @@ fn parse_cddl_files() -> Result<()> { let mut content = fs::read_to_string(&file_path)?; - parse_cddl(&mut content, &Extension::RFC8610Parser) - .unwrap_or_else(|err| panic!("{file_path:?} {err}")); + if let Err(e) = parse_cddl(&mut content, &Extension::RFC8610Parser) { + error_results.push(format!("{}) {file_path:?} {e}", error_results.len() + 1)); + } + } + + let err_msg = error_results.join("\n\n"); + if !err_msg.is_empty() { + panic!("{err_msg}") } Ok(()) diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_1.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_1.cddl new file mode 100644 index 000000000..4ac9fbdf3 --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_1.cddl @@ -0,0 +1,7 @@ +; Figure 1: Using a Group Directly in a Map + +person = { + age: int, + name: tstr, + employer: tstr, +} \ No newline at end of file diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_2.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_2.cddl new file mode 100644 index 000000000..899f96bb1 --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_2.cddl @@ -0,0 +1,7 @@ +; Figure 2: A Basic Group + +pii = ( + age: int, + name: tstr, + employer: tstr, +) \ No newline at end of file diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_3.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_3.cddl new file mode 100644 index 000000000..7e2bfb0ec --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_3.cddl @@ -0,0 +1,11 @@ +; Figure 3: Using a Group by Name + +person = { + pii +} + +pii = ( + age: int, + name: tstr, + employer: tstr, +) \ No newline at end of file diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_4.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_4.cddl new file mode 100644 index 000000000..2934dcbcb --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_4.cddl @@ -0,0 +1,7 @@ +; Figure 4: Using a Parenthesized Group in a Map + +person = {( + age: int, + name: tstr, + employer: tstr, +)} \ No newline at end of file diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_5.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_5.cddl new file mode 100644 index 000000000..8a3253026 --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_5.cddl @@ -0,0 +1,13 @@ +; Figure 5: Maps with Copy/Paste + +person = { + age: int, + name: tstr, + employer: tstr, +} + +dog = { + age: int, + name: tstr, + leash-length: float, +} \ No newline at end of file diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_6.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_6.cddl new file mode 100644 index 000000000..307d13510 --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_6.cddl @@ -0,0 +1,16 @@ +; Figure 6: Using a Group for Factorization + +person = { + identity, + employer: tstr, +} + +dog = { + identity, + leash-length: float, +} + +identity = ( + age: int, + name: tstr, +) \ No newline at end of file From 19aa070202c0e61808320f897a67356fdd438ef0 Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Mon, 15 Jan 2024 22:31:02 +0700 Subject: [PATCH 05/43] feat: sort file reading --- hermes/crates/cbork/cddl-parser/tests/cddl.rs | 18 +++++++++--------- .../tests/cddl/valid_rfc8610_simple_7.cddl | 15 +++++++++++++++ 2 files changed, 24 insertions(+), 9 deletions(-) create mode 100644 hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_7.cddl diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl.rs b/hermes/crates/cbork/cddl-parser/tests/cddl.rs index 096406327..ac13c10b7 100644 --- a/hermes/crates/cbork/cddl-parser/tests/cddl.rs +++ b/hermes/crates/cbork/cddl-parser/tests/cddl.rs @@ -6,23 +6,23 @@ use cddl_parser::{parse_cddl, Extension}; fn parse_cddl_files() -> Result<()> { let entries = fs::read_dir("tests/cddl")?; - let mut error_results = vec![]; + let mut file_paths: Vec<_> = entries + .filter_map(Result::ok) + .filter_map(|x| x.path().is_file().then_some(x.path())) + .collect(); - for entry in entries { - let file_path = entry?.path(); - - if !file_path.is_file() { - continue; - } + file_paths.sort(); + let mut err_messages = vec![]; + for file_path in file_paths { let mut content = fs::read_to_string(&file_path)?; if let Err(e) = parse_cddl(&mut content, &Extension::RFC8610Parser) { - error_results.push(format!("{}) {file_path:?} {e}", error_results.len() + 1)); + err_messages.push(format!("{}) {file_path:?} {e}", err_messages.len() + 1)); } } - let err_msg = error_results.join("\n\n"); + let err_msg = err_messages.join("\n\n"); if !err_msg.is_empty() { panic!("{err_msg}") } diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_7.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_7.cddl new file mode 100644 index 000000000..6e7568153 --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_7.cddl @@ -0,0 +1,15 @@ +; 2.2.2. Choices + +attire = "bow tie" / "necktie" / "Internet attire" +protocol = 6 / 17 + +address = { delivery } + +delivery = ( +street: tstr, ? number: uint, city // +po-box: uint, city // +per-pickup: true ) + +city = ( +name: tstr, zip-code: uint +) \ No newline at end of file From b94d9696dcfa0d9d4dc6d71a0db91243fdfa07bd Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Tue, 16 Jan 2024 12:37:48 +0700 Subject: [PATCH 06/43] fix: whitespace problem for ',', braces, and // --- .../crates/cbork/cddl-parser/src/grammar/rfc_8610.pest | 10 +++++----- .../cddl-parser/tests/cddl/valid_rfc8610_simple_8.cddl | 7 +++++++ .../cddl-parser/tests/cddl/valid_rfc8610_simple_9.cddl | 7 +++++++ 3 files changed, 19 insertions(+), 5 deletions(-) create mode 100644 hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_8.cddl create mode 100644 hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_9.cddl diff --git a/hermes/crates/cbork/cddl-parser/src/grammar/rfc_8610.pest b/hermes/crates/cbork/cddl-parser/src/grammar/rfc_8610.pest index 0c3f32926..bad173558 100644 --- a/hermes/crates/cbork/cddl-parser/src/grammar/rfc_8610.pest +++ b/hermes/crates/cbork/cddl-parser/src/grammar/rfc_8610.pest @@ -38,9 +38,9 @@ tag_generic = ${ "#" ~ ASCII_DIGIT ~ ("." ~ uint)? } type2 = { value | typename_arg - | ( "(" ~ type ~ ")" ) - | ( "{" ~ group ~ "}" ) - | ( "[" ~ group ~ "]" ) + | ( "(" ~ S ~ type ~ ")" ) + | ( "{" ~ S ~ group ~ "}" ) + | ( "[" ~ S ~ group ~ "]" ) | ( "~" ~ typename_arg ) | ( "&" ~ "(" ~ group ~ ")" ) | ( "&" ~ groupname_arg ) @@ -52,9 +52,9 @@ type2 = { rangeop = { "..." | ".." } ctlop = ${ "." ~ id } -group = { grpchoice ~ ( S ~ "//" ~ grpchoice)* } +group = { grpchoice ~ ( S ~ "//" ~ S ~ grpchoice)* } -grpchoice = { ( grpent ~ ","? )* } +grpchoice = { ( grpent ~ ","? ~ S )* } grpent = ${ ( (occur ~ S)? ~ (memberkey ~ S)? ~ type ) diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_8.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_8.cddl new file mode 100644 index 000000000..b7999d21b --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_8.cddl @@ -0,0 +1,7 @@ +; 2.2.2. Choices + +attire /= "swimwear" + +delivery //= ( +lat: float, long: float, drone-type: tstr +) \ No newline at end of file diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_9.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_9.cddl new file mode 100644 index 000000000..a01075770 --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_9.cddl @@ -0,0 +1,7 @@ +; 2.2.2.1. Ranges + +device-address = byte +max-byte = 255 +byte = 0..max-byte ; inclusive range +first-non-byte = 256 +byte1 = 0...first-non-byte ; byte1 is equivalent to byte \ No newline at end of file From b0607d3b2d36009b79108816c4aaa8df3c45fa73 Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Tue, 16 Jan 2024 17:55:41 +0700 Subject: [PATCH 07/43] feat: group elements initial unit test --- .../cddl-parser/src/grammar/cddl_test.pest | 42 ++-- .../cddl-parser/src/grammar/rfc_8610.pest | 46 +++-- hermes/crates/cbork/cddl-parser/tests/cddl.rs | 1 + .../tests/cddl/valid_rfc8610_arrays.cddl | 9 + .../cddl/valid_rfc8610_cuts_in_maps.cddl | 6 + .../valid_rfc8610_cuts_in_maps_alt_1.cddl | 6 + .../valid_rfc8610_cuts_in_maps_alt_2.cddl | 6 + .../valid_rfc8610_cuts_in_maps_alt_3.cddl | 6 + .../cddl/valid_rfc8610_group_to_choice.cddl | 11 ++ ...valid_rfc8610_non_deterministic_order.cddl | 13 ++ .../tests/cddl/valid_rfc8610_occurence.cddl | 7 + .../tests/cddl/valid_rfc8610_ranges.cddl | 16 ++ .../valid_rfc8610_represenation_types.cddl | 10 + .../tests/cddl/valid_rfc8610_simple_9.cddl | 12 +- .../tests/cddl/valid_rfc8610_structs.cddl | 30 +++ .../tests/cddl/valid_rfc8610_structs_alt.cddl | 16 ++ .../tests/cddl/valid_rfc8610_tables.cddl | 8 + .../cbork/cddl-parser/tests/group_elements.rs | 186 ++++++++++++++++++ .../cbork/cddl-parser/tests/identifiers.rs | 75 +++---- .../cbork/cddl-parser/tests/text_sequences.rs | 17 +- 20 files changed, 445 insertions(+), 78 deletions(-) create mode 100644 hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_arrays.cddl create mode 100644 hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_cuts_in_maps.cddl create mode 100644 hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_cuts_in_maps_alt_1.cddl create mode 100644 hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_cuts_in_maps_alt_2.cddl create mode 100644 hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_cuts_in_maps_alt_3.cddl create mode 100644 hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_group_to_choice.cddl create mode 100644 hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_non_deterministic_order.cddl create mode 100644 hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_occurence.cddl create mode 100644 hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_ranges.cddl create mode 100644 hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_represenation_types.cddl create mode 100644 hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_structs.cddl create mode 100644 hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_structs_alt.cddl create mode 100644 hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_tables.cddl create mode 100644 hermes/crates/cbork/cddl-parser/tests/group_elements.rs diff --git a/hermes/crates/cbork/cddl-parser/src/grammar/cddl_test.pest b/hermes/crates/cbork/cddl-parser/src/grammar/cddl_test.pest index 41fd784fd..1251525c8 100644 --- a/hermes/crates/cbork/cddl-parser/src/grammar/cddl_test.pest +++ b/hermes/crates/cbork/cddl-parser/src/grammar/cddl_test.pest @@ -5,38 +5,56 @@ // cspell: words intfloat hexfloat -/// Test Expression for the S Rule. +/// Test Expression for the `group` Rule. +group_TEST = ${ SOI ~ group ~ EOI } + +/// Test Expression for the `grpchoice` Rule. +grpchoice_TEST = ${ SOI ~ grpchoice ~ EOI } + +/// Test Expression for the `grpent` Rule. +grpent_TEST = ${ SOI ~ grpent ~ EOI } + +/// Test Expression for the `memberkey` Rule. +memberkey_TEST = ${ SOI ~ memberkey ~ EOI } + +/// Test Expression for the `bareword` Rule. +bareword_TEST = ${ SOI ~ bareword ~ EOI } + +/// Test Expression for the `occur` Rule. +occur_TEST = ${ SOI ~ occur ~ EOI } + +/// Test Expression for the `S` Rule. S_TEST = ${ SOI ~ S ~ EOI } -/// Test Expression for the COMMENT Rule. +/// Test Expression for the `COMMENT` Rule. COMMENT_TEST = { SOI ~ COMMENT* ~ EOI } -/// Test expression for the URL_BASE64 Rule. +/// Test expression for the `URL_BASE64` Rule. URL_BASE64_TEST = { SOI ~ URL_BASE64 ~ EOI } -/// Test expression to the id Rule. +/// Test expression to the `id` Rule. id_TEST = ${ SOI ~ id ~ EOI} -/// Test expression to the bytes Rule. +/// Test expression to the `bytes` Rule. bytes_TEST = ${ SOI ~ bytes ~ EOI} -/// Test expression to the text Rule. +/// Test expression to the `text` Rule. text_TEST = ${ SOI ~ text ~ EOI} -/// Test expression to the uint Rule. +/// Test expression to the `uint` Rule. uint_TEST = ${ SOI ~ uint ~ EOI} -/// Test expression to the int Rule. +/// Test expression to the `int` Rule. int_TEST = ${ SOI ~ int ~ EOI} -/// Test expression to the intfloat Rule. +/// Test expression to the `intfloat` Rule. intfloat_TEST = ${ SOI ~ intfloat ~ EOI} -/// Test expression to the hexfloat Rule. +/// Test expression to the `hexfloat` Rule. hexfloat_TEST = ${ SOI ~ hexfloat ~ EOI} -/// Test expression to the number Rule. +/// Test expression to the `number` Rule. number_TEST = ${ SOI ~ number ~ EOI} -/// Test expression to the value Rule. +/// Test expression to the `value` Rule. value_TEST = ${ SOI ~ value ~ EOI} \ No newline at end of file diff --git a/hermes/crates/cbork/cddl-parser/src/grammar/rfc_8610.pest b/hermes/crates/cbork/cddl-parser/src/grammar/rfc_8610.pest index bad173558..ffa1bff7c 100644 --- a/hermes/crates/cbork/cddl-parser/src/grammar/rfc_8610.pest +++ b/hermes/crates/cbork/cddl-parser/src/grammar/rfc_8610.pest @@ -11,9 +11,12 @@ cddl = { ~ EOI } + +// ----------------------------------------------------------------------------- +// Rules rule = { - ( typename ~ assignt ~ type) - | ( groupname ~ assigng ~ grpent) + ( typename ~ assignt ~ type ) + | ( groupname ~ assigng ~ grpent ) } typename = ${ id ~ genericparm? } @@ -23,11 +26,14 @@ assignt = { "=" | "/=" } assigng = { "=" | "//=" } genericparm = { "<" ~ id ~ ( "," ~ id )* ~ ">" } -genericarg = { "<" ~ type1 ~ ( "," ~ type1)* ~ ">" } +genericarg = { "<" ~ type1 ~ ( "," ~ type1 )* ~ ">" } -type = { type1 ~ ( S ~ "/" ~ type1)* } -type1 = { type2 ~ ( S ~ ( rangeop | ctlop ) ~ type2)? } +// ----------------------------------------------------------------------------- +// Type Declaration +type = { type1 ~ ( S ~ "/" ~ type1 )* } + +type1 = { type2 ~ ( S ~ ( rangeop | ctlop ) ~ type2 )? } typename_arg = ${ typename ~ genericarg? } groupname_arg = ${ groupname ~ genericarg? } @@ -41,9 +47,9 @@ type2 = { | ( "(" ~ S ~ type ~ ")" ) | ( "{" ~ S ~ group ~ "}" ) | ( "[" ~ S ~ group ~ "]" ) - | ( "~" ~ typename_arg ) - | ( "&" ~ "(" ~ group ~ ")" ) - | ( "&" ~ groupname_arg ) + | ( "~" ~ S ~ typename_arg ) + | ( "&" ~ S ~ "(" ~ S ~ group ~ ")" ) + | ( "&" ~ S ~ groupname_arg ) | tag6 | tag_generic | "#" @@ -52,24 +58,31 @@ type2 = { rangeop = { "..." | ".." } ctlop = ${ "." ~ id } -group = { grpchoice ~ ( S ~ "//" ~ S ~ grpchoice)* } +// ----------------------------------------------------------------------------- +// Group Elements +group = { grpchoice ~ S ~ ( "//" ~ S ~ grpchoice ~ S )* } + +/// Group choice - a collection of group entries in various forms grpchoice = { ( grpent ~ ","? ~ S )* } +/// Group entry - a single element in a group grpent = ${ ( (occur ~ S)? ~ (memberkey ~ S)? ~ type ) | ( (occur ~ S)? ~ groupname ~ genericarg? ) | ( (occur ~ S)? ~ "(" ~ S ~ group ~ S ~ ")" ) } +/// Member key in a struct or an array memberkey = { ( type1 ~ "^"? ~ "=>" ) - | ( bareword ~ ":" ) - | ( value ~ ":" ) + | ( (bareword | value) ~ ":" ) } +/// Bareword - a member key that is not used as a value like "foo", 1, etc. bareword = { id } +/// Occurence indicator occur = { ( uint? ~ "*" ~ uint? ) | "+" @@ -82,7 +95,7 @@ occur = { /// All Literal Values value = { number | text | bytes } -/// Literal Numbers - A float if it has fraction or exponent; int otherwise +/// Literal Numbers - A float if it has fraction or exponent; int otherwise number = { hexfloat | intfloat } /// Hex floats of the form -0x123.abc0p+12 @@ -100,7 +113,6 @@ exponent = ${ ("+" | "-") ~ ASCII_DIGIT+ } /// All integers, singed and unsigned int = ${ "-"? ~ uint } - /// Unsigned Integers uint = ${ ( ASCII_NONZERO_DIGIT ~ ASCII_DIGIT* ) @@ -121,7 +133,7 @@ bytes_text = ${ "'" ~ BCHAR* ~ "'" } // ----------------------------------------------------------------------------- // Simple multiple character sequences -/// identifier, called the `name` in the CDDL spec. +/// identifier, called the `name` in the CDDL spec. id = ${ group_socket | type_socket | @@ -151,7 +163,7 @@ WHITESPACE = _{ " " | "\t" | NEWLINE } COMMENT = _{ ";" ~ (PCHAR | "\t")* ~ NEWLINE } // URL Base64 Characterset. -URL_BASE64_ALPHA = _{ ASCII_ALPHA | ASCII_DIGIT | "-" | "_" } +URL_BASE64_ALPHA = _{ ASCII_ALPHA | ASCII_DIGIT | "-" | "_" } // Optional Padding that goes at the end of Base64. URL_BASE64_PAD = _{ "~" } @@ -159,8 +171,8 @@ URL_BASE64_PAD = _{ "~" } /// A name can start with an alphabetic character (including "@", "_", "$") /// The body of the name can consist of any of the characters from the set -/// {"A" to "Z", "a" to "z", "0" to "9", "_", "-", "@", ".", "$"} -// NAME_BODY = _{ NAME_END | "-" | "." } -- Unused Rule +/// {"A" to "Z", "a" to "z", "0" to "9", "_", "-", "@", ".", "$"} +// NAME_BODY = _{ NAME_END | "-" | "." } -- Unused Rule NAME_START = _{ ASCII_ALPHA | "@" | "_" | "$" } /// A name can end with an alphabetic character (including "@", "_", "$") or a digit. NAME_END = _{ NAME_START | ASCII_DIGIT } diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl.rs b/hermes/crates/cbork/cddl-parser/tests/cddl.rs index ac13c10b7..052fc676d 100644 --- a/hermes/crates/cbork/cddl-parser/tests/cddl.rs +++ b/hermes/crates/cbork/cddl-parser/tests/cddl.rs @@ -3,6 +3,7 @@ use std::{fs, io::Result}; use cddl_parser::{parse_cddl, Extension}; #[test] +#[ignore] fn parse_cddl_files() -> Result<()> { let entries = fs::read_dir("tests/cddl")?; diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_arrays.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_arrays.cddl new file mode 100644 index 000000000..2091e4d4a --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_arrays.cddl @@ -0,0 +1,9 @@ +; 3.4. Arrays + +unlimited-people = [* person] +one-or-two-people = [1*2 person] +at-least-two-people = [2* person] +person = ( + name: tstr, + age: uint, +) \ No newline at end of file diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_cuts_in_maps.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_cuts_in_maps.cddl new file mode 100644 index 000000000..d83ae9a00 --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_cuts_in_maps.cddl @@ -0,0 +1,6 @@ +; 3.5.4. Cuts in Maps + +extensible-map-example = { + ? "optional-key" => int, + * tstr => any +} \ No newline at end of file diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_cuts_in_maps_alt_1.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_cuts_in_maps_alt_1.cddl new file mode 100644 index 000000000..c23b012ff --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_cuts_in_maps_alt_1.cddl @@ -0,0 +1,6 @@ +; 3.5.4. Cuts in Maps + +extensible-map-example = { + ? "optional-key" ^ => int, + * tstr => any +} \ No newline at end of file diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_cuts_in_maps_alt_2.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_cuts_in_maps_alt_2.cddl new file mode 100644 index 000000000..8f077be00 --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_cuts_in_maps_alt_2.cddl @@ -0,0 +1,6 @@ +; 3.5.4. Cuts in Maps + +extensible-map-example = { + ? "optional-key": int, + * tstr => any +} \ No newline at end of file diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_cuts_in_maps_alt_3.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_cuts_in_maps_alt_3.cddl new file mode 100644 index 000000000..d8fdb61d3 --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_cuts_in_maps_alt_3.cddl @@ -0,0 +1,6 @@ +; 3.5.4. Cuts in Maps + +extensible-map-example = { + ? optional-key: int, + * tstr => any +} \ No newline at end of file diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_group_to_choice.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_group_to_choice.cddl new file mode 100644 index 000000000..3c3c066ee --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_group_to_choice.cddl @@ -0,0 +1,11 @@ +; 2.2.2.2. Turning a Group into a Choice + +terminal-color = &basecolors +basecolors = ( + black: 0, red: 1, green: 2, yellow: 3, + blue: 4, magenta: 5, cyan: 6, white: 7, +) +extended-color = &( + basecolors, + orange: 8, pink: 9, purple: 10, brown: 11, +) \ No newline at end of file diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_non_deterministic_order.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_non_deterministic_order.cddl new file mode 100644 index 000000000..9c8738ba3 --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_non_deterministic_order.cddl @@ -0,0 +1,13 @@ +; 3.5.3. Non-deterministic Order + +labeled-values = { + ? fritz: number, + * label => value +} +label = text +value = number + +do-not-do-this = { + int => int, + int => 6, +} \ No newline at end of file diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_occurence.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_occurence.cddl new file mode 100644 index 000000000..10327de08 --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_occurence.cddl @@ -0,0 +1,7 @@ +; 3.2. Occurrence + +apartment = { + kitchen: size, + * bedroom: size, +} +size = float ; in m2 \ No newline at end of file diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_ranges.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_ranges.cddl new file mode 100644 index 000000000..f7190a325 --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_ranges.cddl @@ -0,0 +1,16 @@ +; 2.2.2.1. Ranges + +device-address = byte +max-byte = 255 +byte = 0..max-byte ; inclusive range +first-non-byte = 256 +byte1 = 0...first-non-byte ; byte1 is equivalent to byte + +int-range = 0..10 ; only integers match +float-range = 0.0..10.0 ; only floats match +BAD-range1 = 0..10.0 ; NOT DEFINED +BAD-range2 = 0.0..10 ; NOT DEFINED +numeric-range = int-range / float-range + +non-range-expression = min..max +range-expression = min .. max \ No newline at end of file diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_represenation_types.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_represenation_types.cddl new file mode 100644 index 000000000..3ef42d9be --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_represenation_types.cddl @@ -0,0 +1,10 @@ +; 2.2.3. Representation Types + +my_breakfast = #6.55799(breakfast) ; cbor-any is too general! +breakfast = cereal / porridge +cereal = #6.998(tstr) +porridge = #6.999([liquid, solid]) +liquid = milk / water +milk = 0 +water = 1 +solid = tstr \ No newline at end of file diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_9.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_9.cddl index a01075770..832caf170 100644 --- a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_9.cddl +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_9.cddl @@ -1,7 +1,7 @@ -; 2.2.2.1. Ranges +; This is a comment +person = { g } -device-address = byte -max-byte = 255 -byte = 0..max-byte ; inclusive range -first-non-byte = 256 -byte1 = 0...first-non-byte ; byte1 is equivalent to byte \ No newline at end of file +g = ( + "name": tstr, + age: int, ; "age" is a bareword +) \ No newline at end of file diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_structs.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_structs.cddl new file mode 100644 index 000000000..fa374d67b --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_structs.cddl @@ -0,0 +1,30 @@ +; 3.5.1. Structs + +Geography = [ + city : tstr, + gpsCoordinates : GpsCoordinates, +] + +GpsCoordinates = { + longitude : uint, ; degrees, scaled by 10^7 + latitude : uint, ; degrees, scaled by 10^7 +} + +located-samples = { + sample-point: int, + samples: [+ float], + * equipment-type => equipment-tolerances, +} +equipment-type = [name: tstr, manufacturer: tstr] +equipment-tolerances = [+ [float, float]] + +PersonalData = { + ? displayName: tstr, + NameComponents, + ? age: uint, +} + +NameComponents = ( + ? firstName: tstr, + ? familyName: tstr, +) \ No newline at end of file diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_structs_alt.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_structs_alt.cddl new file mode 100644 index 000000000..569ef728d --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_structs_alt.cddl @@ -0,0 +1,16 @@ +; 3.5.1. Structs + +Geography = [ + city : tstr, + gpsCoordinates : GpsCoordinates, +] + +GpsCoordinates = { + longitude : uint, ; degrees, scaled by 10^7 + latitude : uint, ; degrees, scaled by 10^7 +} + +located-samples = { + "sample-point" => int, + "samples" => [+ float], +} \ No newline at end of file diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_tables.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_tables.cddl new file mode 100644 index 000000000..139ca7599 --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_tables.cddl @@ -0,0 +1,8 @@ +; 3.5.2. Tables + +square-roots = {* x => y} +x = int +y = float + +tostring = {* mynumber => tstr} +mynumber = int / float \ No newline at end of file diff --git a/hermes/crates/cbork/cddl-parser/tests/group_elements.rs b/hermes/crates/cbork/cddl-parser/tests/group_elements.rs new file mode 100644 index 000000000..b75127f9b --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/group_elements.rs @@ -0,0 +1,186 @@ +use cddl_parser::{ + self, + cddl_test::{CDDLTestParser, Parser, Rule}, +}; + +mod identifiers; +use identifiers::{ID_PASSES, ID_FAILS}; + +pub const OCCUR_PASSES: &[&str] = &[ + "5*10", + "0x1*0b110", + "*20", + "5*10", + "0x1*0b110", + "0*5", + "5*", + "*5", + "0b110*", + "0x1*", + + // Pass cases for "+" + "+", + + // Pass cases for "?" + "?", +]; + +pub const OCCUR_FAILS: &[&str] = &[ + "5**10", + "5 * 10", + + // Fail cases for "+" + "++", + + // Fail cases for "?" + "??", + + // Fail cases for uint + "0123", // Leading zero is not allowed for decimal + "0xG", // Invalid hex digit + "0b123", // Invalid binary digit + "0*5*", // Multiple '*' not allowed + "0x1*0b110*", + "0x", + "0b", +]; + +pub const MEMBERKEY_PASSES: &[&str] = &[ + +]; + +pub const MEMBERKEY_FAILS: &[&str] = &[ + +]; + +pub const GRPENT_PASSES: &[&str] = &[ + +]; + +pub const GRPENT_FAILS: &[&str] = &[ + +]; + +pub const GRPCHOICE_PASSES: &[&str] = &[ + +]; + +pub const GRPCHOICE_FAILS: &[&str] = &[ + +]; + +pub const GROUP_PASSES: &[&str] = &[ + +]; + +pub const GROUP_FAILS: &[&str] = &[ + +]; + +#[test] +/// Test if the `occur` rule passes properly. +/// This uses a special rule in the Grammar to test `occur` exhaustively. +fn check_occur() { + let tests = OCCUR_PASSES; + let fails = OCCUR_FAILS; + + for test in tests { + let parse = CDDLTestParser::parse(Rule::occur_TEST, test); + assert!(parse.is_ok()); + } + + for test in fails { + let parse = CDDLTestParser::parse(Rule::occur_TEST, test); + assert!(parse.is_err()); + } +} + +#[test] +/// Test if the `bareword` rule passes properly. +/// This uses a special rule in the Grammar to test `bareword` exhaustively. +fn check_bareword() { + let tests = ID_PASSES; + let fails = ID_FAILS; + + for test in tests { + let parse = CDDLTestParser::parse(Rule::bareword_TEST, test); + assert!(parse.is_ok()); + } + + for test in fails { + let parse = CDDLTestParser::parse(Rule::bareword_TEST, test); + assert!(parse.is_err()); + } +} + +#[test] +/// Test if the `memberkey` rule passes properly. +/// This uses a special rule in the Grammar to test `memberkey` exhaustively. +fn check_memberkey() { + let tests = MEMBERKEY_PASSES; + let fails = MEMBERKEY_FAILS; + + for test in tests { + let parse = CDDLTestParser::parse(Rule::memberkey_TEST, test); + assert!(parse.is_ok()); + } + + for test in fails { + let parse = CDDLTestParser::parse(Rule::memberkey_TEST, test); + assert!(parse.is_err()); + } +} + +#[test] +/// Test if the `grpent` rule passes properly. +/// This uses a special rule in the Grammar to test `grpent` exhaustively. +fn check_grpent() { + let tests = GRPENT_PASSES; + let fails = GRPENT_FAILS; + + for test in tests { + let parse = CDDLTestParser::parse(Rule::grpent_TEST, test); + assert!(parse.is_ok()); + } + + for test in fails { + let parse = CDDLTestParser::parse(Rule::grpent_TEST, test); + assert!(parse.is_err()); + } +} + +#[test] +/// Test if the `grpchoice` rule passes properly. +/// This uses a special rule in the Grammar to test `grpchoice` exhaustively. +fn check_grpchoice() { + let tests = GRPCHOICE_PASSES; + let fails = GRPCHOICE_FAILS; + + for test in tests { + let parse = CDDLTestParser::parse(Rule::grpchoice_TEST, test); + assert!(parse.is_ok()); + } + + for test in fails { + let parse = CDDLTestParser::parse(Rule::grpchoice_TEST, test); + assert!(parse.is_err()); + } +} + +#[test] +/// Test if the `group` rule passes properly. +/// This uses a special rule in the Grammar to test `group` exhaustively. +fn check_group() { + let tests = GROUP_PASSES; + let fails = GROUP_FAILS; + + for test in tests { + let parse = CDDLTestParser::parse(Rule::group_TEST, test); + assert!(parse.is_ok()); + } + + for test in fails { + let parse = CDDLTestParser::parse(Rule::group_TEST, test); + assert!(parse.is_err()); + } +} diff --git a/hermes/crates/cbork/cddl-parser/tests/identifiers.rs b/hermes/crates/cbork/cddl-parser/tests/identifiers.rs index dc69d9f13..4257f075e 100644 --- a/hermes/crates/cbork/cddl-parser/tests/identifiers.rs +++ b/hermes/crates/cbork/cddl-parser/tests/identifiers.rs @@ -5,6 +5,43 @@ use cddl_parser::{ cddl_test::{CDDLTestParser, Parser, Rule}, }; +pub const ID_PASSES: &[&str] = &[ + "$", + "@", + "_", + "a", + "z", + "A", + "Z", + "$$", + "@@", + "__", + "a$", + "a@", + "a_", + "$0", + "@9", + "_a", + "abc", + "aname", + "@aname", + "_aname", + "$aname", + "a$name", + "a.name", + "@a.name", + "$a.name", + "_a.name", + "$$", + "$$groupsocket", + "$", + "$typesocket", +]; + +pub const ID_FAILS: &[&str] = &[ + "aname.", "aname-", "aname%", "a%name4", "a^name5", "a name", "", +]; + #[test] /// Check if the name components pass properly. fn check_name_characters() { @@ -29,42 +66,8 @@ fn check_name_characters() { #[test] /// Test if the `id` rule passes properly. fn check_id() { - let test = vec![ - "$", - "@", - "_", - "a", - "z", - "A", - "Z", - "$$", - "@@", - "__", - "a$", - "a@", - "a_", - "$0", - "@9", - "_a", - "abc", - "aname", - "@aname", - "_aname", - "$aname", - "a$name", - "a.name", - "@a.name", - "$a.name", - "_a.name", - "$$", - "$$groupsocket", - "$", - "$typesocket", - ]; - - let fail = vec![ - "aname.", "aname-", "aname%", "a%name4", "a^name5", "a name", "", - ]; + let test = ID_PASSES; + let fail = ID_FAILS; for test in test { let parse = CDDLTestParser::parse(Rule::id_TEST, test); diff --git a/hermes/crates/cbork/cddl-parser/tests/text_sequences.rs b/hermes/crates/cbork/cddl-parser/tests/text_sequences.rs index 5fe8ca665..b1e805b82 100644 --- a/hermes/crates/cbork/cddl-parser/tests/text_sequences.rs +++ b/hermes/crates/cbork/cddl-parser/tests/text_sequences.rs @@ -3,13 +3,17 @@ use cddl_parser::{ cddl_test::{CDDLTestParser, Parser, Rule}, }; +pub const S_PASSES: &[&str] = &[" ", " ", " \t \t", " \t \r \n \r\n "]; +pub const S_FAILS: &[&str] = &[" a ", "zz", " \t d \t", " \t \r \n \t \r\n x"]; +pub const TEXT_PASSES: &[&str] = &[r#""""#, r#""abc""#, "\"abc\\n\""]; +pub const TEXT_FAILS: &[&str] = &["", "''", "\"abc\n\""]; + #[test] /// Test if the `S` rule passes properly. -/// This uses a special rule in the Grammar to test whitespace exhaustively. +/// This uses a special rule in the Grammar to test whitespace exhaustively. fn check_s() { - let tests = vec![" ", " ", " \t \t", " \t \r \n \r\n "]; - - let fails = vec![" a ", "zz", " \t d \t", " \t \r \n \t \r\n x"]; + let tests = S_PASSES; + let fails = S_FAILS; for test in tests { let parse = CDDLTestParser::parse(Rule::S_TEST, test); @@ -25,9 +29,8 @@ fn check_s() { #[test] /// Test if the `text` rule passes properly. fn check_text() { - let test = vec![r#""""#, r#""abc""#, "\"abc\\n\""]; - - let fail = vec!["", "''", "\"abc\n\""]; + let test = TEXT_PASSES; + let fail = TEXT_FAILS; for test in test { let parse = CDDLTestParser::parse(Rule::text_TEST, test); From 4267afd8129a01632e3cb8d31f39b4cb83097b96 Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Tue, 16 Jan 2024 18:32:45 +0700 Subject: [PATCH 08/43] fix: comments --- hermes/crates/cbork/cddl-parser/src/grammar/rfc_8610.pest | 8 ++++---- hermes/crates/cbork/cddl-parser/tests/cddl.rs | 1 - hermes/crates/cbork/cddl-parser/tests/group_elements.rs | 4 +++- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/hermes/crates/cbork/cddl-parser/src/grammar/rfc_8610.pest b/hermes/crates/cbork/cddl-parser/src/grammar/rfc_8610.pest index ffa1bff7c..b10567ad0 100644 --- a/hermes/crates/cbork/cddl-parser/src/grammar/rfc_8610.pest +++ b/hermes/crates/cbork/cddl-parser/src/grammar/rfc_8610.pest @@ -64,7 +64,7 @@ ctlop = ${ "." ~ id } group = { grpchoice ~ S ~ ( "//" ~ S ~ grpchoice ~ S )* } /// Group choice - a collection of group entries in various forms -grpchoice = { ( grpent ~ ","? ~ S )* } +grpchoice = { ( grpent ~ (S ~ ",")? ~ S )* } /// Group entry - a single element in a group grpent = ${ @@ -75,8 +75,8 @@ grpent = ${ /// Member key in a struct or an array memberkey = { - ( type1 ~ "^"? ~ "=>" ) - | ( (bareword | value) ~ ":" ) + ( type1 ~ S ~ ("^" ~ S)? ~ "=>" ) + | ( (bareword | value) ~ S ~ ":" ) } /// Bareword - a member key that is not used as a value like "foo", 1, etc. @@ -160,7 +160,7 @@ URL_BASE64 = _{ S ~ ( URL_BASE64_ALPHA ~ S)* ~ URL_BASE64_PAD? } S = _{ WHITESPACE* } WHITESPACE = _{ " " | "\t" | NEWLINE } -COMMENT = _{ ";" ~ (PCHAR | "\t")* ~ NEWLINE } +COMMENT = _{ ";" ~ (!NEWLINE ~ (" " | "\t")* ~ PCHAR)* } // URL Base64 Characterset. URL_BASE64_ALPHA = _{ ASCII_ALPHA | ASCII_DIGIT | "-" | "_" } diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl.rs b/hermes/crates/cbork/cddl-parser/tests/cddl.rs index 052fc676d..ac13c10b7 100644 --- a/hermes/crates/cbork/cddl-parser/tests/cddl.rs +++ b/hermes/crates/cbork/cddl-parser/tests/cddl.rs @@ -3,7 +3,6 @@ use std::{fs, io::Result}; use cddl_parser::{parse_cddl, Extension}; #[test] -#[ignore] fn parse_cddl_files() -> Result<()> { let entries = fs::read_dir("tests/cddl")?; diff --git a/hermes/crates/cbork/cddl-parser/tests/group_elements.rs b/hermes/crates/cbork/cddl-parser/tests/group_elements.rs index b75127f9b..acb3c12c4 100644 --- a/hermes/crates/cbork/cddl-parser/tests/group_elements.rs +++ b/hermes/crates/cbork/cddl-parser/tests/group_elements.rs @@ -46,7 +46,9 @@ pub const OCCUR_FAILS: &[&str] = &[ ]; pub const MEMBERKEY_PASSES: &[&str] = &[ - + "foo:", + "foo :", + "\"foo\":", ]; pub const MEMBERKEY_FAILS: &[&str] = &[ From 10884a694cfdbff398308faac066d555396476a8 Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Tue, 16 Jan 2024 22:17:23 +0700 Subject: [PATCH 09/43] fix: grammar correction for comments --- .../cddl-parser/src/grammar/cddl_test.pest | 32 ++++---- .../cddl-parser/src/grammar/rfc_8610.pest | 74 +++++++++---------- .../cbork/cddl-parser/tests/group_elements.rs | 14 +++- 3 files changed, 64 insertions(+), 56 deletions(-) diff --git a/hermes/crates/cbork/cddl-parser/src/grammar/cddl_test.pest b/hermes/crates/cbork/cddl-parser/src/grammar/cddl_test.pest index 1251525c8..3794da9e2 100644 --- a/hermes/crates/cbork/cddl-parser/src/grammar/cddl_test.pest +++ b/hermes/crates/cbork/cddl-parser/src/grammar/cddl_test.pest @@ -6,25 +6,25 @@ // cspell: words intfloat hexfloat /// Test Expression for the `group` Rule. -group_TEST = ${ SOI ~ group ~ EOI } +group_TEST = { SOI ~ group ~ EOI } /// Test Expression for the `grpchoice` Rule. -grpchoice_TEST = ${ SOI ~ grpchoice ~ EOI } +grpchoice_TEST = { SOI ~ grpchoice ~ EOI } /// Test Expression for the `grpent` Rule. -grpent_TEST = ${ SOI ~ grpent ~ EOI } +grpent_TEST = { SOI ~ grpent ~ EOI } /// Test Expression for the `memberkey` Rule. -memberkey_TEST = ${ SOI ~ memberkey ~ EOI } +memberkey_TEST = { SOI ~ memberkey ~ EOI } /// Test Expression for the `bareword` Rule. -bareword_TEST = ${ SOI ~ bareword ~ EOI } +bareword_TEST = { SOI ~ bareword ~ EOI } /// Test Expression for the `occur` Rule. -occur_TEST = ${ SOI ~ occur ~ EOI } +occur_TEST = { SOI ~ occur ~ EOI } /// Test Expression for the `S` Rule. -S_TEST = ${ SOI ~ S ~ EOI } +S_TEST = { SOI ~ S ~ EOI } /// Test Expression for the `COMMENT` Rule. COMMENT_TEST = { SOI ~ COMMENT* ~ EOI } @@ -33,28 +33,28 @@ COMMENT_TEST = { SOI ~ COMMENT* ~ EOI } URL_BASE64_TEST = { SOI ~ URL_BASE64 ~ EOI } /// Test expression to the `id` Rule. -id_TEST = ${ SOI ~ id ~ EOI} +id_TEST = { SOI ~ id ~ EOI} /// Test expression to the `bytes` Rule. -bytes_TEST = ${ SOI ~ bytes ~ EOI} +bytes_TEST = { SOI ~ bytes ~ EOI} /// Test expression to the `text` Rule. -text_TEST = ${ SOI ~ text ~ EOI} +text_TEST = { SOI ~ text ~ EOI} /// Test expression to the `uint` Rule. -uint_TEST = ${ SOI ~ uint ~ EOI} +uint_TEST = { SOI ~ uint ~ EOI} /// Test expression to the `int` Rule. -int_TEST = ${ SOI ~ int ~ EOI} +int_TEST = { SOI ~ int ~ EOI} /// Test expression to the `intfloat` Rule. -intfloat_TEST = ${ SOI ~ intfloat ~ EOI} +intfloat_TEST = { SOI ~ intfloat ~ EOI} /// Test expression to the `hexfloat` Rule. -hexfloat_TEST = ${ SOI ~ hexfloat ~ EOI} +hexfloat_TEST = { SOI ~ hexfloat ~ EOI} /// Test expression to the `number` Rule. -number_TEST = ${ SOI ~ number ~ EOI} +number_TEST = { SOI ~ number ~ EOI} /// Test expression to the `value` Rule. -value_TEST = ${ SOI ~ value ~ EOI} \ No newline at end of file +value_TEST = { SOI ~ value ~ EOI} \ No newline at end of file diff --git a/hermes/crates/cbork/cddl-parser/src/grammar/rfc_8610.pest b/hermes/crates/cbork/cddl-parser/src/grammar/rfc_8610.pest index b10567ad0..3bdd63047 100644 --- a/hermes/crates/cbork/cddl-parser/src/grammar/rfc_8610.pest +++ b/hermes/crates/cbork/cddl-parser/src/grammar/rfc_8610.pest @@ -5,18 +5,14 @@ // cspell: words genericarg rangeop ctlop grpchoice memberkey bareword hexfloat intfloat // cspell: words SCHAR BCHAR PCHAR SESC FFFD Characterset Visiable -cddl = { - SOI - ~ S ~ rule+ - ~ EOI -} +cddl = { SOI ~ rule+ ~ EOI } // ----------------------------------------------------------------------------- // Rules rule = { - ( typename ~ assignt ~ type ) - | ( groupname ~ assigng ~ grpent ) + (typename ~ assignt ~ type) + | (groupname ~ assigng ~ grpent) } typename = ${ id ~ genericparm? } @@ -25,31 +21,31 @@ groupname = ${ id ~ genericparm? } assignt = { "=" | "/=" } assigng = { "=" | "//=" } -genericparm = { "<" ~ id ~ ( "," ~ id )* ~ ">" } -genericarg = { "<" ~ type1 ~ ( "," ~ type1 )* ~ ">" } +genericparm = { "<" ~ id ~ ("," ~ id)* ~ ">" } +genericarg = { "<" ~ type1 ~ ("," ~ type1)* ~ ">" } // ----------------------------------------------------------------------------- // Type Declaration -type = { type1 ~ ( S ~ "/" ~ type1 )* } +type = { type1 ~ ("/" ~ type1)* } -type1 = { type2 ~ ( S ~ ( rangeop | ctlop ) ~ type2 )? } +type1 = { type2 ~ ((rangeop | ctlop) ~ type2)? } typename_arg = ${ typename ~ genericarg? } groupname_arg = ${ groupname ~ genericarg? } -tag6 = ${ "#" ~ "6" ~ ("." ~ uint)? ~ "(" ~ S ~ type ~ S ~ ")" } +tag6 = ${ "#" ~ "6" ~ ("." ~ uint)? ~ "(" ~ type ~ ")" } tag_generic = ${ "#" ~ ASCII_DIGIT ~ ("." ~ uint)? } type2 = { value | typename_arg - | ( "(" ~ S ~ type ~ ")" ) - | ( "{" ~ S ~ group ~ "}" ) - | ( "[" ~ S ~ group ~ "]" ) - | ( "~" ~ S ~ typename_arg ) - | ( "&" ~ S ~ "(" ~ S ~ group ~ ")" ) - | ( "&" ~ S ~ groupname_arg ) + | ("(" ~ type ~ ")") + | ("{" ~ group ~ "}") + | ("[" ~ group ~ "]") + | ("~" ~ typename_arg) + | ("&" ~ "(" ~ group ~ ")") + | ("&" ~ groupname_arg) | tag6 | tag_generic | "#" @@ -61,22 +57,22 @@ ctlop = ${ "." ~ id } // ----------------------------------------------------------------------------- // Group Elements -group = { grpchoice ~ S ~ ( "//" ~ S ~ grpchoice ~ S )* } +group = { grpchoice ~ ("//" ~ grpchoice)* } /// Group choice - a collection of group entries in various forms -grpchoice = { ( grpent ~ (S ~ ",")? ~ S )* } +grpchoice = { (grpent ~ ","?)* } /// Group entry - a single element in a group -grpent = ${ - ( (occur ~ S)? ~ (memberkey ~ S)? ~ type ) - | ( (occur ~ S)? ~ groupname ~ genericarg? ) - | ( (occur ~ S)? ~ "(" ~ S ~ group ~ S ~ ")" ) +grpent = { + (occur? ~ S ~ memberkey? ~ S ~ type) + | (occur? ~ S ~ groupname ~ S ~ genericarg?) + | (occur? ~ S ~ "(" ~ S ~ group ~ S ~ ")") } /// Member key in a struct or an array memberkey = { - ( type1 ~ S ~ ("^" ~ S)? ~ "=>" ) - | ( (bareword | value) ~ S ~ ":" ) + (type1 ~ "^"? ~ "=>") + | ((bareword | value) ~ ":") } /// Bareword - a member key that is not used as a value like "foo", 1, etc. @@ -84,7 +80,7 @@ bareword = { id } /// Occurence indicator occur = { - ( uint? ~ "*" ~ uint? ) + (uint? ~ "*" ~ uint?) | "+" | "?" } @@ -115,9 +111,9 @@ int = ${ "-"? ~ uint } /// Unsigned Integers uint = ${ - ( ASCII_NONZERO_DIGIT ~ ASCII_DIGIT* ) - | ( "0x" ~ ASCII_HEX_DIGIT+ ) - | ( "0b" ~ ASCII_BIN_DIGIT+ ) + (ASCII_NONZERO_DIGIT ~ ASCII_DIGIT*) + | ("0x" ~ ASCII_HEX_DIGIT+) + | ("0b" ~ ASCII_BIN_DIGIT+) | "0" } @@ -126,9 +122,9 @@ text = ${ "\"" ~ SCHAR* ~ "\"" } /// Literal Bytes - Note CDDL Spec incorrectly defines b64''. bytes = ${ bytes_hex | bytes_b64 | bytes_text } -bytes_hex = ${ "h" ~ "'" ~ HEX_PAIR* ~ "'" } -bytes_b64 = ${ "b64" ~ "'" ~ URL_BASE64 ~ "'" } -bytes_text = ${ "'" ~ BCHAR* ~ "'" } +bytes_hex = ${ "h'" ~ (S ~ HEX_PAIR)* ~ S ~ "'" } +bytes_b64 = ${ "b64'" ~ (S ~ URL_BASE64) ~ S ~ "'" } +bytes_text = ${ "'" ~ (S ~ BCHAR)* ~ S ~ "'" } // ----------------------------------------------------------------------------- // Simple multiple character sequences @@ -141,18 +137,18 @@ id = ${ } /// Special form of a name that represents a Group Socket. -group_socket = ${ "$$" ~ ( ( "-" | "." )* ~ NAME_END )* } +group_socket = ${ "$$" ~ (("-" | ".")* ~ NAME_END)* } /// Special form of a name that represents a Type Socket. -type_socket = ${ "$" ~ ( ( "-" | "." )* ~ NAME_END )* } +type_socket = ${ "$" ~ (("-" | ".")* ~ NAME_END)* } /// General form of a name. -name = ${ NAME_START ~ ( ( "-" | "." )* ~ NAME_END )* } +name = ${ NAME_START ~ (("-" | ".")* ~ NAME_END)* } /// A pair of hex digits. (Must always have even numbers of hex digits.) -HEX_PAIR = _{ S ~ ASCII_HEX_DIGIT ~ S ~ ASCII_HEX_DIGIT ~ S } +HEX_PAIR = _{ ASCII_HEX_DIGIT ~ S ~ ASCII_HEX_DIGIT } /// Whitespace is allowed and is ignored. /// This token will keep the whitespace, so it will need to handled when converted to binary. -URL_BASE64 = _{ S ~ ( URL_BASE64_ALPHA ~ S)* ~ URL_BASE64_PAD? } +URL_BASE64 = _{ S ~ (URL_BASE64_ALPHA ~ S)* ~ S ~ URL_BASE64_PAD? } // ----------------------------------------------------------------------------- @@ -160,7 +156,7 @@ URL_BASE64 = _{ S ~ ( URL_BASE64_ALPHA ~ S)* ~ URL_BASE64_PAD? } S = _{ WHITESPACE* } WHITESPACE = _{ " " | "\t" | NEWLINE } -COMMENT = _{ ";" ~ (!NEWLINE ~ (" " | "\t")* ~ PCHAR)* } +COMMENT = _{ ";" ~ (!NEWLINE ~ PCHAR)* } // URL Base64 Characterset. URL_BASE64_ALPHA = _{ ASCII_ALPHA | ASCII_DIGIT | "-" | "_" } diff --git a/hermes/crates/cbork/cddl-parser/tests/group_elements.rs b/hermes/crates/cbork/cddl-parser/tests/group_elements.rs index acb3c12c4..8be519867 100644 --- a/hermes/crates/cbork/cddl-parser/tests/group_elements.rs +++ b/hermes/crates/cbork/cddl-parser/tests/group_elements.rs @@ -17,6 +17,7 @@ pub const OCCUR_PASSES: &[&str] = &[ "*5", "0b110*", "0x1*", + "5 * 10", // Pass cases for "+" "+", @@ -27,7 +28,6 @@ pub const OCCUR_PASSES: &[&str] = &[ pub const OCCUR_FAILS: &[&str] = &[ "5**10", - "5 * 10", // Fail cases for "+" "++", @@ -46,9 +46,21 @@ pub const OCCUR_FAILS: &[&str] = &[ ]; pub const MEMBERKEY_PASSES: &[&str] = &[ + // bareword "foo:", + "foo-bar:", + "foo_bar:", "foo :", + + // values "\"foo\":", + "1:", + "0x123:", + "1.1:", + "-1:", + // "b64'123':", + + // type1 ]; pub const MEMBERKEY_FAILS: &[&str] = &[ From f2c9e221b17b8fb1ebf7b552413a1d61e0543add Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Wed, 17 Jan 2024 16:20:22 +0700 Subject: [PATCH 10/43] fix: grammar correcttion according to abnf --- .../cddl-parser/src/grammar/rfc_8610.pest | 101 ++++++++---------- .../cbork/cddl-parser/tests/group_elements.rs | 13 ++- 2 files changed, 53 insertions(+), 61 deletions(-) diff --git a/hermes/crates/cbork/cddl-parser/src/grammar/rfc_8610.pest b/hermes/crates/cbork/cddl-parser/src/grammar/rfc_8610.pest index 3bdd63047..d9f3c88ca 100644 --- a/hermes/crates/cbork/cddl-parser/src/grammar/rfc_8610.pest +++ b/hermes/crates/cbork/cddl-parser/src/grammar/rfc_8610.pest @@ -5,14 +5,17 @@ // cspell: words genericarg rangeop ctlop grpchoice memberkey bareword hexfloat intfloat // cspell: words SCHAR BCHAR PCHAR SESC FFFD Characterset Visiable -cddl = { SOI ~ rule+ ~ EOI } - +cddl = ${ + SOI + ~ S ~ (rule ~ S)+ + ~ EOI +} // ----------------------------------------------------------------------------- // Rules -rule = { - (typename ~ assignt ~ type) - | (groupname ~ assigng ~ grpent) +rule = ${ + (typename ~ S ~ assignt ~ S ~ type) + | (groupname ~ S ~ assigng ~ S ~ grpent) } typename = ${ id ~ genericparm? } @@ -21,64 +24,53 @@ groupname = ${ id ~ genericparm? } assignt = { "=" | "/=" } assigng = { "=" | "//=" } -genericparm = { "<" ~ id ~ ("," ~ id)* ~ ">" } -genericarg = { "<" ~ type1 ~ ("," ~ type1)* ~ ">" } - +genericparm = ${ "<" ~ S ~ id ~ S ~ ("," ~ S ~ id ~ S)* ~ ">" } +genericarg = ${ "<" ~ S ~ type1 ~ S ~ ("," ~ S ~ type1 ~ S)* ~ ">" } // ----------------------------------------------------------------------------- // Type Declaration -type = { type1 ~ ("/" ~ type1)* } - -type1 = { type2 ~ ((rangeop | ctlop) ~ type2)? } +type = ${ type1 ~ (S ~ "/" ~ S ~ type1)* } -typename_arg = ${ typename ~ genericarg? } -groupname_arg = ${ groupname ~ genericarg? } +type1 = ${ type2 ~ (S ~ (rangeop | ctlop) ~ S ~ type2)? } -tag6 = ${ "#" ~ "6" ~ ("." ~ uint)? ~ "(" ~ type ~ ")" } -tag_generic = ${ "#" ~ ASCII_DIGIT ~ ("." ~ uint)? } - -type2 = { +type2 = ${ value - | typename_arg - | ("(" ~ type ~ ")") - | ("{" ~ group ~ "}") - | ("[" ~ group ~ "]") - | ("~" ~ typename_arg) - | ("&" ~ "(" ~ group ~ ")") - | ("&" ~ groupname_arg) - | tag6 - | tag_generic + | typename ~ genericarg? + | ("(" ~ S ~ type ~ S ~ ")") + | ("{" ~ S ~ group ~ S ~ "}") + | ("[" ~ S ~ group ~ S ~ "]") + | ("~" ~ S ~ typename ~ genericarg?) + | ("&" ~ S ~ "(" ~ S ~ group ~ S ~ ")") + | ("&" ~ S ~ groupname ~ genericarg?) + | ("#" ~ "6" ~ ("." ~ uint)? ~ "(" ~ S ~ type ~ S ~ ")") + | ("#" ~ ASCII_DIGIT ~ ("." ~ uint)?) | "#" } rangeop = { "..." | ".." } ctlop = ${ "." ~ id } - // ----------------------------------------------------------------------------- // Group Elements -group = { grpchoice ~ ("//" ~ grpchoice)* } +group = ${ grpchoice ~ (S ~ "//" ~ S ~ grpchoice)* } -/// Group choice - a collection of group entries in various forms -grpchoice = { (grpent ~ ","?)* } +grpchoice = ${ (grpent ~ optcom)* } -/// Group entry - a single element in a group -grpent = { - (occur? ~ S ~ memberkey? ~ S ~ type) - | (occur? ~ S ~ groupname ~ S ~ genericarg?) - | (occur? ~ S ~ "(" ~ S ~ group ~ S ~ ")") +grpent = ${ + ((occur ~ S)? ~ (memberkey ~ S)? ~ type) + | ((occur ~ S)? ~ groupname ~ genericarg?) + | ((occur ~ S)? ~ "(" ~ S ~ group ~ S ~ ")") } -/// Member key in a struct or an array -memberkey = { - (type1 ~ "^"? ~ "=>") - | ((bareword | value) ~ ":") +memberkey = ${ + (type1 ~ S ~ ("^" ~ S)? ~ "=>") + | ((value | bareword) ~ S ~ ":") } -/// Bareword - a member key that is not used as a value like "foo", 1, etc. bareword = { id } -/// Occurence indicator +optcom = { S ~ ("," ~ S)? } + occur = { (uint? ~ "*" ~ uint?) | "+" @@ -91,7 +83,7 @@ occur = { /// All Literal Values value = { number | text | bytes } -/// Literal Numbers - A float if it has fraction or exponent; int otherwise +/// Literal Numbers - A float if it has fraction or exponent; int otherwise number = { hexfloat | intfloat } /// Hex floats of the form -0x123.abc0p+12 @@ -109,6 +101,7 @@ exponent = ${ ("+" | "-") ~ ASCII_DIGIT+ } /// All integers, singed and unsigned int = ${ "-"? ~ uint } + /// Unsigned Integers uint = ${ (ASCII_NONZERO_DIGIT ~ ASCII_DIGIT*) @@ -122,14 +115,14 @@ text = ${ "\"" ~ SCHAR* ~ "\"" } /// Literal Bytes - Note CDDL Spec incorrectly defines b64''. bytes = ${ bytes_hex | bytes_b64 | bytes_text } -bytes_hex = ${ "h'" ~ (S ~ HEX_PAIR)* ~ S ~ "'" } -bytes_b64 = ${ "b64'" ~ (S ~ URL_BASE64) ~ S ~ "'" } -bytes_text = ${ "'" ~ (S ~ BCHAR)* ~ S ~ "'" } +bytes_hex = ${ "h" ~ "'" ~ HEX_PAIR* ~ "'" } +bytes_b64 = ${ "b64" ~ "'" ~ URL_BASE64 ~ "'" } +bytes_text = ${ "'" ~ BCHAR* ~ "'" } // ----------------------------------------------------------------------------- // Simple multiple character sequences -/// identifier, called the `name` in the CDDL spec. +/// identifier, called the `name` in the CDDL spec. id = ${ group_socket | type_socket | @@ -144,22 +137,22 @@ type_socket = ${ "$" ~ (("-" | ".")* ~ NAME_END)* } name = ${ NAME_START ~ (("-" | ".")* ~ NAME_END)* } /// A pair of hex digits. (Must always have even numbers of hex digits.) -HEX_PAIR = _{ ASCII_HEX_DIGIT ~ S ~ ASCII_HEX_DIGIT } +HEX_PAIR = _{ S ~ ASCII_HEX_DIGIT ~ S ~ ASCII_HEX_DIGIT ~ S } /// Whitespace is allowed and is ignored. /// This token will keep the whitespace, so it will need to handled when converted to binary. -URL_BASE64 = _{ S ~ (URL_BASE64_ALPHA ~ S)* ~ S ~ URL_BASE64_PAD? } +URL_BASE64 = _{ S ~ (URL_BASE64_ALPHA ~ S)* ~ URL_BASE64_PAD? } // ----------------------------------------------------------------------------- // Characters, Whitespace and Comments S = _{ WHITESPACE* } -WHITESPACE = _{ " " | "\t" | NEWLINE } -COMMENT = _{ ";" ~ (!NEWLINE ~ PCHAR)* } +WHITESPACE = _{ " " | "\t" | COMMENT | NEWLINE } +COMMENT = _{ ";" ~ (PCHAR | "\t")* ~ NEWLINE } // URL Base64 Characterset. -URL_BASE64_ALPHA = _{ ASCII_ALPHA | ASCII_DIGIT | "-" | "_" } +URL_BASE64_ALPHA = _{ ASCII_ALPHA | ASCII_DIGIT | "-" | "_" } // Optional Padding that goes at the end of Base64. URL_BASE64_PAD = _{ "~" } @@ -167,8 +160,8 @@ URL_BASE64_PAD = _{ "~" } /// A name can start with an alphabetic character (including "@", "_", "$") /// The body of the name can consist of any of the characters from the set -/// {"A" to "Z", "a" to "z", "0" to "9", "_", "-", "@", ".", "$"} -// NAME_BODY = _{ NAME_END | "-" | "." } -- Unused Rule +/// {"A" to "Z", "a" to "z", "0" to "9", "_", "-", "@", ".", "$"} +// NAME_BODY = _{ NAME_END | "-" | "." } -- Unused Rule NAME_START = _{ ASCII_ALPHA | "@" | "_" | "$" } /// A name can end with an alphabetic character (including "@", "_", "$") or a digit. NAME_END = _{ NAME_START | ASCII_DIGIT } @@ -195,4 +188,4 @@ SCHAR_ASCII_VISIBLE = _{ ' '..'!' | '#'..'[' | ']'..'~' } BCHAR_ASCII_VISIBLE = _{ ' '..'&' | '('..'[' | ']'..'~' } /// Valid non ascii unicode Characters -UNICODE_CHAR = _{ '\u{80}'..'\u{10FFFD}' } +UNICODE_CHAR = _{ '\u{80}'..'\u{10FFFD}' } \ No newline at end of file diff --git a/hermes/crates/cbork/cddl-parser/tests/group_elements.rs b/hermes/crates/cbork/cddl-parser/tests/group_elements.rs index 8be519867..f9e1ad808 100644 --- a/hermes/crates/cbork/cddl-parser/tests/group_elements.rs +++ b/hermes/crates/cbork/cddl-parser/tests/group_elements.rs @@ -7,7 +7,12 @@ mod identifiers; use identifiers::{ID_PASSES, ID_FAILS}; pub const OCCUR_PASSES: &[&str] = &[ + "*", + "+", + "?", "5*10", + "5 * 10", + "5\t\n*\n10", "0x1*0b110", "*20", "5*10", @@ -17,13 +22,6 @@ pub const OCCUR_PASSES: &[&str] = &[ "*5", "0b110*", "0x1*", - "5 * 10", - - // Pass cases for "+" - "+", - - // Pass cases for "?" - "?", ]; pub const OCCUR_FAILS: &[&str] = &[ @@ -61,6 +59,7 @@ pub const MEMBERKEY_PASSES: &[&str] = &[ // "b64'123':", // type1 + "tstr =>", ]; pub const MEMBERKEY_FAILS: &[&str] = &[ From 5dd9c204189d8228650d6849f866acb77372b44e Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Wed, 17 Jan 2024 16:37:55 +0700 Subject: [PATCH 11/43] fix: remove genericarg from typename and groupname --- hermes/crates/cbork/cddl-parser/src/grammar/rfc_8610.pest | 4 ++-- hermes/crates/cbork/cddl-parser/tests/group_elements.rs | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/hermes/crates/cbork/cddl-parser/src/grammar/rfc_8610.pest b/hermes/crates/cbork/cddl-parser/src/grammar/rfc_8610.pest index d9f3c88ca..2699db492 100644 --- a/hermes/crates/cbork/cddl-parser/src/grammar/rfc_8610.pest +++ b/hermes/crates/cbork/cddl-parser/src/grammar/rfc_8610.pest @@ -18,8 +18,8 @@ rule = ${ | (groupname ~ S ~ assigng ~ S ~ grpent) } -typename = ${ id ~ genericparm? } -groupname = ${ id ~ genericparm? } +typename = { id } +groupname = { id } assignt = { "=" | "/=" } assigng = { "=" | "//=" } diff --git a/hermes/crates/cbork/cddl-parser/tests/group_elements.rs b/hermes/crates/cbork/cddl-parser/tests/group_elements.rs index f9e1ad808..7ce1ebf65 100644 --- a/hermes/crates/cbork/cddl-parser/tests/group_elements.rs +++ b/hermes/crates/cbork/cddl-parser/tests/group_elements.rs @@ -56,7 +56,9 @@ pub const MEMBERKEY_PASSES: &[&str] = &[ "0x123:", "1.1:", "-1:", - // "b64'123':", + "b64'1234':", + "h'1234':", + "h'12 34\n':", // type1 "tstr =>", From 1c0bf324b169ac69acf2c2bbae61b61ab1add833 Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Wed, 17 Jan 2024 18:57:20 +0700 Subject: [PATCH 12/43] fix: unit tests --- .../cddl-parser/src/grammar/cddl_test.pest | 3 - .../cddl-parser/src/grammar/rfc_8610.pest | 42 +-- .../cbork/cddl-parser/tests/byte_sequences.rs | 95 ++---- .../cbork/cddl-parser/tests/character_sets.rs | 2 +- .../cbork/cddl-parser/tests/literal_values.rs | 280 +++++++++--------- 5 files changed, 177 insertions(+), 245 deletions(-) diff --git a/hermes/crates/cbork/cddl-parser/src/grammar/cddl_test.pest b/hermes/crates/cbork/cddl-parser/src/grammar/cddl_test.pest index 3794da9e2..1dcb02a81 100644 --- a/hermes/crates/cbork/cddl-parser/src/grammar/cddl_test.pest +++ b/hermes/crates/cbork/cddl-parser/src/grammar/cddl_test.pest @@ -29,9 +29,6 @@ S_TEST = { SOI ~ S ~ EOI } /// Test Expression for the `COMMENT` Rule. COMMENT_TEST = { SOI ~ COMMENT* ~ EOI } -/// Test expression for the `URL_BASE64` Rule. -URL_BASE64_TEST = { SOI ~ URL_BASE64 ~ EOI } - /// Test expression to the `id` Rule. id_TEST = { SOI ~ id ~ EOI} diff --git a/hermes/crates/cbork/cddl-parser/src/grammar/rfc_8610.pest b/hermes/crates/cbork/cddl-parser/src/grammar/rfc_8610.pest index 2699db492..385b34a7d 100644 --- a/hermes/crates/cbork/cddl-parser/src/grammar/rfc_8610.pest +++ b/hermes/crates/cbork/cddl-parser/src/grammar/rfc_8610.pest @@ -69,6 +69,7 @@ memberkey = ${ bareword = { id } +/// Optional Comma - Note eligible for producing pairs as this might be useful for linting optcom = { S ~ ("," ~ S)? } occur = { @@ -83,7 +84,7 @@ occur = { /// All Literal Values value = { number | text | bytes } -/// Literal Numbers - A float if it has fraction or exponent; int otherwise +/// Literal Numbers - A float if it has fraction or exponent; int otherwise number = { hexfloat | intfloat } /// Hex floats of the form -0x123.abc0p+12 @@ -113,35 +114,15 @@ uint = ${ /// Literal Text text = ${ "\"" ~ SCHAR* ~ "\"" } -/// Literal Bytes - Note CDDL Spec incorrectly defines b64''. -bytes = ${ bytes_hex | bytes_b64 | bytes_text } -bytes_hex = ${ "h" ~ "'" ~ HEX_PAIR* ~ "'" } -bytes_b64 = ${ "b64" ~ "'" ~ URL_BASE64 ~ "'" } -bytes_text = ${ "'" ~ BCHAR* ~ "'" } +/// Literal Bytes. +bytes = ${ bsqual? ~ "'" ~ BCHAR* ~ "'" } +bsqual = { "h" | "b64" } // ----------------------------------------------------------------------------- // Simple multiple character sequences -/// identifier, called the `name` in the CDDL spec. -id = ${ - group_socket | - type_socket | - name -} - -/// Special form of a name that represents a Group Socket. -group_socket = ${ "$$" ~ (("-" | ".")* ~ NAME_END)* } -/// Special form of a name that represents a Type Socket. -type_socket = ${ "$" ~ (("-" | ".")* ~ NAME_END)* } -/// General form of a name. -name = ${ NAME_START ~ (("-" | ".")* ~ NAME_END)* } - -/// A pair of hex digits. (Must always have even numbers of hex digits.) -HEX_PAIR = _{ S ~ ASCII_HEX_DIGIT ~ S ~ ASCII_HEX_DIGIT ~ S } - -/// Whitespace is allowed and is ignored. -/// This token will keep the whitespace, so it will need to handled when converted to binary. -URL_BASE64 = _{ S ~ (URL_BASE64_ALPHA ~ S)* ~ URL_BASE64_PAD? } +/// identifier, called the `name` in the CDDL spec. +id = ${ NAME_START ~ (("-" | ".")* ~ NAME_END)* } // ----------------------------------------------------------------------------- @@ -151,11 +132,6 @@ S = _{ WHITESPACE* } WHITESPACE = _{ " " | "\t" | COMMENT | NEWLINE } COMMENT = _{ ";" ~ (PCHAR | "\t")* ~ NEWLINE } -// URL Base64 Characterset. -URL_BASE64_ALPHA = _{ ASCII_ALPHA | ASCII_DIGIT | "-" | "_" } -// Optional Padding that goes at the end of Base64. -URL_BASE64_PAD = _{ "~" } - // Identifier Name Character sets. /// A name can start with an alphabetic character (including "@", "_", "$") @@ -173,12 +149,12 @@ PCHAR = _{ ASCII_VISIBLE | UNICODE_CHAR } SCHAR = _{ SCHAR_ASCII_VISIBLE | UNICODE_CHAR | SESC } /// The set of characters valid for a byte string. -BCHAR = _{ BCHAR_ASCII_VISIBLE | UNICODE_CHAR | SESC | NEWLINE } +BCHAR = _{ '\u{20}'..'\u{26}' | '\u{28}'..'\u{5B}' | '\u{5D}'..'\u{10FFFD}' | SESC | NEWLINE } /// Escaping code to allow invalid characters to be used in text or byte strings. SESC = ${ "\\" ~ (ASCII_VISIBLE | UNICODE_CHAR) } -/// All Visiable Ascii characters. +/// All Visible Ascii characters. ASCII_VISIBLE = _{ ' '..'~' } /// Ascii subset valid for text strings. diff --git a/hermes/crates/cbork/cddl-parser/tests/byte_sequences.rs b/hermes/crates/cbork/cddl-parser/tests/byte_sequences.rs index d17203754..55fd00a0b 100644 --- a/hermes/crates/cbork/cddl-parser/tests/byte_sequences.rs +++ b/hermes/crates/cbork/cddl-parser/tests/byte_sequences.rs @@ -5,79 +5,36 @@ use cddl_parser::{ cddl_test::{CDDLTestParser, Parser, Rule}, }; -#[test] -/// Test if the `HEX_PAIR` rule passes properly. -fn check_hexpair() { - let hex_pairs = vec!["00", "ab", "de", "0f", "f0"]; - - let not_hex_pairs = vec!["0", " 0", "0 ", "az", "0p"]; - - for hp in hex_pairs { - let parse = CDDLTestParser::parse(Rule::HEX_PAIR, hp); - assert!(parse.is_ok()); - } - - for hp in not_hex_pairs { - let parse = CDDLTestParser::parse(Rule::HEX_PAIR, hp); - assert!(parse.is_err()); - } -} - -#[test] -/// Test if the `URL_BASE64` rule passes properly. -fn check_url_base64() { - let tests = vec![ - "abcdefghijklmnopq rstuvw yz01\t23456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ~", - "abcdefghijklmnopqrstuvwyz0123456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ", - ]; - - let fails = vec![ - "abcdefghijklmnopq # rstuvw yz01\t23456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ~ ", - "abcdefghijklmnopq $ rstuvw yz01\t23456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ~\t", - "abcdefghijklmnopq % rstuvw yz01\t23456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ~\n", - "abcdefghijklmnopq ^ rstuvw yz01\t23456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ~\r", - "abcdefghijklmnopq & rstuvw yz01\t23456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ~\r\n", - ]; - - for test in tests { - let parse = CDDLTestParser::parse(Rule::URL_BASE64_TEST, test); - assert!(parse.is_ok()); - } - - for test in fails { - let parse = CDDLTestParser::parse(Rule::URL_BASE64_TEST, test); - assert!(parse.is_err()); - } -} +pub const BYTES_PASSES: &[&str] = &[ + "h''", + "b64''", + "''", + "h'00'", + "h'00112233445566778899aabbccddeeff0123456789abcdef'", + "h'001'", + "h'0 1 2 3 4 5 6 7 8 9 a b c d e f'", + "h'0 \n\n\r f'", + "''", + "'text\n that gets converted \\\' into a byte string...'", +]; + +pub const BYTES_FAILS: &[&str] = &[ + "h64", + "b64", + "\"\"", + "h ''", + "h '0 \t f'", + "h' 0 1 2 3 4 5\r 6 7 \n 8 9 a\r\n\t b c d e f'", + "b64'abcdefghijklmnopq rstuvw yz01\t23456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ~'", + "b64'abcdefghijklmnopq rstuvw yz01\t23456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ'", + "'\u{7}'", +]; #[test] /// Test if the `bytes` rule passes properly. fn check_bytes() { - let test = vec![ - "h''", - "b64''", - "''", - "h'00'", - "h'00112233445566778899aabbccddeeff0123456789abcdef'", - "h'0 1 2 3 4 5 6 7 8 9 a b c d e f'", - "h' 0 1 2 3 4 5\r 6 7 \n 8 9 a\r\n\t b c d e f'", - "b64'abcdefghijklmnopq rstuvw yz01\t23456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ~'", - "b64'abcdefghijklmnopq rstuvw yz01\t23456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ'", - "''", - "'text\n that gets converted \\\' into a byte string...'", - ]; - - let fail = vec![ - "h64", - "b64", - "\"\"", - "h ''", - "b64 ''", - "h'001'", - "b64'abcdefghijklmnopq # rstuvw yz01\t23456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ~'", - "b64'abcdefghijklmnopq & rstuvw yz01\t23456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ'", - "'\u{7}'", - ]; + let test = BYTES_PASSES; + let fail = BYTES_FAILS; for test in test { let parse = CDDLTestParser::parse(Rule::bytes_TEST, test); diff --git a/hermes/crates/cbork/cddl-parser/tests/character_sets.rs b/hermes/crates/cbork/cddl-parser/tests/character_sets.rs index b713355ef..28997b3ff 100644 --- a/hermes/crates/cbork/cddl-parser/tests/character_sets.rs +++ b/hermes/crates/cbork/cddl-parser/tests/character_sets.rs @@ -44,7 +44,7 @@ fn check_bchar() { for x in ('\u{0}'..='\u{ff}').map(char::from) { let test = format!("{x}"); let parse = CDDLTestParser::parse(Rule::BCHAR, &test); - if x != '\n' && x != '\r' && x < ' ' || x == '\u{27}' || x == '\u{5c}' || x == '\u{7f}' { + if !matches!(x, '\n' | '\r') && x < ' ' || matches!(x, '\t' | '\'' | '\\') { assert!(parse.is_err()); } else { assert!(parse.is_ok()); diff --git a/hermes/crates/cbork/cddl-parser/tests/literal_values.rs b/hermes/crates/cbork/cddl-parser/tests/literal_values.rs index c2492e76d..9a6e0ec42 100644 --- a/hermes/crates/cbork/cddl-parser/tests/literal_values.rs +++ b/hermes/crates/cbork/cddl-parser/tests/literal_values.rs @@ -5,23 +5,137 @@ use cddl_parser::{ cddl_test::{CDDLTestParser, Parser, Rule}, }; +mod byte_sequences; +use byte_sequences::BYTES_PASSES; + /// Note, the `text`, `bytes` and `id` tests are elsewhere. +pub const UINT_PASSES: &[&str] = &[ + "10", + "101", + "2034", + "30456", + "123456789", + "0x123456789abcdefABCDEF", + "0b0001110101010101", + "0", +]; + +pub const UINT_FAILS: &[&str] = &[ + " a ", "zz", "0123zzz", "0xdog", "0b777" +]; + +pub const INT_PASSES: &[&str] = &[ + "10", + "101", + "2034", + "30456", + "123456789", + "0x123456789abcdefABCDEF", + "0b0001110101010101", + "0", + "-10", + "-101", + "-2034", + "-30456", + "-123456789", + "-0x123456789abcdefABCDEF", + "-0b0001110101010101", + "-0", +]; + +pub const INT_FAILS: &[&str] = &[ + " a ", "zz", "0123zzz", "0xdog", "0b777" +]; + + +pub const INTFLOAT_PASSES: &[&str] = &[ + "10", + "101", + "2034", + "30456", + "123456789", + "0", + "-10", + "-101", + "-2034", + "-30456", + "-123456789", + "123.456", + "123.456", + "123e+789", + "123e-789", + "123.456e+789", + "123.456e-789", +]; + +pub const INTFLOAT_FAILS: &[&str] = &[ + " a ", "zz", "0123zzz", "0xdog", "0b777" +]; + + +pub const HEXFLOAT_PASSES: &[&str] = &[ + "0xabcp+123", + "-0xabcp+123", + "0xabcp-123", + "-0xabcp-123", + "0xabc.defp+123", + "-0xabc.defp+123", + "0xabc.defp-123", + "-0xabc.defp-123", +]; + +pub const HEXFLOAT_FAILS: &[&str] = &[ + " a ", "zz", "0123zzz", "0xdog", "0b777" +]; + + +pub const NUMBER_PASSES: &[&str] = &[ + "0xabcp+123", + "-0xabcp+123", + "0xabcp-123", + "-0xabcp-123", + "0xabc.defp+123", + "-0xabc.defp+123", + "0xabc.defp-123", + "-0xabc.defp-123", + "10", + "101", + "2034", + "30456", + "123456789", + "0", + "-10", + "-101", + "-2034", + "-30456", + "-123456789", + "123.456", + "123.456", + "123e+789", + "123e-789", + "123.456e+789", + "123.456e-789", +]; + +pub const NUMBER_FAILS: &[&str] = &[ + " a ", "zz", "0123zzz", "0xdog", "0b777" +]; + +pub const VALUE_PASSES: &[&str] = &[ + // Ideally we would define these somewhere central and just use them where needed. + // r#""""#, + // r#""abc""#, + // "\"abc\\n\"", +]; + +pub const VALUE_FAILS: &[&str] = &[]; + #[test] /// Test if the `uint` rule passes properly. fn check_uint() { - let tests = vec![ - "10", - "101", - "2034", - "30456", - "123456789", - "0x123456789abcdefABCDEF", - "0b0001110101010101", - "0", - ]; - - let fails = vec![" a ", "zz", "0123zzz", "0xdog", "0b777"]; + let tests = UINT_PASSES; + let fails = UINT_FAILS; for test in tests { let parse = CDDLTestParser::parse(Rule::uint_TEST, test); @@ -37,26 +151,8 @@ fn check_uint() { #[test] /// Test if the `uint` rule passes properly. fn check_int() { - let tests = vec![ - "10", - "101", - "2034", - "30456", - "123456789", - "0x123456789abcdefABCDEF", - "0b0001110101010101", - "0", - "-10", - "-101", - "-2034", - "-30456", - "-123456789", - "-0x123456789abcdefABCDEF", - "-0b0001110101010101", - "-0", - ]; - - let fails = vec![" a ", "zz", "0123zzz", "0xdog", "0b777"]; + let tests = INT_PASSES; + let fails = INT_FAILS; for test in tests { let parse = CDDLTestParser::parse(Rule::int_TEST, test); @@ -72,27 +168,8 @@ fn check_int() { #[test] /// Test if the `uint` rule passes properly. fn check_intfloat() { - let tests = vec![ - "10", - "101", - "2034", - "30456", - "123456789", - "0", - "-10", - "-101", - "-2034", - "-30456", - "-123456789", - "123.456", - "123.456", - "123e+789", - "123e-789", - "123.456e+789", - "123.456e-789", - ]; - - let fails = vec![" a ", "zz", "0123zzz", "0xdog", "0b777"]; + let tests = INTFLOAT_PASSES; + let fails = INTFLOAT_FAILS; for test in tests { let parse = CDDLTestParser::parse(Rule::intfloat_TEST, test); @@ -108,18 +185,8 @@ fn check_intfloat() { #[test] /// Test if the `uint` rule passes properly. fn check_hexfloat() { - let tests = vec![ - "0xabcp+123", - "-0xabcp+123", - "0xabcp-123", - "-0xabcp-123", - "0xabc.defp+123", - "-0xabc.defp+123", - "0xabc.defp-123", - "-0xabc.defp-123", - ]; - - let fails = vec![" a ", "zz", "0123zzz", "0xdog", "0b777"]; + let tests = HEXFLOAT_PASSES; + let fails = HEXFLOAT_FAILS; for test in tests { let parse = CDDLTestParser::parse(Rule::hexfloat_TEST, test); @@ -135,35 +202,8 @@ fn check_hexfloat() { #[test] /// Test if the `number` rule passes properly. fn check_number() { - let tests = vec![ - "0xabcp+123", - "-0xabcp+123", - "0xabcp-123", - "-0xabcp-123", - "0xabc.defp+123", - "-0xabc.defp+123", - "0xabc.defp-123", - "-0xabc.defp-123", - "10", - "101", - "2034", - "30456", - "123456789", - "0", - "-10", - "-101", - "-2034", - "-30456", - "-123456789", - "123.456", - "123.456", - "123e+789", - "123e-789", - "123.456e+789", - "123.456e-789", - ]; - - let fails = vec![" a ", "zz", "0123zzz", "0xdog", "0b777"]; + let tests = NUMBER_PASSES; + let fails = NUMBER_FAILS; for test in tests { let parse = CDDLTestParser::parse(Rule::number_TEST, test); @@ -179,51 +219,13 @@ fn check_number() { #[test] /// Test if the `uint` rule passes properly. fn check_value() { - let tests = vec![ - "0xabcp+123", - "-0xabcp+123", - "0xabcp-123", - "-0xabcp-123", - "0xabc.defp+123", - "-0xabc.defp+123", - "0xabc.defp-123", - "-0xabc.defp-123", - "10", - "101", - "2034", - "30456", - "123456789", - "0", - "-10", - "-101", - "-2034", - "-30456", - "-123456789", - "123.456", - "123.456", - "123e+789", - "123e-789", - "123.456e+789", - "123.456e-789", - // Ideally we would define these somewhere central and just use them where needed. - "h''", - "b64''", - "''", - "h'00'", - "h'00112233445566778899aabbccddeeff0123456789abcdef'", - "h'0 1 2 3 4 5 6 7 8 9 a b c d e f'", - "h' 0 1 2 3 4 5\r 6 7 \n 8 9 a\r\n\t b c d e f'", - "b64'abcdefghijklmnopq rstuvw yz01\t23456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ~'", - "b64'abcdefghijklmnopq rstuvw yz01\t23456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ'", - "''", - "'text\n that gets converted \\\' into a byte string...'", - // Ideally we would define these somewhere central and just use them where needed. - r#""""#, - r#""abc""#, - "\"abc\\n\"", - ]; - - let fails = vec![" a ", "zz", "0123zzz", "0xdog", "0b777"]; + let tests: Vec<_> = VALUE_PASSES.into_iter() + .chain(NUMBER_PASSES.into_iter()) + .chain(BYTES_PASSES.into_iter()) + .collect(); + let fails: Vec<_> = VALUE_FAILS.into_iter() + .chain(NUMBER_FAILS.into_iter()) + .collect(); for test in tests { let parse = CDDLTestParser::parse(Rule::value_TEST, test); From 1e7d1cecf060becf9d1d0b042c8dd1db1ed8c682 Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Wed, 17 Jan 2024 19:45:57 +0700 Subject: [PATCH 13/43] feat: initial group_elements test cases --- .../cddl-parser/src/grammar/cddl_test.pest | 3 + .../cbork/cddl-parser/tests/group_elements.rs | 87 +++++++++++++++++-- 2 files changed, 82 insertions(+), 8 deletions(-) diff --git a/hermes/crates/cbork/cddl-parser/src/grammar/cddl_test.pest b/hermes/crates/cbork/cddl-parser/src/grammar/cddl_test.pest index 1dcb02a81..26fcd3a12 100644 --- a/hermes/crates/cbork/cddl-parser/src/grammar/cddl_test.pest +++ b/hermes/crates/cbork/cddl-parser/src/grammar/cddl_test.pest @@ -20,6 +20,9 @@ memberkey_TEST = { SOI ~ memberkey ~ EOI } /// Test Expression for the `bareword` Rule. bareword_TEST = { SOI ~ bareword ~ EOI } +/// Test Expression for the `optcom` Rule. +optcom_TEST = { SOI ~ optcom ~ EOI } + /// Test Expression for the `occur` Rule. occur_TEST = { SOI ~ occur ~ EOI } diff --git a/hermes/crates/cbork/cddl-parser/tests/group_elements.rs b/hermes/crates/cbork/cddl-parser/tests/group_elements.rs index 7ce1ebf65..de1cdb020 100644 --- a/hermes/crates/cbork/cddl-parser/tests/group_elements.rs +++ b/hermes/crates/cbork/cddl-parser/tests/group_elements.rs @@ -43,6 +43,19 @@ pub const OCCUR_FAILS: &[&str] = &[ "0b", ]; +pub const OPTCOM_PASSES: &[&str] = &[ + "", + ",", + " ,", + " , ", + "\n,\n", + "\n", +]; + +pub const OPTCOM_FAILS: &[&str] = &[ + ",,", +]; + pub const MEMBERKEY_PASSES: &[&str] = &[ // bareword "foo:", @@ -62,34 +75,74 @@ pub const MEMBERKEY_PASSES: &[&str] = &[ // type1 "tstr =>", + "id =>", + "# =>", + "1..2 =>", + "1...2 =>", + "\"foo\" =>", + "\"foo\" ^=>", + "\"foo\"^ =>", + "\"foo\" ^ =>", + "1 =>", + "0x123 =>", + "1.1 =>", + "-1 =>", + "b64'1234' =>", + "h'1234' =>", + "h'12 34\n' =>", ]; pub const MEMBERKEY_FAILS: &[&str] = &[ - + "#:", + "foo::", ]; pub const GRPENT_PASSES: &[&str] = &[ - + "foo: 1", + "foo: 1", + "foo-bar:\t\n1", + "foo :\n1", + "foo: #", + "tstr => any", + "tstr => { foo: bar }", + "tstr => { foo: bar, baz }", + "tstr => [foo: bar, baz]", ]; pub const GRPENT_FAILS: &[&str] = &[ - + "tstr => (foo: bar)", ]; pub const GRPCHOICE_PASSES: &[&str] = &[ - + "foo: 1", + "foo: 1, bar: 2", + "foo: 1, bar: 2,", + "foo: 1\nbar: 2", + "foo: 1 bar: 2", + "foo => 1 bar: 2", + "foo => 1, bar => 2", + "foo => 1, bar: 2", + "foo => 1bar: 2", ]; pub const GRPCHOICE_FAILS: &[&str] = &[ - + "foo: ,", + "foo:", + "foo: bar: 2", + "foo => bar: 2", ]; pub const GROUP_PASSES: &[&str] = &[ - + "(foo: 1)", + "(foo: 1) // (bar: 2)", + "(foo: 1) // (bar: 2)", + "(street: tstr, ? number: uint, city // po-box: uint, city // per-pickup: true)", + "(+ a // b / c)", + "((+ a) // (b / c))", ]; pub const GROUP_FAILS: &[&str] = &[ - + "(foo: 1) / (bar: 2)", ]; #[test] @@ -114,7 +167,7 @@ fn check_occur() { /// Test if the `bareword` rule passes properly. /// This uses a special rule in the Grammar to test `bareword` exhaustively. fn check_bareword() { - let tests = ID_PASSES; + let tests: &[&str] = ID_PASSES; let fails = ID_FAILS; for test in tests { @@ -128,6 +181,24 @@ fn check_bareword() { } } +#[test] +/// Test if the `optcom` rule passes properly. +/// This uses a special rule in the Grammar to test `optcom` exhaustively. +fn check_optcom() { + let tests = OPTCOM_PASSES; + let fails = OPTCOM_FAILS; + + for test in tests { + let parse = CDDLTestParser::parse(Rule::optcom_TEST, test); + assert!(parse.is_ok()); + } + + for test in fails { + let parse = CDDLTestParser::parse(Rule::optcom_TEST, test); + assert!(parse.is_err()); + } +} + #[test] /// Test if the `memberkey` rule passes properly. /// This uses a special rule in the Grammar to test `memberkey` exhaustively. From 8e1cc71969e273cc4d46eda05215e6f461a8f65c Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Wed, 17 Jan 2024 22:18:37 +0700 Subject: [PATCH 14/43] fix: put atomic test back --- .../cddl-parser/src/grammar/cddl_test.pest | 34 +++++++++---------- .../cbork/cddl-parser/tests/group_elements.rs | 8 ++--- 2 files changed, 19 insertions(+), 23 deletions(-) diff --git a/hermes/crates/cbork/cddl-parser/src/grammar/cddl_test.pest b/hermes/crates/cbork/cddl-parser/src/grammar/cddl_test.pest index 26fcd3a12..92cc4588d 100644 --- a/hermes/crates/cbork/cddl-parser/src/grammar/cddl_test.pest +++ b/hermes/crates/cbork/cddl-parser/src/grammar/cddl_test.pest @@ -6,55 +6,55 @@ // cspell: words intfloat hexfloat /// Test Expression for the `group` Rule. -group_TEST = { SOI ~ group ~ EOI } +group_TEST = ${ SOI ~ group ~ EOI } /// Test Expression for the `grpchoice` Rule. -grpchoice_TEST = { SOI ~ grpchoice ~ EOI } +grpchoice_TEST = ${ SOI ~ grpchoice ~ EOI } /// Test Expression for the `grpent` Rule. -grpent_TEST = { SOI ~ grpent ~ EOI } +grpent_TEST = ${ SOI ~ grpent ~ EOI } /// Test Expression for the `memberkey` Rule. -memberkey_TEST = { SOI ~ memberkey ~ EOI } +memberkey_TEST = ${ SOI ~ memberkey ~ EOI } /// Test Expression for the `bareword` Rule. -bareword_TEST = { SOI ~ bareword ~ EOI } +bareword_TEST = ${ SOI ~ bareword ~ EOI } /// Test Expression for the `optcom` Rule. -optcom_TEST = { SOI ~ optcom ~ EOI } +optcom_TEST = ${ SOI ~ optcom ~ EOI } /// Test Expression for the `occur` Rule. -occur_TEST = { SOI ~ occur ~ EOI } +occur_TEST = ${ SOI ~ occur ~ EOI } /// Test Expression for the `S` Rule. -S_TEST = { SOI ~ S ~ EOI } +S_TEST = ${ SOI ~ S ~ EOI } /// Test Expression for the `COMMENT` Rule. COMMENT_TEST = { SOI ~ COMMENT* ~ EOI } /// Test expression to the `id` Rule. -id_TEST = { SOI ~ id ~ EOI} +id_TEST = ${ SOI ~ id ~ EOI} /// Test expression to the `bytes` Rule. -bytes_TEST = { SOI ~ bytes ~ EOI} +bytes_TEST = ${ SOI ~ bytes ~ EOI} /// Test expression to the `text` Rule. -text_TEST = { SOI ~ text ~ EOI} +text_TEST = ${ SOI ~ text ~ EOI} /// Test expression to the `uint` Rule. -uint_TEST = { SOI ~ uint ~ EOI} +uint_TEST = ${ SOI ~ uint ~ EOI} /// Test expression to the `int` Rule. -int_TEST = { SOI ~ int ~ EOI} +int_TEST = ${ SOI ~ int ~ EOI} /// Test expression to the `intfloat` Rule. -intfloat_TEST = { SOI ~ intfloat ~ EOI} +intfloat_TEST = ${ SOI ~ intfloat ~ EOI} /// Test expression to the `hexfloat` Rule. -hexfloat_TEST = { SOI ~ hexfloat ~ EOI} +hexfloat_TEST = ${ SOI ~ hexfloat ~ EOI} /// Test expression to the `number` Rule. -number_TEST = { SOI ~ number ~ EOI} +number_TEST = ${ SOI ~ number ~ EOI} /// Test expression to the `value` Rule. -value_TEST = { SOI ~ value ~ EOI} \ No newline at end of file +value_TEST = ${ SOI ~ value ~ EOI} \ No newline at end of file diff --git a/hermes/crates/cbork/cddl-parser/tests/group_elements.rs b/hermes/crates/cbork/cddl-parser/tests/group_elements.rs index de1cdb020..3e9f648cc 100644 --- a/hermes/crates/cbork/cddl-parser/tests/group_elements.rs +++ b/hermes/crates/cbork/cddl-parser/tests/group_elements.rs @@ -11,8 +11,6 @@ pub const OCCUR_PASSES: &[&str] = &[ "+", "?", "5*10", - "5 * 10", - "5\t\n*\n10", "0x1*0b110", "*20", "5*10", @@ -26,11 +24,9 @@ pub const OCCUR_PASSES: &[&str] = &[ pub const OCCUR_FAILS: &[&str] = &[ "5**10", - - // Fail cases for "+" + "5 * 10", + "5\t\n*\n10", "++", - - // Fail cases for "?" "??", // Fail cases for uint From 7c9fa066b7d648972318fbc48eb6d0a809b1dd92 Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Thu, 18 Jan 2024 18:07:53 +0700 Subject: [PATCH 15/43] revert: deleted grammar and tests --- .../cddl-parser/src/grammar/cddl_test.pest | 3 + .../cddl-parser/src/grammar/rfc_8610.pest | 27 +++++++-- .../cbork/cddl-parser/tests/byte_sequences.rs | 55 +++++++++++++++++++ 3 files changed, 81 insertions(+), 4 deletions(-) diff --git a/hermes/crates/cbork/cddl-parser/src/grammar/cddl_test.pest b/hermes/crates/cbork/cddl-parser/src/grammar/cddl_test.pest index 92cc4588d..b72bf5d1d 100644 --- a/hermes/crates/cbork/cddl-parser/src/grammar/cddl_test.pest +++ b/hermes/crates/cbork/cddl-parser/src/grammar/cddl_test.pest @@ -32,6 +32,9 @@ S_TEST = ${ SOI ~ S ~ EOI } /// Test Expression for the `COMMENT` Rule. COMMENT_TEST = { SOI ~ COMMENT* ~ EOI } +/// Test expression for the URL_BASE64 Rule. +URL_BASE64_TEST = { SOI ~ URL_BASE64 ~ EOI } + /// Test expression to the `id` Rule. id_TEST = ${ SOI ~ id ~ EOI} diff --git a/hermes/crates/cbork/cddl-parser/src/grammar/rfc_8610.pest b/hermes/crates/cbork/cddl-parser/src/grammar/rfc_8610.pest index 385b34a7d..98413862a 100644 --- a/hermes/crates/cbork/cddl-parser/src/grammar/rfc_8610.pest +++ b/hermes/crates/cbork/cddl-parser/src/grammar/rfc_8610.pest @@ -115,14 +115,29 @@ uint = ${ text = ${ "\"" ~ SCHAR* ~ "\"" } /// Literal Bytes. -bytes = ${ bsqual? ~ "'" ~ BCHAR* ~ "'" } -bsqual = { "h" | "b64" } +bytes = ${ bytes_hex | bytes_b64 | bytes_text } +bytes_hex = ${ "h" ~ "'" ~ HEX_PAIR* ~ "'" } +bytes_b64 = ${ "b64" ~ "'" ~ URL_BASE64 ~ "'" } +bytes_text = ${ "'" ~ BCHAR* ~ "'" } // ----------------------------------------------------------------------------- // Simple multiple character sequences /// identifier, called the `name` in the CDDL spec. -id = ${ NAME_START ~ (("-" | ".")* ~ NAME_END)* } +id = ${ group_socket | type_socket | name } +/// Special form of a name that represents a Group Socket. +group_socket = ${ "$$" ~ ( ( "-" | "." )* ~ NAME_END )* } +/// Special form of a name that represents a Type Socket. +type_socket = ${ "$" ~ ( ( "-" | "." )* ~ NAME_END )* } +/// General form of a name. +name = ${ NAME_START ~ ( ( "-" | "." )* ~ NAME_END )* } + +/// A pair of hex digits. (Must always have even numbers of hex digits.) +HEX_PAIR = _{ S ~ ASCII_HEX_DIGIT ~ S ~ ASCII_HEX_DIGIT ~ S } + +/// Whitespace is allowed and is ignored. +/// This token will keep the whitespace, so it will need to handled when converted to binary. +URL_BASE64 = _{ S ~ ( URL_BASE64_ALPHA ~ S)* ~ URL_BASE64_PAD? } // ----------------------------------------------------------------------------- @@ -132,6 +147,10 @@ S = _{ WHITESPACE* } WHITESPACE = _{ " " | "\t" | COMMENT | NEWLINE } COMMENT = _{ ";" ~ (PCHAR | "\t")* ~ NEWLINE } +URL_BASE64_ALPHA = _{ ASCII_ALPHA | ASCII_DIGIT | "-" | "_" } +// Optional Padding that goes at the end of Base64. +URL_BASE64_PAD = _{ "~" } + // Identifier Name Character sets. /// A name can start with an alphabetic character (including "@", "_", "$") @@ -149,7 +168,7 @@ PCHAR = _{ ASCII_VISIBLE | UNICODE_CHAR } SCHAR = _{ SCHAR_ASCII_VISIBLE | UNICODE_CHAR | SESC } /// The set of characters valid for a byte string. -BCHAR = _{ '\u{20}'..'\u{26}' | '\u{28}'..'\u{5B}' | '\u{5D}'..'\u{10FFFD}' | SESC | NEWLINE } +BCHAR = _{ BCHAR_ASCII_VISIBLE | UNICODE_CHAR | SESC | NEWLINE } /// Escaping code to allow invalid characters to be used in text or byte strings. SESC = ${ "\\" ~ (ASCII_VISIBLE | UNICODE_CHAR) } diff --git a/hermes/crates/cbork/cddl-parser/tests/byte_sequences.rs b/hermes/crates/cbork/cddl-parser/tests/byte_sequences.rs index 55fd00a0b..c14e062f5 100644 --- a/hermes/crates/cbork/cddl-parser/tests/byte_sequences.rs +++ b/hermes/crates/cbork/cddl-parser/tests/byte_sequences.rs @@ -5,6 +5,27 @@ use cddl_parser::{ cddl_test::{CDDLTestParser, Parser, Rule}, }; +pub const HEXPAIR_PASSES: &[&str] = &[ + "00", "ab", "de", "0f", "f0" +]; + +pub const HEXPAIR_FAILS: &[&str] = &[ + "0", " 0", "0 ", "az", "0p" +]; + +pub const URL_BASE64_PASSES: &[&str] = &[ + "abcdefghijklmnopq rstuvw yz01\t23456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ~", + "abcdefghijklmnopqrstuvwyz0123456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ", +]; + +pub const URL_BASE64_FAILS: &[&str] = &[ + "abcdefghijklmnopq # rstuvw yz01\t23456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ~ ", + "abcdefghijklmnopq $ rstuvw yz01\t23456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ~\t", + "abcdefghijklmnopq % rstuvw yz01\t23456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ~\n", + "abcdefghijklmnopq ^ rstuvw yz01\t23456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ~\r", + "abcdefghijklmnopq & rstuvw yz01\t23456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ~\r\n", +]; + pub const BYTES_PASSES: &[&str] = &[ "h''", "b64''", @@ -30,6 +51,40 @@ pub const BYTES_FAILS: &[&str] = &[ "'\u{7}'", ]; +#[test] +/// Test if the `HEX_PAIR` rule passes properly. +fn check_hexpair() { + let hex_pairs = HEXPAIR_PASSES; + let not_hex_pairs = HEXPAIR_FAILS; + + for hp in hex_pairs { + let parse = CDDLTestParser::parse(Rule::HEX_PAIR, hp); + assert!(parse.is_ok()); + } + + for hp in not_hex_pairs { + let parse = CDDLTestParser::parse(Rule::HEX_PAIR, hp); + assert!(parse.is_err()); + } +} + +#[test] +/// Test if the `URL_BASE64` rule passes properly. +fn check_url_base64() { + let tests = URL_BASE64_PASSES; + let fails = URL_BASE64_FAILS; + + for test in tests { + let parse = CDDLTestParser::parse(Rule::URL_BASE64_TEST, test); + assert!(parse.is_ok()); + } + + for test in fails { + let parse = CDDLTestParser::parse(Rule::URL_BASE64_TEST, test); + assert!(parse.is_err()); + } +} + #[test] /// Test if the `bytes` rule passes properly. fn check_bytes() { From f0cf1e606b842bc615109dfe29e4c298950db255 Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Fri, 19 Jan 2024 20:54:21 +0700 Subject: [PATCH 16/43] fix: correct test cases --- .../cbork/cddl-parser/src/grammar/rfc_8610.pest | 6 +++--- .../cbork/cddl-parser/tests/byte_sequences.rs | 16 ++++++++++------ .../cbork/cddl-parser/tests/character_sets.rs | 2 +- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/hermes/crates/cbork/cddl-parser/src/grammar/rfc_8610.pest b/hermes/crates/cbork/cddl-parser/src/grammar/rfc_8610.pest index 98413862a..f686d3eb3 100644 --- a/hermes/crates/cbork/cddl-parser/src/grammar/rfc_8610.pest +++ b/hermes/crates/cbork/cddl-parser/src/grammar/rfc_8610.pest @@ -143,9 +143,9 @@ URL_BASE64 = _{ S ~ ( URL_BASE64_ALPHA ~ S)* ~ URL_BASE64_PAD? } // ----------------------------------------------------------------------------- // Characters, Whitespace and Comments -S = _{ WHITESPACE* } -WHITESPACE = _{ " " | "\t" | COMMENT | NEWLINE } -COMMENT = _{ ";" ~ (PCHAR | "\t")* ~ NEWLINE } +S = _{ (COMMENT | WHITESPACE)* } +WHITESPACE = _{ " " | "\t" | NEWLINE } +COMMENT = { ";" ~ (PCHAR | "\t")* ~ NEWLINE } URL_BASE64_ALPHA = _{ ASCII_ALPHA | ASCII_DIGIT | "-" | "_" } // Optional Padding that goes at the end of Base64. diff --git a/hermes/crates/cbork/cddl-parser/tests/byte_sequences.rs b/hermes/crates/cbork/cddl-parser/tests/byte_sequences.rs index c14e062f5..edadbce16 100644 --- a/hermes/crates/cbork/cddl-parser/tests/byte_sequences.rs +++ b/hermes/crates/cbork/cddl-parser/tests/byte_sequences.rs @@ -31,11 +31,15 @@ pub const BYTES_PASSES: &[&str] = &[ "b64''", "''", "h'00'", + "h'63666F6FF6'", + "h'68656c6c6f20776f726c64'", + "h'4 86 56c 6c6f'", + "h' 20776 f726c64'", "h'00112233445566778899aabbccddeeff0123456789abcdef'", - "h'001'", "h'0 1 2 3 4 5 6 7 8 9 a b c d e f'", + "h' 0 1 2 3 4 5\r 6 7 \n 8 9 a\r\n\t b c d e f'", "h'0 \n\n\r f'", - "''", + "b64'aHR0cHM6Ly93d3cuZXhhbXBsZS5jb20vcGFnZT9wYXJhbTE9dmFsdWUxJnBhcmFtMj12YWx1ZTI~'", "'text\n that gets converted \\\' into a byte string...'", ]; @@ -44,10 +48,10 @@ pub const BYTES_FAILS: &[&str] = &[ "b64", "\"\"", "h ''", - "h '0 \t f'", - "h' 0 1 2 3 4 5\r 6 7 \n 8 9 a\r\n\t b c d e f'", - "b64'abcdefghijklmnopq rstuvw yz01\t23456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ~'", - "b64'abcdefghijklmnopq rstuvw yz01\t23456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ'", + "b64 ''", + "h'001'", + "b64'abcdefghijklmnopq # rstuvw yz01\t23456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ~'", + "b64'abcdefghijklmnopq & rstuvw yz01\t23456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ'", "'\u{7}'", ]; diff --git a/hermes/crates/cbork/cddl-parser/tests/character_sets.rs b/hermes/crates/cbork/cddl-parser/tests/character_sets.rs index 28997b3ff..77b4682ec 100644 --- a/hermes/crates/cbork/cddl-parser/tests/character_sets.rs +++ b/hermes/crates/cbork/cddl-parser/tests/character_sets.rs @@ -44,7 +44,7 @@ fn check_bchar() { for x in ('\u{0}'..='\u{ff}').map(char::from) { let test = format!("{x}"); let parse = CDDLTestParser::parse(Rule::BCHAR, &test); - if !matches!(x, '\n' | '\r') && x < ' ' || matches!(x, '\t' | '\'' | '\\') { + if !matches!(x, '\n' | '\r') && x < ' ' || matches!(x, '\t' | '\'' | '\\' | '\u{7f}') { assert!(parse.is_err()); } else { assert!(parse.is_ok()); From 3c2bbd3f5f0e359952eaf451da658b7fbf28c86c Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Mon, 22 Jan 2024 18:10:06 +0700 Subject: [PATCH 17/43] feat: type decl tests --- hermes/crates/cbork/cddl-parser/src/grammar/cddl_test.pest | 3 +++ 1 file changed, 3 insertions(+) diff --git a/hermes/crates/cbork/cddl-parser/src/grammar/cddl_test.pest b/hermes/crates/cbork/cddl-parser/src/grammar/cddl_test.pest index b72bf5d1d..5e8e226fc 100644 --- a/hermes/crates/cbork/cddl-parser/src/grammar/cddl_test.pest +++ b/hermes/crates/cbork/cddl-parser/src/grammar/cddl_test.pest @@ -5,6 +5,9 @@ // cspell: words intfloat hexfloat +/// Test Expression for the `ctlop` Rule. +ctlop_TEST = ${ SOI ~ ctlop ~ EOI } + /// Test Expression for the `group` Rule. group_TEST = ${ SOI ~ group ~ EOI } From 0682361342c84c8e49fbcd1596a24f67570b54bc Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Mon, 22 Jan 2024 18:10:16 +0700 Subject: [PATCH 18/43] feat: type decl tests --- .../cddl-parser/tests/type_declarations.rs | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 hermes/crates/cbork/cddl-parser/tests/type_declarations.rs diff --git a/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs b/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs new file mode 100644 index 000000000..5c137ba0f --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs @@ -0,0 +1,59 @@ +use cddl_parser::{ + self, + cddl_test::{CDDLTestParser, Parser, Rule}, +}; + +pub const CTLOP_PASSES: &[&str] = &[ + ".$", + ".@", + "._", + ".a", + ".z", + ".A", + ".Z", + ".$$", + ".@@", + ".__", + ".a$", + ".a@", + ".a_", + ".$0", + ".@9", + "._a", + ".abc", + ".aname", + ".@aname", + "._aname", + ".$aname", + ".a$name", + ".a.name", + ".@a.name", + ".$a.name", + "._a.name", + ".$$", + ".$$groupsocket", + ".$", + ".$typesocket", +]; + +pub const CTLOP_FAILS: &[&str] = &[ + "aname.", ".", "..", "aname.", "aname-", "aname%", "a%name4", "a^name5", "a name", "", +]; + +#[test] +/// Test if the `ctlop` rule passes properly. +/// This uses a special rule in the Grammar to test `ctlop` exhaustively. +fn check_ctlop() { + let passes = CTLOP_PASSES; + let fails = CTLOP_FAILS; + + for test in passes { + let parse = CDDLTestParser::parse(Rule::ctlop_TEST, test); + assert!(parse.is_ok()); + } + + for test in fails { + let parse = CDDLTestParser::parse(Rule::ctlop_TEST, test); + assert!(parse.is_err()); + } +} \ No newline at end of file From a2d738c7d8760ba6f2f8fce2639145c26f19ebae Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Mon, 22 Jan 2024 18:18:00 +0700 Subject: [PATCH 19/43] feat: setup type decl test --- .../cddl-parser/src/grammar/cddl_test.pest | 12 ++ .../cddl-parser/tests/type_declarations.rs | 104 ++++++++++++++++++ 2 files changed, 116 insertions(+) diff --git a/hermes/crates/cbork/cddl-parser/src/grammar/cddl_test.pest b/hermes/crates/cbork/cddl-parser/src/grammar/cddl_test.pest index 5e8e226fc..c6e868913 100644 --- a/hermes/crates/cbork/cddl-parser/src/grammar/cddl_test.pest +++ b/hermes/crates/cbork/cddl-parser/src/grammar/cddl_test.pest @@ -5,6 +5,18 @@ // cspell: words intfloat hexfloat +/// Test Expression for the `type` Rule. +type_TEST = ${ SOI ~ type ~ EOI } + +/// Test Expression for the `type1` Rule. +type1_TEST = ${ SOI ~ type1 ~ EOI } + +/// Test Expression for the `type2` Rule. +type2_TEST = ${ SOI ~ type2 ~ EOI } + +/// Test Expression for the `rangeop` Rule. +rangeop_TEST = ${ SOI ~ rangeop ~ EOI } + /// Test Expression for the `ctlop` Rule. ctlop_TEST = ${ SOI ~ ctlop ~ EOI } diff --git a/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs b/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs index 5c137ba0f..e3b6e5bd7 100644 --- a/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs +++ b/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs @@ -40,6 +40,38 @@ pub const CTLOP_FAILS: &[&str] = &[ "aname.", ".", "..", "aname.", "aname-", "aname%", "a%name4", "a^name5", "a name", "", ]; +pub const RANGEOP_PASSES: &[&str] = &[ + "..", "..." +]; + +pub const RANGEOP_FAILS: &[&str] = &[ + ".", "", "....", ".. .", ". .." +]; + +pub const TYPE2_PASSES: &[&str] = &[ + +]; + +pub const TYPE2_FAILS: &[&str] = &[ + +]; + +pub const TYPE1_PASSES: &[&str] = &[ + +]; + +pub const TYPE1_FAILS: &[&str] = &[ + +]; + +pub const TYPE_PASSES: &[&str] = &[ + +]; + +pub const TYPE_FAILS: &[&str] = &[ + +]; + #[test] /// Test if the `ctlop` rule passes properly. /// This uses a special rule in the Grammar to test `ctlop` exhaustively. @@ -56,4 +88,76 @@ fn check_ctlop() { let parse = CDDLTestParser::parse(Rule::ctlop_TEST, test); assert!(parse.is_err()); } +} + +#[test] +/// Test if the `rangeop` rule passes properly. +/// This uses a special rule in the Grammar to test `rangeop` exhaustively. +fn check_rangeop() { + let passes = RANGEOP_PASSES; + let fails = RANGEOP_FAILS; + + for test in passes { + let parse = CDDLTestParser::parse(Rule::rangeop_TEST, test); + assert!(parse.is_ok()); + } + + for test in fails { + let parse = CDDLTestParser::parse(Rule::rangeop_TEST, test); + assert!(parse.is_err()); + } +} + +#[test] +/// Test if the `type2` rule passes properly. +/// This uses a special rule in the Grammar to test `type2` exhaustively. +fn check_type2() { + let passes = TYPE2_PASSES; + let fails = TYPE2_FAILS; + + for test in passes { + let parse = CDDLTestParser::parse(Rule::type2_TEST, test); + assert!(parse.is_ok()); + } + + for test in fails { + let parse = CDDLTestParser::parse(Rule::type2_TEST, test); + assert!(parse.is_err()); + } +} + +#[test] +/// Test if the `type1` rule passes properly. +/// This uses a special rule in the Grammar to test `type1` exhaustively. +fn check_type1() { + let passes = TYPE1_PASSES; + let fails = TYPE1_FAILS; + + for test in passes { + let parse = CDDLTestParser::parse(Rule::type1_TEST, test); + assert!(parse.is_ok()); + } + + for test in fails { + let parse = CDDLTestParser::parse(Rule::type1_TEST, test); + assert!(parse.is_err()); + } +} + +#[test] +/// Test if the `type` rule passes properly. +/// This uses a special rule in the Grammar to test `type` exhaustively. +fn check_type() { + let passes = TYPE_PASSES; + let fails = TYPE_FAILS; + + for test in passes { + let parse = CDDLTestParser::parse(Rule::type_TEST, test); + assert!(parse.is_ok()); + } + + for test in fails { + let parse = CDDLTestParser::parse(Rule::type_TEST, test); + assert!(parse.is_err()); + } } \ No newline at end of file From a3410f6ad566f15b65ab83cf2a2e61a2ec341cfb Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Mon, 22 Jan 2024 20:15:02 +0700 Subject: [PATCH 20/43] feat: type1 test cases --- .../cddl-parser/tests/type_declarations.rs | 70 +++++++++++++++++-- 1 file changed, 66 insertions(+), 4 deletions(-) diff --git a/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs b/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs index e3b6e5bd7..bc64bd7f6 100644 --- a/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs +++ b/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs @@ -49,19 +49,60 @@ pub const RANGEOP_FAILS: &[&str] = &[ ]; pub const TYPE2_PASSES: &[&str] = &[ - + "#", + "#1", + "#1.1", + "#1.1", + "#6", + "#6.11", + "#6.11(tstr)", + "#6.11(\tstr\n)", + "#6.11({ foo })", + "#6.11([ foo ])", + "#6.11(#3.1)", + "&foo", + "& foo", + "&((+ a) // (b / c))", + "&\t( foo: bar )", + "~foo", + "~ foo", + "foo", + "[ foo bar ]", + "{ foo bar }", + "(a)", + "(a / b)", + "(#)", + "((a))", + "1", + "h'1111'", + "true", + "foo", ]; pub const TYPE2_FAILS: &[&str] = &[ - + "", + "##", + "#1.", + "#6.11 (tstr)", + "#6.11(( foo: uint ))", + "&", + "& foo ", + "(foo bar)", ]; pub const TYPE1_PASSES: &[&str] = &[ - + "1..2", + "1...2", + "0..10.0", // BAD range 1 + "0.0..10", // BAD range 2 + "0..max-byte", + "1.0..2.0", + "1.0...2.0", + "foo.bar", ]; pub const TYPE1_FAILS: &[&str] = &[ - + "" ]; pub const TYPE_PASSES: &[&str] = &[ @@ -144,6 +185,27 @@ fn check_type1() { } } +#[test] +/// Test if the `type1` rule passes properly based on composition of type2 test cases. +fn check_type1_composition() { + // type2 composition testing + for (i, test_i) in [TYPE2_PASSES, TYPE_FAILS].into_iter().flatten().enumerate() { + for (j, test_j) in [CTLOP_PASSES, RANGEOP_PASSES].into_iter().flatten().enumerate() { + for (k, test_k) in [TYPE2_PASSES, TYPE_FAILS].into_iter().flatten().enumerate() { + let input = [test_i.to_owned(), test_j.to_owned(), test_k.to_owned()].join(" "); + let parse = CDDLTestParser::parse(Rule::type1_TEST, &input); + if (0..TYPE2_PASSES.len()).contains(&i) + && (0..(CTLOP_PASSES.len() + RANGEOP_PASSES.len())).contains(&j) + && (0..TYPE2_PASSES.len()).contains(&k) { + assert!(parse.is_ok()); + } else { + assert!(parse.is_err()); + } + } + } + } +} + #[test] /// Test if the `type` rule passes properly. /// This uses a special rule in the Grammar to test `type` exhaustively. From 68e61b48b311123392b3a1a91453bd4d3c0a0cc0 Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Mon, 22 Jan 2024 20:37:04 +0700 Subject: [PATCH 21/43] feat: composition testing --- .../cddl-parser/tests/type_declarations.rs | 33 ++++++++++++++++--- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs b/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs index bc64bd7f6..995faeff5 100644 --- a/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs +++ b/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs @@ -106,11 +106,19 @@ pub const TYPE1_FAILS: &[&str] = &[ ]; pub const TYPE_PASSES: &[&str] = &[ - + "1 / 2", + "1\n/\t2", + "1 / 2 / 3 / 4", + "1 / (2 / (3 / 4))", + "# / #", ]; pub const TYPE_FAILS: &[&str] = &[ - + "", + "1 \\ 2", + "1 // 2", + "1 2", + "1 / 2 3", ]; #[test] @@ -190,12 +198,11 @@ fn check_type1() { fn check_type1_composition() { // type2 composition testing for (i, test_i) in [TYPE2_PASSES, TYPE_FAILS].into_iter().flatten().enumerate() { - for (j, test_j) in [CTLOP_PASSES, RANGEOP_PASSES].into_iter().flatten().enumerate() { + for (_, test_j) in [CTLOP_PASSES, RANGEOP_PASSES].into_iter().flatten().enumerate() { for (k, test_k) in [TYPE2_PASSES, TYPE_FAILS].into_iter().flatten().enumerate() { let input = [test_i.to_owned(), test_j.to_owned(), test_k.to_owned()].join(" "); let parse = CDDLTestParser::parse(Rule::type1_TEST, &input); if (0..TYPE2_PASSES.len()).contains(&i) - && (0..(CTLOP_PASSES.len() + RANGEOP_PASSES.len())).contains(&j) && (0..TYPE2_PASSES.len()).contains(&k) { assert!(parse.is_ok()); } else { @@ -222,4 +229,22 @@ fn check_type() { let parse = CDDLTestParser::parse(Rule::type_TEST, test); assert!(parse.is_err()); } +} + +#[test] +/// Test if the `type` rule passes properly based on composition of type2 test cases. +fn check_type_composition() { + // type2 composition testing + for (i, test_i) in [TYPE2_PASSES, TYPE_FAILS].into_iter().flatten().enumerate() { + for (j, test_j) in [TYPE2_PASSES, TYPE_FAILS].into_iter().flatten().enumerate() { + let input = [test_i.to_owned(), "/", test_j.to_owned()].join(" "); + let parse = CDDLTestParser::parse(Rule::type_TEST, &input); + + if (0..TYPE2_PASSES.len()).contains(&i) && (0..TYPE2_PASSES.len()).contains(&j) { + assert!(parse.is_ok()); + } else { + assert!(parse.is_err()); + } + } + } } \ No newline at end of file From 0c5942a62c4376d1afab2b4dd60adf92bc46983e Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Mon, 22 Jan 2024 20:58:06 +0700 Subject: [PATCH 22/43] feat: rules test cases --- .../cddl-parser/src/grammar/cddl_test.pest | 21 ++ .../cddl-parser/src/grammar/rfc_8610.pest | 4 +- .../crates/cbork/cddl-parser/tests/rules.rs | 186 ++++++++++++++++++ 3 files changed, 209 insertions(+), 2 deletions(-) create mode 100644 hermes/crates/cbork/cddl-parser/tests/rules.rs diff --git a/hermes/crates/cbork/cddl-parser/src/grammar/cddl_test.pest b/hermes/crates/cbork/cddl-parser/src/grammar/cddl_test.pest index c6e868913..c2557e02e 100644 --- a/hermes/crates/cbork/cddl-parser/src/grammar/cddl_test.pest +++ b/hermes/crates/cbork/cddl-parser/src/grammar/cddl_test.pest @@ -5,6 +5,27 @@ // cspell: words intfloat hexfloat +/// Test Expression for the `rule` Rule. +rule_TEST = ${ SOI ~ rule ~ EOI } + +/// Test Expression for the `typename` Rule. +typename_TEST = ${ SOI ~ typename ~ EOI } + +/// Test Expression for the `groupname` Rule. +groupname_TEST = ${ SOI ~ groupname ~ EOI } + +/// Test Expression for the `assignt` Rule. +assignt_TEST = ${ SOI ~ assignt ~ EOI } + +/// Test Expression for the `assigng` Rule. +assigng_TEST = ${ SOI ~ assigng ~ EOI } + +/// Test Expression for the `genericparm` Rule. +genericparm_TEST = ${ SOI ~ genericparm ~ EOI } + +/// Test Expression for the `genericarg` Rule. +genericarg_TEST = ${ SOI ~ genericarg ~ EOI } + /// Test Expression for the `type` Rule. type_TEST = ${ SOI ~ type ~ EOI } diff --git a/hermes/crates/cbork/cddl-parser/src/grammar/rfc_8610.pest b/hermes/crates/cbork/cddl-parser/src/grammar/rfc_8610.pest index f686d3eb3..361144be6 100644 --- a/hermes/crates/cbork/cddl-parser/src/grammar/rfc_8610.pest +++ b/hermes/crates/cbork/cddl-parser/src/grammar/rfc_8610.pest @@ -14,8 +14,8 @@ cddl = ${ // ----------------------------------------------------------------------------- // Rules rule = ${ - (typename ~ S ~ assignt ~ S ~ type) - | (groupname ~ S ~ assigng ~ S ~ grpent) + (typename ~ genericparm? ~ S ~ assignt ~ S ~ type) + | (groupname ~ genericparm? ~ S ~ assigng ~ S ~ grpent) } typename = { id } diff --git a/hermes/crates/cbork/cddl-parser/tests/rules.rs b/hermes/crates/cbork/cddl-parser/tests/rules.rs new file mode 100644 index 000000000..26be571f1 --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/rules.rs @@ -0,0 +1,186 @@ +use cddl_parser::{ + self, + cddl_test::{CDDLTestParser, Parser, Rule}, +}; + +pub const GENERICARG_PASSES: &[&str] = &[ + +]; + +pub const GENERICARG_FAILS: &[&str] = &[ + +]; + +pub const GENERICPARM_PASSES: &[&str] = &[ + +]; + +pub const GENERICPARM_FAILS: &[&str] = &[ + +]; + +pub const ASSIGNG_PASSES: &[&str] = &[ + +]; + +pub const ASSIGNG_FAILS: &[&str] = &[ + +]; + +pub const ASSIGNT_PASSES: &[&str] = &[ + +]; + +pub const ASSIGNT_FAILS: &[&str] = &[ + +]; + +pub const TYPENAME_PASSES: &[&str] = &[ + +]; + +pub const TYPENAME_FAILS: &[&str] = &[ + +]; + +pub const GROUPNAME_PASSES: &[&str] = &[ + +]; + +pub const GROUPNAME_FAILS: &[&str] = &[ + +]; + +pub const RULE_PASSES: &[&str] = &[ + +]; + +pub const RULE_FAILS: &[&str] = &[ + +]; + +#[test] +/// Test if the `genericarg` rule passes properly. +/// This uses a special rule in the Grammar to test `genericarg` exhaustively. +fn check_genericarg() { + let tests = GENERICARG_PASSES; + let fails = GENERICARG_FAILS; + + for test in tests { + let parse = CDDLTestParser::parse(Rule::genericarg_TEST, test); + assert!(parse.is_ok()); + } + + for test in fails { + let parse = CDDLTestParser::parse(Rule::genericarg_TEST, test); + assert!(parse.is_err()); + } +} + +#[test] +/// Test if the `genericparm` rule passes properly. +/// This uses a special rule in the Grammar to test `genericparm` exhaustively. +fn check_genericparm() { + let tests = GENERICPARM_PASSES; + let fails = GENERICPARM_FAILS; + + for test in tests { + let parse = CDDLTestParser::parse(Rule::genericparm_TEST, test); + assert!(parse.is_ok()); + } + + for test in fails { + let parse = CDDLTestParser::parse(Rule::occur_TEST, test); + assert!(parse.is_err()); + } +} + +#[test] +/// Test if the `assigng` rule passes properly. +/// This uses a special rule in the Grammar to test `assigng` exhaustively. +fn check_assigng() { + let tests = ASSIGNG_PASSES; + let fails = ASSIGNG_FAILS; + + for test in tests { + let parse = CDDLTestParser::parse(Rule::assigng_TEST, test); + assert!(parse.is_ok()); + } + + for test in fails { + let parse = CDDLTestParser::parse(Rule::assigng_TEST, test); + assert!(parse.is_err()); + } +} + +#[test] +/// Test if the `assignt` rule passes properly. +/// This uses a special rule in the Grammar to test `assignt` exhaustively. +fn check_assignt() { + let tests = ASSIGNT_PASSES; + let fails = ASSIGNT_FAILS; + + for test in tests { + let parse = CDDLTestParser::parse(Rule::assignt_TEST, test); + assert!(parse.is_ok()); + } + + for test in fails { + let parse = CDDLTestParser::parse(Rule::assignt_TEST, test); + assert!(parse.is_err()); + } +} + +#[test] +/// Test if the `typename` rule passes properly. +/// This uses a special rule in the Grammar to test `typename` exhaustively. +fn check_typename() { + let tests = TYPENAME_PASSES; + let fails = TYPENAME_FAILS; + + for test in tests { + let parse = CDDLTestParser::parse(Rule::typename_TEST, test); + assert!(parse.is_ok()); + } + + for test in fails { + let parse = CDDLTestParser::parse(Rule::typename_TEST, test); + assert!(parse.is_err()); + } +} + +#[test] +/// Test if the `groupname` rule passes properly. +/// This uses a special rule in the Grammar to test `groupname` exhaustively. +fn check_groupname() { + let tests = GROUPNAME_PASSES; + let fails = GROUPNAME_FAILS; + + for test in tests { + let parse = CDDLTestParser::parse(Rule::groupname_TEST, test); + assert!(parse.is_ok()); + } + + for test in fails { + let parse = CDDLTestParser::parse(Rule::groupname_TEST, test); + assert!(parse.is_err()); + } +} + +#[test] +/// Test if the `rule` rule passes properly. +/// This uses a special rule in the Grammar to test `rule` exhaustively. +fn check_rule() { + let tests = RULE_PASSES; + let fails = RULE_FAILS; + + for test in tests { + let parse = CDDLTestParser::parse(Rule::rule_TEST, test); + assert!(parse.is_ok()); + } + + for test in fails { + let parse = CDDLTestParser::parse(Rule::rule_TEST, test); + assert!(parse.is_err()); + } +} \ No newline at end of file From 0754589c2567dddc7e5984dbc5bee2e4f1cf585b Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Tue, 23 Jan 2024 14:45:39 +0700 Subject: [PATCH 23/43] feat: rules test cases --- .../crates/cbork/cddl-parser/tests/rules.rs | 58 ++++++++++++------- .../cddl-parser/tests/type_declarations.rs | 5 +- 2 files changed, 42 insertions(+), 21 deletions(-) diff --git a/hermes/crates/cbork/cddl-parser/tests/rules.rs b/hermes/crates/cbork/cddl-parser/tests/rules.rs index 26be571f1..37bbd794b 100644 --- a/hermes/crates/cbork/cddl-parser/tests/rules.rs +++ b/hermes/crates/cbork/cddl-parser/tests/rules.rs @@ -3,53 +3,71 @@ use cddl_parser::{ cddl_test::{CDDLTestParser, Parser, Rule}, }; -pub const GENERICARG_PASSES: &[&str] = &[ +mod identifiers; +use identifiers::{ID_PASSES, ID_FAILS}; +pub const GENERICARG_PASSES: &[&str] = &[ + "", + "<{ foo: bar }>", + "<{ h'1234': uint }>", + "<1...10>", + "<\n1...10\t>", + "<{ foo: bar }, { foo: baz }>", + "<{ foo: bar }, 1..10>", ]; pub const GENERICARG_FAILS: &[&str] = &[ - + "", + "<>", + "", + "<( foo: bar )>", + "", ]; pub const GENERICPARM_PASSES: &[&str] = &[ - + "", + "", + "", + "", ]; pub const GENERICPARM_FAILS: &[&str] = &[ - + "", + "<>", + "", + "<{ foo: bar }>", + "<{ h'1234': uint }>", + "<1...10>", + "<\n1...10\t>", ]; pub const ASSIGNG_PASSES: &[&str] = &[ - + "=", + "//=" ]; pub const ASSIGNG_FAILS: &[&str] = &[ - + "==", + "/=", ]; pub const ASSIGNT_PASSES: &[&str] = &[ - + "=", + "/=" ]; pub const ASSIGNT_FAILS: &[&str] = &[ - + "==", + "//=" ]; -pub const TYPENAME_PASSES: &[&str] = &[ +pub const TYPENAME_PASSES: &[&str] = &ID_PASSES; -]; - -pub const TYPENAME_FAILS: &[&str] = &[ - -]; +pub const TYPENAME_FAILS: &[&str] = &ID_FAILS; -pub const GROUPNAME_PASSES: &[&str] = &[ +pub const GROUPNAME_PASSES: &[&str] = &ID_PASSES; -]; - -pub const GROUPNAME_FAILS: &[&str] = &[ - -]; +pub const GROUPNAME_FAILS: &[&str] = &ID_FAILS; pub const RULE_PASSES: &[&str] = &[ diff --git a/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs b/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs index 995faeff5..96bc4a032 100644 --- a/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs +++ b/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs @@ -92,17 +92,20 @@ pub const TYPE2_FAILS: &[&str] = &[ pub const TYPE1_PASSES: &[&str] = &[ "1..2", + "1 .. 2", + "1\t..\n2", "1...2", "0..10.0", // BAD range 1 "0.0..10", // BAD range 2 "0..max-byte", + "min-type..max-byte", "1.0..2.0", "1.0...2.0", "foo.bar", ]; pub const TYPE1_FAILS: &[&str] = &[ - "" + "", ]; pub const TYPE_PASSES: &[&str] = &[ From 58ec3ac3fee9c7fd3be56c81b0383cc77365010c Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Tue, 23 Jan 2024 20:20:16 +0700 Subject: [PATCH 24/43] feat: all examples from rfc8610 --- .../tests/cddl/valid_rfc8610_ctrl_bits.cddl | 17 ++++++++++++ .../tests/cddl/valid_rfc8610_ctrl_ord.cddl | 8 ++++++ .../tests/cddl/valid_rfc8610_ctrl_regexp.cddl | 5 ++++ .../tests/cddl/valid_rfc8610_ctrl_size.cddl | 8 ++++++ .../cddl/valid_rfc8610_ctrl_within_and.cddl | 9 +++++++ .../tests/cddl/valid_rfc8610_generics.cddl | 4 +++ .../cddl/valid_rfc8610_op_precedence.cddl | 21 +++++++++++++++ ...ddl => valid_rfc8610_reputon_compact.cddl} | 0 ...1.cddl => valid_rfc8610_reputon_full.cddl} | 0 .../tests/cddl/valid_rfc8610_socket_plug.cddl | 15 +++++++++++ .../cddl/valid_rfc8610_socket_plug_ext.cddl | 26 ++++++++++++++++++ .../tests/cddl/valid_rfc8610_tags.cddl | 7 +++++ .../tests/cddl/valid_rfc8610_unwrapping.cddl | 27 +++++++++++++++++++ .../cddl/valid_rfc8610_unwrapping_alt.cddl | 12 +++++++++ 14 files changed, 159 insertions(+) create mode 100644 hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_ctrl_bits.cddl create mode 100644 hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_ctrl_ord.cddl create mode 100644 hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_ctrl_regexp.cddl create mode 100644 hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_ctrl_size.cddl create mode 100644 hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_ctrl_within_and.cddl create mode 100644 hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_generics.cddl create mode 100644 hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_op_precedence.cddl rename hermes/crates/cbork/cddl-parser/tests/cddl/{valid_rfc8610_example_7071_compact.cddl => valid_rfc8610_reputon_compact.cddl} (100%) rename hermes/crates/cbork/cddl-parser/tests/cddl/{valid_rfc8610_example_7071.cddl => valid_rfc8610_reputon_full.cddl} (100%) create mode 100644 hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_socket_plug.cddl create mode 100644 hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_socket_plug_ext.cddl create mode 100644 hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_tags.cddl create mode 100644 hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_unwrapping.cddl create mode 100644 hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_unwrapping_alt.cddl diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_ctrl_bits.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_ctrl_bits.cddl new file mode 100644 index 000000000..b44d5bc73 --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_ctrl_bits.cddl @@ -0,0 +1,17 @@ +; 3.8.2. Control Operator .bits + +tcpflagbytes = bstr .bits flags +flags = &( + fin: 8, + syn: 9, + rst: 10, + psh: 11, + ack: 12, + urg: 13, + ece: 14, + cwr: 15, + ns: 0, +) / (4..7) ; data offset bits + +rwxbits = uint .bits rwx +rwx = &(r: 2, w: 1, x: 0) \ No newline at end of file diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_ctrl_ord.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_ctrl_ord.cddl new file mode 100644 index 000000000..3cc737c91 --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_ctrl_ord.cddl @@ -0,0 +1,8 @@ +; 3.8.6. Control Operators .lt, .le, .gt, .ge, .eq, .ne, and .default + +speed = number .ge 0 ; unit: m/s + +timer = { + time: uint, + ? displayed-step: (number .gt 0) .default 1 +} \ No newline at end of file diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_ctrl_regexp.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_ctrl_regexp.cddl new file mode 100644 index 000000000..a2293acd1 --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_ctrl_regexp.cddl @@ -0,0 +1,5 @@ +; 3.8.3. Control Operator .regexp + +nai = tstr .regexp "[A-Za-z0-9]+@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)+" + +; "N1@CH57HF.4Znqe0.dYJRN.igjf" should pass this regexp \ No newline at end of file diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_ctrl_size.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_ctrl_size.cddl new file mode 100644 index 000000000..6a2b27601 --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_ctrl_size.cddl @@ -0,0 +1,8 @@ +; 3.8.1. Control Operator .size + +full-address = [[+ label], ip4, ip6] +ip4 = bstr .size 4 +ip6 = bstr .size 16 +label = bstr .size (1..63) + +audio_sample = uint .size 3 ; 24-bit, equivalent to 0...16777216 \ No newline at end of file diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_ctrl_within_and.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_ctrl_within_and.cddl new file mode 100644 index 000000000..d34003df0 --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_ctrl_within_and.cddl @@ -0,0 +1,9 @@ +; 3.8.5. Control Operators .within and .and + +message = $message .within message-structure +message-structure = [message_type, *message_option] +message_type = 0..255 +message_option = any + +$message /= [3, dough: text, topping: [* text]] +$message /= [4, noodles: text, sauce: text, parmesan: bool] \ No newline at end of file diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_generics.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_generics.cddl new file mode 100644 index 000000000..a541cd121 --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_generics.cddl @@ -0,0 +1,4 @@ +; 3.10. Generics + +messages = message<"reboot", "now"> / message<"sleep", 1..100> +message = {type: t, value: v} \ No newline at end of file diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_op_precedence.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_op_precedence.cddl new file mode 100644 index 000000000..39c494fc8 --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_op_precedence.cddl @@ -0,0 +1,21 @@ +; 3.11. Operator Precedence + +t = [group1] +group1 = (a / b // c / d) +a = 1 b = 2 c = 3 d = 4 + +t = {group2} +group2 = (? ab: a / b // cd: c / d) +a = 1 b = 2 c = 3 d = 4 + +t = [group3] +group3 = (+ a / b / c) +a = 1 b = 2 c = 3 + +t = [group4] +group4 = (+ a // b / c) +a = 1 b = 2 c = 3 + +t = [group4a] +group4a = ((+ a) // (b / c)) +a = 1 b = 2 c = 3 \ No newline at end of file diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_example_7071_compact.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_reputon_compact.cddl similarity index 100% rename from hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_example_7071_compact.cddl rename to hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_reputon_compact.cddl diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_example_7071.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_reputon_full.cddl similarity index 100% rename from hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_example_7071.cddl rename to hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_reputon_full.cddl diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_socket_plug.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_socket_plug.cddl new file mode 100644 index 000000000..e42eac779 --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_socket_plug.cddl @@ -0,0 +1,15 @@ +; 3.9. Socket/Plug + +tcp-header = {seq: uint, ack: uint, * $$tcp-option} + +; later, in a different file + +$$tcp-option //= ( +sack: [+(left: uint, right: uint)] +) + +; and, maybe in another file + +$$tcp-option //= ( + sack-permitted: true +) \ No newline at end of file diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_socket_plug_ext.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_socket_plug_ext.cddl new file mode 100644 index 000000000..8725ca015 --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_socket_plug_ext.cddl @@ -0,0 +1,26 @@ +; 3.9. Socket/Plug + +PersonalData = { + ? displayName: tstr, + NameComponents, + ? age: uint, + * $$personaldata-extensions +} + +NameComponents = ( + ? firstName: tstr, + ? familyName: tstr, +) + +; The above already works as is. +; But then, we can add later: + +$$personaldata-extensions //= ( + favorite-salsa: tstr, +) + +; and again, somewhere else: + +$$personaldata-extensions //= ( + shoesize: uint, +) \ No newline at end of file diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_tags.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_tags.cddl new file mode 100644 index 000000000..ad55a45d4 --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_tags.cddl @@ -0,0 +1,7 @@ +; 3.6. Tags + +biguint = #6.2(bstr) + +buuid = #6.37(bstr) + +my_uri = #6.32(tstr) / tstr \ No newline at end of file diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_unwrapping.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_unwrapping.cddl new file mode 100644 index 000000000..0d7fe9aaa --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_unwrapping.cddl @@ -0,0 +1,27 @@ +; 3.7. Unwrapping + +basic-header-group = ( + field1: int, + field2: text, +) + +basic-header = [ basic-header-group ] + +advanced-header = [ + basic-header-group, + field3: bytes, + field4: number, ; as in the tagged type "time" +] + +; Unwrapping simplifies this to: + +basic-header = [ + field1: int, + field2: text, +] + +advanced-header = [ + ~basic-header, + field3: bytes, + field4: ~time, +] \ No newline at end of file diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_unwrapping_alt.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_unwrapping_alt.cddl new file mode 100644 index 000000000..24813ddb7 --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_unwrapping_alt.cddl @@ -0,0 +1,12 @@ +; Unwrapping simplifies this to: + +basic-header = [ + field1: int, + field2: text, +] + +advanced-header = [ + ~basic-header, + field3: bytes, + field4: ~time, +] From e72029e8095c5d8ee5a3874b20369ae81c3523c2 Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Wed, 24 Jan 2024 20:45:25 +0700 Subject: [PATCH 25/43] feat: add rule level tests --- hermes/crates/cbork/cddl-parser/src/lib.rs | 6 +- .../cbork/cddl-parser/tests/byte_sequences.rs | 8 +- hermes/crates/cbork/cddl-parser/tests/cddl.rs | 54 ++- .../tests/cddl/invalid_rfc8610_sample_1.cddl | 1 + .../tests/cddl/invalid_rfc8610_sample_2.cddl | 2 + .../tests/cddl/invalid_rfc8610_sample_3.cddl | 3 + .../cbork/cddl-parser/tests/group_elements.rs | 372 ++++++++---------- .../cbork/cddl-parser/tests/literal_values.rs | 29 +- .../crates/cbork/cddl-parser/tests/rules.rs | 266 +++++++------ .../cddl-parser/tests/type_declarations.rs | 44 +-- 10 files changed, 386 insertions(+), 399 deletions(-) create mode 100644 hermes/crates/cbork/cddl-parser/tests/cddl/invalid_rfc8610_sample_1.cddl create mode 100644 hermes/crates/cbork/cddl-parser/tests/cddl/invalid_rfc8610_sample_2.cddl create mode 100644 hermes/crates/cbork/cddl-parser/tests/cddl/invalid_rfc8610_sample_3.cddl diff --git a/hermes/crates/cbork/cddl-parser/src/lib.rs b/hermes/crates/cbork/cddl-parser/src/lib.rs index 6ff3c2184..f79fb7bf8 100644 --- a/hermes/crates/cbork/cddl-parser/src/lib.rs +++ b/hermes/crates/cbork/cddl-parser/src/lib.rs @@ -138,9 +138,9 @@ pub fn parse_cddl<'a>( }, }; - result.map(Box::new).map_err(|e| { - Box::new(CDDLError::from(e)) - }) + result + .map(Box::new) + .map_err(|e| Box::new(CDDLError::from(e))) } #[cfg(test)] diff --git a/hermes/crates/cbork/cddl-parser/tests/byte_sequences.rs b/hermes/crates/cbork/cddl-parser/tests/byte_sequences.rs index edadbce16..7355955ae 100644 --- a/hermes/crates/cbork/cddl-parser/tests/byte_sequences.rs +++ b/hermes/crates/cbork/cddl-parser/tests/byte_sequences.rs @@ -5,13 +5,9 @@ use cddl_parser::{ cddl_test::{CDDLTestParser, Parser, Rule}, }; -pub const HEXPAIR_PASSES: &[&str] = &[ - "00", "ab", "de", "0f", "f0" -]; +pub const HEXPAIR_PASSES: &[&str] = &["00", "ab", "de", "0f", "f0"]; -pub const HEXPAIR_FAILS: &[&str] = &[ - "0", " 0", "0 ", "az", "0p" -]; +pub const HEXPAIR_FAILS: &[&str] = &["0", " 0", "0 ", "az", "0p"]; pub const URL_BASE64_PASSES: &[&str] = &[ "abcdefghijklmnopq rstuvw yz01\t23456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ~", diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl.rs b/hermes/crates/cbork/cddl-parser/tests/cddl.rs index ac13c10b7..c9afe8be4 100644 --- a/hermes/crates/cbork/cddl-parser/tests/cddl.rs +++ b/hermes/crates/cbork/cddl-parser/tests/cddl.rs @@ -4,28 +4,46 @@ use cddl_parser::{parse_cddl, Extension}; #[test] fn parse_cddl_files() -> Result<()> { - let entries = fs::read_dir("tests/cddl")?; - - let mut file_paths: Vec<_> = entries - .filter_map(Result::ok) - .filter_map(|x| x.path().is_file().then_some(x.path())) - .collect(); + let entries = fs::read_dir("tests/cddl")?; + + let mut file_paths: Vec<_> = entries + .filter_map(Result::ok) + .filter_map(|x| x.path().is_file().then_some(x.path())) + .collect(); + + file_paths.sort(); + + let valid_file_paths = file_paths + .iter() + .filter(|p| matches!(p.to_str().map(|p| p.starts_with("valid")), Some(true))); + let invalid_file_paths = file_paths + .iter() + .filter(|p| matches!(p.to_str().map(|p| p.starts_with("invalid")), Some(true))); + + // test for valid files + let mut err_messages = vec![]; + for file_path in valid_file_paths { + let mut content = fs::read_to_string(&file_path)?; + + if let Err(e) = parse_cddl(&mut content, &Extension::RFC8610Parser) { + err_messages.push(format!("{}) {file_path:?} {e}", err_messages.len() + 1)); + } + } - file_paths.sort(); + // test for invalid files + for file_path in invalid_file_paths { + let mut content = fs::read_to_string(&file_path)?; - let mut err_messages = vec![]; - for file_path in file_paths { - let mut content = fs::read_to_string(&file_path)?; + let result = parse_cddl(&mut content, &Extension::RFC8610Parser); - if let Err(e) = parse_cddl(&mut content, &Extension::RFC8610Parser) { - err_messages.push(format!("{}) {file_path:?} {e}", err_messages.len() + 1)); + assert!(result.is_err()); } - } - let err_msg = err_messages.join("\n\n"); - if !err_msg.is_empty() { - panic!("{err_msg}") - } + // summary + let err_msg = err_messages.join("\n\n"); + if !err_msg.is_empty() { + panic!("{err_msg}") + } - Ok(()) + Ok(()) } diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/invalid_rfc8610_sample_1.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/invalid_rfc8610_sample_1.cddl new file mode 100644 index 000000000..4e825ea28 --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/invalid_rfc8610_sample_1.cddl @@ -0,0 +1 @@ +unlimited-people = [* person \ No newline at end of file diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/invalid_rfc8610_sample_2.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/invalid_rfc8610_sample_2.cddl new file mode 100644 index 000000000..fab5a16b5 --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/invalid_rfc8610_sample_2.cddl @@ -0,0 +1,2 @@ +sample = { + tstr => tstr \ No newline at end of file diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/invalid_rfc8610_sample_3.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/invalid_rfc8610_sample_3.cddl new file mode 100644 index 000000000..9c1d92eea --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/invalid_rfc8610_sample_3.cddl @@ -0,0 +1,3 @@ +sample = ; comment here { + tstr => tstr +} \ No newline at end of file diff --git a/hermes/crates/cbork/cddl-parser/tests/group_elements.rs b/hermes/crates/cbork/cddl-parser/tests/group_elements.rs index 3e9f648cc..49ccaf03c 100644 --- a/hermes/crates/cbork/cddl-parser/tests/group_elements.rs +++ b/hermes/crates/cbork/cddl-parser/tests/group_elements.rs @@ -1,268 +1,244 @@ use cddl_parser::{ - self, - cddl_test::{CDDLTestParser, Parser, Rule}, + self, + cddl_test::{CDDLTestParser, Parser, Rule}, }; mod identifiers; -use identifiers::{ID_PASSES, ID_FAILS}; +use identifiers::{ID_FAILS, ID_PASSES}; pub const OCCUR_PASSES: &[&str] = &[ - "*", - "+", - "?", - "5*10", - "0x1*0b110", - "*20", - "5*10", - "0x1*0b110", - "0*5", - "5*", - "*5", - "0b110*", - "0x1*", + "*", + "+", + "?", + "5*10", + "0x1*0b110", + "*20", + "5*10", + "0x1*0b110", + "0*5", + "5*", + "*5", + "0b110*", + "0x1*", ]; pub const OCCUR_FAILS: &[&str] = &[ - "5**10", - "5 * 10", - "5\t\n*\n10", - "++", - "??", - - // Fail cases for uint - "0123", // Leading zero is not allowed for decimal - "0xG", // Invalid hex digit - "0b123", // Invalid binary digit - "0*5*", // Multiple '*' not allowed - "0x1*0b110*", - "0x", - "0b", + "5**10", + "5 * 10", + "5\t\n*\n10", + "++", + "??", + // Fail cases for uint + "0123", // Leading zero is not allowed for decimal + "0xG", // Invalid hex digit + "0b123", // Invalid binary digit + "0*5*", // Multiple '*' not allowed + "0x1*0b110*", + "0x", + "0b", ]; -pub const OPTCOM_PASSES: &[&str] = &[ - "", - ",", - " ,", - " , ", - "\n,\n", - "\n", -]; +pub const OPTCOM_PASSES: &[&str] = &["", ",", " ,", " , ", "\n,\n", "\n"]; -pub const OPTCOM_FAILS: &[&str] = &[ - ",,", -]; +pub const OPTCOM_FAILS: &[&str] = &[",,"]; pub const MEMBERKEY_PASSES: &[&str] = &[ - // bareword - "foo:", - "foo-bar:", - "foo_bar:", - "foo :", - - // values - "\"foo\":", - "1:", - "0x123:", - "1.1:", - "-1:", - "b64'1234':", - "h'1234':", - "h'12 34\n':", - - // type1 - "tstr =>", - "id =>", - "# =>", - "1..2 =>", - "1...2 =>", - "\"foo\" =>", - "\"foo\" ^=>", - "\"foo\"^ =>", - "\"foo\" ^ =>", - "1 =>", - "0x123 =>", - "1.1 =>", - "-1 =>", - "b64'1234' =>", - "h'1234' =>", - "h'12 34\n' =>", + // bareword + "foo:", + "foo-bar:", + "foo_bar:", + "foo :", + // values + "\"foo\":", + "1:", + "0x123:", + "1.1:", + "-1:", + "b64'1234':", + "h'1234':", + "h'12 34\n':", + // type1 + "tstr =>", + "id =>", + "# =>", + "1..2 =>", + "1...2 =>", + "\"foo\" =>", + "\"foo\" ^=>", + "\"foo\"^ =>", + "\"foo\" ^ =>", + "1 =>", + "0x123 =>", + "1.1 =>", + "-1 =>", + "b64'1234' =>", + "h'1234' =>", + "h'12 34\n' =>", ]; -pub const MEMBERKEY_FAILS: &[&str] = &[ - "#:", - "foo::", -]; +pub const MEMBERKEY_FAILS: &[&str] = &["#:", "foo::"]; pub const GRPENT_PASSES: &[&str] = &[ - "foo: 1", - "foo: 1", - "foo-bar:\t\n1", - "foo :\n1", - "foo: #", - "tstr => any", - "tstr => { foo: bar }", - "tstr => { foo: bar, baz }", - "tstr => [foo: bar, baz]", + "foo: 1", + "foo: 1", + "foo-bar:\t\n1", + "foo :\n1", + "foo: #", + "tstr => any", + "tstr => { foo: bar }", + "tstr => { foo: bar, baz }", + "tstr => [foo: bar, baz]", ]; -pub const GRPENT_FAILS: &[&str] = &[ - "tstr => (foo: bar)", -]; +pub const GRPENT_FAILS: &[&str] = &["tstr => (foo: bar)"]; pub const GRPCHOICE_PASSES: &[&str] = &[ - "foo: 1", - "foo: 1, bar: 2", - "foo: 1, bar: 2,", - "foo: 1\nbar: 2", - "foo: 1 bar: 2", - "foo => 1 bar: 2", - "foo => 1, bar => 2", - "foo => 1, bar: 2", - "foo => 1bar: 2", + "foo: 1", + "foo: 1, bar: 2", + "foo: 1, bar: 2,", + "foo: 1\nbar: 2", + "foo: 1 bar: 2", + "foo => 1 bar: 2", + "foo => 1, bar => 2", + "foo => 1, bar: 2", + "foo => 1bar: 2", ]; -pub const GRPCHOICE_FAILS: &[&str] = &[ - "foo: ,", - "foo:", - "foo: bar: 2", - "foo => bar: 2", -]; +pub const GRPCHOICE_FAILS: &[&str] = &["foo: ,", "foo:", "foo: bar: 2", "foo => bar: 2"]; pub const GROUP_PASSES: &[&str] = &[ - "(foo: 1)", - "(foo: 1) // (bar: 2)", - "(foo: 1) // (bar: 2)", - "(street: tstr, ? number: uint, city // po-box: uint, city // per-pickup: true)", - "(+ a // b / c)", - "((+ a) // (b / c))", + "(foo: 1)", + "(foo: 1) // (bar: 2)", + "(foo: 1) // (bar: 2)", + "(street: tstr, ? number: uint, city // po-box: uint, city // per-pickup: true)", + "(+ a // b / c)", + "((+ a) // (b / c))", ]; -pub const GROUP_FAILS: &[&str] = &[ - "(foo: 1) / (bar: 2)", -]; +pub const GROUP_FAILS: &[&str] = &["(foo: 1) / (bar: 2)"]; #[test] /// Test if the `occur` rule passes properly. /// This uses a special rule in the Grammar to test `occur` exhaustively. fn check_occur() { - let tests = OCCUR_PASSES; - let fails = OCCUR_FAILS; - - for test in tests { - let parse = CDDLTestParser::parse(Rule::occur_TEST, test); - assert!(parse.is_ok()); - } - - for test in fails { - let parse = CDDLTestParser::parse(Rule::occur_TEST, test); - assert!(parse.is_err()); - } + let tests = OCCUR_PASSES; + let fails = OCCUR_FAILS; + + for test in tests { + let parse = CDDLTestParser::parse(Rule::occur_TEST, test); + assert!(parse.is_ok()); + } + + for test in fails { + let parse = CDDLTestParser::parse(Rule::occur_TEST, test); + assert!(parse.is_err()); + } } #[test] /// Test if the `bareword` rule passes properly. /// This uses a special rule in the Grammar to test `bareword` exhaustively. fn check_bareword() { - let tests: &[&str] = ID_PASSES; - let fails = ID_FAILS; - - for test in tests { - let parse = CDDLTestParser::parse(Rule::bareword_TEST, test); - assert!(parse.is_ok()); - } - - for test in fails { - let parse = CDDLTestParser::parse(Rule::bareword_TEST, test); - assert!(parse.is_err()); - } + let tests: &[&str] = ID_PASSES; + let fails = ID_FAILS; + + for test in tests { + let parse = CDDLTestParser::parse(Rule::bareword_TEST, test); + assert!(parse.is_ok()); + } + + for test in fails { + let parse = CDDLTestParser::parse(Rule::bareword_TEST, test); + assert!(parse.is_err()); + } } #[test] /// Test if the `optcom` rule passes properly. /// This uses a special rule in the Grammar to test `optcom` exhaustively. fn check_optcom() { - let tests = OPTCOM_PASSES; - let fails = OPTCOM_FAILS; - - for test in tests { - let parse = CDDLTestParser::parse(Rule::optcom_TEST, test); - assert!(parse.is_ok()); - } - - for test in fails { - let parse = CDDLTestParser::parse(Rule::optcom_TEST, test); - assert!(parse.is_err()); - } + let tests = OPTCOM_PASSES; + let fails = OPTCOM_FAILS; + + for test in tests { + let parse = CDDLTestParser::parse(Rule::optcom_TEST, test); + assert!(parse.is_ok()); + } + + for test in fails { + let parse = CDDLTestParser::parse(Rule::optcom_TEST, test); + assert!(parse.is_err()); + } } #[test] /// Test if the `memberkey` rule passes properly. /// This uses a special rule in the Grammar to test `memberkey` exhaustively. fn check_memberkey() { - let tests = MEMBERKEY_PASSES; - let fails = MEMBERKEY_FAILS; - - for test in tests { - let parse = CDDLTestParser::parse(Rule::memberkey_TEST, test); - assert!(parse.is_ok()); - } - - for test in fails { - let parse = CDDLTestParser::parse(Rule::memberkey_TEST, test); - assert!(parse.is_err()); - } + let tests = MEMBERKEY_PASSES; + let fails = MEMBERKEY_FAILS; + + for test in tests { + let parse = CDDLTestParser::parse(Rule::memberkey_TEST, test); + assert!(parse.is_ok()); + } + + for test in fails { + let parse = CDDLTestParser::parse(Rule::memberkey_TEST, test); + assert!(parse.is_err()); + } } #[test] /// Test if the `grpent` rule passes properly. /// This uses a special rule in the Grammar to test `grpent` exhaustively. fn check_grpent() { - let tests = GRPENT_PASSES; - let fails = GRPENT_FAILS; - - for test in tests { - let parse = CDDLTestParser::parse(Rule::grpent_TEST, test); - assert!(parse.is_ok()); - } - - for test in fails { - let parse = CDDLTestParser::parse(Rule::grpent_TEST, test); - assert!(parse.is_err()); - } + let tests = GRPENT_PASSES; + let fails = GRPENT_FAILS; + + for test in tests { + let parse = CDDLTestParser::parse(Rule::grpent_TEST, test); + assert!(parse.is_ok()); + } + + for test in fails { + let parse = CDDLTestParser::parse(Rule::grpent_TEST, test); + assert!(parse.is_err()); + } } #[test] /// Test if the `grpchoice` rule passes properly. /// This uses a special rule in the Grammar to test `grpchoice` exhaustively. fn check_grpchoice() { - let tests = GRPCHOICE_PASSES; - let fails = GRPCHOICE_FAILS; - - for test in tests { - let parse = CDDLTestParser::parse(Rule::grpchoice_TEST, test); - assert!(parse.is_ok()); - } - - for test in fails { - let parse = CDDLTestParser::parse(Rule::grpchoice_TEST, test); - assert!(parse.is_err()); - } + let tests = GRPCHOICE_PASSES; + let fails = GRPCHOICE_FAILS; + + for test in tests { + let parse = CDDLTestParser::parse(Rule::grpchoice_TEST, test); + assert!(parse.is_ok()); + } + + for test in fails { + let parse = CDDLTestParser::parse(Rule::grpchoice_TEST, test); + assert!(parse.is_err()); + } } #[test] /// Test if the `group` rule passes properly. /// This uses a special rule in the Grammar to test `group` exhaustively. fn check_group() { - let tests = GROUP_PASSES; - let fails = GROUP_FAILS; - - for test in tests { - let parse = CDDLTestParser::parse(Rule::group_TEST, test); - assert!(parse.is_ok()); - } - - for test in fails { - let parse = CDDLTestParser::parse(Rule::group_TEST, test); - assert!(parse.is_err()); - } + let tests = GROUP_PASSES; + let fails = GROUP_FAILS; + + for test in tests { + let parse = CDDLTestParser::parse(Rule::group_TEST, test); + assert!(parse.is_ok()); + } + + for test in fails { + let parse = CDDLTestParser::parse(Rule::group_TEST, test); + assert!(parse.is_err()); + } } diff --git a/hermes/crates/cbork/cddl-parser/tests/literal_values.rs b/hermes/crates/cbork/cddl-parser/tests/literal_values.rs index 9a6e0ec42..9a428cc99 100644 --- a/hermes/crates/cbork/cddl-parser/tests/literal_values.rs +++ b/hermes/crates/cbork/cddl-parser/tests/literal_values.rs @@ -21,9 +21,7 @@ pub const UINT_PASSES: &[&str] = &[ "0", ]; -pub const UINT_FAILS: &[&str] = &[ - " a ", "zz", "0123zzz", "0xdog", "0b777" -]; +pub const UINT_FAILS: &[&str] = &[" a ", "zz", "0123zzz", "0xdog", "0b777"]; pub const INT_PASSES: &[&str] = &[ "10", @@ -44,10 +42,7 @@ pub const INT_PASSES: &[&str] = &[ "-0", ]; -pub const INT_FAILS: &[&str] = &[ - " a ", "zz", "0123zzz", "0xdog", "0b777" -]; - +pub const INT_FAILS: &[&str] = &[" a ", "zz", "0123zzz", "0xdog", "0b777"]; pub const INTFLOAT_PASSES: &[&str] = &[ "10", @@ -69,10 +64,7 @@ pub const INTFLOAT_PASSES: &[&str] = &[ "123.456e-789", ]; -pub const INTFLOAT_FAILS: &[&str] = &[ - " a ", "zz", "0123zzz", "0xdog", "0b777" -]; - +pub const INTFLOAT_FAILS: &[&str] = &[" a ", "zz", "0123zzz", "0xdog", "0b777"]; pub const HEXFLOAT_PASSES: &[&str] = &[ "0xabcp+123", @@ -85,10 +77,7 @@ pub const HEXFLOAT_PASSES: &[&str] = &[ "-0xabc.defp-123", ]; -pub const HEXFLOAT_FAILS: &[&str] = &[ - " a ", "zz", "0123zzz", "0xdog", "0b777" -]; - +pub const HEXFLOAT_FAILS: &[&str] = &[" a ", "zz", "0123zzz", "0xdog", "0b777"]; pub const NUMBER_PASSES: &[&str] = &[ "0xabcp+123", @@ -118,9 +107,7 @@ pub const NUMBER_PASSES: &[&str] = &[ "123.456e-789", ]; -pub const NUMBER_FAILS: &[&str] = &[ - " a ", "zz", "0123zzz", "0xdog", "0b777" -]; +pub const NUMBER_FAILS: &[&str] = &[" a ", "zz", "0123zzz", "0xdog", "0b777"]; pub const VALUE_PASSES: &[&str] = &[ // Ideally we would define these somewhere central and just use them where needed. @@ -219,11 +206,13 @@ fn check_number() { #[test] /// Test if the `uint` rule passes properly. fn check_value() { - let tests: Vec<_> = VALUE_PASSES.into_iter() + let tests: Vec<_> = VALUE_PASSES + .into_iter() .chain(NUMBER_PASSES.into_iter()) .chain(BYTES_PASSES.into_iter()) .collect(); - let fails: Vec<_> = VALUE_FAILS.into_iter() + let fails: Vec<_> = VALUE_FAILS + .into_iter() .chain(NUMBER_FAILS.into_iter()) .collect(); diff --git a/hermes/crates/cbork/cddl-parser/tests/rules.rs b/hermes/crates/cbork/cddl-parser/tests/rules.rs index 37bbd794b..399767dcf 100644 --- a/hermes/crates/cbork/cddl-parser/tests/rules.rs +++ b/hermes/crates/cbork/cddl-parser/tests/rules.rs @@ -1,65 +1,45 @@ use cddl_parser::{ - self, - cddl_test::{CDDLTestParser, Parser, Rule}, + self, + cddl_test::{CDDLTestParser, Parser, Rule}, }; mod identifiers; -use identifiers::{ID_PASSES, ID_FAILS}; +use identifiers::{ID_FAILS, ID_PASSES}; +mod type_declarations; +use type_declarations::{TYPE_FAILS, TYPE_PASSES}; pub const GENERICARG_PASSES: &[&str] = &[ - "", - "<{ foo: bar }>", - "<{ h'1234': uint }>", - "<1...10>", - "<\n1...10\t>", - "<{ foo: bar }, { foo: baz }>", - "<{ foo: bar }, 1..10>", + "", + "<{ foo: bar }>", + "<{ h'1234': uint }>", + "<1...10>", + "<\n1...10\t>", + "<{ foo: bar }, { foo: baz }>", + "<{ foo: bar }, 1..10>", ]; -pub const GENERICARG_FAILS: &[&str] = &[ - "", - "<>", - "", - "<( foo: bar )>", - "", -]; +pub const GENERICARG_FAILS: &[&str] = + &["", "<>", "", "<( foo: bar )>", ""]; -pub const GENERICPARM_PASSES: &[&str] = &[ - "", - "", - "", - "", -]; +pub const GENERICPARM_PASSES: &[&str] = &["", "", "", ""]; pub const GENERICPARM_FAILS: &[&str] = &[ - "", - "<>", - "", - "<{ foo: bar }>", - "<{ h'1234': uint }>", - "<1...10>", - "<\n1...10\t>", + "", + "<>", + "", + "<{ foo: bar }>", + "<{ h'1234': uint }>", + "<1...10>", + "<\n1...10\t>", ]; -pub const ASSIGNG_PASSES: &[&str] = &[ - "=", - "//=" -]; +pub const ASSIGNG_PASSES: &[&str] = &["=", "//="]; -pub const ASSIGNG_FAILS: &[&str] = &[ - "==", - "/=", -]; +pub const ASSIGNG_FAILS: &[&str] = &["==", "/="]; -pub const ASSIGNT_PASSES: &[&str] = &[ - "=", - "/=" -]; +pub const ASSIGNT_PASSES: &[&str] = &["=", "/="]; -pub const ASSIGNT_FAILS: &[&str] = &[ - "==", - "//=" -]; +pub const ASSIGNT_FAILS: &[&str] = &["==", "//="]; pub const TYPENAME_PASSES: &[&str] = &ID_PASSES; @@ -69,131 +49,163 @@ pub const GROUPNAME_PASSES: &[&str] = &ID_PASSES; pub const GROUPNAME_FAILS: &[&str] = &ID_FAILS; -pub const RULE_PASSES: &[&str] = &[ - +pub const RULE_GROUP_PASSES: &[&str] = &[ + "foo = (bar: baz)", + "t //= (foo: bar)", + "t //= foo", + "t //= foo", + "t //= foo: bar", + "t //= 2*2 foo: bar", + "delivery //= ( lat: float, long: float, drone-type: tstr )" ]; -pub const RULE_FAILS: &[&str] = &[ - +pub const RULE_GROUP_FAILS: &[&str] = &[ + "foo = bar: baz", + "t /= (foo: bar)" ]; #[test] /// Test if the `genericarg` rule passes properly. /// This uses a special rule in the Grammar to test `genericarg` exhaustively. fn check_genericarg() { - let tests = GENERICARG_PASSES; - let fails = GENERICARG_FAILS; - - for test in tests { - let parse = CDDLTestParser::parse(Rule::genericarg_TEST, test); - assert!(parse.is_ok()); - } - - for test in fails { - let parse = CDDLTestParser::parse(Rule::genericarg_TEST, test); - assert!(parse.is_err()); - } + let tests = GENERICARG_PASSES; + let fails = GENERICARG_FAILS; + + for test in tests { + let parse = CDDLTestParser::parse(Rule::genericarg_TEST, test); + assert!(parse.is_ok()); + } + + for test in fails { + let parse = CDDLTestParser::parse(Rule::genericarg_TEST, test); + assert!(parse.is_err()); + } } #[test] /// Test if the `genericparm` rule passes properly. /// This uses a special rule in the Grammar to test `genericparm` exhaustively. fn check_genericparm() { - let tests = GENERICPARM_PASSES; - let fails = GENERICPARM_FAILS; - - for test in tests { - let parse = CDDLTestParser::parse(Rule::genericparm_TEST, test); - assert!(parse.is_ok()); - } - - for test in fails { - let parse = CDDLTestParser::parse(Rule::occur_TEST, test); - assert!(parse.is_err()); - } + let tests = GENERICPARM_PASSES; + let fails = GENERICPARM_FAILS; + + for test in tests { + let parse = CDDLTestParser::parse(Rule::genericparm_TEST, test); + assert!(parse.is_ok()); + } + + for test in fails { + let parse = CDDLTestParser::parse(Rule::occur_TEST, test); + assert!(parse.is_err()); + } } #[test] /// Test if the `assigng` rule passes properly. /// This uses a special rule in the Grammar to test `assigng` exhaustively. fn check_assigng() { - let tests = ASSIGNG_PASSES; - let fails = ASSIGNG_FAILS; - - for test in tests { - let parse = CDDLTestParser::parse(Rule::assigng_TEST, test); - assert!(parse.is_ok()); - } - - for test in fails { - let parse = CDDLTestParser::parse(Rule::assigng_TEST, test); - assert!(parse.is_err()); - } + let tests = ASSIGNG_PASSES; + let fails = ASSIGNG_FAILS; + + for test in tests { + let parse = CDDLTestParser::parse(Rule::assigng_TEST, test); + assert!(parse.is_ok()); + } + + for test in fails { + let parse = CDDLTestParser::parse(Rule::assigng_TEST, test); + assert!(parse.is_err()); + } } #[test] /// Test if the `assignt` rule passes properly. /// This uses a special rule in the Grammar to test `assignt` exhaustively. fn check_assignt() { - let tests = ASSIGNT_PASSES; - let fails = ASSIGNT_FAILS; - - for test in tests { - let parse = CDDLTestParser::parse(Rule::assignt_TEST, test); - assert!(parse.is_ok()); - } - - for test in fails { - let parse = CDDLTestParser::parse(Rule::assignt_TEST, test); - assert!(parse.is_err()); - } + let tests = ASSIGNT_PASSES; + let fails = ASSIGNT_FAILS; + + for test in tests { + let parse = CDDLTestParser::parse(Rule::assignt_TEST, test); + assert!(parse.is_ok()); + } + + for test in fails { + let parse = CDDLTestParser::parse(Rule::assignt_TEST, test); + assert!(parse.is_err()); + } } #[test] /// Test if the `typename` rule passes properly. /// This uses a special rule in the Grammar to test `typename` exhaustively. fn check_typename() { - let tests = TYPENAME_PASSES; - let fails = TYPENAME_FAILS; - - for test in tests { - let parse = CDDLTestParser::parse(Rule::typename_TEST, test); - assert!(parse.is_ok()); - } - - for test in fails { - let parse = CDDLTestParser::parse(Rule::typename_TEST, test); - assert!(parse.is_err()); - } + let tests = TYPENAME_PASSES; + let fails = TYPENAME_FAILS; + + for test in tests { + let parse = CDDLTestParser::parse(Rule::typename_TEST, test); + assert!(parse.is_ok()); + } + + for test in fails { + let parse = CDDLTestParser::parse(Rule::typename_TEST, test); + assert!(parse.is_err()); + } } #[test] /// Test if the `groupname` rule passes properly. /// This uses a special rule in the Grammar to test `groupname` exhaustively. fn check_groupname() { - let tests = GROUPNAME_PASSES; - let fails = GROUPNAME_FAILS; - - for test in tests { - let parse = CDDLTestParser::parse(Rule::groupname_TEST, test); - assert!(parse.is_ok()); - } + let tests = GROUPNAME_PASSES; + let fails = GROUPNAME_FAILS; + + for test in tests { + let parse = CDDLTestParser::parse(Rule::groupname_TEST, test); + assert!(parse.is_ok()); + } + + for test in fails { + let parse = CDDLTestParser::parse(Rule::groupname_TEST, test); + assert!(parse.is_err()); + } +} - for test in fails { - let parse = CDDLTestParser::parse(Rule::groupname_TEST, test); - assert!(parse.is_err()); - } +#[test] +/// Test if the `rule` rule passes properly for type variant. +fn check_rule_type_composition() { + for (i, test_i) in [TYPENAME_PASSES, TYPENAME_FAILS] + .into_iter() + .flatten() + .enumerate() + { + for (j, test_j) in [ASSIGNT_PASSES].into_iter().flatten().enumerate() { + for (k, test_k) in [TYPE_PASSES, TYPE_FAILS].into_iter().flatten().enumerate() { + let input = [test_i.to_owned(), test_j.to_owned(), test_k.to_owned()].join(" "); + let parse = CDDLTestParser::parse(Rule::rule_TEST, &input); + if (0..TYPENAME_PASSES.len()).contains(&i) + && (0..ASSIGNT_PASSES.len()).contains(&j) + && (0..TYPE_PASSES.len()).contains(&k) + { + assert!(parse.is_ok()); + } else { + assert!(parse.is_err()); + } + } + } + } } #[test] -/// Test if the `rule` rule passes properly. -/// This uses a special rule in the Grammar to test `rule` exhaustively. -fn check_rule() { - let tests = RULE_PASSES; - let fails = RULE_FAILS; +/// Test if the `rule` rule passes properly for group variant. +fn check_rule_group() { + let tests = RULE_GROUP_PASSES; + let fails = RULE_GROUP_FAILS; for test in tests { let parse = CDDLTestParser::parse(Rule::rule_TEST, test); + assert!(parse.is_ok()); } @@ -201,4 +213,4 @@ fn check_rule() { let parse = CDDLTestParser::parse(Rule::rule_TEST, test); assert!(parse.is_err()); } -} \ No newline at end of file +} diff --git a/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs b/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs index 96bc4a032..8f1c553d3 100644 --- a/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs +++ b/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs @@ -1,7 +1,7 @@ use cddl_parser::{ self, cddl_test::{CDDLTestParser, Parser, Rule}, -}; +}; pub const CTLOP_PASSES: &[&str] = &[ ".$", @@ -40,13 +40,9 @@ pub const CTLOP_FAILS: &[&str] = &[ "aname.", ".", "..", "aname.", "aname-", "aname%", "a%name4", "a^name5", "a name", "", ]; -pub const RANGEOP_PASSES: &[&str] = &[ - "..", "..." -]; +pub const RANGEOP_PASSES: &[&str] = &["..", "..."]; -pub const RANGEOP_FAILS: &[&str] = &[ - ".", "", "....", ".. .", ". .." -]; +pub const RANGEOP_FAILS: &[&str] = &[".", "", "....", ".. .", ". .."]; pub const TYPE2_PASSES: &[&str] = &[ "#", @@ -104,9 +100,7 @@ pub const TYPE1_PASSES: &[&str] = &[ "foo.bar", ]; -pub const TYPE1_FAILS: &[&str] = &[ - "", -]; +pub const TYPE1_FAILS: &[&str] = &[""]; pub const TYPE_PASSES: &[&str] = &[ "1 / 2", @@ -116,13 +110,7 @@ pub const TYPE_PASSES: &[&str] = &[ "# / #", ]; -pub const TYPE_FAILS: &[&str] = &[ - "", - "1 \\ 2", - "1 // 2", - "1 2", - "1 / 2 3", -]; +pub const TYPE_FAILS: &[&str] = &["", "1 \\ 2", "1 // 2", "1 2", "1 / 2 3"]; #[test] /// Test if the `ctlop` rule passes properly. @@ -135,7 +123,7 @@ fn check_ctlop() { let parse = CDDLTestParser::parse(Rule::ctlop_TEST, test); assert!(parse.is_ok()); } - + for test in fails { let parse = CDDLTestParser::parse(Rule::ctlop_TEST, test); assert!(parse.is_err()); @@ -153,7 +141,7 @@ fn check_rangeop() { let parse = CDDLTestParser::parse(Rule::rangeop_TEST, test); assert!(parse.is_ok()); } - + for test in fails { let parse = CDDLTestParser::parse(Rule::rangeop_TEST, test); assert!(parse.is_err()); @@ -171,7 +159,7 @@ fn check_type2() { let parse = CDDLTestParser::parse(Rule::type2_TEST, test); assert!(parse.is_ok()); } - + for test in fails { let parse = CDDLTestParser::parse(Rule::type2_TEST, test); assert!(parse.is_err()); @@ -189,7 +177,7 @@ fn check_type1() { let parse = CDDLTestParser::parse(Rule::type1_TEST, test); assert!(parse.is_ok()); } - + for test in fails { let parse = CDDLTestParser::parse(Rule::type1_TEST, test); assert!(parse.is_err()); @@ -199,14 +187,16 @@ fn check_type1() { #[test] /// Test if the `type1` rule passes properly based on composition of type2 test cases. fn check_type1_composition() { - // type2 composition testing for (i, test_i) in [TYPE2_PASSES, TYPE_FAILS].into_iter().flatten().enumerate() { - for (_, test_j) in [CTLOP_PASSES, RANGEOP_PASSES].into_iter().flatten().enumerate() { + for (_, test_j) in [CTLOP_PASSES, RANGEOP_PASSES] + .into_iter() + .flatten() + .enumerate() + { for (k, test_k) in [TYPE2_PASSES, TYPE_FAILS].into_iter().flatten().enumerate() { let input = [test_i.to_owned(), test_j.to_owned(), test_k.to_owned()].join(" "); let parse = CDDLTestParser::parse(Rule::type1_TEST, &input); - if (0..TYPE2_PASSES.len()).contains(&i) - && (0..TYPE2_PASSES.len()).contains(&k) { + if (0..TYPE2_PASSES.len()).contains(&i) && (0..TYPE2_PASSES.len()).contains(&k) { assert!(parse.is_ok()); } else { assert!(parse.is_err()); @@ -227,7 +217,7 @@ fn check_type() { let parse = CDDLTestParser::parse(Rule::type_TEST, test); assert!(parse.is_ok()); } - + for test in fails { let parse = CDDLTestParser::parse(Rule::type_TEST, test); assert!(parse.is_err()); @@ -250,4 +240,4 @@ fn check_type_composition() { } } } -} \ No newline at end of file +} From 4cc17367d1b4155d9759af8e1c0fce3f97b34331 Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Wed, 24 Jan 2024 22:26:07 +0700 Subject: [PATCH 26/43] refactor: use general passes and fails function in unit test --- hermes/crates/cbork/cddl-parser/src/lib.rs | 6 +- .../cbork/cddl-parser/tests/byte_sequences.rs | 55 ++++----- hermes/crates/cbork/cddl-parser/tests/cddl.rs | 10 +- .../cbork/cddl-parser/tests/comments.rs | 81 ++++++------ .../cbork/cddl-parser/tests/group_elements.rs | 105 ++++++---------- .../cbork/cddl-parser/tests/identifiers.rs | 2 +- .../cbork/cddl-parser/tests/literal_values.rs | 108 ++++++---------- .../crates/cbork/cddl-parser/tests/rules.rs | 115 ++++++------------ .../cbork/cddl-parser/tests/text_sequences.rs | 42 +++---- .../cddl-parser/tests/type_declarations.rs | 69 ++++------- 10 files changed, 231 insertions(+), 362 deletions(-) diff --git a/hermes/crates/cbork/cddl-parser/src/lib.rs b/hermes/crates/cbork/cddl-parser/src/lib.rs index f79fb7bf8..ec5fd8e30 100644 --- a/hermes/crates/cbork/cddl-parser/src/lib.rs +++ b/hermes/crates/cbork/cddl-parser/src/lib.rs @@ -116,7 +116,7 @@ pub struct CDDLError(CDDLErrorType); /// ``` pub fn parse_cddl<'a>( input: &'a mut String, extension: &Extension, -) -> Result>, Box> { +) -> Result, CDDLError> { input.push_str("\n\n"); input.push_str(POSTLUDE); @@ -138,9 +138,7 @@ pub fn parse_cddl<'a>( }, }; - result - .map(Box::new) - .map_err(|e| Box::new(CDDLError::from(e))) + result.map_err( CDDLError::from) } #[cfg(test)] diff --git a/hermes/crates/cbork/cddl-parser/tests/byte_sequences.rs b/hermes/crates/cbork/cddl-parser/tests/byte_sequences.rs index 7355955ae..b04aef9ca 100644 --- a/hermes/crates/cbork/cddl-parser/tests/byte_sequences.rs +++ b/hermes/crates/cbork/cddl-parser/tests/byte_sequences.rs @@ -51,53 +51,46 @@ pub const BYTES_FAILS: &[&str] = &[ "'\u{7}'", ]; -#[test] -/// Test if the `HEX_PAIR` rule passes properly. -fn check_hexpair() { - let hex_pairs = HEXPAIR_PASSES; - let not_hex_pairs = HEXPAIR_FAILS; - - for hp in hex_pairs { - let parse = CDDLTestParser::parse(Rule::HEX_PAIR, hp); +pub fn passes_tests_rule(rule_type: Rule, test_data: &[&str]) { + for test in test_data { + let parse = CDDLTestParser::parse(rule_type, test); assert!(parse.is_ok()); } +} - for hp in not_hex_pairs { - let parse = CDDLTestParser::parse(Rule::HEX_PAIR, hp); +pub fn fails_tests_rule(rule_type: Rule, test_data: &[&str]) { + for test in test_data { + let parse = CDDLTestParser::parse(rule_type, test); assert!(parse.is_err()); } } +#[test] +/// Test if the `HEX_PAIR` rule passes properly. +fn check_hexpair() { + let passes = HEXPAIR_PASSES; + let fails = HEXPAIR_FAILS; + + passes_tests_rule(Rule::HEX_PAIR, passes); + fails_tests_rule(Rule::HEX_PAIR, fails); +} + #[test] /// Test if the `URL_BASE64` rule passes properly. fn check_url_base64() { - let tests = URL_BASE64_PASSES; + let passes = URL_BASE64_PASSES; let fails = URL_BASE64_FAILS; - for test in tests { - let parse = CDDLTestParser::parse(Rule::URL_BASE64_TEST, test); - assert!(parse.is_ok()); - } - - for test in fails { - let parse = CDDLTestParser::parse(Rule::URL_BASE64_TEST, test); - assert!(parse.is_err()); - } + passes_tests_rule(Rule::URL_BASE64_TEST, passes); + fails_tests_rule(Rule::URL_BASE64_TEST, fails); } #[test] /// Test if the `bytes` rule passes properly. fn check_bytes() { - let test = BYTES_PASSES; - let fail = BYTES_FAILS; + let passes = BYTES_PASSES; + let fails = BYTES_FAILS; - for test in test { - let parse = CDDLTestParser::parse(Rule::bytes_TEST, test); - assert!(parse.is_ok()); - } - - for test in fail { - let parse = CDDLTestParser::parse(Rule::bytes_TEST, test); - assert!(parse.is_err()); - } + passes_tests_rule(Rule::bytes_TEST, passes); + fails_tests_rule(Rule::bytes_TEST, fails); } diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl.rs b/hermes/crates/cbork/cddl-parser/tests/cddl.rs index c9afe8be4..33de3b6e0 100644 --- a/hermes/crates/cbork/cddl-parser/tests/cddl.rs +++ b/hermes/crates/cbork/cddl-parser/tests/cddl.rs @@ -3,8 +3,8 @@ use std::{fs, io::Result}; use cddl_parser::{parse_cddl, Extension}; #[test] -fn parse_cddl_files() -> Result<()> { - let entries = fs::read_dir("tests/cddl")?; +fn parse_cddl_files() { + let entries = fs::read_dir("tests/cddl").expect("`tests/cddl` directory must exist"); let mut file_paths: Vec<_> = entries .filter_map(Result::ok) @@ -23,7 +23,7 @@ fn parse_cddl_files() -> Result<()> { // test for valid files let mut err_messages = vec![]; for file_path in valid_file_paths { - let mut content = fs::read_to_string(&file_path)?; + let mut content = fs::read_to_string(&file_path).expect("failed to read a file"); if let Err(e) = parse_cddl(&mut content, &Extension::RFC8610Parser) { err_messages.push(format!("{}) {file_path:?} {e}", err_messages.len() + 1)); @@ -32,7 +32,7 @@ fn parse_cddl_files() -> Result<()> { // test for invalid files for file_path in invalid_file_paths { - let mut content = fs::read_to_string(&file_path)?; + let mut content = fs::read_to_string(&file_path).expect("failed to read a file"); let result = parse_cddl(&mut content, &Extension::RFC8610Parser); @@ -44,6 +44,4 @@ fn parse_cddl_files() -> Result<()> { if !err_msg.is_empty() { panic!("{err_msg}") } - - Ok(()) } diff --git a/hermes/crates/cbork/cddl-parser/tests/comments.rs b/hermes/crates/cbork/cddl-parser/tests/comments.rs index eb3328df2..b23c4d60e 100644 --- a/hermes/crates/cbork/cddl-parser/tests/comments.rs +++ b/hermes/crates/cbork/cddl-parser/tests/comments.rs @@ -3,53 +3,56 @@ use cddl_parser::{ cddl_test::{CDDLTestParser, Parser, Rule}, }; +pub const COMMENT_PASSES: &[&str] = &["; A Comment \n", "; And another\r", ";more\r\n"]; + +pub const COMMENT_FAILS: &[&str] = &["not a comment\n"]; + +pub const WHITESPACE_COMMENT_PASSES: &[&str] = &[ + " ", + " ", + " \t \t", + " \t \r \n \r\n ", + "; A Comment\r", + " \t ; A Comment \n", + "; One Comment\n; Two Comments\n", + "; One Comment \n; Two Comments\r; Another Comment\r\n", + "\t; One Comment \n\t; Two Comments\r; Another Comment\r\n", + "\t; A Comment \n ; Another Comment \t \r\n \t ; A Final Comment \r\n", +]; + +pub const WHITESPACE_COMMENT_FAILS: &[&str] = &["not a comment"]; + +pub fn passes_tests_rule(rule_type: Rule, test_data: &[&str]) { + for test in test_data { + let parse = CDDLTestParser::parse(rule_type, test); + assert!(parse.is_ok()); + } +} + +pub fn fails_tests_rule(rule_type: Rule, test_data: &[&str]) { + for test in test_data { + let parse = CDDLTestParser::parse(rule_type, test); + assert!(parse.is_err()); + } +} + #[test] /// Test if the `COMMENT` rule passes properly. fn check_comment() { - let comment1 = "; A Comment \n"; - let comment2 = "; And another\r"; - let comment3 = ";more\r\n"; - let not_comment = "not a comment\n"; + let passes = COMMENT_PASSES; + let fails = COMMENT_FAILS; - let parse = CDDLTestParser::parse(Rule::COMMENT, comment1); - assert!(parse.is_ok()); - - let parse = CDDLTestParser::parse(Rule::COMMENT, comment2); - assert!(parse.is_ok()); - - let parse = CDDLTestParser::parse(Rule::COMMENT, comment3); - assert!(parse.is_ok()); - - let parse = CDDLTestParser::parse(Rule::COMMENT, not_comment); - assert!(parse.is_err()); + passes_tests_rule(Rule::COMMENT_TEST, passes); + fails_tests_rule(Rule::COMMENT_TEST, fails); } #[test] /// Test if the `COMMENT` rule passes properly with whitespace. -/// This uses a special rule in the Grammar to test whitespace exhaustively. +/// This uses a special rule in the Grammar to test whitespace exhaustively. fn check_whitespace_comments() { - let tests = vec![ - " ", - " ", - " \t \t", - " \t \r \n \r\n ", - "; A Comment\r", - " \t ; A Comment \n", - "; One Comment\n; Two Comments\n", - "; One Comment \n; Two Comments\r; Another Comment\r\n", - "\t; One Comment \n\t; Two Comments\r; Another Comment\r\n", - "\t; A Comment \n ; Another Comment \t \r\n \t ; A Final Comment \r\n", - ]; - - let fails = vec!["not a comment"]; - - for test in tests { - let parse = CDDLTestParser::parse(Rule::COMMENT_TEST, test); - assert!(parse.is_ok()); - } + let passes = WHITESPACE_COMMENT_PASSES; + let fails = WHITESPACE_COMMENT_FAILS; - for test in fails { - let parse = CDDLTestParser::parse(Rule::COMMENT_TEST, test); - assert!(parse.is_err()); - } + passes_tests_rule(Rule::COMMENT_TEST, passes); + fails_tests_rule(Rule::COMMENT_TEST, fails); } diff --git a/hermes/crates/cbork/cddl-parser/tests/group_elements.rs b/hermes/crates/cbork/cddl-parser/tests/group_elements.rs index 49ccaf03c..d1b9e93d6 100644 --- a/hermes/crates/cbork/cddl-parser/tests/group_elements.rs +++ b/hermes/crates/cbork/cddl-parser/tests/group_elements.rs @@ -117,128 +117,93 @@ pub const GROUP_PASSES: &[&str] = &[ pub const GROUP_FAILS: &[&str] = &["(foo: 1) / (bar: 2)"]; +pub fn passes_tests_rule(rule_type: Rule, test_data: &[&str]) { + for test in test_data { + let parse = CDDLTestParser::parse(rule_type, test); + assert!(parse.is_ok()); + } +} + +pub fn fails_tests_rule(rule_type: Rule, test_data: &[&str]) { + for test in test_data { + let parse = CDDLTestParser::parse(rule_type, test); + assert!(parse.is_err()); + } +} + #[test] /// Test if the `occur` rule passes properly. /// This uses a special rule in the Grammar to test `occur` exhaustively. fn check_occur() { - let tests = OCCUR_PASSES; + let passes = OCCUR_PASSES; let fails = OCCUR_FAILS; - for test in tests { - let parse = CDDLTestParser::parse(Rule::occur_TEST, test); - assert!(parse.is_ok()); - } - - for test in fails { - let parse = CDDLTestParser::parse(Rule::occur_TEST, test); - assert!(parse.is_err()); - } + passes_tests_rule(Rule::occur_TEST, passes); + fails_tests_rule(Rule::occur_TEST, fails); } #[test] /// Test if the `bareword` rule passes properly. /// This uses a special rule in the Grammar to test `bareword` exhaustively. fn check_bareword() { - let tests: &[&str] = ID_PASSES; + let passes = ID_PASSES; let fails = ID_FAILS; - for test in tests { - let parse = CDDLTestParser::parse(Rule::bareword_TEST, test); - assert!(parse.is_ok()); - } - - for test in fails { - let parse = CDDLTestParser::parse(Rule::bareword_TEST, test); - assert!(parse.is_err()); - } + passes_tests_rule(Rule::bareword_TEST, passes); + fails_tests_rule(Rule::bareword_TEST, fails); } #[test] /// Test if the `optcom` rule passes properly. /// This uses a special rule in the Grammar to test `optcom` exhaustively. fn check_optcom() { - let tests = OPTCOM_PASSES; + let passes = OPTCOM_PASSES; let fails = OPTCOM_FAILS; - for test in tests { - let parse = CDDLTestParser::parse(Rule::optcom_TEST, test); - assert!(parse.is_ok()); - } - - for test in fails { - let parse = CDDLTestParser::parse(Rule::optcom_TEST, test); - assert!(parse.is_err()); - } + passes_tests_rule(Rule::optcom_TEST, passes); + fails_tests_rule(Rule::optcom_TEST, fails); } #[test] /// Test if the `memberkey` rule passes properly. /// This uses a special rule in the Grammar to test `memberkey` exhaustively. fn check_memberkey() { - let tests = MEMBERKEY_PASSES; + let passes = MEMBERKEY_PASSES; let fails = MEMBERKEY_FAILS; - for test in tests { - let parse = CDDLTestParser::parse(Rule::memberkey_TEST, test); - assert!(parse.is_ok()); - } - - for test in fails { - let parse = CDDLTestParser::parse(Rule::memberkey_TEST, test); - assert!(parse.is_err()); - } + passes_tests_rule(Rule::memberkey_TEST, passes); + fails_tests_rule(Rule::memberkey_TEST, fails); } #[test] /// Test if the `grpent` rule passes properly. /// This uses a special rule in the Grammar to test `grpent` exhaustively. fn check_grpent() { - let tests = GRPENT_PASSES; + let passes = GRPENT_PASSES; let fails = GRPENT_FAILS; - for test in tests { - let parse = CDDLTestParser::parse(Rule::grpent_TEST, test); - assert!(parse.is_ok()); - } - - for test in fails { - let parse = CDDLTestParser::parse(Rule::grpent_TEST, test); - assert!(parse.is_err()); - } + passes_tests_rule(Rule::grpent_TEST, passes); + fails_tests_rule(Rule::grpent_TEST, fails); } #[test] /// Test if the `grpchoice` rule passes properly. /// This uses a special rule in the Grammar to test `grpchoice` exhaustively. fn check_grpchoice() { - let tests = GRPCHOICE_PASSES; + let passes = GRPCHOICE_PASSES; let fails = GRPCHOICE_FAILS; - for test in tests { - let parse = CDDLTestParser::parse(Rule::grpchoice_TEST, test); - assert!(parse.is_ok()); - } - - for test in fails { - let parse = CDDLTestParser::parse(Rule::grpchoice_TEST, test); - assert!(parse.is_err()); - } + passes_tests_rule(Rule::grpchoice_TEST, passes); + fails_tests_rule(Rule::grpchoice_TEST, fails); } #[test] /// Test if the `group` rule passes properly. /// This uses a special rule in the Grammar to test `group` exhaustively. fn check_group() { - let tests = GROUP_PASSES; + let passes = GROUP_PASSES; let fails = GROUP_FAILS; - for test in tests { - let parse = CDDLTestParser::parse(Rule::group_TEST, test); - assert!(parse.is_ok()); - } - - for test in fails { - let parse = CDDLTestParser::parse(Rule::group_TEST, test); - assert!(parse.is_err()); - } + passes_tests_rule(Rule::group_TEST, passes); + fails_tests_rule(Rule::group_TEST, fails); } diff --git a/hermes/crates/cbork/cddl-parser/tests/identifiers.rs b/hermes/crates/cbork/cddl-parser/tests/identifiers.rs index 4257f075e..af1f19ee6 100644 --- a/hermes/crates/cbork/cddl-parser/tests/identifiers.rs +++ b/hermes/crates/cbork/cddl-parser/tests/identifiers.rs @@ -50,7 +50,7 @@ fn check_name_characters() { let parse_start = CDDLTestParser::parse(Rule::NAME_START, &test); let parse_end = CDDLTestParser::parse(Rule::NAME_END, &test); - if x.is_ascii_alphabetic() || x == '@' || x == '_' || x == '$' { + if x.is_ascii_alphabetic() || matches!(x, '@' | '_' | '$') { assert!(parse_start.is_ok()); assert!(parse_end.is_ok()); } else if x.is_ascii_digit() { diff --git a/hermes/crates/cbork/cddl-parser/tests/literal_values.rs b/hermes/crates/cbork/cddl-parser/tests/literal_values.rs index 9a428cc99..ec34239ff 100644 --- a/hermes/crates/cbork/cddl-parser/tests/literal_values.rs +++ b/hermes/crates/cbork/cddl-parser/tests/literal_values.rs @@ -1,5 +1,7 @@ // cspell: words xdog intfloat hexfloat xabcp defp rstuvw +use std::ops::Deref; + use cddl_parser::{ self, cddl_test::{CDDLTestParser, Parser, Rule}, @@ -7,8 +9,8 @@ use cddl_parser::{ mod byte_sequences; use byte_sequences::BYTES_PASSES; - -/// Note, the `text`, `bytes` and `id` tests are elsewhere. +mod text_sequences; +use text_sequences::TEXT_PASSES; pub const UINT_PASSES: &[&str] = &[ "10", @@ -109,120 +111,90 @@ pub const NUMBER_PASSES: &[&str] = &[ pub const NUMBER_FAILS: &[&str] = &[" a ", "zz", "0123zzz", "0xdog", "0b777"]; -pub const VALUE_PASSES: &[&str] = &[ - // Ideally we would define these somewhere central and just use them where needed. - // r#""""#, - // r#""abc""#, - // "\"abc\\n\"", -]; +pub const VALUE_PASSES: &[&str] = &[]; pub const VALUE_FAILS: &[&str] = &[]; -#[test] -/// Test if the `uint` rule passes properly. -fn check_uint() { - let tests = UINT_PASSES; - let fails = UINT_FAILS; - - for test in tests { - let parse = CDDLTestParser::parse(Rule::uint_TEST, test); +pub fn passes_tests_rule(rule_type: Rule, test_data: &[&str]) { + for test in test_data { + let parse = CDDLTestParser::parse(rule_type, test); assert!(parse.is_ok()); } +} - for test in fails { - let parse = CDDLTestParser::parse(Rule::uint_TEST, test); +pub fn fails_tests_rule(rule_type: Rule, test_data: &[&str]) { + for test in test_data { + let parse = CDDLTestParser::parse(rule_type, test); assert!(parse.is_err()); } } +#[test] +/// Test if the `uint` rule passes properly. +fn check_uint() { + let passes = UINT_PASSES; + let fails = UINT_FAILS; + + passes_tests_rule(Rule::uint_TEST, passes); + fails_tests_rule(Rule::uint_TEST, fails); +} + #[test] /// Test if the `uint` rule passes properly. fn check_int() { - let tests = INT_PASSES; + let passes = INT_PASSES; let fails = INT_FAILS; - for test in tests { - let parse = CDDLTestParser::parse(Rule::int_TEST, test); - assert!(parse.is_ok()); - } - - for test in fails { - let parse = CDDLTestParser::parse(Rule::int_TEST, test); - assert!(parse.is_err()); - } + passes_tests_rule(Rule::int_TEST, passes); + fails_tests_rule(Rule::int_TEST, fails); } #[test] /// Test if the `uint` rule passes properly. fn check_intfloat() { - let tests = INTFLOAT_PASSES; + let passes = INTFLOAT_PASSES; let fails = INTFLOAT_FAILS; - for test in tests { - let parse = CDDLTestParser::parse(Rule::intfloat_TEST, test); - assert!(parse.is_ok()); - } - - for test in fails { - let parse = CDDLTestParser::parse(Rule::intfloat_TEST, test); - assert!(parse.is_err()); - } + passes_tests_rule(Rule::intfloat_TEST, passes); + fails_tests_rule(Rule::intfloat_TEST, fails); } #[test] /// Test if the `uint` rule passes properly. fn check_hexfloat() { - let tests = HEXFLOAT_PASSES; + let passes = HEXFLOAT_PASSES; let fails = HEXFLOAT_FAILS; - for test in tests { - let parse = CDDLTestParser::parse(Rule::hexfloat_TEST, test); - assert!(parse.is_ok()); - } - - for test in fails { - let parse = CDDLTestParser::parse(Rule::hexfloat_TEST, test); - assert!(parse.is_err()); - } + passes_tests_rule(Rule::hexfloat_TEST, passes); + fails_tests_rule(Rule::hexfloat_TEST, fails); } #[test] /// Test if the `number` rule passes properly. fn check_number() { - let tests = NUMBER_PASSES; + let passes = NUMBER_PASSES; let fails = NUMBER_FAILS; - for test in tests { - let parse = CDDLTestParser::parse(Rule::number_TEST, test); - assert!(parse.is_ok()); - } - - for test in fails { - let parse = CDDLTestParser::parse(Rule::number_TEST, test); - assert!(parse.is_err()); - } + passes_tests_rule(Rule::number_TEST, passes); + fails_tests_rule(Rule::number_TEST, fails); } #[test] /// Test if the `uint` rule passes properly. fn check_value() { - let tests: Vec<_> = VALUE_PASSES + let passes: Vec<_> = VALUE_PASSES .into_iter() .chain(NUMBER_PASSES.into_iter()) .chain(BYTES_PASSES.into_iter()) + .chain(TEXT_PASSES.into_iter()) + .map(Deref::deref) .collect(); let fails: Vec<_> = VALUE_FAILS .into_iter() .chain(NUMBER_FAILS.into_iter()) + .map(Deref::deref) .collect(); - for test in tests { - let parse = CDDLTestParser::parse(Rule::value_TEST, test); - assert!(parse.is_ok()); - } - - for test in fails { - let parse = CDDLTestParser::parse(Rule::value_TEST, test); - assert!(parse.is_err()); - } + passes_tests_rule(Rule::value_TEST, &passes); + fails_tests_rule(Rule::value_TEST, &fails); } diff --git a/hermes/crates/cbork/cddl-parser/tests/rules.rs b/hermes/crates/cbork/cddl-parser/tests/rules.rs index 399767dcf..c93baa1d6 100644 --- a/hermes/crates/cbork/cddl-parser/tests/rules.rs +++ b/hermes/crates/cbork/cddl-parser/tests/rules.rs @@ -56,120 +56,89 @@ pub const RULE_GROUP_PASSES: &[&str] = &[ "t //= foo", "t //= foo: bar", "t //= 2*2 foo: bar", - "delivery //= ( lat: float, long: float, drone-type: tstr )" + "delivery //= ( lat: float, long: float, drone-type: tstr )", ]; -pub const RULE_GROUP_FAILS: &[&str] = &[ - "foo = bar: baz", - "t /= (foo: bar)" -]; +pub const RULE_GROUP_FAILS: &[&str] = &["foo = bar: baz", "t /= (foo: bar)"]; + +pub fn passes_tests_rule(rule_type: Rule, test_data: &[&str]) { + for test in test_data { + let parse = CDDLTestParser::parse(rule_type, test); + assert!(parse.is_ok()); + } +} + +pub fn fails_tests_rule(rule_type: Rule, test_data: &[&str]) { + for test in test_data { + let parse = CDDLTestParser::parse(rule_type, test); + assert!(parse.is_err()); + } +} #[test] /// Test if the `genericarg` rule passes properly. /// This uses a special rule in the Grammar to test `genericarg` exhaustively. fn check_genericarg() { - let tests = GENERICARG_PASSES; + let passes = GENERICARG_PASSES; let fails = GENERICARG_FAILS; - for test in tests { - let parse = CDDLTestParser::parse(Rule::genericarg_TEST, test); - assert!(parse.is_ok()); - } - - for test in fails { - let parse = CDDLTestParser::parse(Rule::genericarg_TEST, test); - assert!(parse.is_err()); - } + passes_tests_rule(Rule::genericarg_TEST, passes); + fails_tests_rule(Rule::genericarg_TEST, fails); } #[test] /// Test if the `genericparm` rule passes properly. /// This uses a special rule in the Grammar to test `genericparm` exhaustively. fn check_genericparm() { - let tests = GENERICPARM_PASSES; + let passes = GENERICPARM_PASSES; let fails = GENERICPARM_FAILS; - for test in tests { - let parse = CDDLTestParser::parse(Rule::genericparm_TEST, test); - assert!(parse.is_ok()); - } - - for test in fails { - let parse = CDDLTestParser::parse(Rule::occur_TEST, test); - assert!(parse.is_err()); - } + passes_tests_rule(Rule::genericparm_TEST, passes); + fails_tests_rule(Rule::genericparm_TEST, fails); } #[test] /// Test if the `assigng` rule passes properly. /// This uses a special rule in the Grammar to test `assigng` exhaustively. fn check_assigng() { - let tests = ASSIGNG_PASSES; + let passes = ASSIGNG_PASSES; let fails = ASSIGNG_FAILS; - for test in tests { - let parse = CDDLTestParser::parse(Rule::assigng_TEST, test); - assert!(parse.is_ok()); - } - - for test in fails { - let parse = CDDLTestParser::parse(Rule::assigng_TEST, test); - assert!(parse.is_err()); - } + passes_tests_rule(Rule::assigng_TEST, passes); + fails_tests_rule(Rule::assigng_TEST, fails); } #[test] /// Test if the `assignt` rule passes properly. /// This uses a special rule in the Grammar to test `assignt` exhaustively. fn check_assignt() { - let tests = ASSIGNT_PASSES; + let passes = ASSIGNT_PASSES; let fails = ASSIGNT_FAILS; - for test in tests { - let parse = CDDLTestParser::parse(Rule::assignt_TEST, test); - assert!(parse.is_ok()); - } - - for test in fails { - let parse = CDDLTestParser::parse(Rule::assignt_TEST, test); - assert!(parse.is_err()); - } + passes_tests_rule(Rule::assignt_TEST, passes); + fails_tests_rule(Rule::assignt_TEST, fails); } #[test] /// Test if the `typename` rule passes properly. /// This uses a special rule in the Grammar to test `typename` exhaustively. fn check_typename() { - let tests = TYPENAME_PASSES; + let passes = TYPENAME_PASSES; let fails = TYPENAME_FAILS; - for test in tests { - let parse = CDDLTestParser::parse(Rule::typename_TEST, test); - assert!(parse.is_ok()); - } - - for test in fails { - let parse = CDDLTestParser::parse(Rule::typename_TEST, test); - assert!(parse.is_err()); - } + passes_tests_rule(Rule::typename_TEST, passes); + fails_tests_rule(Rule::typename_TEST, fails); } #[test] /// Test if the `groupname` rule passes properly. /// This uses a special rule in the Grammar to test `groupname` exhaustively. fn check_groupname() { - let tests = GROUPNAME_PASSES; + let passes = GROUPNAME_PASSES; let fails = GROUPNAME_FAILS; - for test in tests { - let parse = CDDLTestParser::parse(Rule::groupname_TEST, test); - assert!(parse.is_ok()); - } - - for test in fails { - let parse = CDDLTestParser::parse(Rule::groupname_TEST, test); - assert!(parse.is_err()); - } + passes_tests_rule(Rule::groupname_TEST, passes); + fails_tests_rule(Rule::groupname_TEST, fails); } #[test] @@ -200,17 +169,9 @@ fn check_rule_type_composition() { #[test] /// Test if the `rule` rule passes properly for group variant. fn check_rule_group() { - let tests = RULE_GROUP_PASSES; - let fails = RULE_GROUP_FAILS; - - for test in tests { - let parse = CDDLTestParser::parse(Rule::rule_TEST, test); - - assert!(parse.is_ok()); - } + let passes = RULE_GROUP_PASSES; + let fails = RULE_GROUP_FAILS; - for test in fails { - let parse = CDDLTestParser::parse(Rule::rule_TEST, test); - assert!(parse.is_err()); - } + passes_tests_rule(Rule::rule_TEST, passes); + fails_tests_rule(Rule::rule_TEST, fails); } diff --git a/hermes/crates/cbork/cddl-parser/tests/text_sequences.rs b/hermes/crates/cbork/cddl-parser/tests/text_sequences.rs index b1e805b82..14d41a110 100644 --- a/hermes/crates/cbork/cddl-parser/tests/text_sequences.rs +++ b/hermes/crates/cbork/cddl-parser/tests/text_sequences.rs @@ -8,37 +8,37 @@ pub const S_FAILS: &[&str] = &[" a ", "zz", " \t d \t", " \t \r \n \t \r\n x"] pub const TEXT_PASSES: &[&str] = &[r#""""#, r#""abc""#, "\"abc\\n\""]; pub const TEXT_FAILS: &[&str] = &["", "''", "\"abc\n\""]; +pub fn passes_tests_rule(rule_type: Rule, test_data: &[&str]) { + for test in test_data { + let parse = CDDLTestParser::parse(rule_type, test); + assert!(parse.is_ok()); + } +} + +pub fn fails_tests_rule(rule_type: Rule, test_data: &[&str]) { + for test in test_data { + let parse = CDDLTestParser::parse(rule_type, test); + assert!(parse.is_err()); + } +} + #[test] /// Test if the `S` rule passes properly. /// This uses a special rule in the Grammar to test whitespace exhaustively. fn check_s() { - let tests = S_PASSES; + let passes = S_PASSES; let fails = S_FAILS; - for test in tests { - let parse = CDDLTestParser::parse(Rule::S_TEST, test); - assert!(parse.is_ok()); - } - - for test in fails { - let parse = CDDLTestParser::parse(Rule::S_TEST, test); - assert!(parse.is_err()); - } + passes_tests_rule(Rule::S_TEST, passes); + fails_tests_rule(Rule::S_TEST, fails); } #[test] /// Test if the `text` rule passes properly. fn check_text() { - let test = TEXT_PASSES; - let fail = TEXT_FAILS; + let passes = TEXT_PASSES; + let fails = TEXT_FAILS; - for test in test { - let parse = CDDLTestParser::parse(Rule::text_TEST, test); - assert!(parse.is_ok()); - } - - for test in fail { - let parse = CDDLTestParser::parse(Rule::text_TEST, test); - assert!(parse.is_err()); - } + passes_tests_rule(Rule::text_TEST, passes); + fails_tests_rule(Rule::text_TEST, fails); } diff --git a/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs b/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs index 8f1c553d3..51195cde3 100644 --- a/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs +++ b/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs @@ -112,6 +112,20 @@ pub const TYPE_PASSES: &[&str] = &[ pub const TYPE_FAILS: &[&str] = &["", "1 \\ 2", "1 // 2", "1 2", "1 / 2 3"]; +pub fn passes_tests_rule(rule_type: Rule, test_data: &[&str]) { + for test in test_data { + let parse = CDDLTestParser::parse(rule_type, test); + assert!(parse.is_ok()); + } +} + +pub fn fails_tests_rule(rule_type: Rule, test_data: &[&str]) { + for test in test_data { + let parse = CDDLTestParser::parse(rule_type, test); + assert!(parse.is_err()); + } +} + #[test] /// Test if the `ctlop` rule passes properly. /// This uses a special rule in the Grammar to test `ctlop` exhaustively. @@ -119,15 +133,8 @@ fn check_ctlop() { let passes = CTLOP_PASSES; let fails = CTLOP_FAILS; - for test in passes { - let parse = CDDLTestParser::parse(Rule::ctlop_TEST, test); - assert!(parse.is_ok()); - } - - for test in fails { - let parse = CDDLTestParser::parse(Rule::ctlop_TEST, test); - assert!(parse.is_err()); - } + passes_tests_rule(Rule::ctlop_TEST, passes); + fails_tests_rule(Rule::ctlop_TEST, fails); } #[test] @@ -137,15 +144,8 @@ fn check_rangeop() { let passes = RANGEOP_PASSES; let fails = RANGEOP_FAILS; - for test in passes { - let parse = CDDLTestParser::parse(Rule::rangeop_TEST, test); - assert!(parse.is_ok()); - } - - for test in fails { - let parse = CDDLTestParser::parse(Rule::rangeop_TEST, test); - assert!(parse.is_err()); - } + passes_tests_rule(Rule::rangeop_TEST, passes); + fails_tests_rule(Rule::rangeop_TEST, fails); } #[test] @@ -155,15 +155,8 @@ fn check_type2() { let passes = TYPE2_PASSES; let fails = TYPE2_FAILS; - for test in passes { - let parse = CDDLTestParser::parse(Rule::type2_TEST, test); - assert!(parse.is_ok()); - } - - for test in fails { - let parse = CDDLTestParser::parse(Rule::type2_TEST, test); - assert!(parse.is_err()); - } + passes_tests_rule(Rule::type2_TEST, passes); + fails_tests_rule(Rule::type2_TEST, fails); } #[test] @@ -173,15 +166,8 @@ fn check_type1() { let passes = TYPE1_PASSES; let fails = TYPE1_FAILS; - for test in passes { - let parse = CDDLTestParser::parse(Rule::type1_TEST, test); - assert!(parse.is_ok()); - } - - for test in fails { - let parse = CDDLTestParser::parse(Rule::type1_TEST, test); - assert!(parse.is_err()); - } + passes_tests_rule(Rule::type1_TEST, passes); + fails_tests_rule(Rule::type1_TEST, fails); } #[test] @@ -213,15 +199,8 @@ fn check_type() { let passes = TYPE_PASSES; let fails = TYPE_FAILS; - for test in passes { - let parse = CDDLTestParser::parse(Rule::type_TEST, test); - assert!(parse.is_ok()); - } - - for test in fails { - let parse = CDDLTestParser::parse(Rule::type_TEST, test); - assert!(parse.is_err()); - } + passes_tests_rule(Rule::type_TEST, passes); + fails_tests_rule(Rule::type_TEST, fails); } #[test] From e1bf0ba9e3293dddef0e882b0eeb8b8990c96eec Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Wed, 24 Jan 2024 22:34:02 +0700 Subject: [PATCH 27/43] fix: error msg for cddl --- hermes/crates/cbork/cddl-parser/tests/cddl.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl.rs b/hermes/crates/cbork/cddl-parser/tests/cddl.rs index 33de3b6e0..4e1d7f7d1 100644 --- a/hermes/crates/cbork/cddl-parser/tests/cddl.rs +++ b/hermes/crates/cbork/cddl-parser/tests/cddl.rs @@ -23,7 +23,7 @@ fn parse_cddl_files() { // test for valid files let mut err_messages = vec![]; for file_path in valid_file_paths { - let mut content = fs::read_to_string(&file_path).expect("failed to read a file"); + let mut content = fs::read_to_string(&file_path).expect(&format!("failed to read `{:?}`", &file_path)); if let Err(e) = parse_cddl(&mut content, &Extension::RFC8610Parser) { err_messages.push(format!("{}) {file_path:?} {e}", err_messages.len() + 1)); @@ -32,7 +32,7 @@ fn parse_cddl_files() { // test for invalid files for file_path in invalid_file_paths { - let mut content = fs::read_to_string(&file_path).expect("failed to read a file"); + let mut content = fs::read_to_string(&file_path).expect(&format!("failed to read `{:?}`", &file_path)); let result = parse_cddl(&mut content, &Extension::RFC8610Parser); From d391c814515ebb1e7e30c3cadebe482d56d01a42 Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Wed, 24 Jan 2024 23:19:15 +0700 Subject: [PATCH 28/43] fix: cddl test file name reading --- hermes/crates/cbork/cddl-parser/tests/cddl.rs | 12 +++++++----- .../tests/cddl/invalid_rfc8610_sample_1.cddl | 2 +- .../tests/cddl/invalid_rfc8610_sample_4.cddl | 2 ++ .../tests/cddl/invalid_rfc8610_sample_5.cddl | 1 + .../tests/cddl/invalid_rfc8610_sample_6.cddl | 1 + .../tests/cddl/invalid_rfc8610_sample_7.cddl | 1 + .../tests/cddl/invalid_rfc8610_sample_8.cddl | 1 + .../tests/cddl/invalid_rfc8610_sample_9.cddl | 2 ++ 8 files changed, 16 insertions(+), 6 deletions(-) create mode 100644 hermes/crates/cbork/cddl-parser/tests/cddl/invalid_rfc8610_sample_4.cddl create mode 100644 hermes/crates/cbork/cddl-parser/tests/cddl/invalid_rfc8610_sample_5.cddl create mode 100644 hermes/crates/cbork/cddl-parser/tests/cddl/invalid_rfc8610_sample_6.cddl create mode 100644 hermes/crates/cbork/cddl-parser/tests/cddl/invalid_rfc8610_sample_7.cddl create mode 100644 hermes/crates/cbork/cddl-parser/tests/cddl/invalid_rfc8610_sample_8.cddl create mode 100644 hermes/crates/cbork/cddl-parser/tests/cddl/invalid_rfc8610_sample_9.cddl diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl.rs b/hermes/crates/cbork/cddl-parser/tests/cddl.rs index 4e1d7f7d1..c08c50e64 100644 --- a/hermes/crates/cbork/cddl-parser/tests/cddl.rs +++ b/hermes/crates/cbork/cddl-parser/tests/cddl.rs @@ -1,4 +1,4 @@ -use std::{fs, io::Result}; +use std::{ffi::OsStr, fs, io::Result}; use cddl_parser::{parse_cddl, Extension}; @@ -15,10 +15,10 @@ fn parse_cddl_files() { let valid_file_paths = file_paths .iter() - .filter(|p| matches!(p.to_str().map(|p| p.starts_with("valid")), Some(true))); + .filter(|p| matches!(p.file_name().and_then(OsStr::to_str).map(|p| p.starts_with("valid")), Some(true))); let invalid_file_paths = file_paths .iter() - .filter(|p| matches!(p.to_str().map(|p| p.starts_with("invalid")), Some(true))); + .filter(|p| matches!(p.file_name().and_then(OsStr::to_str).map(|p| p.starts_with("invalid")), Some(true))); // test for valid files let mut err_messages = vec![]; @@ -36,12 +36,14 @@ fn parse_cddl_files() { let result = parse_cddl(&mut content, &Extension::RFC8610Parser); - assert!(result.is_err()); + if result.is_ok() { + panic!("{:?} is expected to fail", &file_path); + } } // summary let err_msg = err_messages.join("\n\n"); if !err_msg.is_empty() { - panic!("{err_msg}") + panic!("{err_msg}"); } } diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/invalid_rfc8610_sample_1.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/invalid_rfc8610_sample_1.cddl index 4e825ea28..f0b22f1e4 100644 --- a/hermes/crates/cbork/cddl-parser/tests/cddl/invalid_rfc8610_sample_1.cddl +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/invalid_rfc8610_sample_1.cddl @@ -1 +1 @@ -unlimited-people = [* person \ No newline at end of file +parentheses_mismatch = ( int \ No newline at end of file diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/invalid_rfc8610_sample_4.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/invalid_rfc8610_sample_4.cddl new file mode 100644 index 000000000..615c9c7f3 --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/invalid_rfc8610_sample_4.cddl @@ -0,0 +1,2 @@ +outer = { inner: int } +incorrectly_nested = outer inner: int \ No newline at end of file diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/invalid_rfc8610_sample_5.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/invalid_rfc8610_sample_5.cddl new file mode 100644 index 000000000..494d46c73 --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/invalid_rfc8610_sample_5.cddl @@ -0,0 +1 @@ +missing_definition \ No newline at end of file diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/invalid_rfc8610_sample_6.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/invalid_rfc8610_sample_6.cddl new file mode 100644 index 000000000..6120ddd74 --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/invalid_rfc8610_sample_6.cddl @@ -0,0 +1 @@ +invalid_char = int & \ No newline at end of file diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/invalid_rfc8610_sample_7.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/invalid_rfc8610_sample_7.cddl new file mode 100644 index 000000000..a89f99313 --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/invalid_rfc8610_sample_7.cddl @@ -0,0 +1 @@ +empty_rule = \ No newline at end of file diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/invalid_rfc8610_sample_8.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/invalid_rfc8610_sample_8.cddl new file mode 100644 index 000000000..0ad76396f --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/invalid_rfc8610_sample_8.cddl @@ -0,0 +1 @@ +unexpected_token = % \ No newline at end of file diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/invalid_rfc8610_sample_9.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/invalid_rfc8610_sample_9.cddl new file mode 100644 index 000000000..ebad7d015 --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/invalid_rfc8610_sample_9.cddl @@ -0,0 +1,2 @@ +missing_group_open int +missing_group_close = int \ No newline at end of file From 6da42d3cf0b6135f3243cfa14e4a841c147b62da Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Wed, 24 Jan 2024 23:46:34 +0700 Subject: [PATCH 29/43] fix: cspell --- .../cddl-parser/src/grammar/cddl_test.pest | 4 ++- .../cddl-parser/src/grammar/rfc_8610.pest | 4 +-- hermes/crates/cbork/cddl-parser/src/lib.rs | 6 ++-- .../cbork/cddl-parser/tests/byte_sequences.rs | 2 +- hermes/crates/cbork/cddl-parser/tests/cddl.rs | 28 +++++++++++++------ .../tests/cddl/invalid_rfc8610_sample_2.cddl | 2 ++ .../tests/cddl/invalid_rfc8610_sample_3.cddl | 2 ++ .../tests/cddl/valid_rfc8610_arrays.cddl | 2 ++ .../tests/cddl/valid_rfc8610_ctrl_bits.cddl | 2 ++ .../tests/cddl/valid_rfc8610_ctrl_regexp.cddl | 2 ++ .../cddl/valid_rfc8610_cuts_in_maps.cddl | 2 ++ .../valid_rfc8610_cuts_in_maps_alt_1.cddl | 2 ++ .../valid_rfc8610_cuts_in_maps_alt_2.cddl | 2 ++ .../valid_rfc8610_cuts_in_maps_alt_3.cddl | 2 ++ .../cddl/valid_rfc8610_group_to_choice.cddl | 2 ++ .../valid_rfc8610_represenation_types.cddl | 2 ++ .../cddl/valid_rfc8610_reputon_compact.cddl | 2 ++ .../cddl/valid_rfc8610_reputon_full.cddl | 2 ++ .../tests/cddl/valid_rfc8610_simple_1.cddl | 2 ++ .../tests/cddl/valid_rfc8610_simple_2.cddl | 2 ++ .../tests/cddl/valid_rfc8610_simple_3.cddl | 2 ++ .../tests/cddl/valid_rfc8610_simple_4.cddl | 2 ++ .../tests/cddl/valid_rfc8610_simple_5.cddl | 2 ++ .../tests/cddl/valid_rfc8610_simple_6.cddl | 2 ++ .../tests/cddl/valid_rfc8610_simple_7.cddl | 2 ++ .../tests/cddl/valid_rfc8610_simple_8.cddl | 2 ++ .../tests/cddl/valid_rfc8610_simple_9.cddl | 2 ++ .../cddl/valid_rfc8610_socket_plug_ext.cddl | 2 ++ .../tests/cddl/valid_rfc8610_structs.cddl | 2 ++ .../tests/cddl/valid_rfc8610_structs_alt.cddl | 2 ++ .../tests/cddl/valid_rfc8610_tables.cddl | 2 ++ .../tests/cddl/valid_rfc8610_tags.cddl | 2 ++ .../cbork/cddl-parser/tests/group_elements.rs | 3 ++ .../crates/cbork/cddl-parser/tests/rules.rs | 3 ++ .../cddl-parser/tests/type_declarations.rs | 3 ++ 35 files changed, 91 insertions(+), 16 deletions(-) diff --git a/hermes/crates/cbork/cddl-parser/src/grammar/cddl_test.pest b/hermes/crates/cbork/cddl-parser/src/grammar/cddl_test.pest index c2557e02e..4eb04bda5 100644 --- a/hermes/crates/cbork/cddl-parser/src/grammar/cddl_test.pest +++ b/hermes/crates/cbork/cddl-parser/src/grammar/cddl_test.pest @@ -3,7 +3,9 @@ // Test Expressions ONLY TO Be USED by Unit Tests. // Extends `cddl.pest` with rules needed to properly check sub-rules. -// cspell: words intfloat hexfloat +// cspell: words intfloat hexfloat groupname assignt +// cspell: words assigng genericparm genericarg rangeop ctlop +// cspell: words grpchoice grpent memberkey bareword optcom /// Test Expression for the `rule` Rule. rule_TEST = ${ SOI ~ rule ~ EOI } diff --git a/hermes/crates/cbork/cddl-parser/src/grammar/rfc_8610.pest b/hermes/crates/cbork/cddl-parser/src/grammar/rfc_8610.pest index 361144be6..442a0efae 100644 --- a/hermes/crates/cbork/cddl-parser/src/grammar/rfc_8610.pest +++ b/hermes/crates/cbork/cddl-parser/src/grammar/rfc_8610.pest @@ -1,9 +1,9 @@ //! CDDL Grammar adapted from RFC8610 Appendix B //! https://www.rfc-editor.org/rfc/rfc8610#appendix-B -// cspell: words assignt groupname grpent genericparm assigng +// cspell: words assignt groupname grpent genericparm assigng optcom // cspell: words genericarg rangeop ctlop grpchoice memberkey bareword hexfloat intfloat -// cspell: words SCHAR BCHAR PCHAR SESC FFFD Characterset Visiable +// cspell: words SCHAR BCHAR PCHAR SESC FFFD Characterset Visiable cddl = ${ SOI diff --git a/hermes/crates/cbork/cddl-parser/src/lib.rs b/hermes/crates/cbork/cddl-parser/src/lib.rs index ec5fd8e30..d4d4ca771 100644 --- a/hermes/crates/cbork/cddl-parser/src/lib.rs +++ b/hermes/crates/cbork/cddl-parser/src/lib.rs @@ -114,9 +114,7 @@ pub struct CDDLError(CDDLErrorType); /// let result = parse_cddl(&mut input, &Extension::CDDLParser); /// assert!(result.is_ok()); /// ``` -pub fn parse_cddl<'a>( - input: &'a mut String, extension: &Extension, -) -> Result, CDDLError> { +pub fn parse_cddl<'a>(input: &'a mut String, extension: &Extension) -> Result, CDDLError> { input.push_str("\n\n"); input.push_str(POSTLUDE); @@ -138,7 +136,7 @@ pub fn parse_cddl<'a>( }, }; - result.map_err( CDDLError::from) + result.map_err(CDDLError::from) } #[cfg(test)] diff --git a/hermes/crates/cbork/cddl-parser/tests/byte_sequences.rs b/hermes/crates/cbork/cddl-parser/tests/byte_sequences.rs index b04aef9ca..20caf97af 100644 --- a/hermes/crates/cbork/cddl-parser/tests/byte_sequences.rs +++ b/hermes/crates/cbork/cddl-parser/tests/byte_sequences.rs @@ -1,4 +1,4 @@ -// cspell: words hexpair rstuvw abcdefghijklmnopqrstuvwyz rstuvw +// cspell: words hexpair rstuvw abcdefghijklmnopqrstuvwyz rstuvw Xhhb Bhcm use cddl_parser::{ self, diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl.rs b/hermes/crates/cbork/cddl-parser/tests/cddl.rs index c08c50e64..a7ed6a765 100644 --- a/hermes/crates/cbork/cddl-parser/tests/cddl.rs +++ b/hermes/crates/cbork/cddl-parser/tests/cddl.rs @@ -13,17 +13,28 @@ fn parse_cddl_files() { file_paths.sort(); - let valid_file_paths = file_paths - .iter() - .filter(|p| matches!(p.file_name().and_then(OsStr::to_str).map(|p| p.starts_with("valid")), Some(true))); - let invalid_file_paths = file_paths - .iter() - .filter(|p| matches!(p.file_name().and_then(OsStr::to_str).map(|p| p.starts_with("invalid")), Some(true))); + let valid_file_paths = file_paths.iter().filter(|p| { + matches!( + p.file_name() + .and_then(OsStr::to_str) + .map(|p| p.starts_with("valid")), + Some(true) + ) + }); + let invalid_file_paths = file_paths.iter().filter(|p| { + matches!( + p.file_name() + .and_then(OsStr::to_str) + .map(|p| p.starts_with("invalid")), + Some(true) + ) + }); // test for valid files let mut err_messages = vec![]; for file_path in valid_file_paths { - let mut content = fs::read_to_string(&file_path).expect(&format!("failed to read `{:?}`", &file_path)); + let mut content = + fs::read_to_string(&file_path).expect(&format!("failed to read `{:?}`", &file_path)); if let Err(e) = parse_cddl(&mut content, &Extension::RFC8610Parser) { err_messages.push(format!("{}) {file_path:?} {e}", err_messages.len() + 1)); @@ -32,7 +43,8 @@ fn parse_cddl_files() { // test for invalid files for file_path in invalid_file_paths { - let mut content = fs::read_to_string(&file_path).expect(&format!("failed to read `{:?}`", &file_path)); + let mut content = + fs::read_to_string(&file_path).expect(&format!("failed to read `{:?}`", &file_path)); let result = parse_cddl(&mut content, &Extension::RFC8610Parser); diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/invalid_rfc8610_sample_2.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/invalid_rfc8610_sample_2.cddl index fab5a16b5..e05f50fdf 100644 --- a/hermes/crates/cbork/cddl-parser/tests/cddl/invalid_rfc8610_sample_2.cddl +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/invalid_rfc8610_sample_2.cddl @@ -1,2 +1,4 @@ +; cspell: words tstr + sample = { tstr => tstr \ No newline at end of file diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/invalid_rfc8610_sample_3.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/invalid_rfc8610_sample_3.cddl index 9c1d92eea..cc09425d0 100644 --- a/hermes/crates/cbork/cddl-parser/tests/cddl/invalid_rfc8610_sample_3.cddl +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/invalid_rfc8610_sample_3.cddl @@ -1,3 +1,5 @@ +; cspell: words tstr + sample = ; comment here { tstr => tstr } \ No newline at end of file diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_arrays.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_arrays.cddl index 2091e4d4a..04465a589 100644 --- a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_arrays.cddl +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_arrays.cddl @@ -1,5 +1,7 @@ ; 3.4. Arrays +; cspell: words tstr + unlimited-people = [* person] one-or-two-people = [1*2 person] at-least-two-people = [2* person] diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_ctrl_bits.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_ctrl_bits.cddl index b44d5bc73..096b876e7 100644 --- a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_ctrl_bits.cddl +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_ctrl_bits.cddl @@ -1,5 +1,7 @@ ; 3.8.2. Control Operator .bits +; cspell: words tcpflagbytes rwxbits + tcpflagbytes = bstr .bits flags flags = &( fin: 8, diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_ctrl_regexp.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_ctrl_regexp.cddl index a2293acd1..04c4f6a61 100644 --- a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_ctrl_regexp.cddl +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_ctrl_regexp.cddl @@ -1,5 +1,7 @@ ; 3.8.3. Control Operator .regexp +; cspell: words tstr + nai = tstr .regexp "[A-Za-z0-9]+@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)+" ; "N1@CH57HF.4Znqe0.dYJRN.igjf" should pass this regexp \ No newline at end of file diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_cuts_in_maps.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_cuts_in_maps.cddl index d83ae9a00..e2b49c184 100644 --- a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_cuts_in_maps.cddl +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_cuts_in_maps.cddl @@ -1,5 +1,7 @@ ; 3.5.4. Cuts in Maps +; cspell: words tstr + extensible-map-example = { ? "optional-key" => int, * tstr => any diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_cuts_in_maps_alt_1.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_cuts_in_maps_alt_1.cddl index c23b012ff..ac73c3649 100644 --- a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_cuts_in_maps_alt_1.cddl +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_cuts_in_maps_alt_1.cddl @@ -1,5 +1,7 @@ ; 3.5.4. Cuts in Maps +; cspell: words tstr + extensible-map-example = { ? "optional-key" ^ => int, * tstr => any diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_cuts_in_maps_alt_2.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_cuts_in_maps_alt_2.cddl index 8f077be00..1283a7c7d 100644 --- a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_cuts_in_maps_alt_2.cddl +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_cuts_in_maps_alt_2.cddl @@ -1,5 +1,7 @@ ; 3.5.4. Cuts in Maps +; cspell: words tstr + extensible-map-example = { ? "optional-key": int, * tstr => any diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_cuts_in_maps_alt_3.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_cuts_in_maps_alt_3.cddl index d8fdb61d3..f8e239051 100644 --- a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_cuts_in_maps_alt_3.cddl +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_cuts_in_maps_alt_3.cddl @@ -1,5 +1,7 @@ ; 3.5.4. Cuts in Maps +; cspell: words tstr + extensible-map-example = { ? optional-key: int, * tstr => any diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_group_to_choice.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_group_to_choice.cddl index 3c3c066ee..e172f919f 100644 --- a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_group_to_choice.cddl +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_group_to_choice.cddl @@ -1,5 +1,7 @@ ; 2.2.2.2. Turning a Group into a Choice +; cspell: words basecolors + terminal-color = &basecolors basecolors = ( black: 0, red: 1, green: 2, yellow: 3, diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_represenation_types.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_represenation_types.cddl index 3ef42d9be..dfd0f4069 100644 --- a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_represenation_types.cddl +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_represenation_types.cddl @@ -1,5 +1,7 @@ ; 2.2.3. Representation Types +; cspell: words cbor tstr + my_breakfast = #6.55799(breakfast) ; cbor-any is too general! breakfast = cereal / porridge cereal = #6.998(tstr) diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_reputon_compact.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_reputon_compact.cddl index 856c517df..350acd521 100644 --- a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_reputon_compact.cddl +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_reputon_compact.cddl @@ -1,3 +1,5 @@ +; cspell: words reputons reputon + reputation-object = { application: text reputons: [* reputon] diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_reputon_full.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_reputon_full.cddl index 3da6a1b7f..9fdf25462 100644 --- a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_reputon_full.cddl +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_reputon_full.cddl @@ -1,3 +1,5 @@ +; cspell: words reputons reputon + reputation-object = { reputation-context, reputon-list diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_1.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_1.cddl index 4ac9fbdf3..a3a5a0160 100644 --- a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_1.cddl +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_1.cddl @@ -1,5 +1,7 @@ ; Figure 1: Using a Group Directly in a Map +; cspell: words tstr + person = { age: int, name: tstr, diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_2.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_2.cddl index 899f96bb1..73da918a3 100644 --- a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_2.cddl +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_2.cddl @@ -1,5 +1,7 @@ ; Figure 2: A Basic Group +; cspell: words tstr + pii = ( age: int, name: tstr, diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_3.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_3.cddl index 7e2bfb0ec..cb53425a1 100644 --- a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_3.cddl +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_3.cddl @@ -1,5 +1,7 @@ ; Figure 3: Using a Group by Name +; cspell: words tstr + person = { pii } diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_4.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_4.cddl index 2934dcbcb..d787a8838 100644 --- a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_4.cddl +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_4.cddl @@ -1,5 +1,7 @@ ; Figure 4: Using a Parenthesized Group in a Map +; cspell: words tstr + person = {( age: int, name: tstr, diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_5.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_5.cddl index 8a3253026..deae51f7e 100644 --- a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_5.cddl +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_5.cddl @@ -1,5 +1,7 @@ ; Figure 5: Maps with Copy/Paste +; cspell: words tstr + person = { age: int, name: tstr, diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_6.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_6.cddl index 307d13510..1be65c13e 100644 --- a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_6.cddl +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_6.cddl @@ -1,5 +1,7 @@ ; Figure 6: Using a Group for Factorization +; cspell: words tstr + person = { identity, employer: tstr, diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_7.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_7.cddl index 6e7568153..0bf9dba22 100644 --- a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_7.cddl +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_7.cddl @@ -1,5 +1,7 @@ ; 2.2.2. Choices +; cspell: words tstr + attire = "bow tie" / "necktie" / "Internet attire" protocol = 6 / 17 diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_8.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_8.cddl index b7999d21b..d55779ca6 100644 --- a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_8.cddl +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_8.cddl @@ -1,5 +1,7 @@ ; 2.2.2. Choices +; cspell: words tstr + attire /= "swimwear" delivery //= ( diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_9.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_9.cddl index 832caf170..517e44c45 100644 --- a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_9.cddl +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_simple_9.cddl @@ -1,3 +1,5 @@ +; cspell: words tstr bareword + ; This is a comment person = { g } diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_socket_plug_ext.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_socket_plug_ext.cddl index 8725ca015..70d981cc1 100644 --- a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_socket_plug_ext.cddl +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_socket_plug_ext.cddl @@ -1,5 +1,7 @@ ; 3.9. Socket/Plug +; cspell: words tstr personaldata shoesize + PersonalData = { ? displayName: tstr, NameComponents, diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_structs.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_structs.cddl index fa374d67b..f58d9f46a 100644 --- a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_structs.cddl +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_structs.cddl @@ -1,5 +1,7 @@ ; 3.5.1. Structs +; cspell: words tstr + Geography = [ city : tstr, gpsCoordinates : GpsCoordinates, diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_structs_alt.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_structs_alt.cddl index 569ef728d..4787b9674 100644 --- a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_structs_alt.cddl +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_structs_alt.cddl @@ -1,5 +1,7 @@ ; 3.5.1. Structs +; cspell: words tstr + Geography = [ city : tstr, gpsCoordinates : GpsCoordinates, diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_tables.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_tables.cddl index 139ca7599..bce2eab8c 100644 --- a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_tables.cddl +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_tables.cddl @@ -1,5 +1,7 @@ ; 3.5.2. Tables +; cspell: words tstr tostring mynumber + square-roots = {* x => y} x = int y = float diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_tags.cddl b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_tags.cddl index ad55a45d4..471f76cd8 100644 --- a/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_tags.cddl +++ b/hermes/crates/cbork/cddl-parser/tests/cddl/valid_rfc8610_tags.cddl @@ -1,5 +1,7 @@ ; 3.6. Tags +; cspell: words biguint buuid tstr + biguint = #6.2(bstr) buuid = #6.37(bstr) diff --git a/hermes/crates/cbork/cddl-parser/tests/group_elements.rs b/hermes/crates/cbork/cddl-parser/tests/group_elements.rs index d1b9e93d6..82d79efd2 100644 --- a/hermes/crates/cbork/cddl-parser/tests/group_elements.rs +++ b/hermes/crates/cbork/cddl-parser/tests/group_elements.rs @@ -1,3 +1,6 @@ +// cspell: words OPTCOM MEMBERKEY bareword tstr GRPENT GRPCHOICE +// cspell: words optcom memberkey grpent grpchoice + use cddl_parser::{ self, cddl_test::{CDDLTestParser, Parser, Rule}, diff --git a/hermes/crates/cbork/cddl-parser/tests/rules.rs b/hermes/crates/cbork/cddl-parser/tests/rules.rs index c93baa1d6..e91daa76e 100644 --- a/hermes/crates/cbork/cddl-parser/tests/rules.rs +++ b/hermes/crates/cbork/cddl-parser/tests/rules.rs @@ -1,3 +1,6 @@ +// cspell: words GENERICARG bigfloat ASSIGNG GROUPNAME tstr genericarg GENERICARG +// cspell: words assigng assignt ASSIGNT GENERICPARM genericparm + use cddl_parser::{ self, cddl_test::{CDDLTestParser, Parser, Rule}, diff --git a/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs b/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs index 51195cde3..0c6f9310b 100644 --- a/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs +++ b/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs @@ -1,3 +1,6 @@ +// cspell: words CTLOP aname groupsocket typesocket RANGEOP tstr ctlop +// cspell: words rangeop RANGEOP + use cddl_parser::{ self, cddl_test::{CDDLTestParser, Parser, Rule}, From dd7f8a4187b5fc09f857529ed1e802e0ced7eb84 Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Thu, 25 Jan 2024 00:15:23 +0700 Subject: [PATCH 30/43] chore: lintfix --- hermes/crates/cbork/cddl-parser/src/lib.rs | 6 ++++-- .../cbork/cddl-parser/tests/byte_sequences.rs | 2 ++ hermes/crates/cbork/cddl-parser/tests/cddl.rs | 15 +++++---------- hermes/crates/cbork/cddl-parser/tests/comments.rs | 2 ++ .../cbork/cddl-parser/tests/group_elements.rs | 2 ++ .../cbork/cddl-parser/tests/literal_values.rs | 14 ++++++++------ hermes/crates/cbork/cddl-parser/tests/rules.rs | 10 ++++++---- .../cbork/cddl-parser/tests/text_sequences.rs | 2 ++ .../cbork/cddl-parser/tests/type_declarations.rs | 10 ++++++++-- 9 files changed, 39 insertions(+), 24 deletions(-) diff --git a/hermes/crates/cbork/cddl-parser/src/lib.rs b/hermes/crates/cbork/cddl-parser/src/lib.rs index d4d4ca771..6f67d1123 100644 --- a/hermes/crates/cbork/cddl-parser/src/lib.rs +++ b/hermes/crates/cbork/cddl-parser/src/lib.rs @@ -114,7 +114,9 @@ pub struct CDDLError(CDDLErrorType); /// let result = parse_cddl(&mut input, &Extension::CDDLParser); /// assert!(result.is_ok()); /// ``` -pub fn parse_cddl<'a>(input: &'a mut String, extension: &Extension) -> Result, CDDLError> { +pub fn parse_cddl<'a>( + input: &'a mut String, extension: &Extension, +) -> Result, Box> { input.push_str("\n\n"); input.push_str(POSTLUDE); @@ -136,7 +138,7 @@ pub fn parse_cddl<'a>(input: &'a mut String, extension: &Extension) -> Result = VALUE_PASSES - .into_iter() - .chain(NUMBER_PASSES.into_iter()) - .chain(BYTES_PASSES.into_iter()) - .chain(TEXT_PASSES.into_iter()) + .iter() + .chain(NUMBER_PASSES) + .chain(BYTES_PASSES) + .chain(TEXT_PASSES) .map(Deref::deref) .collect(); let fails: Vec<_> = VALUE_FAILS - .into_iter() - .chain(NUMBER_FAILS.into_iter()) + .iter() + .chain(NUMBER_FAILS) .map(Deref::deref) .collect(); diff --git a/hermes/crates/cbork/cddl-parser/tests/rules.rs b/hermes/crates/cbork/cddl-parser/tests/rules.rs index e91daa76e..472f93820 100644 --- a/hermes/crates/cbork/cddl-parser/tests/rules.rs +++ b/hermes/crates/cbork/cddl-parser/tests/rules.rs @@ -44,13 +44,13 @@ pub const ASSIGNT_PASSES: &[&str] = &["=", "/="]; pub const ASSIGNT_FAILS: &[&str] = &["==", "//="]; -pub const TYPENAME_PASSES: &[&str] = &ID_PASSES; +pub const TYPENAME_PASSES: &[&str] = ID_PASSES; -pub const TYPENAME_FAILS: &[&str] = &ID_FAILS; +pub const TYPENAME_FAILS: &[&str] = ID_FAILS; -pub const GROUPNAME_PASSES: &[&str] = &ID_PASSES; +pub const GROUPNAME_PASSES: &[&str] = ID_PASSES; -pub const GROUPNAME_FAILS: &[&str] = &ID_FAILS; +pub const GROUPNAME_FAILS: &[&str] = ID_FAILS; pub const RULE_GROUP_PASSES: &[&str] = &[ "foo = (bar: baz)", @@ -64,6 +64,7 @@ pub const RULE_GROUP_PASSES: &[&str] = &[ pub const RULE_GROUP_FAILS: &[&str] = &["foo = bar: baz", "t /= (foo: bar)"]; +/// # Panics pub fn passes_tests_rule(rule_type: Rule, test_data: &[&str]) { for test in test_data { let parse = CDDLTestParser::parse(rule_type, test); @@ -71,6 +72,7 @@ pub fn passes_tests_rule(rule_type: Rule, test_data: &[&str]) { } } +/// # Panics pub fn fails_tests_rule(rule_type: Rule, test_data: &[&str]) { for test in test_data { let parse = CDDLTestParser::parse(rule_type, test); diff --git a/hermes/crates/cbork/cddl-parser/tests/text_sequences.rs b/hermes/crates/cbork/cddl-parser/tests/text_sequences.rs index 14d41a110..08912812f 100644 --- a/hermes/crates/cbork/cddl-parser/tests/text_sequences.rs +++ b/hermes/crates/cbork/cddl-parser/tests/text_sequences.rs @@ -8,6 +8,7 @@ pub const S_FAILS: &[&str] = &[" a ", "zz", " \t d \t", " \t \r \n \t \r\n x"] pub const TEXT_PASSES: &[&str] = &[r#""""#, r#""abc""#, "\"abc\\n\""]; pub const TEXT_FAILS: &[&str] = &["", "''", "\"abc\n\""]; +/// # Panics pub fn passes_tests_rule(rule_type: Rule, test_data: &[&str]) { for test in test_data { let parse = CDDLTestParser::parse(rule_type, test); @@ -15,6 +16,7 @@ pub fn passes_tests_rule(rule_type: Rule, test_data: &[&str]) { } } +/// # Panics pub fn fails_tests_rule(rule_type: Rule, test_data: &[&str]) { for test in test_data { let parse = CDDLTestParser::parse(rule_type, test); diff --git a/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs b/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs index 0c6f9310b..5a7f8c7e9 100644 --- a/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs +++ b/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs @@ -115,6 +115,7 @@ pub const TYPE_PASSES: &[&str] = &[ pub const TYPE_FAILS: &[&str] = &["", "1 \\ 2", "1 // 2", "1 2", "1 / 2 3"]; +/// # Panics pub fn passes_tests_rule(rule_type: Rule, test_data: &[&str]) { for test in test_data { let parse = CDDLTestParser::parse(rule_type, test); @@ -122,6 +123,7 @@ pub fn passes_tests_rule(rule_type: Rule, test_data: &[&str]) { } } +/// # Panics pub fn fails_tests_rule(rule_type: Rule, test_data: &[&str]) { for test in test_data { let parse = CDDLTestParser::parse(rule_type, test); @@ -176,8 +178,9 @@ fn check_type1() { #[test] /// Test if the `type1` rule passes properly based on composition of type2 test cases. fn check_type1_composition() { + let j_len = CTLOP_PASSES.len() + RANGEOP_PASSES.len(); for (i, test_i) in [TYPE2_PASSES, TYPE_FAILS].into_iter().flatten().enumerate() { - for (_, test_j) in [CTLOP_PASSES, RANGEOP_PASSES] + for (j, test_j) in [CTLOP_PASSES, RANGEOP_PASSES] .into_iter() .flatten() .enumerate() @@ -185,7 +188,10 @@ fn check_type1_composition() { for (k, test_k) in [TYPE2_PASSES, TYPE_FAILS].into_iter().flatten().enumerate() { let input = [test_i.to_owned(), test_j.to_owned(), test_k.to_owned()].join(" "); let parse = CDDLTestParser::parse(Rule::type1_TEST, &input); - if (0..TYPE2_PASSES.len()).contains(&i) && (0..TYPE2_PASSES.len()).contains(&k) { + if (0..TYPE2_PASSES.len()).contains(&i) + && (0..j_len).contains(&j) + && (0..TYPE2_PASSES.len()).contains(&k) + { assert!(parse.is_ok()); } else { assert!(parse.is_err()); From 07eed72b1e9741283689d59e144858b4cf700520 Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Thu, 25 Jan 2024 00:19:07 +0700 Subject: [PATCH 31/43] refactor: cddl filter fn --- hermes/crates/cbork/cddl-parser/tests/cddl.rs | 24 +++++++------------ 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl.rs b/hermes/crates/cbork/cddl-parser/tests/cddl.rs index 922be67ec..388cfb196 100644 --- a/hermes/crates/cbork/cddl-parser/tests/cddl.rs +++ b/hermes/crates/cbork/cddl-parser/tests/cddl.rs @@ -14,22 +14,14 @@ fn parse_cddl_files() { file_paths.sort(); - let valid_file_paths = file_paths.iter().filter(|p| { - matches!( - p.file_name() - .and_then(OsStr::to_str) - .map(|p| p.starts_with("valid")), - Some(true) - ) - }); - let invalid_file_paths = file_paths.iter().filter(|p| { - matches!( - p.file_name() - .and_then(OsStr::to_str) - .map(|p| p.starts_with("invalid")), - Some(true) - ) - }); + let valid_file_paths = file_paths + .iter() + .filter_map(|p| p.file_name().and_then(OsStr::to_str)) + .filter(|p| p.starts_with("valid")); + let invalid_file_paths = file_paths + .iter() + .filter_map(|p| p.file_name().and_then(OsStr::to_str)) + .filter(|p| p.starts_with("invalid")); // test for valid files let mut err_messages = vec![]; From edf532ea4f750dda099ac251059d2adc48dab5e6 Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Thu, 25 Jan 2024 00:20:36 +0700 Subject: [PATCH 32/43] refactor: reset --- hermes/crates/cbork/cddl-parser/tests/cddl.rs | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl.rs b/hermes/crates/cbork/cddl-parser/tests/cddl.rs index 388cfb196..922be67ec 100644 --- a/hermes/crates/cbork/cddl-parser/tests/cddl.rs +++ b/hermes/crates/cbork/cddl-parser/tests/cddl.rs @@ -14,14 +14,22 @@ fn parse_cddl_files() { file_paths.sort(); - let valid_file_paths = file_paths - .iter() - .filter_map(|p| p.file_name().and_then(OsStr::to_str)) - .filter(|p| p.starts_with("valid")); - let invalid_file_paths = file_paths - .iter() - .filter_map(|p| p.file_name().and_then(OsStr::to_str)) - .filter(|p| p.starts_with("invalid")); + let valid_file_paths = file_paths.iter().filter(|p| { + matches!( + p.file_name() + .and_then(OsStr::to_str) + .map(|p| p.starts_with("valid")), + Some(true) + ) + }); + let invalid_file_paths = file_paths.iter().filter(|p| { + matches!( + p.file_name() + .and_then(OsStr::to_str) + .map(|p| p.starts_with("invalid")), + Some(true) + ) + }); // test for valid files let mut err_messages = vec![]; From 4a7720399ba24f4a84afcd677c6dd172cc053d08 Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Thu, 25 Jan 2024 09:14:19 +0700 Subject: [PATCH 33/43] fix: pub(crate) for unit tests --- .../cbork/cddl-parser/tests/byte_sequences.rs | 16 +++++----- .../cbork/cddl-parser/tests/comments.rs | 12 +++---- .../cbork/cddl-parser/tests/group_elements.rs | 28 ++++++++-------- .../cbork/cddl-parser/tests/identifiers.rs | 4 +-- .../cbork/cddl-parser/tests/literal_values.rs | 28 ++++++++-------- .../crates/cbork/cddl-parser/tests/rules.rs | 32 +++++++++---------- .../cbork/cddl-parser/tests/text_sequences.rs | 12 +++---- .../cddl-parser/tests/type_declarations.rs | 24 +++++++------- 8 files changed, 78 insertions(+), 78 deletions(-) diff --git a/hermes/crates/cbork/cddl-parser/tests/byte_sequences.rs b/hermes/crates/cbork/cddl-parser/tests/byte_sequences.rs index b90cc604a..04b7ac529 100644 --- a/hermes/crates/cbork/cddl-parser/tests/byte_sequences.rs +++ b/hermes/crates/cbork/cddl-parser/tests/byte_sequences.rs @@ -5,16 +5,16 @@ use cddl_parser::{ cddl_test::{CDDLTestParser, Parser, Rule}, }; -pub const HEXPAIR_PASSES: &[&str] = &["00", "ab", "de", "0f", "f0"]; +pub(crate) const HEXPAIR_PASSES: &[&str] = &["00", "ab", "de", "0f", "f0"]; -pub const HEXPAIR_FAILS: &[&str] = &["0", " 0", "0 ", "az", "0p"]; +pub(crate) const HEXPAIR_FAILS: &[&str] = &["0", " 0", "0 ", "az", "0p"]; -pub const URL_BASE64_PASSES: &[&str] = &[ +pub(crate) const URL_BASE64_PASSES: &[&str] = &[ "abcdefghijklmnopq rstuvw yz01\t23456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ~", "abcdefghijklmnopqrstuvwyz0123456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ", ]; -pub const URL_BASE64_FAILS: &[&str] = &[ +pub(crate) const URL_BASE64_FAILS: &[&str] = &[ "abcdefghijklmnopq # rstuvw yz01\t23456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ~ ", "abcdefghijklmnopq $ rstuvw yz01\t23456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ~\t", "abcdefghijklmnopq % rstuvw yz01\t23456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ~\n", @@ -22,7 +22,7 @@ pub const URL_BASE64_FAILS: &[&str] = &[ "abcdefghijklmnopq & rstuvw yz01\t23456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ~\r\n", ]; -pub const BYTES_PASSES: &[&str] = &[ +pub(crate) const BYTES_PASSES: &[&str] = &[ "h''", "b64''", "''", @@ -39,7 +39,7 @@ pub const BYTES_PASSES: &[&str] = &[ "'text\n that gets converted \\\' into a byte string...'", ]; -pub const BYTES_FAILS: &[&str] = &[ +pub(crate) const BYTES_FAILS: &[&str] = &[ "h64", "b64", "\"\"", @@ -52,7 +52,7 @@ pub const BYTES_FAILS: &[&str] = &[ ]; /// # Panics -pub fn passes_tests_rule(rule_type: Rule, test_data: &[&str]) { +pub(crate) fn passes_tests_rule(rule_type: Rule, test_data: &[&str]) { for test in test_data { let parse = CDDLTestParser::parse(rule_type, test); assert!(parse.is_ok()); @@ -60,7 +60,7 @@ pub fn passes_tests_rule(rule_type: Rule, test_data: &[&str]) { } /// # Panics -pub fn fails_tests_rule(rule_type: Rule, test_data: &[&str]) { +pub(crate) fn fails_tests_rule(rule_type: Rule, test_data: &[&str]) { for test in test_data { let parse = CDDLTestParser::parse(rule_type, test); assert!(parse.is_err()); diff --git a/hermes/crates/cbork/cddl-parser/tests/comments.rs b/hermes/crates/cbork/cddl-parser/tests/comments.rs index 61777faa2..0cce0dea2 100644 --- a/hermes/crates/cbork/cddl-parser/tests/comments.rs +++ b/hermes/crates/cbork/cddl-parser/tests/comments.rs @@ -3,11 +3,11 @@ use cddl_parser::{ cddl_test::{CDDLTestParser, Parser, Rule}, }; -pub const COMMENT_PASSES: &[&str] = &["; A Comment \n", "; And another\r", ";more\r\n"]; +pub(crate) const COMMENT_PASSES: &[&str] = &["; A Comment \n", "; And another\r", ";more\r\n"]; -pub const COMMENT_FAILS: &[&str] = &["not a comment\n"]; +pub(crate) const COMMENT_FAILS: &[&str] = &["not a comment\n"]; -pub const WHITESPACE_COMMENT_PASSES: &[&str] = &[ +pub(crate) const WHITESPACE_COMMENT_PASSES: &[&str] = &[ " ", " ", " \t \t", @@ -20,10 +20,10 @@ pub const WHITESPACE_COMMENT_PASSES: &[&str] = &[ "\t; A Comment \n ; Another Comment \t \r\n \t ; A Final Comment \r\n", ]; -pub const WHITESPACE_COMMENT_FAILS: &[&str] = &["not a comment"]; +pub(crate) const WHITESPACE_COMMENT_FAILS: &[&str] = &["not a comment"]; /// # Panics -pub fn passes_tests_rule(rule_type: Rule, test_data: &[&str]) { +pub(crate) fn passes_tests_rule(rule_type: Rule, test_data: &[&str]) { for test in test_data { let parse = CDDLTestParser::parse(rule_type, test); assert!(parse.is_ok()); @@ -31,7 +31,7 @@ pub fn passes_tests_rule(rule_type: Rule, test_data: &[&str]) { } /// # Panics -pub fn fails_tests_rule(rule_type: Rule, test_data: &[&str]) { +pub(crate) fn fails_tests_rule(rule_type: Rule, test_data: &[&str]) { for test in test_data { let parse = CDDLTestParser::parse(rule_type, test); assert!(parse.is_err()); diff --git a/hermes/crates/cbork/cddl-parser/tests/group_elements.rs b/hermes/crates/cbork/cddl-parser/tests/group_elements.rs index a05619c8d..0474496e8 100644 --- a/hermes/crates/cbork/cddl-parser/tests/group_elements.rs +++ b/hermes/crates/cbork/cddl-parser/tests/group_elements.rs @@ -9,7 +9,7 @@ use cddl_parser::{ mod identifiers; use identifiers::{ID_FAILS, ID_PASSES}; -pub const OCCUR_PASSES: &[&str] = &[ +pub(crate) const OCCUR_PASSES: &[&str] = &[ "*", "+", "?", @@ -25,7 +25,7 @@ pub const OCCUR_PASSES: &[&str] = &[ "0x1*", ]; -pub const OCCUR_FAILS: &[&str] = &[ +pub(crate) const OCCUR_FAILS: &[&str] = &[ "5**10", "5 * 10", "5\t\n*\n10", @@ -41,11 +41,11 @@ pub const OCCUR_FAILS: &[&str] = &[ "0b", ]; -pub const OPTCOM_PASSES: &[&str] = &["", ",", " ,", " , ", "\n,\n", "\n"]; +pub(crate) const OPTCOM_PASSES: &[&str] = &["", ",", " ,", " , ", "\n,\n", "\n"]; -pub const OPTCOM_FAILS: &[&str] = &[",,"]; +pub(crate) const OPTCOM_FAILS: &[&str] = &[",,"]; -pub const MEMBERKEY_PASSES: &[&str] = &[ +pub(crate) const MEMBERKEY_PASSES: &[&str] = &[ // bareword "foo:", "foo-bar:", @@ -79,9 +79,9 @@ pub const MEMBERKEY_PASSES: &[&str] = &[ "h'12 34\n' =>", ]; -pub const MEMBERKEY_FAILS: &[&str] = &["#:", "foo::"]; +pub(crate) const MEMBERKEY_FAILS: &[&str] = &["#:", "foo::"]; -pub const GRPENT_PASSES: &[&str] = &[ +pub(crate) const GRPENT_PASSES: &[&str] = &[ "foo: 1", "foo: 1", "foo-bar:\t\n1", @@ -93,9 +93,9 @@ pub const GRPENT_PASSES: &[&str] = &[ "tstr => [foo: bar, baz]", ]; -pub const GRPENT_FAILS: &[&str] = &["tstr => (foo: bar)"]; +pub(crate) const GRPENT_FAILS: &[&str] = &["tstr => (foo: bar)"]; -pub const GRPCHOICE_PASSES: &[&str] = &[ +pub(crate) const GRPCHOICE_PASSES: &[&str] = &[ "foo: 1", "foo: 1, bar: 2", "foo: 1, bar: 2,", @@ -107,9 +107,9 @@ pub const GRPCHOICE_PASSES: &[&str] = &[ "foo => 1bar: 2", ]; -pub const GRPCHOICE_FAILS: &[&str] = &["foo: ,", "foo:", "foo: bar: 2", "foo => bar: 2"]; +pub(crate) const GRPCHOICE_FAILS: &[&str] = &["foo: ,", "foo:", "foo: bar: 2", "foo => bar: 2"]; -pub const GROUP_PASSES: &[&str] = &[ +pub(crate) const GROUP_PASSES: &[&str] = &[ "(foo: 1)", "(foo: 1) // (bar: 2)", "(foo: 1) // (bar: 2)", @@ -118,10 +118,10 @@ pub const GROUP_PASSES: &[&str] = &[ "((+ a) // (b / c))", ]; -pub const GROUP_FAILS: &[&str] = &["(foo: 1) / (bar: 2)"]; +pub(crate) const GROUP_FAILS: &[&str] = &["(foo: 1) / (bar: 2)"]; /// # Panics -pub fn passes_tests_rule(rule_type: Rule, test_data: &[&str]) { +pub(crate) fn passes_tests_rule(rule_type: Rule, test_data: &[&str]) { for test in test_data { let parse = CDDLTestParser::parse(rule_type, test); assert!(parse.is_ok()); @@ -129,7 +129,7 @@ pub fn passes_tests_rule(rule_type: Rule, test_data: &[&str]) { } /// # Panics -pub fn fails_tests_rule(rule_type: Rule, test_data: &[&str]) { +pub(crate) fn fails_tests_rule(rule_type: Rule, test_data: &[&str]) { for test in test_data { let parse = CDDLTestParser::parse(rule_type, test); assert!(parse.is_err()); diff --git a/hermes/crates/cbork/cddl-parser/tests/identifiers.rs b/hermes/crates/cbork/cddl-parser/tests/identifiers.rs index af1f19ee6..cb98f2f8e 100644 --- a/hermes/crates/cbork/cddl-parser/tests/identifiers.rs +++ b/hermes/crates/cbork/cddl-parser/tests/identifiers.rs @@ -5,7 +5,7 @@ use cddl_parser::{ cddl_test::{CDDLTestParser, Parser, Rule}, }; -pub const ID_PASSES: &[&str] = &[ +pub(crate) const ID_PASSES: &[&str] = &[ "$", "@", "_", @@ -38,7 +38,7 @@ pub const ID_PASSES: &[&str] = &[ "$typesocket", ]; -pub const ID_FAILS: &[&str] = &[ +pub(crate) const ID_FAILS: &[&str] = &[ "aname.", "aname-", "aname%", "a%name4", "a^name5", "a name", "", ]; diff --git a/hermes/crates/cbork/cddl-parser/tests/literal_values.rs b/hermes/crates/cbork/cddl-parser/tests/literal_values.rs index 46fa60958..7a876f41a 100644 --- a/hermes/crates/cbork/cddl-parser/tests/literal_values.rs +++ b/hermes/crates/cbork/cddl-parser/tests/literal_values.rs @@ -12,7 +12,7 @@ use byte_sequences::BYTES_PASSES; mod text_sequences; use text_sequences::TEXT_PASSES; -pub const UINT_PASSES: &[&str] = &[ +pub(crate) const UINT_PASSES: &[&str] = &[ "10", "101", "2034", @@ -23,9 +23,9 @@ pub const UINT_PASSES: &[&str] = &[ "0", ]; -pub const UINT_FAILS: &[&str] = &[" a ", "zz", "0123zzz", "0xdog", "0b777"]; +pub(crate) const UINT_FAILS: &[&str] = &[" a ", "zz", "0123zzz", "0xdog", "0b777"]; -pub const INT_PASSES: &[&str] = &[ +pub(crate) const INT_PASSES: &[&str] = &[ "10", "101", "2034", @@ -44,9 +44,9 @@ pub const INT_PASSES: &[&str] = &[ "-0", ]; -pub const INT_FAILS: &[&str] = &[" a ", "zz", "0123zzz", "0xdog", "0b777"]; +pub(crate) const INT_FAILS: &[&str] = &[" a ", "zz", "0123zzz", "0xdog", "0b777"]; -pub const INTFLOAT_PASSES: &[&str] = &[ +pub(crate) const INTFLOAT_PASSES: &[&str] = &[ "10", "101", "2034", @@ -66,9 +66,9 @@ pub const INTFLOAT_PASSES: &[&str] = &[ "123.456e-789", ]; -pub const INTFLOAT_FAILS: &[&str] = &[" a ", "zz", "0123zzz", "0xdog", "0b777"]; +pub(crate) const INTFLOAT_FAILS: &[&str] = &[" a ", "zz", "0123zzz", "0xdog", "0b777"]; -pub const HEXFLOAT_PASSES: &[&str] = &[ +pub(crate) const HEXFLOAT_PASSES: &[&str] = &[ "0xabcp+123", "-0xabcp+123", "0xabcp-123", @@ -79,9 +79,9 @@ pub const HEXFLOAT_PASSES: &[&str] = &[ "-0xabc.defp-123", ]; -pub const HEXFLOAT_FAILS: &[&str] = &[" a ", "zz", "0123zzz", "0xdog", "0b777"]; +pub(crate) const HEXFLOAT_FAILS: &[&str] = &[" a ", "zz", "0123zzz", "0xdog", "0b777"]; -pub const NUMBER_PASSES: &[&str] = &[ +pub(crate) const NUMBER_PASSES: &[&str] = &[ "0xabcp+123", "-0xabcp+123", "0xabcp-123", @@ -109,14 +109,14 @@ pub const NUMBER_PASSES: &[&str] = &[ "123.456e-789", ]; -pub const NUMBER_FAILS: &[&str] = &[" a ", "zz", "0123zzz", "0xdog", "0b777"]; +pub(crate) const NUMBER_FAILS: &[&str] = &[" a ", "zz", "0123zzz", "0xdog", "0b777"]; -pub const VALUE_PASSES: &[&str] = &[]; +pub(crate) const VALUE_PASSES: &[&str] = &[]; -pub const VALUE_FAILS: &[&str] = &[]; +pub(crate) const VALUE_FAILS: &[&str] = &[]; /// # Panics -pub fn passes_tests_rule(rule_type: Rule, test_data: &[&str]) { +pub(crate) fn passes_tests_rule(rule_type: Rule, test_data: &[&str]) { for test in test_data { let parse = CDDLTestParser::parse(rule_type, test); assert!(parse.is_ok()); @@ -124,7 +124,7 @@ pub fn passes_tests_rule(rule_type: Rule, test_data: &[&str]) { } /// # Panics -pub fn fails_tests_rule(rule_type: Rule, test_data: &[&str]) { +pub(crate) fn fails_tests_rule(rule_type: Rule, test_data: &[&str]) { for test in test_data { let parse = CDDLTestParser::parse(rule_type, test); assert!(parse.is_err()); diff --git a/hermes/crates/cbork/cddl-parser/tests/rules.rs b/hermes/crates/cbork/cddl-parser/tests/rules.rs index 472f93820..883f8bedc 100644 --- a/hermes/crates/cbork/cddl-parser/tests/rules.rs +++ b/hermes/crates/cbork/cddl-parser/tests/rules.rs @@ -11,7 +11,7 @@ use identifiers::{ID_FAILS, ID_PASSES}; mod type_declarations; use type_declarations::{TYPE_FAILS, TYPE_PASSES}; -pub const GENERICARG_PASSES: &[&str] = &[ +pub(crate) const GENERICARG_PASSES: &[&str] = &[ "", "<{ foo: bar }>", "<{ h'1234': uint }>", @@ -21,12 +21,12 @@ pub const GENERICARG_PASSES: &[&str] = &[ "<{ foo: bar }, 1..10>", ]; -pub const GENERICARG_FAILS: &[&str] = +pub(crate) const GENERICARG_FAILS: &[&str] = &["", "<>", "", "<( foo: bar )>", ""]; -pub const GENERICPARM_PASSES: &[&str] = &["", "", "", ""]; +pub(crate) const GENERICPARM_PASSES: &[&str] = &["", "", "", ""]; -pub const GENERICPARM_FAILS: &[&str] = &[ +pub(crate) const GENERICPARM_FAILS: &[&str] = &[ "", "<>", "", @@ -36,23 +36,23 @@ pub const GENERICPARM_FAILS: &[&str] = &[ "<\n1...10\t>", ]; -pub const ASSIGNG_PASSES: &[&str] = &["=", "//="]; +pub(crate) const ASSIGNG_PASSES: &[&str] = &["=", "//="]; -pub const ASSIGNG_FAILS: &[&str] = &["==", "/="]; +pub(crate) const ASSIGNG_FAILS: &[&str] = &["==", "/="]; -pub const ASSIGNT_PASSES: &[&str] = &["=", "/="]; +pub(crate) const ASSIGNT_PASSES: &[&str] = &["=", "/="]; -pub const ASSIGNT_FAILS: &[&str] = &["==", "//="]; +pub(crate) const ASSIGNT_FAILS: &[&str] = &["==", "//="]; -pub const TYPENAME_PASSES: &[&str] = ID_PASSES; +pub(crate) const TYPENAME_PASSES: &[&str] = ID_PASSES; -pub const TYPENAME_FAILS: &[&str] = ID_FAILS; +pub(crate) const TYPENAME_FAILS: &[&str] = ID_FAILS; -pub const GROUPNAME_PASSES: &[&str] = ID_PASSES; +pub(crate) const GROUPNAME_PASSES: &[&str] = ID_PASSES; -pub const GROUPNAME_FAILS: &[&str] = ID_FAILS; +pub(crate) const GROUPNAME_FAILS: &[&str] = ID_FAILS; -pub const RULE_GROUP_PASSES: &[&str] = &[ +pub(crate) const RULE_GROUP_PASSES: &[&str] = &[ "foo = (bar: baz)", "t //= (foo: bar)", "t //= foo", @@ -62,10 +62,10 @@ pub const RULE_GROUP_PASSES: &[&str] = &[ "delivery //= ( lat: float, long: float, drone-type: tstr )", ]; -pub const RULE_GROUP_FAILS: &[&str] = &["foo = bar: baz", "t /= (foo: bar)"]; +pub(crate) const RULE_GROUP_FAILS: &[&str] = &["foo = bar: baz", "t /= (foo: bar)"]; /// # Panics -pub fn passes_tests_rule(rule_type: Rule, test_data: &[&str]) { +pub(crate) fn passes_tests_rule(rule_type: Rule, test_data: &[&str]) { for test in test_data { let parse = CDDLTestParser::parse(rule_type, test); assert!(parse.is_ok()); @@ -73,7 +73,7 @@ pub fn passes_tests_rule(rule_type: Rule, test_data: &[&str]) { } /// # Panics -pub fn fails_tests_rule(rule_type: Rule, test_data: &[&str]) { +pub(crate) fn fails_tests_rule(rule_type: Rule, test_data: &[&str]) { for test in test_data { let parse = CDDLTestParser::parse(rule_type, test); assert!(parse.is_err()); diff --git a/hermes/crates/cbork/cddl-parser/tests/text_sequences.rs b/hermes/crates/cbork/cddl-parser/tests/text_sequences.rs index 08912812f..566a35dea 100644 --- a/hermes/crates/cbork/cddl-parser/tests/text_sequences.rs +++ b/hermes/crates/cbork/cddl-parser/tests/text_sequences.rs @@ -3,13 +3,13 @@ use cddl_parser::{ cddl_test::{CDDLTestParser, Parser, Rule}, }; -pub const S_PASSES: &[&str] = &[" ", " ", " \t \t", " \t \r \n \r\n "]; -pub const S_FAILS: &[&str] = &[" a ", "zz", " \t d \t", " \t \r \n \t \r\n x"]; -pub const TEXT_PASSES: &[&str] = &[r#""""#, r#""abc""#, "\"abc\\n\""]; -pub const TEXT_FAILS: &[&str] = &["", "''", "\"abc\n\""]; +pub(crate) const S_PASSES: &[&str] = &[" ", " ", " \t \t", " \t \r \n \r\n "]; +pub(crate) const S_FAILS: &[&str] = &[" a ", "zz", " \t d \t", " \t \r \n \t \r\n x"]; +pub(crate) const TEXT_PASSES: &[&str] = &[r#""""#, r#""abc""#, "\"abc\\n\""]; +pub(crate) const TEXT_FAILS: &[&str] = &["", "''", "\"abc\n\""]; /// # Panics -pub fn passes_tests_rule(rule_type: Rule, test_data: &[&str]) { +pub(crate) fn passes_tests_rule(rule_type: Rule, test_data: &[&str]) { for test in test_data { let parse = CDDLTestParser::parse(rule_type, test); assert!(parse.is_ok()); @@ -17,7 +17,7 @@ pub fn passes_tests_rule(rule_type: Rule, test_data: &[&str]) { } /// # Panics -pub fn fails_tests_rule(rule_type: Rule, test_data: &[&str]) { +pub(crate) fn fails_tests_rule(rule_type: Rule, test_data: &[&str]) { for test in test_data { let parse = CDDLTestParser::parse(rule_type, test); assert!(parse.is_err()); diff --git a/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs b/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs index 5a7f8c7e9..b74222658 100644 --- a/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs +++ b/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs @@ -6,7 +6,7 @@ use cddl_parser::{ cddl_test::{CDDLTestParser, Parser, Rule}, }; -pub const CTLOP_PASSES: &[&str] = &[ +pub(crate) const CTLOP_PASSES: &[&str] = &[ ".$", ".@", "._", @@ -39,15 +39,15 @@ pub const CTLOP_PASSES: &[&str] = &[ ".$typesocket", ]; -pub const CTLOP_FAILS: &[&str] = &[ +pub(crate) const CTLOP_FAILS: &[&str] = &[ "aname.", ".", "..", "aname.", "aname-", "aname%", "a%name4", "a^name5", "a name", "", ]; -pub const RANGEOP_PASSES: &[&str] = &["..", "..."]; +pub(crate) const RANGEOP_PASSES: &[&str] = &["..", "..."]; -pub const RANGEOP_FAILS: &[&str] = &[".", "", "....", ".. .", ". .."]; +pub(crate) const RANGEOP_FAILS: &[&str] = &[".", "", "....", ".. .", ". .."]; -pub const TYPE2_PASSES: &[&str] = &[ +pub(crate) const TYPE2_PASSES: &[&str] = &[ "#", "#1", "#1.1", @@ -78,7 +78,7 @@ pub const TYPE2_PASSES: &[&str] = &[ "foo", ]; -pub const TYPE2_FAILS: &[&str] = &[ +pub(crate) const TYPE2_FAILS: &[&str] = &[ "", "##", "#1.", @@ -89,7 +89,7 @@ pub const TYPE2_FAILS: &[&str] = &[ "(foo bar)", ]; -pub const TYPE1_PASSES: &[&str] = &[ +pub(crate) const TYPE1_PASSES: &[&str] = &[ "1..2", "1 .. 2", "1\t..\n2", @@ -103,9 +103,9 @@ pub const TYPE1_PASSES: &[&str] = &[ "foo.bar", ]; -pub const TYPE1_FAILS: &[&str] = &[""]; +pub(crate) const TYPE1_FAILS: &[&str] = &[""]; -pub const TYPE_PASSES: &[&str] = &[ +pub(crate) const TYPE_PASSES: &[&str] = &[ "1 / 2", "1\n/\t2", "1 / 2 / 3 / 4", @@ -113,10 +113,10 @@ pub const TYPE_PASSES: &[&str] = &[ "# / #", ]; -pub const TYPE_FAILS: &[&str] = &["", "1 \\ 2", "1 // 2", "1 2", "1 / 2 3"]; +pub(crate) const TYPE_FAILS: &[&str] = &["", "1 \\ 2", "1 // 2", "1 2", "1 / 2 3"]; /// # Panics -pub fn passes_tests_rule(rule_type: Rule, test_data: &[&str]) { +pub(crate) fn passes_tests_rule(rule_type: Rule, test_data: &[&str]) { for test in test_data { let parse = CDDLTestParser::parse(rule_type, test); assert!(parse.is_ok()); @@ -124,7 +124,7 @@ pub fn passes_tests_rule(rule_type: Rule, test_data: &[&str]) { } /// # Panics -pub fn fails_tests_rule(rule_type: Rule, test_data: &[&str]) { +pub(crate) fn fails_tests_rule(rule_type: Rule, test_data: &[&str]) { for test in test_data { let parse = CDDLTestParser::parse(rule_type, test); assert!(parse.is_err()); From 321aaaadb954f5a7e6dff82feed99d37c9a8ec9d Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Thu, 25 Jan 2024 09:15:38 +0700 Subject: [PATCH 34/43] chore: fmtfix --- hermes/crates/cbork/cddl-parser/tests/rules.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/hermes/crates/cbork/cddl-parser/tests/rules.rs b/hermes/crates/cbork/cddl-parser/tests/rules.rs index 883f8bedc..8bcae36e6 100644 --- a/hermes/crates/cbork/cddl-parser/tests/rules.rs +++ b/hermes/crates/cbork/cddl-parser/tests/rules.rs @@ -24,7 +24,8 @@ pub(crate) const GENERICARG_PASSES: &[&str] = &[ pub(crate) const GENERICARG_FAILS: &[&str] = &["", "<>", "", "<( foo: bar )>", ""]; -pub(crate) const GENERICPARM_PASSES: &[&str] = &["", "", "", ""]; +pub(crate) const GENERICPARM_PASSES: &[&str] = + &["", "", "", ""]; pub(crate) const GENERICPARM_FAILS: &[&str] = &[ "", From 3689b59944692d055ecfd396ca6887c43fb65922 Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Thu, 25 Jan 2024 17:08:24 +0700 Subject: [PATCH 35/43] refactor: don't dry util functions --- .../cbork/cddl-parser/tests/byte_sequences.rs | 33 +++----------- hermes/crates/cbork/cddl-parser/tests/cddl.rs | 24 +++++----- .../cbork/cddl-parser/tests/comments.rs | 30 +++---------- .../cbork/cddl-parser/tests/group_elements.rs | 45 +++++-------------- .../cbork/cddl-parser/tests/identifiers.rs | 17 +++---- .../cbork/cddl-parser/tests/literal_values.rs | 42 +++++------------ .../crates/cbork/cddl-parser/tests/rules.rs | 40 +++++------------ .../cbork/cddl-parser/tests/text_sequences.rs | 30 +++---------- .../cddl-parser/tests/type_declarations.rs | 34 ++++---------- 9 files changed, 74 insertions(+), 221 deletions(-) diff --git a/hermes/crates/cbork/cddl-parser/tests/byte_sequences.rs b/hermes/crates/cbork/cddl-parser/tests/byte_sequences.rs index 04b7ac529..43f4e0099 100644 --- a/hermes/crates/cbork/cddl-parser/tests/byte_sequences.rs +++ b/hermes/crates/cbork/cddl-parser/tests/byte_sequences.rs @@ -1,9 +1,9 @@ // cspell: words hexpair rstuvw abcdefghijklmnopqrstuvwyz rstuvw Xhhb Bhcm -use cddl_parser::{ - self, - cddl_test::{CDDLTestParser, Parser, Rule}, -}; +use cddl_parser::cddl_test::Rule; + +#[path = "./common/mod.rs"] +mod common; pub(crate) const HEXPAIR_PASSES: &[&str] = &["00", "ab", "de", "0f", "f0"]; @@ -51,30 +51,13 @@ pub(crate) const BYTES_FAILS: &[&str] = &[ "'\u{7}'", ]; -/// # Panics -pub(crate) fn passes_tests_rule(rule_type: Rule, test_data: &[&str]) { - for test in test_data { - let parse = CDDLTestParser::parse(rule_type, test); - assert!(parse.is_ok()); - } -} - -/// # Panics -pub(crate) fn fails_tests_rule(rule_type: Rule, test_data: &[&str]) { - for test in test_data { - let parse = CDDLTestParser::parse(rule_type, test); - assert!(parse.is_err()); - } -} - #[test] /// Test if the `HEX_PAIR` rule passes properly. fn check_hexpair() { let passes = HEXPAIR_PASSES; let fails = HEXPAIR_FAILS; - passes_tests_rule(Rule::HEX_PAIR, passes); - fails_tests_rule(Rule::HEX_PAIR, fails); + common::check_tests_rule(Rule::HEX_PAIR, passes, fails); } #[test] @@ -83,8 +66,7 @@ fn check_url_base64() { let passes = URL_BASE64_PASSES; let fails = URL_BASE64_FAILS; - passes_tests_rule(Rule::URL_BASE64_TEST, passes); - fails_tests_rule(Rule::URL_BASE64_TEST, fails); + common::check_tests_rule(Rule::URL_BASE64_TEST, passes, fails); } #[test] @@ -93,6 +75,5 @@ fn check_bytes() { let passes = BYTES_PASSES; let fails = BYTES_FAILS; - passes_tests_rule(Rule::bytes_TEST, passes); - fails_tests_rule(Rule::bytes_TEST, fails); + common::check_tests_rule(Rule::bytes_TEST, passes, fails); } diff --git a/hermes/crates/cbork/cddl-parser/tests/cddl.rs b/hermes/crates/cbork/cddl-parser/tests/cddl.rs index 922be67ec..628ac0acb 100644 --- a/hermes/crates/cbork/cddl-parser/tests/cddl.rs +++ b/hermes/crates/cbork/cddl-parser/tests/cddl.rs @@ -15,20 +15,16 @@ fn parse_cddl_files() { file_paths.sort(); let valid_file_paths = file_paths.iter().filter(|p| { - matches!( - p.file_name() - .and_then(OsStr::to_str) - .map(|p| p.starts_with("valid")), - Some(true) - ) + p.file_name() + .and_then(OsStr::to_str) + .map(|p| p.starts_with("valid")) + .is_some_and(|p| p) }); let invalid_file_paths = file_paths.iter().filter(|p| { - matches!( - p.file_name() - .and_then(OsStr::to_str) - .map(|p| p.starts_with("invalid")), - Some(true) - ) + p.file_name() + .and_then(OsStr::to_str) + .map(|p| p.starts_with("invalid")) + .is_some_and(|p| p) }); // test for valid files @@ -36,7 +32,7 @@ fn parse_cddl_files() { for file_path in valid_file_paths { let mut content = fs::read_to_string(file_path).expect("failed to read a file"); - if let Err(e) = parse_cddl(&mut content, &Extension::RFC8610Parser) { + if let Err(e) = parse_cddl(&mut content, &Extension::CDDLParser) { err_messages.push(format!("{}) {file_path:?} {e}", err_messages.len() + 1)); } } @@ -45,7 +41,7 @@ fn parse_cddl_files() { for file_path in invalid_file_paths { let mut content = fs::read_to_string(file_path).expect("failed to read a file"); - let result = parse_cddl(&mut content, &Extension::RFC8610Parser); + let result = parse_cddl(&mut content, &Extension::CDDLParser); assert!(result.is_err(), "{:?} is expected to fail", &file_path); } diff --git a/hermes/crates/cbork/cddl-parser/tests/comments.rs b/hermes/crates/cbork/cddl-parser/tests/comments.rs index 0cce0dea2..eb80ad1de 100644 --- a/hermes/crates/cbork/cddl-parser/tests/comments.rs +++ b/hermes/crates/cbork/cddl-parser/tests/comments.rs @@ -1,7 +1,7 @@ -use cddl_parser::{ - self, - cddl_test::{CDDLTestParser, Parser, Rule}, -}; +use cddl_parser::{self, cddl_test::Rule}; + +#[path = "./common/mod.rs"] +mod common; pub(crate) const COMMENT_PASSES: &[&str] = &["; A Comment \n", "; And another\r", ";more\r\n"]; @@ -22,30 +22,13 @@ pub(crate) const WHITESPACE_COMMENT_PASSES: &[&str] = &[ pub(crate) const WHITESPACE_COMMENT_FAILS: &[&str] = &["not a comment"]; -/// # Panics -pub(crate) fn passes_tests_rule(rule_type: Rule, test_data: &[&str]) { - for test in test_data { - let parse = CDDLTestParser::parse(rule_type, test); - assert!(parse.is_ok()); - } -} - -/// # Panics -pub(crate) fn fails_tests_rule(rule_type: Rule, test_data: &[&str]) { - for test in test_data { - let parse = CDDLTestParser::parse(rule_type, test); - assert!(parse.is_err()); - } -} - #[test] /// Test if the `COMMENT` rule passes properly. fn check_comment() { let passes = COMMENT_PASSES; let fails = COMMENT_FAILS; - passes_tests_rule(Rule::COMMENT_TEST, passes); - fails_tests_rule(Rule::COMMENT_TEST, fails); + common::check_tests_rule(Rule::COMMENT_TEST, passes, fails); } #[test] @@ -55,6 +38,5 @@ fn check_whitespace_comments() { let passes = WHITESPACE_COMMENT_PASSES; let fails = WHITESPACE_COMMENT_FAILS; - passes_tests_rule(Rule::COMMENT_TEST, passes); - fails_tests_rule(Rule::COMMENT_TEST, fails); + common::check_tests_rule(Rule::COMMENT_TEST, passes, fails); } diff --git a/hermes/crates/cbork/cddl-parser/tests/group_elements.rs b/hermes/crates/cbork/cddl-parser/tests/group_elements.rs index 0474496e8..a26e94977 100644 --- a/hermes/crates/cbork/cddl-parser/tests/group_elements.rs +++ b/hermes/crates/cbork/cddl-parser/tests/group_elements.rs @@ -1,10 +1,10 @@ // cspell: words OPTCOM MEMBERKEY bareword tstr GRPENT GRPCHOICE // cspell: words optcom memberkey grpent grpchoice -use cddl_parser::{ - self, - cddl_test::{CDDLTestParser, Parser, Rule}, -}; +use cddl_parser::{self, cddl_test::Rule}; + +#[path = "./common/mod.rs"] +mod common; mod identifiers; use identifiers::{ID_FAILS, ID_PASSES}; @@ -120,22 +120,6 @@ pub(crate) const GROUP_PASSES: &[&str] = &[ pub(crate) const GROUP_FAILS: &[&str] = &["(foo: 1) / (bar: 2)"]; -/// # Panics -pub(crate) fn passes_tests_rule(rule_type: Rule, test_data: &[&str]) { - for test in test_data { - let parse = CDDLTestParser::parse(rule_type, test); - assert!(parse.is_ok()); - } -} - -/// # Panics -pub(crate) fn fails_tests_rule(rule_type: Rule, test_data: &[&str]) { - for test in test_data { - let parse = CDDLTestParser::parse(rule_type, test); - assert!(parse.is_err()); - } -} - #[test] /// Test if the `occur` rule passes properly. /// This uses a special rule in the Grammar to test `occur` exhaustively. @@ -143,8 +127,7 @@ fn check_occur() { let passes = OCCUR_PASSES; let fails = OCCUR_FAILS; - passes_tests_rule(Rule::occur_TEST, passes); - fails_tests_rule(Rule::occur_TEST, fails); + common::check_tests_rule(Rule::occur_TEST, passes, fails); } #[test] @@ -154,8 +137,7 @@ fn check_bareword() { let passes = ID_PASSES; let fails = ID_FAILS; - passes_tests_rule(Rule::bareword_TEST, passes); - fails_tests_rule(Rule::bareword_TEST, fails); + common::check_tests_rule(Rule::bareword_TEST, passes, fails); } #[test] @@ -165,8 +147,7 @@ fn check_optcom() { let passes = OPTCOM_PASSES; let fails = OPTCOM_FAILS; - passes_tests_rule(Rule::optcom_TEST, passes); - fails_tests_rule(Rule::optcom_TEST, fails); + common::check_tests_rule(Rule::optcom_TEST, passes, fails); } #[test] @@ -176,8 +157,7 @@ fn check_memberkey() { let passes = MEMBERKEY_PASSES; let fails = MEMBERKEY_FAILS; - passes_tests_rule(Rule::memberkey_TEST, passes); - fails_tests_rule(Rule::memberkey_TEST, fails); + common::check_tests_rule(Rule::memberkey_TEST, passes, fails); } #[test] @@ -187,8 +167,7 @@ fn check_grpent() { let passes = GRPENT_PASSES; let fails = GRPENT_FAILS; - passes_tests_rule(Rule::grpent_TEST, passes); - fails_tests_rule(Rule::grpent_TEST, fails); + common::check_tests_rule(Rule::grpent_TEST, passes, fails); } #[test] @@ -198,8 +177,7 @@ fn check_grpchoice() { let passes = GRPCHOICE_PASSES; let fails = GRPCHOICE_FAILS; - passes_tests_rule(Rule::grpchoice_TEST, passes); - fails_tests_rule(Rule::grpchoice_TEST, fails); + common::check_tests_rule(Rule::grpchoice_TEST, passes, fails); } #[test] @@ -209,6 +187,5 @@ fn check_group() { let passes = GROUP_PASSES; let fails = GROUP_FAILS; - passes_tests_rule(Rule::group_TEST, passes); - fails_tests_rule(Rule::group_TEST, fails); + common::check_tests_rule(Rule::group_TEST, passes, fails); } diff --git a/hermes/crates/cbork/cddl-parser/tests/identifiers.rs b/hermes/crates/cbork/cddl-parser/tests/identifiers.rs index cb98f2f8e..30eed3370 100644 --- a/hermes/crates/cbork/cddl-parser/tests/identifiers.rs +++ b/hermes/crates/cbork/cddl-parser/tests/identifiers.rs @@ -5,6 +5,9 @@ use cddl_parser::{ cddl_test::{CDDLTestParser, Parser, Rule}, }; +#[path = "./common/mod.rs"] +mod common; + pub(crate) const ID_PASSES: &[&str] = &[ "$", "@", @@ -66,16 +69,8 @@ fn check_name_characters() { #[test] /// Test if the `id` rule passes properly. fn check_id() { - let test = ID_PASSES; - let fail = ID_FAILS; - - for test in test { - let parse = CDDLTestParser::parse(Rule::id_TEST, test); - assert!(parse.is_ok()); - } + let passes = ID_PASSES; + let fails = ID_FAILS; - for test in fail { - let parse = CDDLTestParser::parse(Rule::id_TEST, test); - assert!(parse.is_err()); - } + common::check_tests_rule(Rule::id_TEST, passes, fails); } diff --git a/hermes/crates/cbork/cddl-parser/tests/literal_values.rs b/hermes/crates/cbork/cddl-parser/tests/literal_values.rs index 7a876f41a..a5e7d36cc 100644 --- a/hermes/crates/cbork/cddl-parser/tests/literal_values.rs +++ b/hermes/crates/cbork/cddl-parser/tests/literal_values.rs @@ -2,10 +2,10 @@ use std::ops::Deref; -use cddl_parser::{ - self, - cddl_test::{CDDLTestParser, Parser, Rule}, -}; +use cddl_parser::{self, cddl_test::Rule}; + +#[path = "./common/mod.rs"] +mod common; mod byte_sequences; use byte_sequences::BYTES_PASSES; @@ -115,30 +115,13 @@ pub(crate) const VALUE_PASSES: &[&str] = &[]; pub(crate) const VALUE_FAILS: &[&str] = &[]; -/// # Panics -pub(crate) fn passes_tests_rule(rule_type: Rule, test_data: &[&str]) { - for test in test_data { - let parse = CDDLTestParser::parse(rule_type, test); - assert!(parse.is_ok()); - } -} - -/// # Panics -pub(crate) fn fails_tests_rule(rule_type: Rule, test_data: &[&str]) { - for test in test_data { - let parse = CDDLTestParser::parse(rule_type, test); - assert!(parse.is_err()); - } -} - #[test] /// Test if the `uint` rule passes properly. fn check_uint() { let passes = UINT_PASSES; let fails = UINT_FAILS; - passes_tests_rule(Rule::uint_TEST, passes); - fails_tests_rule(Rule::uint_TEST, fails); + common::check_tests_rule(Rule::uint_TEST, passes, fails); } #[test] @@ -147,8 +130,7 @@ fn check_int() { let passes = INT_PASSES; let fails = INT_FAILS; - passes_tests_rule(Rule::int_TEST, passes); - fails_tests_rule(Rule::int_TEST, fails); + common::check_tests_rule(Rule::int_TEST, passes, fails); } #[test] @@ -157,8 +139,7 @@ fn check_intfloat() { let passes = INTFLOAT_PASSES; let fails = INTFLOAT_FAILS; - passes_tests_rule(Rule::intfloat_TEST, passes); - fails_tests_rule(Rule::intfloat_TEST, fails); + common::check_tests_rule(Rule::intfloat_TEST, passes, fails); } #[test] @@ -167,8 +148,7 @@ fn check_hexfloat() { let passes = HEXFLOAT_PASSES; let fails = HEXFLOAT_FAILS; - passes_tests_rule(Rule::hexfloat_TEST, passes); - fails_tests_rule(Rule::hexfloat_TEST, fails); + common::check_tests_rule(Rule::hexfloat_TEST, passes, fails); } #[test] @@ -177,8 +157,7 @@ fn check_number() { let passes = NUMBER_PASSES; let fails = NUMBER_FAILS; - passes_tests_rule(Rule::number_TEST, passes); - fails_tests_rule(Rule::number_TEST, fails); + common::check_tests_rule(Rule::number_TEST, passes, fails); } #[test] @@ -197,6 +176,5 @@ fn check_value() { .map(Deref::deref) .collect(); - passes_tests_rule(Rule::value_TEST, &passes); - fails_tests_rule(Rule::value_TEST, &fails); + common::check_tests_rule(Rule::value_TEST, &passes, &fails); } diff --git a/hermes/crates/cbork/cddl-parser/tests/rules.rs b/hermes/crates/cbork/cddl-parser/tests/rules.rs index 8bcae36e6..19f4308b7 100644 --- a/hermes/crates/cbork/cddl-parser/tests/rules.rs +++ b/hermes/crates/cbork/cddl-parser/tests/rules.rs @@ -6,6 +6,9 @@ use cddl_parser::{ cddl_test::{CDDLTestParser, Parser, Rule}, }; +#[path = "./common/mod.rs"] +mod common; + mod identifiers; use identifiers::{ID_FAILS, ID_PASSES}; mod type_declarations; @@ -65,22 +68,6 @@ pub(crate) const RULE_GROUP_PASSES: &[&str] = &[ pub(crate) const RULE_GROUP_FAILS: &[&str] = &["foo = bar: baz", "t /= (foo: bar)"]; -/// # Panics -pub(crate) fn passes_tests_rule(rule_type: Rule, test_data: &[&str]) { - for test in test_data { - let parse = CDDLTestParser::parse(rule_type, test); - assert!(parse.is_ok()); - } -} - -/// # Panics -pub(crate) fn fails_tests_rule(rule_type: Rule, test_data: &[&str]) { - for test in test_data { - let parse = CDDLTestParser::parse(rule_type, test); - assert!(parse.is_err()); - } -} - #[test] /// Test if the `genericarg` rule passes properly. /// This uses a special rule in the Grammar to test `genericarg` exhaustively. @@ -88,8 +75,7 @@ fn check_genericarg() { let passes = GENERICARG_PASSES; let fails = GENERICARG_FAILS; - passes_tests_rule(Rule::genericarg_TEST, passes); - fails_tests_rule(Rule::genericarg_TEST, fails); + common::check_tests_rule(Rule::genericarg_TEST, passes, fails); } #[test] @@ -99,8 +85,7 @@ fn check_genericparm() { let passes = GENERICPARM_PASSES; let fails = GENERICPARM_FAILS; - passes_tests_rule(Rule::genericparm_TEST, passes); - fails_tests_rule(Rule::genericparm_TEST, fails); + common::check_tests_rule(Rule::genericparm_TEST, passes, fails); } #[test] @@ -110,8 +95,7 @@ fn check_assigng() { let passes = ASSIGNG_PASSES; let fails = ASSIGNG_FAILS; - passes_tests_rule(Rule::assigng_TEST, passes); - fails_tests_rule(Rule::assigng_TEST, fails); + common::check_tests_rule(Rule::assigng_TEST, passes, fails); } #[test] @@ -121,8 +105,7 @@ fn check_assignt() { let passes = ASSIGNT_PASSES; let fails = ASSIGNT_FAILS; - passes_tests_rule(Rule::assignt_TEST, passes); - fails_tests_rule(Rule::assignt_TEST, fails); + common::check_tests_rule(Rule::assignt_TEST, passes, fails); } #[test] @@ -132,8 +115,7 @@ fn check_typename() { let passes = TYPENAME_PASSES; let fails = TYPENAME_FAILS; - passes_tests_rule(Rule::typename_TEST, passes); - fails_tests_rule(Rule::typename_TEST, fails); + common::check_tests_rule(Rule::typename_TEST, passes, fails); } #[test] @@ -143,8 +125,7 @@ fn check_groupname() { let passes = GROUPNAME_PASSES; let fails = GROUPNAME_FAILS; - passes_tests_rule(Rule::groupname_TEST, passes); - fails_tests_rule(Rule::groupname_TEST, fails); + common::check_tests_rule(Rule::groupname_TEST, passes, fails); } #[test] @@ -178,6 +159,5 @@ fn check_rule_group() { let passes = RULE_GROUP_PASSES; let fails = RULE_GROUP_FAILS; - passes_tests_rule(Rule::rule_TEST, passes); - fails_tests_rule(Rule::rule_TEST, fails); + common::check_tests_rule(Rule::rule_TEST, passes, fails); } diff --git a/hermes/crates/cbork/cddl-parser/tests/text_sequences.rs b/hermes/crates/cbork/cddl-parser/tests/text_sequences.rs index 566a35dea..ce24998ff 100644 --- a/hermes/crates/cbork/cddl-parser/tests/text_sequences.rs +++ b/hermes/crates/cbork/cddl-parser/tests/text_sequences.rs @@ -1,29 +1,13 @@ -use cddl_parser::{ - self, - cddl_test::{CDDLTestParser, Parser, Rule}, -}; +use cddl_parser::{self, cddl_test::Rule}; + +#[path = "./common/mod.rs"] +mod common; pub(crate) const S_PASSES: &[&str] = &[" ", " ", " \t \t", " \t \r \n \r\n "]; pub(crate) const S_FAILS: &[&str] = &[" a ", "zz", " \t d \t", " \t \r \n \t \r\n x"]; pub(crate) const TEXT_PASSES: &[&str] = &[r#""""#, r#""abc""#, "\"abc\\n\""]; pub(crate) const TEXT_FAILS: &[&str] = &["", "''", "\"abc\n\""]; -/// # Panics -pub(crate) fn passes_tests_rule(rule_type: Rule, test_data: &[&str]) { - for test in test_data { - let parse = CDDLTestParser::parse(rule_type, test); - assert!(parse.is_ok()); - } -} - -/// # Panics -pub(crate) fn fails_tests_rule(rule_type: Rule, test_data: &[&str]) { - for test in test_data { - let parse = CDDLTestParser::parse(rule_type, test); - assert!(parse.is_err()); - } -} - #[test] /// Test if the `S` rule passes properly. /// This uses a special rule in the Grammar to test whitespace exhaustively. @@ -31,8 +15,7 @@ fn check_s() { let passes = S_PASSES; let fails = S_FAILS; - passes_tests_rule(Rule::S_TEST, passes); - fails_tests_rule(Rule::S_TEST, fails); + common::check_tests_rule(Rule::S_TEST, passes, fails); } #[test] @@ -41,6 +24,5 @@ fn check_text() { let passes = TEXT_PASSES; let fails = TEXT_FAILS; - passes_tests_rule(Rule::text_TEST, passes); - fails_tests_rule(Rule::text_TEST, fails); + common::check_tests_rule(Rule::text_TEST, passes, fails); } diff --git a/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs b/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs index b74222658..51671d41b 100644 --- a/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs +++ b/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs @@ -6,6 +6,9 @@ use cddl_parser::{ cddl_test::{CDDLTestParser, Parser, Rule}, }; +#[path = "./common/mod.rs"] +mod common; + pub(crate) const CTLOP_PASSES: &[&str] = &[ ".$", ".@", @@ -115,22 +118,6 @@ pub(crate) const TYPE_PASSES: &[&str] = &[ pub(crate) const TYPE_FAILS: &[&str] = &["", "1 \\ 2", "1 // 2", "1 2", "1 / 2 3"]; -/// # Panics -pub(crate) fn passes_tests_rule(rule_type: Rule, test_data: &[&str]) { - for test in test_data { - let parse = CDDLTestParser::parse(rule_type, test); - assert!(parse.is_ok()); - } -} - -/// # Panics -pub(crate) fn fails_tests_rule(rule_type: Rule, test_data: &[&str]) { - for test in test_data { - let parse = CDDLTestParser::parse(rule_type, test); - assert!(parse.is_err()); - } -} - #[test] /// Test if the `ctlop` rule passes properly. /// This uses a special rule in the Grammar to test `ctlop` exhaustively. @@ -138,8 +125,7 @@ fn check_ctlop() { let passes = CTLOP_PASSES; let fails = CTLOP_FAILS; - passes_tests_rule(Rule::ctlop_TEST, passes); - fails_tests_rule(Rule::ctlop_TEST, fails); + common::check_tests_rule(Rule::ctlop_TEST, passes, fails); } #[test] @@ -149,8 +135,7 @@ fn check_rangeop() { let passes = RANGEOP_PASSES; let fails = RANGEOP_FAILS; - passes_tests_rule(Rule::rangeop_TEST, passes); - fails_tests_rule(Rule::rangeop_TEST, fails); + common::check_tests_rule(Rule::rangeop_TEST, passes, fails); } #[test] @@ -160,8 +145,7 @@ fn check_type2() { let passes = TYPE2_PASSES; let fails = TYPE2_FAILS; - passes_tests_rule(Rule::type2_TEST, passes); - fails_tests_rule(Rule::type2_TEST, fails); + common::check_tests_rule(Rule::type2_TEST, passes, fails); } #[test] @@ -171,8 +155,7 @@ fn check_type1() { let passes = TYPE1_PASSES; let fails = TYPE1_FAILS; - passes_tests_rule(Rule::type1_TEST, passes); - fails_tests_rule(Rule::type1_TEST, fails); + common::check_tests_rule(Rule::type1_TEST, passes, fails); } #[test] @@ -208,8 +191,7 @@ fn check_type() { let passes = TYPE_PASSES; let fails = TYPE_FAILS; - passes_tests_rule(Rule::type_TEST, passes); - fails_tests_rule(Rule::type_TEST, fails); + common::check_tests_rule(Rule::type_TEST, passes, fails); } #[test] From 0dca0dc68c4befc68d4eac92502a90208627dafc Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Thu, 25 Jan 2024 17:11:46 +0700 Subject: [PATCH 36/43] fix: disable lint warning --- hermes/crates/cbork/cddl-parser/tests/byte_sequences.rs | 1 + hermes/crates/cbork/cddl-parser/tests/comments.rs | 1 + hermes/crates/cbork/cddl-parser/tests/group_elements.rs | 1 + hermes/crates/cbork/cddl-parser/tests/identifiers.rs | 1 + hermes/crates/cbork/cddl-parser/tests/literal_values.rs | 1 + hermes/crates/cbork/cddl-parser/tests/rules.rs | 1 + hermes/crates/cbork/cddl-parser/tests/text_sequences.rs | 1 + hermes/crates/cbork/cddl-parser/tests/type_declarations.rs | 1 + 8 files changed, 8 insertions(+) diff --git a/hermes/crates/cbork/cddl-parser/tests/byte_sequences.rs b/hermes/crates/cbork/cddl-parser/tests/byte_sequences.rs index 43f4e0099..caa12dabf 100644 --- a/hermes/crates/cbork/cddl-parser/tests/byte_sequences.rs +++ b/hermes/crates/cbork/cddl-parser/tests/byte_sequences.rs @@ -3,6 +3,7 @@ use cddl_parser::cddl_test::Rule; #[path = "./common/mod.rs"] +#[allow(clippy::duplicate_mod)] mod common; pub(crate) const HEXPAIR_PASSES: &[&str] = &["00", "ab", "de", "0f", "f0"]; diff --git a/hermes/crates/cbork/cddl-parser/tests/comments.rs b/hermes/crates/cbork/cddl-parser/tests/comments.rs index eb80ad1de..fedfdbc93 100644 --- a/hermes/crates/cbork/cddl-parser/tests/comments.rs +++ b/hermes/crates/cbork/cddl-parser/tests/comments.rs @@ -1,6 +1,7 @@ use cddl_parser::{self, cddl_test::Rule}; #[path = "./common/mod.rs"] +#[allow(clippy::duplicate_mod)] mod common; pub(crate) const COMMENT_PASSES: &[&str] = &["; A Comment \n", "; And another\r", ";more\r\n"]; diff --git a/hermes/crates/cbork/cddl-parser/tests/group_elements.rs b/hermes/crates/cbork/cddl-parser/tests/group_elements.rs index a26e94977..529183695 100644 --- a/hermes/crates/cbork/cddl-parser/tests/group_elements.rs +++ b/hermes/crates/cbork/cddl-parser/tests/group_elements.rs @@ -4,6 +4,7 @@ use cddl_parser::{self, cddl_test::Rule}; #[path = "./common/mod.rs"] +#[allow(clippy::duplicate_mod)] mod common; mod identifiers; diff --git a/hermes/crates/cbork/cddl-parser/tests/identifiers.rs b/hermes/crates/cbork/cddl-parser/tests/identifiers.rs index 30eed3370..fb7143d08 100644 --- a/hermes/crates/cbork/cddl-parser/tests/identifiers.rs +++ b/hermes/crates/cbork/cddl-parser/tests/identifiers.rs @@ -6,6 +6,7 @@ use cddl_parser::{ }; #[path = "./common/mod.rs"] +#[allow(clippy::duplicate_mod)] mod common; pub(crate) const ID_PASSES: &[&str] = &[ diff --git a/hermes/crates/cbork/cddl-parser/tests/literal_values.rs b/hermes/crates/cbork/cddl-parser/tests/literal_values.rs index a5e7d36cc..027816b65 100644 --- a/hermes/crates/cbork/cddl-parser/tests/literal_values.rs +++ b/hermes/crates/cbork/cddl-parser/tests/literal_values.rs @@ -5,6 +5,7 @@ use std::ops::Deref; use cddl_parser::{self, cddl_test::Rule}; #[path = "./common/mod.rs"] +#[allow(clippy::duplicate_mod)] mod common; mod byte_sequences; diff --git a/hermes/crates/cbork/cddl-parser/tests/rules.rs b/hermes/crates/cbork/cddl-parser/tests/rules.rs index 19f4308b7..2d82d453a 100644 --- a/hermes/crates/cbork/cddl-parser/tests/rules.rs +++ b/hermes/crates/cbork/cddl-parser/tests/rules.rs @@ -7,6 +7,7 @@ use cddl_parser::{ }; #[path = "./common/mod.rs"] +#[allow(clippy::duplicate_mod)] mod common; mod identifiers; diff --git a/hermes/crates/cbork/cddl-parser/tests/text_sequences.rs b/hermes/crates/cbork/cddl-parser/tests/text_sequences.rs index ce24998ff..34daa2297 100644 --- a/hermes/crates/cbork/cddl-parser/tests/text_sequences.rs +++ b/hermes/crates/cbork/cddl-parser/tests/text_sequences.rs @@ -1,6 +1,7 @@ use cddl_parser::{self, cddl_test::Rule}; #[path = "./common/mod.rs"] +#[allow(clippy::duplicate_mod)] mod common; pub(crate) const S_PASSES: &[&str] = &[" ", " ", " \t \t", " \t \r \n \r\n "]; diff --git a/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs b/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs index 51671d41b..1982e1632 100644 --- a/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs +++ b/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs @@ -7,6 +7,7 @@ use cddl_parser::{ }; #[path = "./common/mod.rs"] +#[allow(clippy::duplicate_mod)] mod common; pub(crate) const CTLOP_PASSES: &[&str] = &[ From dd119232bfd110c97f5f2d753d068915c1fde1c0 Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Thu, 25 Jan 2024 17:16:13 +0700 Subject: [PATCH 37/43] chore: fmtfix --- hermes/crates/cbork/cddl-parser/src/grammar/rfc_8610.pest | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hermes/crates/cbork/cddl-parser/src/grammar/rfc_8610.pest b/hermes/crates/cbork/cddl-parser/src/grammar/rfc_8610.pest index 442a0efae..4a609ca51 100644 --- a/hermes/crates/cbork/cddl-parser/src/grammar/rfc_8610.pest +++ b/hermes/crates/cbork/cddl-parser/src/grammar/rfc_8610.pest @@ -3,7 +3,7 @@ // cspell: words assignt groupname grpent genericparm assigng optcom // cspell: words genericarg rangeop ctlop grpchoice memberkey bareword hexfloat intfloat -// cspell: words SCHAR BCHAR PCHAR SESC FFFD Characterset Visiable +// cspell: words SCHAR BCHAR PCHAR SESC FFFD Characterset Visiable cddl = ${ SOI From 05f863b6395235cadd3e7a0ae07f3b54253dc2ac Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Thu, 25 Jan 2024 17:20:26 +0700 Subject: [PATCH 38/43] refactor: change common path --- hermes/crates/cbork/cddl-parser/tests/byte_sequences.rs | 2 +- hermes/crates/cbork/cddl-parser/tests/comments.rs | 2 +- hermes/crates/cbork/cddl-parser/tests/group_elements.rs | 2 +- hermes/crates/cbork/cddl-parser/tests/identifiers.rs | 2 +- hermes/crates/cbork/cddl-parser/tests/literal_values.rs | 2 +- hermes/crates/cbork/cddl-parser/tests/rules.rs | 2 +- hermes/crates/cbork/cddl-parser/tests/text_sequences.rs | 2 +- hermes/crates/cbork/cddl-parser/tests/type_declarations.rs | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/hermes/crates/cbork/cddl-parser/tests/byte_sequences.rs b/hermes/crates/cbork/cddl-parser/tests/byte_sequences.rs index caa12dabf..ed879645c 100644 --- a/hermes/crates/cbork/cddl-parser/tests/byte_sequences.rs +++ b/hermes/crates/cbork/cddl-parser/tests/byte_sequences.rs @@ -2,7 +2,7 @@ use cddl_parser::cddl_test::Rule; -#[path = "./common/mod.rs"] +#[path = "common/mod.rs"] #[allow(clippy::duplicate_mod)] mod common; diff --git a/hermes/crates/cbork/cddl-parser/tests/comments.rs b/hermes/crates/cbork/cddl-parser/tests/comments.rs index fedfdbc93..efa255e7c 100644 --- a/hermes/crates/cbork/cddl-parser/tests/comments.rs +++ b/hermes/crates/cbork/cddl-parser/tests/comments.rs @@ -1,6 +1,6 @@ use cddl_parser::{self, cddl_test::Rule}; -#[path = "./common/mod.rs"] +#[path = "common/mod.rs"] #[allow(clippy::duplicate_mod)] mod common; diff --git a/hermes/crates/cbork/cddl-parser/tests/group_elements.rs b/hermes/crates/cbork/cddl-parser/tests/group_elements.rs index 529183695..9c1288a3e 100644 --- a/hermes/crates/cbork/cddl-parser/tests/group_elements.rs +++ b/hermes/crates/cbork/cddl-parser/tests/group_elements.rs @@ -3,7 +3,7 @@ use cddl_parser::{self, cddl_test::Rule}; -#[path = "./common/mod.rs"] +#[path = "common/mod.rs"] #[allow(clippy::duplicate_mod)] mod common; diff --git a/hermes/crates/cbork/cddl-parser/tests/identifiers.rs b/hermes/crates/cbork/cddl-parser/tests/identifiers.rs index fb7143d08..071060ae3 100644 --- a/hermes/crates/cbork/cddl-parser/tests/identifiers.rs +++ b/hermes/crates/cbork/cddl-parser/tests/identifiers.rs @@ -5,7 +5,7 @@ use cddl_parser::{ cddl_test::{CDDLTestParser, Parser, Rule}, }; -#[path = "./common/mod.rs"] +#[path = "common/mod.rs"] #[allow(clippy::duplicate_mod)] mod common; diff --git a/hermes/crates/cbork/cddl-parser/tests/literal_values.rs b/hermes/crates/cbork/cddl-parser/tests/literal_values.rs index 027816b65..b179c0b14 100644 --- a/hermes/crates/cbork/cddl-parser/tests/literal_values.rs +++ b/hermes/crates/cbork/cddl-parser/tests/literal_values.rs @@ -4,7 +4,7 @@ use std::ops::Deref; use cddl_parser::{self, cddl_test::Rule}; -#[path = "./common/mod.rs"] +#[path = "common/mod.rs"] #[allow(clippy::duplicate_mod)] mod common; diff --git a/hermes/crates/cbork/cddl-parser/tests/rules.rs b/hermes/crates/cbork/cddl-parser/tests/rules.rs index 2d82d453a..459216abb 100644 --- a/hermes/crates/cbork/cddl-parser/tests/rules.rs +++ b/hermes/crates/cbork/cddl-parser/tests/rules.rs @@ -6,7 +6,7 @@ use cddl_parser::{ cddl_test::{CDDLTestParser, Parser, Rule}, }; -#[path = "./common/mod.rs"] +#[path = "common/mod.rs"] #[allow(clippy::duplicate_mod)] mod common; diff --git a/hermes/crates/cbork/cddl-parser/tests/text_sequences.rs b/hermes/crates/cbork/cddl-parser/tests/text_sequences.rs index 34daa2297..a2c50b0f4 100644 --- a/hermes/crates/cbork/cddl-parser/tests/text_sequences.rs +++ b/hermes/crates/cbork/cddl-parser/tests/text_sequences.rs @@ -1,6 +1,6 @@ use cddl_parser::{self, cddl_test::Rule}; -#[path = "./common/mod.rs"] +#[path = "common/mod.rs"] #[allow(clippy::duplicate_mod)] mod common; diff --git a/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs b/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs index 1982e1632..f9258d9ce 100644 --- a/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs +++ b/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs @@ -6,7 +6,7 @@ use cddl_parser::{ cddl_test::{CDDLTestParser, Parser, Rule}, }; -#[path = "./common/mod.rs"] +#[path = "common/mod.rs"] #[allow(clippy::duplicate_mod)] mod common; From 29bef0eadaa64db22dcb74d7de8158e2214857b6 Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Thu, 25 Jan 2024 17:24:55 +0700 Subject: [PATCH 39/43] feat: common --- .../cbork/cddl-parser/tests/common/mod.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 hermes/crates/cbork/cddl-parser/tests/common/mod.rs diff --git a/hermes/crates/cbork/cddl-parser/tests/common/mod.rs b/hermes/crates/cbork/cddl-parser/tests/common/mod.rs new file mode 100644 index 000000000..bd04024a2 --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/common/mod.rs @@ -0,0 +1,17 @@ +use cddl_parser::{ + self, + cddl_test::{CDDLTestParser, Parser, Rule}, +}; + +/// # Panics +pub(crate) fn check_tests_rule(rule_type: Rule, passes: &[&str], fails: &[&str]) { + for test in passes { + let parse = CDDLTestParser::parse(rule_type, test); + assert!(parse.is_ok()); + } + + for test in fails { + let parse = CDDLTestParser::parse(rule_type, test); + assert!(parse.is_err()); + } +} From 188cb15460e81a14880693aed31cdf5b4d4fd58f Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Thu, 25 Jan 2024 19:52:56 +0700 Subject: [PATCH 40/43] refactor: remove tmp vars --- .../cbork/cddl-parser/tests/byte_sequences.rs | 15 ++----- .../cbork/cddl-parser/tests/comments.rs | 14 +++---- .../cbork/cddl-parser/tests/group_elements.rs | 35 ++++------------- .../cbork/cddl-parser/tests/identifiers.rs | 5 +-- .../cbork/cddl-parser/tests/literal_values.rs | 25 +++--------- .../crates/cbork/cddl-parser/tests/rules.rs | 39 ++++++------------- .../cbork/cddl-parser/tests/text_sequences.rs | 10 +---- .../cddl-parser/tests/type_declarations.rs | 25 +++--------- 8 files changed, 40 insertions(+), 128 deletions(-) diff --git a/hermes/crates/cbork/cddl-parser/tests/byte_sequences.rs b/hermes/crates/cbork/cddl-parser/tests/byte_sequences.rs index ed879645c..f5f69e76e 100644 --- a/hermes/crates/cbork/cddl-parser/tests/byte_sequences.rs +++ b/hermes/crates/cbork/cddl-parser/tests/byte_sequences.rs @@ -55,26 +55,17 @@ pub(crate) const BYTES_FAILS: &[&str] = &[ #[test] /// Test if the `HEX_PAIR` rule passes properly. fn check_hexpair() { - let passes = HEXPAIR_PASSES; - let fails = HEXPAIR_FAILS; - - common::check_tests_rule(Rule::HEX_PAIR, passes, fails); + common::check_tests_rule(Rule::HEX_PAIR, HEXPAIR_PASSES, HEXPAIR_FAILS); } #[test] /// Test if the `URL_BASE64` rule passes properly. fn check_url_base64() { - let passes = URL_BASE64_PASSES; - let fails = URL_BASE64_FAILS; - - common::check_tests_rule(Rule::URL_BASE64_TEST, passes, fails); + common::check_tests_rule(Rule::URL_BASE64_TEST, URL_BASE64_PASSES, URL_BASE64_FAILS); } #[test] /// Test if the `bytes` rule passes properly. fn check_bytes() { - let passes = BYTES_PASSES; - let fails = BYTES_FAILS; - - common::check_tests_rule(Rule::bytes_TEST, passes, fails); + common::check_tests_rule(Rule::bytes_TEST, BYTES_PASSES, BYTES_FAILS); } diff --git a/hermes/crates/cbork/cddl-parser/tests/comments.rs b/hermes/crates/cbork/cddl-parser/tests/comments.rs index efa255e7c..ff7659c2d 100644 --- a/hermes/crates/cbork/cddl-parser/tests/comments.rs +++ b/hermes/crates/cbork/cddl-parser/tests/comments.rs @@ -26,18 +26,16 @@ pub(crate) const WHITESPACE_COMMENT_FAILS: &[&str] = &["not a comment"]; #[test] /// Test if the `COMMENT` rule passes properly. fn check_comment() { - let passes = COMMENT_PASSES; - let fails = COMMENT_FAILS; - - common::check_tests_rule(Rule::COMMENT_TEST, passes, fails); + common::check_tests_rule(Rule::COMMENT_TEST, COMMENT_PASSES, COMMENT_FAILS); } #[test] /// Test if the `COMMENT` rule passes properly with whitespace. /// This uses a special rule in the Grammar to test whitespace exhaustively. fn check_whitespace_comments() { - let passes = WHITESPACE_COMMENT_PASSES; - let fails = WHITESPACE_COMMENT_FAILS; - - common::check_tests_rule(Rule::COMMENT_TEST, passes, fails); + common::check_tests_rule( + Rule::COMMENT_TEST, + WHITESPACE_COMMENT_PASSES, + WHITESPACE_COMMENT_FAILS, + ); } diff --git a/hermes/crates/cbork/cddl-parser/tests/group_elements.rs b/hermes/crates/cbork/cddl-parser/tests/group_elements.rs index 9c1288a3e..b8744f0de 100644 --- a/hermes/crates/cbork/cddl-parser/tests/group_elements.rs +++ b/hermes/crates/cbork/cddl-parser/tests/group_elements.rs @@ -125,68 +125,47 @@ pub(crate) const GROUP_FAILS: &[&str] = &["(foo: 1) / (bar: 2)"]; /// Test if the `occur` rule passes properly. /// This uses a special rule in the Grammar to test `occur` exhaustively. fn check_occur() { - let passes = OCCUR_PASSES; - let fails = OCCUR_FAILS; - - common::check_tests_rule(Rule::occur_TEST, passes, fails); + common::check_tests_rule(Rule::occur_TEST, OCCUR_PASSES, OCCUR_FAILS); } #[test] /// Test if the `bareword` rule passes properly. /// This uses a special rule in the Grammar to test `bareword` exhaustively. fn check_bareword() { - let passes = ID_PASSES; - let fails = ID_FAILS; - - common::check_tests_rule(Rule::bareword_TEST, passes, fails); + common::check_tests_rule(Rule::bareword_TEST, ID_PASSES, ID_FAILS); } #[test] /// Test if the `optcom` rule passes properly. /// This uses a special rule in the Grammar to test `optcom` exhaustively. fn check_optcom() { - let passes = OPTCOM_PASSES; - let fails = OPTCOM_FAILS; - - common::check_tests_rule(Rule::optcom_TEST, passes, fails); + common::check_tests_rule(Rule::optcom_TEST, OPTCOM_PASSES, OPTCOM_FAILS); } #[test] /// Test if the `memberkey` rule passes properly. /// This uses a special rule in the Grammar to test `memberkey` exhaustively. fn check_memberkey() { - let passes = MEMBERKEY_PASSES; - let fails = MEMBERKEY_FAILS; - - common::check_tests_rule(Rule::memberkey_TEST, passes, fails); + common::check_tests_rule(Rule::memberkey_TEST, MEMBERKEY_PASSES, MEMBERKEY_FAILS); } #[test] /// Test if the `grpent` rule passes properly. /// This uses a special rule in the Grammar to test `grpent` exhaustively. fn check_grpent() { - let passes = GRPENT_PASSES; - let fails = GRPENT_FAILS; - - common::check_tests_rule(Rule::grpent_TEST, passes, fails); + common::check_tests_rule(Rule::grpent_TEST, GRPENT_PASSES, GRPENT_FAILS); } #[test] /// Test if the `grpchoice` rule passes properly. /// This uses a special rule in the Grammar to test `grpchoice` exhaustively. fn check_grpchoice() { - let passes = GRPCHOICE_PASSES; - let fails = GRPCHOICE_FAILS; - - common::check_tests_rule(Rule::grpchoice_TEST, passes, fails); + common::check_tests_rule(Rule::grpchoice_TEST, GRPCHOICE_PASSES, GRPCHOICE_FAILS); } #[test] /// Test if the `group` rule passes properly. /// This uses a special rule in the Grammar to test `group` exhaustively. fn check_group() { - let passes = GROUP_PASSES; - let fails = GROUP_FAILS; - - common::check_tests_rule(Rule::group_TEST, passes, fails); + common::check_tests_rule(Rule::group_TEST, GROUP_PASSES, GROUP_FAILS); } diff --git a/hermes/crates/cbork/cddl-parser/tests/identifiers.rs b/hermes/crates/cbork/cddl-parser/tests/identifiers.rs index 071060ae3..4483e4efd 100644 --- a/hermes/crates/cbork/cddl-parser/tests/identifiers.rs +++ b/hermes/crates/cbork/cddl-parser/tests/identifiers.rs @@ -70,8 +70,5 @@ fn check_name_characters() { #[test] /// Test if the `id` rule passes properly. fn check_id() { - let passes = ID_PASSES; - let fails = ID_FAILS; - - common::check_tests_rule(Rule::id_TEST, passes, fails); + common::check_tests_rule(Rule::id_TEST, ID_PASSES, ID_FAILS); } diff --git a/hermes/crates/cbork/cddl-parser/tests/literal_values.rs b/hermes/crates/cbork/cddl-parser/tests/literal_values.rs index b179c0b14..f19c379f8 100644 --- a/hermes/crates/cbork/cddl-parser/tests/literal_values.rs +++ b/hermes/crates/cbork/cddl-parser/tests/literal_values.rs @@ -119,46 +119,31 @@ pub(crate) const VALUE_FAILS: &[&str] = &[]; #[test] /// Test if the `uint` rule passes properly. fn check_uint() { - let passes = UINT_PASSES; - let fails = UINT_FAILS; - - common::check_tests_rule(Rule::uint_TEST, passes, fails); + common::check_tests_rule(Rule::uint_TEST, UINT_PASSES, UINT_FAILS); } #[test] /// Test if the `uint` rule passes properly. fn check_int() { - let passes = INT_PASSES; - let fails = INT_FAILS; - - common::check_tests_rule(Rule::int_TEST, passes, fails); + common::check_tests_rule(Rule::int_TEST, INT_PASSES, INT_FAILS); } #[test] /// Test if the `uint` rule passes properly. fn check_intfloat() { - let passes = INTFLOAT_PASSES; - let fails = INTFLOAT_FAILS; - - common::check_tests_rule(Rule::intfloat_TEST, passes, fails); + common::check_tests_rule(Rule::intfloat_TEST, INTFLOAT_PASSES, INTFLOAT_FAILS); } #[test] /// Test if the `uint` rule passes properly. fn check_hexfloat() { - let passes = HEXFLOAT_PASSES; - let fails = HEXFLOAT_FAILS; - - common::check_tests_rule(Rule::hexfloat_TEST, passes, fails); + common::check_tests_rule(Rule::hexfloat_TEST, HEXFLOAT_PASSES, HEXFLOAT_FAILS); } #[test] /// Test if the `number` rule passes properly. fn check_number() { - let passes = NUMBER_PASSES; - let fails = NUMBER_FAILS; - - common::check_tests_rule(Rule::number_TEST, passes, fails); + common::check_tests_rule(Rule::number_TEST, NUMBER_PASSES, NUMBER_FAILS); } #[test] diff --git a/hermes/crates/cbork/cddl-parser/tests/rules.rs b/hermes/crates/cbork/cddl-parser/tests/rules.rs index 459216abb..094d95939 100644 --- a/hermes/crates/cbork/cddl-parser/tests/rules.rs +++ b/hermes/crates/cbork/cddl-parser/tests/rules.rs @@ -73,60 +73,46 @@ pub(crate) const RULE_GROUP_FAILS: &[&str] = &["foo = bar: baz", "t /= (foo: bar /// Test if the `genericarg` rule passes properly. /// This uses a special rule in the Grammar to test `genericarg` exhaustively. fn check_genericarg() { - let passes = GENERICARG_PASSES; - let fails = GENERICARG_FAILS; - - common::check_tests_rule(Rule::genericarg_TEST, passes, fails); + common::check_tests_rule(Rule::genericarg_TEST, GENERICARG_PASSES, GENERICARG_FAILS); } #[test] /// Test if the `genericparm` rule passes properly. /// This uses a special rule in the Grammar to test `genericparm` exhaustively. fn check_genericparm() { - let passes = GENERICPARM_PASSES; - let fails = GENERICPARM_FAILS; - - common::check_tests_rule(Rule::genericparm_TEST, passes, fails); + common::check_tests_rule( + Rule::genericparm_TEST, + GENERICPARM_PASSES, + GENERICPARM_FAILS, + ); } #[test] /// Test if the `assigng` rule passes properly. /// This uses a special rule in the Grammar to test `assigng` exhaustively. fn check_assigng() { - let passes = ASSIGNG_PASSES; - let fails = ASSIGNG_FAILS; - - common::check_tests_rule(Rule::assigng_TEST, passes, fails); + common::check_tests_rule(Rule::assigng_TEST, ASSIGNG_PASSES, ASSIGNG_FAILS); } #[test] /// Test if the `assignt` rule passes properly. /// This uses a special rule in the Grammar to test `assignt` exhaustively. fn check_assignt() { - let passes = ASSIGNT_PASSES; - let fails = ASSIGNT_FAILS; - - common::check_tests_rule(Rule::assignt_TEST, passes, fails); + common::check_tests_rule(Rule::assignt_TEST, ASSIGNT_PASSES, ASSIGNT_FAILS); } #[test] /// Test if the `typename` rule passes properly. /// This uses a special rule in the Grammar to test `typename` exhaustively. fn check_typename() { - let passes = TYPENAME_PASSES; - let fails = TYPENAME_FAILS; - - common::check_tests_rule(Rule::typename_TEST, passes, fails); + common::check_tests_rule(Rule::typename_TEST, TYPENAME_PASSES, TYPENAME_FAILS); } #[test] /// Test if the `groupname` rule passes properly. /// This uses a special rule in the Grammar to test `groupname` exhaustively. fn check_groupname() { - let passes = GROUPNAME_PASSES; - let fails = GROUPNAME_FAILS; - - common::check_tests_rule(Rule::groupname_TEST, passes, fails); + common::check_tests_rule(Rule::groupname_TEST, GROUPNAME_PASSES, GROUPNAME_FAILS); } #[test] @@ -157,8 +143,5 @@ fn check_rule_type_composition() { #[test] /// Test if the `rule` rule passes properly for group variant. fn check_rule_group() { - let passes = RULE_GROUP_PASSES; - let fails = RULE_GROUP_FAILS; - - common::check_tests_rule(Rule::rule_TEST, passes, fails); + common::check_tests_rule(Rule::rule_TEST, RULE_GROUP_PASSES, RULE_GROUP_FAILS); } diff --git a/hermes/crates/cbork/cddl-parser/tests/text_sequences.rs b/hermes/crates/cbork/cddl-parser/tests/text_sequences.rs index a2c50b0f4..56735bd2e 100644 --- a/hermes/crates/cbork/cddl-parser/tests/text_sequences.rs +++ b/hermes/crates/cbork/cddl-parser/tests/text_sequences.rs @@ -13,17 +13,11 @@ pub(crate) const TEXT_FAILS: &[&str] = &["", "''", "\"abc\n\""]; /// Test if the `S` rule passes properly. /// This uses a special rule in the Grammar to test whitespace exhaustively. fn check_s() { - let passes = S_PASSES; - let fails = S_FAILS; - - common::check_tests_rule(Rule::S_TEST, passes, fails); + common::check_tests_rule(Rule::S_TEST, S_PASSES, S_FAILS); } #[test] /// Test if the `text` rule passes properly. fn check_text() { - let passes = TEXT_PASSES; - let fails = TEXT_FAILS; - - common::check_tests_rule(Rule::text_TEST, passes, fails); + common::check_tests_rule(Rule::text_TEST, TEXT_PASSES, TEXT_FAILS); } diff --git a/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs b/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs index f9258d9ce..786b20ace 100644 --- a/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs +++ b/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs @@ -123,40 +123,28 @@ pub(crate) const TYPE_FAILS: &[&str] = &["", "1 \\ 2", "1 // 2", "1 2", "1 / 2 3 /// Test if the `ctlop` rule passes properly. /// This uses a special rule in the Grammar to test `ctlop` exhaustively. fn check_ctlop() { - let passes = CTLOP_PASSES; - let fails = CTLOP_FAILS; - - common::check_tests_rule(Rule::ctlop_TEST, passes, fails); + common::check_tests_rule(Rule::ctlop_TEST, CTLOP_PASSES, CTLOP_FAILS); } #[test] /// Test if the `rangeop` rule passes properly. /// This uses a special rule in the Grammar to test `rangeop` exhaustively. fn check_rangeop() { - let passes = RANGEOP_PASSES; - let fails = RANGEOP_FAILS; - - common::check_tests_rule(Rule::rangeop_TEST, passes, fails); + common::check_tests_rule(Rule::rangeop_TEST, RANGEOP_PASSES, RANGEOP_FAILS); } #[test] /// Test if the `type2` rule passes properly. /// This uses a special rule in the Grammar to test `type2` exhaustively. fn check_type2() { - let passes = TYPE2_PASSES; - let fails = TYPE2_FAILS; - - common::check_tests_rule(Rule::type2_TEST, passes, fails); + common::check_tests_rule(Rule::type2_TEST, TYPE2_PASSES, TYPE2_FAILS); } #[test] /// Test if the `type1` rule passes properly. /// This uses a special rule in the Grammar to test `type1` exhaustively. fn check_type1() { - let passes = TYPE1_PASSES; - let fails = TYPE1_FAILS; - - common::check_tests_rule(Rule::type1_TEST, passes, fails); + common::check_tests_rule(Rule::type1_TEST, TYPE1_PASSES, TYPE1_FAILS); } #[test] @@ -189,10 +177,7 @@ fn check_type1_composition() { /// Test if the `type` rule passes properly. /// This uses a special rule in the Grammar to test `type` exhaustively. fn check_type() { - let passes = TYPE_PASSES; - let fails = TYPE_FAILS; - - common::check_tests_rule(Rule::type_TEST, passes, fails); + common::check_tests_rule(Rule::type_TEST, TYPE_PASSES, TYPE_FAILS); } #[test] From 51405061372de709b60497e718771c4f193bcbfc Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Fri, 26 Jan 2024 15:59:47 +0700 Subject: [PATCH 41/43] feat: common consts --- .../cbork/cddl-parser/tests/byte_sequences.rs | 49 +------- .../cbork/cddl-parser/tests/comments.rs | 22 +--- .../tests/common/byte_sequences.rs | 45 +++++++ .../cddl-parser/tests/common/comments.rs | 18 +++ .../tests/common/group_elements.rs | 110 ++++++++++++++++ .../cddl-parser/tests/common/identifiers.rs | 36 ++++++ .../tests/common/literal_values.rs | 102 +++++++++++++++ .../cbork/cddl-parser/tests/common/mod.rs | 9 ++ .../cbork/cddl-parser/tests/common/rules.rs | 55 ++++++++ .../tests/common/text_sequences.rs | 4 + .../tests/common/type_declarations.rs | 108 ++++++++++++++++ .../cbork/cddl-parser/tests/group_elements.rs | 117 +----------------- .../cbork/cddl-parser/tests/identifiers.rs | 40 +----- .../cbork/cddl-parser/tests/literal_values.rs | 111 +---------------- .../crates/cbork/cddl-parser/tests/rules.rs | 62 +--------- .../cbork/cddl-parser/tests/text_sequences.rs | 8 +- .../cddl-parser/tests/type_declarations.rs | 112 +---------------- 17 files changed, 495 insertions(+), 513 deletions(-) create mode 100644 hermes/crates/cbork/cddl-parser/tests/common/byte_sequences.rs create mode 100644 hermes/crates/cbork/cddl-parser/tests/common/comments.rs create mode 100644 hermes/crates/cbork/cddl-parser/tests/common/group_elements.rs create mode 100644 hermes/crates/cbork/cddl-parser/tests/common/identifiers.rs create mode 100644 hermes/crates/cbork/cddl-parser/tests/common/literal_values.rs create mode 100644 hermes/crates/cbork/cddl-parser/tests/common/rules.rs create mode 100644 hermes/crates/cbork/cddl-parser/tests/common/text_sequences.rs create mode 100644 hermes/crates/cbork/cddl-parser/tests/common/type_declarations.rs diff --git a/hermes/crates/cbork/cddl-parser/tests/byte_sequences.rs b/hermes/crates/cbork/cddl-parser/tests/byte_sequences.rs index f5f69e76e..5df53da86 100644 --- a/hermes/crates/cbork/cddl-parser/tests/byte_sequences.rs +++ b/hermes/crates/cbork/cddl-parser/tests/byte_sequences.rs @@ -2,55 +2,8 @@ use cddl_parser::cddl_test::Rule; -#[path = "common/mod.rs"] -#[allow(clippy::duplicate_mod)] mod common; - -pub(crate) const HEXPAIR_PASSES: &[&str] = &["00", "ab", "de", "0f", "f0"]; - -pub(crate) const HEXPAIR_FAILS: &[&str] = &["0", " 0", "0 ", "az", "0p"]; - -pub(crate) const URL_BASE64_PASSES: &[&str] = &[ - "abcdefghijklmnopq rstuvw yz01\t23456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ~", - "abcdefghijklmnopqrstuvwyz0123456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ", -]; - -pub(crate) const URL_BASE64_FAILS: &[&str] = &[ - "abcdefghijklmnopq # rstuvw yz01\t23456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ~ ", - "abcdefghijklmnopq $ rstuvw yz01\t23456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ~\t", - "abcdefghijklmnopq % rstuvw yz01\t23456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ~\n", - "abcdefghijklmnopq ^ rstuvw yz01\t23456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ~\r", - "abcdefghijklmnopq & rstuvw yz01\t23456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ~\r\n", -]; - -pub(crate) const BYTES_PASSES: &[&str] = &[ - "h''", - "b64''", - "''", - "h'00'", - "h'63666F6FF6'", - "h'68656c6c6f20776f726c64'", - "h'4 86 56c 6c6f'", - "h' 20776 f726c64'", - "h'00112233445566778899aabbccddeeff0123456789abcdef'", - "h'0 1 2 3 4 5 6 7 8 9 a b c d e f'", - "h' 0 1 2 3 4 5\r 6 7 \n 8 9 a\r\n\t b c d e f'", - "h'0 \n\n\r f'", - "b64'aHR0cHM6Ly93d3cuZXhhbXBsZS5jb20vcGFnZT9wYXJhbTE9dmFsdWUxJnBhcmFtMj12YWx1ZTI~'", - "'text\n that gets converted \\\' into a byte string...'", -]; - -pub(crate) const BYTES_FAILS: &[&str] = &[ - "h64", - "b64", - "\"\"", - "h ''", - "b64 ''", - "h'001'", - "b64'abcdefghijklmnopq # rstuvw yz01\t23456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ~'", - "b64'abcdefghijklmnopq & rstuvw yz01\t23456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ'", - "'\u{7}'", -]; +use common::byte_sequences::*; #[test] /// Test if the `HEX_PAIR` rule passes properly. diff --git a/hermes/crates/cbork/cddl-parser/tests/comments.rs b/hermes/crates/cbork/cddl-parser/tests/comments.rs index ff7659c2d..f819072ab 100644 --- a/hermes/crates/cbork/cddl-parser/tests/comments.rs +++ b/hermes/crates/cbork/cddl-parser/tests/comments.rs @@ -1,27 +1,7 @@ use cddl_parser::{self, cddl_test::Rule}; -#[path = "common/mod.rs"] -#[allow(clippy::duplicate_mod)] mod common; - -pub(crate) const COMMENT_PASSES: &[&str] = &["; A Comment \n", "; And another\r", ";more\r\n"]; - -pub(crate) const COMMENT_FAILS: &[&str] = &["not a comment\n"]; - -pub(crate) const WHITESPACE_COMMENT_PASSES: &[&str] = &[ - " ", - " ", - " \t \t", - " \t \r \n \r\n ", - "; A Comment\r", - " \t ; A Comment \n", - "; One Comment\n; Two Comments\n", - "; One Comment \n; Two Comments\r; Another Comment\r\n", - "\t; One Comment \n\t; Two Comments\r; Another Comment\r\n", - "\t; A Comment \n ; Another Comment \t \r\n \t ; A Final Comment \r\n", -]; - -pub(crate) const WHITESPACE_COMMENT_FAILS: &[&str] = &["not a comment"]; +use common::comments::*; #[test] /// Test if the `COMMENT` rule passes properly. diff --git a/hermes/crates/cbork/cddl-parser/tests/common/byte_sequences.rs b/hermes/crates/cbork/cddl-parser/tests/common/byte_sequences.rs new file mode 100644 index 000000000..ae84b94ac --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/common/byte_sequences.rs @@ -0,0 +1,45 @@ +pub(crate) const HEXPAIR_PASSES: &[&str] = &["00", "ab", "de", "0f", "f0"]; + +pub(crate) const HEXPAIR_FAILS: &[&str] = &["0", " 0", "0 ", "az", "0p"]; + +pub(crate) const URL_BASE64_PASSES: &[&str] = &[ + "abcdefghijklmnopq rstuvw yz01\t23456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ~", + "abcdefghijklmnopqrstuvwyz0123456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ", +]; + +pub(crate) const URL_BASE64_FAILS: &[&str] = &[ + "abcdefghijklmnopq # rstuvw yz01\t23456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ~ ", + "abcdefghijklmnopq $ rstuvw yz01\t23456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ~\t", + "abcdefghijklmnopq % rstuvw yz01\t23456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ~\n", + "abcdefghijklmnopq ^ rstuvw yz01\t23456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ~\r", + "abcdefghijklmnopq & rstuvw yz01\t23456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ~\r\n", +]; + +pub(crate) const BYTES_PASSES: &[&str] = &[ + "h''", + "b64''", + "''", + "h'00'", + "h'63666F6FF6'", + "h'68656c6c6f20776f726c64'", + "h'4 86 56c 6c6f'", + "h' 20776 f726c64'", + "h'00112233445566778899aabbccddeeff0123456789abcdef'", + "h'0 1 2 3 4 5 6 7 8 9 a b c d e f'", + "h' 0 1 2 3 4 5\r 6 7 \n 8 9 a\r\n\t b c d e f'", + "h'0 \n\n\r f'", + "b64'aHR0cHM6Ly93d3cuZXhhbXBsZS5jb20vcGFnZT9wYXJhbTE9dmFsdWUxJnBhcmFtMj12YWx1ZTI~'", + "'text\n that gets converted \\\' into a byte string...'", +]; + +pub(crate) const BYTES_FAILS: &[&str] = &[ + "h64", + "b64", + "\"\"", + "h ''", + "b64 ''", + "h'001'", + "b64'abcdefghijklmnopq # rstuvw yz01\t23456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ~'", + "b64'abcdefghijklmnopq & rstuvw yz01\t23456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ'", + "'\u{7}'", +]; \ No newline at end of file diff --git a/hermes/crates/cbork/cddl-parser/tests/common/comments.rs b/hermes/crates/cbork/cddl-parser/tests/common/comments.rs new file mode 100644 index 000000000..6c61f38e2 --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/common/comments.rs @@ -0,0 +1,18 @@ +pub(crate) const COMMENT_PASSES: &[&str] = &["; A Comment \n", "; And another\r", ";more\r\n"]; + +pub(crate) const COMMENT_FAILS: &[&str] = &["not a comment\n"]; + +pub(crate) const WHITESPACE_COMMENT_PASSES: &[&str] = &[ + " ", + " ", + " \t \t", + " \t \r \n \r\n ", + "; A Comment\r", + " \t ; A Comment \n", + "; One Comment\n; Two Comments\n", + "; One Comment \n; Two Comments\r; Another Comment\r\n", + "\t; One Comment \n\t; Two Comments\r; Another Comment\r\n", + "\t; A Comment \n ; Another Comment \t \r\n \t ; A Final Comment \r\n", +]; + +pub(crate) const WHITESPACE_COMMENT_FAILS: &[&str] = &["not a comment"]; \ No newline at end of file diff --git a/hermes/crates/cbork/cddl-parser/tests/common/group_elements.rs b/hermes/crates/cbork/cddl-parser/tests/common/group_elements.rs new file mode 100644 index 000000000..5ac47cadc --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/common/group_elements.rs @@ -0,0 +1,110 @@ +pub(crate) const OCCUR_PASSES: &[&str] = &[ + "*", + "+", + "?", + "5*10", + "0x1*0b110", + "*20", + "5*10", + "0x1*0b110", + "0*5", + "5*", + "*5", + "0b110*", + "0x1*", +]; + +pub(crate) const OCCUR_FAILS: &[&str] = &[ + "5**10", + "5 * 10", + "5\t\n*\n10", + "++", + "??", + // Fail cases for uint + "0123", // Leading zero is not allowed for decimal + "0xG", // Invalid hex digit + "0b123", // Invalid binary digit + "0*5*", // Multiple '*' not allowed + "0x1*0b110*", + "0x", + "0b", +]; + +pub(crate) const OPTCOM_PASSES: &[&str] = &["", ",", " ,", " , ", "\n,\n", "\n"]; + +pub(crate) const OPTCOM_FAILS: &[&str] = &[",,"]; + +pub(crate) const MEMBERKEY_PASSES: &[&str] = &[ + // bareword + "foo:", + "foo-bar:", + "foo_bar:", + "foo :", + // values + "\"foo\":", + "1:", + "0x123:", + "1.1:", + "-1:", + "b64'1234':", + "h'1234':", + "h'12 34\n':", + // type1 + "tstr =>", + "id =>", + "# =>", + "1..2 =>", + "1...2 =>", + "\"foo\" =>", + "\"foo\" ^=>", + "\"foo\"^ =>", + "\"foo\" ^ =>", + "1 =>", + "0x123 =>", + "1.1 =>", + "-1 =>", + "b64'1234' =>", + "h'1234' =>", + "h'12 34\n' =>", +]; + +pub(crate) const MEMBERKEY_FAILS: &[&str] = &["#:", "foo::"]; + +pub(crate) const GRPENT_PASSES: &[&str] = &[ + "foo: 1", + "foo: 1", + "foo-bar:\t\n1", + "foo :\n1", + "foo: #", + "tstr => any", + "tstr => { foo: bar }", + "tstr => { foo: bar, baz }", + "tstr => [foo: bar, baz]", +]; + +pub(crate) const GRPENT_FAILS: &[&str] = &["tstr => (foo: bar)"]; + +pub(crate) const GRPCHOICE_PASSES: &[&str] = &[ + "foo: 1", + "foo: 1, bar: 2", + "foo: 1, bar: 2,", + "foo: 1\nbar: 2", + "foo: 1 bar: 2", + "foo => 1 bar: 2", + "foo => 1, bar => 2", + "foo => 1, bar: 2", + "foo => 1bar: 2", +]; + +pub(crate) const GRPCHOICE_FAILS: &[&str] = &["foo: ,", "foo:", "foo: bar: 2", "foo => bar: 2"]; + +pub(crate) const GROUP_PASSES: &[&str] = &[ + "(foo: 1)", + "(foo: 1) // (bar: 2)", + "(foo: 1) // (bar: 2)", + "(street: tstr, ? number: uint, city // po-box: uint, city // per-pickup: true)", + "(+ a // b / c)", + "((+ a) // (b / c))", +]; + +pub(crate) const GROUP_FAILS: &[&str] = &["(foo: 1) / (bar: 2)"]; diff --git a/hermes/crates/cbork/cddl-parser/tests/common/identifiers.rs b/hermes/crates/cbork/cddl-parser/tests/common/identifiers.rs new file mode 100644 index 000000000..fbac23ff1 --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/common/identifiers.rs @@ -0,0 +1,36 @@ +pub(crate) const ID_PASSES: &[&str] = &[ + "$", + "@", + "_", + "a", + "z", + "A", + "Z", + "$$", + "@@", + "__", + "a$", + "a@", + "a_", + "$0", + "@9", + "_a", + "abc", + "aname", + "@aname", + "_aname", + "$aname", + "a$name", + "a.name", + "@a.name", + "$a.name", + "_a.name", + "$$", + "$$groupsocket", + "$", + "$typesocket", +]; + +pub(crate) const ID_FAILS: &[&str] = &[ + "aname.", "aname-", "aname%", "a%name4", "a^name5", "a name", "", +]; diff --git a/hermes/crates/cbork/cddl-parser/tests/common/literal_values.rs b/hermes/crates/cbork/cddl-parser/tests/common/literal_values.rs new file mode 100644 index 000000000..8b4161b26 --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/common/literal_values.rs @@ -0,0 +1,102 @@ +pub(crate) const UINT_PASSES: &[&str] = &[ + "10", + "101", + "2034", + "30456", + "123456789", + "0x123456789abcdefABCDEF", + "0b0001110101010101", + "0", +]; + +pub(crate) const UINT_FAILS: &[&str] = &[" a ", "zz", "0123zzz", "0xdog", "0b777"]; + +pub(crate) const INT_PASSES: &[&str] = &[ + "10", + "101", + "2034", + "30456", + "123456789", + "0x123456789abcdefABCDEF", + "0b0001110101010101", + "0", + "-10", + "-101", + "-2034", + "-30456", + "-123456789", + "-0x123456789abcdefABCDEF", + "-0b0001110101010101", + "-0", +]; + +pub(crate) const INT_FAILS: &[&str] = &[" a ", "zz", "0123zzz", "0xdog", "0b777"]; + +pub(crate) const INTFLOAT_PASSES: &[&str] = &[ + "10", + "101", + "2034", + "30456", + "123456789", + "0", + "-10", + "-101", + "-2034", + "-30456", + "-123456789", + "123.456", + "123.456", + "123e+789", + "123e-789", + "123.456e+789", + "123.456e-789", +]; + +pub(crate) const INTFLOAT_FAILS: &[&str] = &[" a ", "zz", "0123zzz", "0xdog", "0b777"]; + +pub(crate) const HEXFLOAT_PASSES: &[&str] = &[ + "0xabcp+123", + "-0xabcp+123", + "0xabcp-123", + "-0xabcp-123", + "0xabc.defp+123", + "-0xabc.defp+123", + "0xabc.defp-123", + "-0xabc.defp-123", +]; + +pub(crate) const HEXFLOAT_FAILS: &[&str] = &[" a ", "zz", "0123zzz", "0xdog", "0b777"]; + +pub(crate) const NUMBER_PASSES: &[&str] = &[ + "0xabcp+123", + "-0xabcp+123", + "0xabcp-123", + "-0xabcp-123", + "0xabc.defp+123", + "-0xabc.defp+123", + "0xabc.defp-123", + "-0xabc.defp-123", + "10", + "101", + "2034", + "30456", + "123456789", + "0", + "-10", + "-101", + "-2034", + "-30456", + "-123456789", + "123.456", + "123.456", + "123e+789", + "123e-789", + "123.456e+789", + "123.456e-789", +]; + +pub(crate) const NUMBER_FAILS: &[&str] = &[" a ", "zz", "0123zzz", "0xdog", "0b777"]; + +pub(crate) const VALUE_PASSES: &[&str] = &[]; + +pub(crate) const VALUE_FAILS: &[&str] = &[]; diff --git a/hermes/crates/cbork/cddl-parser/tests/common/mod.rs b/hermes/crates/cbork/cddl-parser/tests/common/mod.rs index bd04024a2..fd6dbb23e 100644 --- a/hermes/crates/cbork/cddl-parser/tests/common/mod.rs +++ b/hermes/crates/cbork/cddl-parser/tests/common/mod.rs @@ -3,6 +3,15 @@ use cddl_parser::{ cddl_test::{CDDLTestParser, Parser, Rule}, }; +pub(crate) mod byte_sequences; +pub(crate) mod comments; +pub(crate) mod group_elements; +pub(crate) mod identifiers; +pub(crate) mod literal_values; +pub(crate) mod rules; +pub(crate) mod text_sequences; +pub(crate) mod type_declarations; + /// # Panics pub(crate) fn check_tests_rule(rule_type: Rule, passes: &[&str], fails: &[&str]) { for test in passes { diff --git a/hermes/crates/cbork/cddl-parser/tests/common/rules.rs b/hermes/crates/cbork/cddl-parser/tests/common/rules.rs new file mode 100644 index 000000000..257915012 --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/common/rules.rs @@ -0,0 +1,55 @@ +use super::identifiers::{ID_PASSES, ID_FAILS}; + +pub(crate) const GENERICARG_PASSES: &[&str] = &[ + "", + "<{ foo: bar }>", + "<{ h'1234': uint }>", + "<1...10>", + "<\n1...10\t>", + "<{ foo: bar }, { foo: baz }>", + "<{ foo: bar }, 1..10>", +]; + +pub(crate) const GENERICARG_FAILS: &[&str] = + &["", "<>", "", "<( foo: bar )>", ""]; + +pub(crate) const GENERICPARM_PASSES: &[&str] = + &["", "", "", ""]; + +pub(crate) const GENERICPARM_FAILS: &[&str] = &[ + "", + "<>", + "", + "<{ foo: bar }>", + "<{ h'1234': uint }>", + "<1...10>", + "<\n1...10\t>", +]; + +pub(crate) const ASSIGNG_PASSES: &[&str] = &["=", "//="]; + +pub(crate) const ASSIGNG_FAILS: &[&str] = &["==", "/="]; + +pub(crate) const ASSIGNT_PASSES: &[&str] = &["=", "/="]; + +pub(crate) const ASSIGNT_FAILS: &[&str] = &["==", "//="]; + +pub(crate) const TYPENAME_PASSES: &[&str] = ID_PASSES; + +pub(crate) const TYPENAME_FAILS: &[&str] = ID_FAILS; + +pub(crate) const GROUPNAME_PASSES: &[&str] = ID_PASSES; + +pub(crate) const GROUPNAME_FAILS: &[&str] = ID_FAILS; + +pub(crate) const RULE_GROUP_PASSES: &[&str] = &[ + "foo = (bar: baz)", + "t //= (foo: bar)", + "t //= foo", + "t //= foo", + "t //= foo: bar", + "t //= 2*2 foo: bar", + "delivery //= ( lat: float, long: float, drone-type: tstr )", +]; + +pub(crate) const RULE_GROUP_FAILS: &[&str] = &["foo = bar: baz", "t /= (foo: bar)"]; diff --git a/hermes/crates/cbork/cddl-parser/tests/common/text_sequences.rs b/hermes/crates/cbork/cddl-parser/tests/common/text_sequences.rs new file mode 100644 index 000000000..ccd171542 --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/common/text_sequences.rs @@ -0,0 +1,4 @@ +pub(crate) const S_PASSES: &[&str] = &[" ", " ", " \t \t", " \t \r \n \r\n "]; +pub(crate) const S_FAILS: &[&str] = &[" a ", "zz", " \t d \t", " \t \r \n \t \r\n x"]; +pub(crate) const TEXT_PASSES: &[&str] = &[r#""""#, r#""abc""#, "\"abc\\n\""]; +pub(crate) const TEXT_FAILS: &[&str] = &["", "''", "\"abc\n\""]; diff --git a/hermes/crates/cbork/cddl-parser/tests/common/type_declarations.rs b/hermes/crates/cbork/cddl-parser/tests/common/type_declarations.rs new file mode 100644 index 000000000..374e978b1 --- /dev/null +++ b/hermes/crates/cbork/cddl-parser/tests/common/type_declarations.rs @@ -0,0 +1,108 @@ +pub(crate) const CTLOP_PASSES: &[&str] = &[ + ".$", + ".@", + "._", + ".a", + ".z", + ".A", + ".Z", + ".$$", + ".@@", + ".__", + ".a$", + ".a@", + ".a_", + ".$0", + ".@9", + "._a", + ".abc", + ".aname", + ".@aname", + "._aname", + ".$aname", + ".a$name", + ".a.name", + ".@a.name", + ".$a.name", + "._a.name", + ".$$", + ".$$groupsocket", + ".$", + ".$typesocket", +]; + +pub(crate) const CTLOP_FAILS: &[&str] = &[ + "aname.", ".", "..", "aname.", "aname-", "aname%", "a%name4", "a^name5", "a name", "", +]; + +pub(crate) const RANGEOP_PASSES: &[&str] = &["..", "..."]; + +pub(crate) const RANGEOP_FAILS: &[&str] = &[".", "", "....", ".. .", ". .."]; + +pub(crate) const TYPE2_PASSES: &[&str] = &[ + "#", + "#1", + "#1.1", + "#1.1", + "#6", + "#6.11", + "#6.11(tstr)", + "#6.11(\tstr\n)", + "#6.11({ foo })", + "#6.11([ foo ])", + "#6.11(#3.1)", + "&foo", + "& foo", + "&((+ a) // (b / c))", + "&\t( foo: bar )", + "~foo", + "~ foo", + "foo", + "[ foo bar ]", + "{ foo bar }", + "(a)", + "(a / b)", + "(#)", + "((a))", + "1", + "h'1111'", + "true", + "foo", +]; + +pub(crate) const TYPE2_FAILS: &[&str] = &[ + "", + "##", + "#1.", + "#6.11 (tstr)", + "#6.11(( foo: uint ))", + "&", + "& foo ", + "(foo bar)", +]; + +pub(crate) const TYPE1_PASSES: &[&str] = &[ + "1..2", + "1 .. 2", + "1\t..\n2", + "1...2", + "0..10.0", // BAD range 1 + "0.0..10", // BAD range 2 + "0..max-byte", + "min-type..max-byte", + "1.0..2.0", + "1.0...2.0", + "foo.bar", +]; + +pub(crate) const TYPE1_FAILS: &[&str] = &[""]; + +pub(crate) const TYPE_PASSES: &[&str] = &[ + "1 / 2", + "1\n/\t2", + "1 / 2 / 3 / 4", + "1 / (2 / (3 / 4))", + "# / #", +]; + +pub(crate) const TYPE_FAILS: &[&str] = &["", "1 \\ 2", "1 // 2", "1 2", "1 / 2 3"]; diff --git a/hermes/crates/cbork/cddl-parser/tests/group_elements.rs b/hermes/crates/cbork/cddl-parser/tests/group_elements.rs index b8744f0de..0fa4296de 100644 --- a/hermes/crates/cbork/cddl-parser/tests/group_elements.rs +++ b/hermes/crates/cbork/cddl-parser/tests/group_elements.rs @@ -3,123 +3,8 @@ use cddl_parser::{self, cddl_test::Rule}; -#[path = "common/mod.rs"] -#[allow(clippy::duplicate_mod)] mod common; - -mod identifiers; -use identifiers::{ID_FAILS, ID_PASSES}; - -pub(crate) const OCCUR_PASSES: &[&str] = &[ - "*", - "+", - "?", - "5*10", - "0x1*0b110", - "*20", - "5*10", - "0x1*0b110", - "0*5", - "5*", - "*5", - "0b110*", - "0x1*", -]; - -pub(crate) const OCCUR_FAILS: &[&str] = &[ - "5**10", - "5 * 10", - "5\t\n*\n10", - "++", - "??", - // Fail cases for uint - "0123", // Leading zero is not allowed for decimal - "0xG", // Invalid hex digit - "0b123", // Invalid binary digit - "0*5*", // Multiple '*' not allowed - "0x1*0b110*", - "0x", - "0b", -]; - -pub(crate) const OPTCOM_PASSES: &[&str] = &["", ",", " ,", " , ", "\n,\n", "\n"]; - -pub(crate) const OPTCOM_FAILS: &[&str] = &[",,"]; - -pub(crate) const MEMBERKEY_PASSES: &[&str] = &[ - // bareword - "foo:", - "foo-bar:", - "foo_bar:", - "foo :", - // values - "\"foo\":", - "1:", - "0x123:", - "1.1:", - "-1:", - "b64'1234':", - "h'1234':", - "h'12 34\n':", - // type1 - "tstr =>", - "id =>", - "# =>", - "1..2 =>", - "1...2 =>", - "\"foo\" =>", - "\"foo\" ^=>", - "\"foo\"^ =>", - "\"foo\" ^ =>", - "1 =>", - "0x123 =>", - "1.1 =>", - "-1 =>", - "b64'1234' =>", - "h'1234' =>", - "h'12 34\n' =>", -]; - -pub(crate) const MEMBERKEY_FAILS: &[&str] = &["#:", "foo::"]; - -pub(crate) const GRPENT_PASSES: &[&str] = &[ - "foo: 1", - "foo: 1", - "foo-bar:\t\n1", - "foo :\n1", - "foo: #", - "tstr => any", - "tstr => { foo: bar }", - "tstr => { foo: bar, baz }", - "tstr => [foo: bar, baz]", -]; - -pub(crate) const GRPENT_FAILS: &[&str] = &["tstr => (foo: bar)"]; - -pub(crate) const GRPCHOICE_PASSES: &[&str] = &[ - "foo: 1", - "foo: 1, bar: 2", - "foo: 1, bar: 2,", - "foo: 1\nbar: 2", - "foo: 1 bar: 2", - "foo => 1 bar: 2", - "foo => 1, bar => 2", - "foo => 1, bar: 2", - "foo => 1bar: 2", -]; - -pub(crate) const GRPCHOICE_FAILS: &[&str] = &["foo: ,", "foo:", "foo: bar: 2", "foo => bar: 2"]; - -pub(crate) const GROUP_PASSES: &[&str] = &[ - "(foo: 1)", - "(foo: 1) // (bar: 2)", - "(foo: 1) // (bar: 2)", - "(street: tstr, ? number: uint, city // po-box: uint, city // per-pickup: true)", - "(+ a // b / c)", - "((+ a) // (b / c))", -]; - -pub(crate) const GROUP_FAILS: &[&str] = &["(foo: 1) / (bar: 2)"]; +use common::{group_elements::*, identifiers::*}; #[test] /// Test if the `occur` rule passes properly. diff --git a/hermes/crates/cbork/cddl-parser/tests/identifiers.rs b/hermes/crates/cbork/cddl-parser/tests/identifiers.rs index 4483e4efd..c4f3e7dfa 100644 --- a/hermes/crates/cbork/cddl-parser/tests/identifiers.rs +++ b/hermes/crates/cbork/cddl-parser/tests/identifiers.rs @@ -5,46 +5,8 @@ use cddl_parser::{ cddl_test::{CDDLTestParser, Parser, Rule}, }; -#[path = "common/mod.rs"] -#[allow(clippy::duplicate_mod)] mod common; - -pub(crate) const ID_PASSES: &[&str] = &[ - "$", - "@", - "_", - "a", - "z", - "A", - "Z", - "$$", - "@@", - "__", - "a$", - "a@", - "a_", - "$0", - "@9", - "_a", - "abc", - "aname", - "@aname", - "_aname", - "$aname", - "a$name", - "a.name", - "@a.name", - "$a.name", - "_a.name", - "$$", - "$$groupsocket", - "$", - "$typesocket", -]; - -pub(crate) const ID_FAILS: &[&str] = &[ - "aname.", "aname-", "aname%", "a%name4", "a^name5", "a name", "", -]; +use common::identifiers::*; #[test] /// Check if the name components pass properly. diff --git a/hermes/crates/cbork/cddl-parser/tests/literal_values.rs b/hermes/crates/cbork/cddl-parser/tests/literal_values.rs index f19c379f8..c29fcedeb 100644 --- a/hermes/crates/cbork/cddl-parser/tests/literal_values.rs +++ b/hermes/crates/cbork/cddl-parser/tests/literal_values.rs @@ -4,117 +4,8 @@ use std::ops::Deref; use cddl_parser::{self, cddl_test::Rule}; -#[path = "common/mod.rs"] -#[allow(clippy::duplicate_mod)] mod common; - -mod byte_sequences; -use byte_sequences::BYTES_PASSES; -mod text_sequences; -use text_sequences::TEXT_PASSES; - -pub(crate) const UINT_PASSES: &[&str] = &[ - "10", - "101", - "2034", - "30456", - "123456789", - "0x123456789abcdefABCDEF", - "0b0001110101010101", - "0", -]; - -pub(crate) const UINT_FAILS: &[&str] = &[" a ", "zz", "0123zzz", "0xdog", "0b777"]; - -pub(crate) const INT_PASSES: &[&str] = &[ - "10", - "101", - "2034", - "30456", - "123456789", - "0x123456789abcdefABCDEF", - "0b0001110101010101", - "0", - "-10", - "-101", - "-2034", - "-30456", - "-123456789", - "-0x123456789abcdefABCDEF", - "-0b0001110101010101", - "-0", -]; - -pub(crate) const INT_FAILS: &[&str] = &[" a ", "zz", "0123zzz", "0xdog", "0b777"]; - -pub(crate) const INTFLOAT_PASSES: &[&str] = &[ - "10", - "101", - "2034", - "30456", - "123456789", - "0", - "-10", - "-101", - "-2034", - "-30456", - "-123456789", - "123.456", - "123.456", - "123e+789", - "123e-789", - "123.456e+789", - "123.456e-789", -]; - -pub(crate) const INTFLOAT_FAILS: &[&str] = &[" a ", "zz", "0123zzz", "0xdog", "0b777"]; - -pub(crate) const HEXFLOAT_PASSES: &[&str] = &[ - "0xabcp+123", - "-0xabcp+123", - "0xabcp-123", - "-0xabcp-123", - "0xabc.defp+123", - "-0xabc.defp+123", - "0xabc.defp-123", - "-0xabc.defp-123", -]; - -pub(crate) const HEXFLOAT_FAILS: &[&str] = &[" a ", "zz", "0123zzz", "0xdog", "0b777"]; - -pub(crate) const NUMBER_PASSES: &[&str] = &[ - "0xabcp+123", - "-0xabcp+123", - "0xabcp-123", - "-0xabcp-123", - "0xabc.defp+123", - "-0xabc.defp+123", - "0xabc.defp-123", - "-0xabc.defp-123", - "10", - "101", - "2034", - "30456", - "123456789", - "0", - "-10", - "-101", - "-2034", - "-30456", - "-123456789", - "123.456", - "123.456", - "123e+789", - "123e-789", - "123.456e+789", - "123.456e-789", -]; - -pub(crate) const NUMBER_FAILS: &[&str] = &[" a ", "zz", "0123zzz", "0xdog", "0b777"]; - -pub(crate) const VALUE_PASSES: &[&str] = &[]; - -pub(crate) const VALUE_FAILS: &[&str] = &[]; +use common::{byte_sequences::*, literal_values::*, text_sequences::*}; #[test] /// Test if the `uint` rule passes properly. diff --git a/hermes/crates/cbork/cddl-parser/tests/rules.rs b/hermes/crates/cbork/cddl-parser/tests/rules.rs index 094d95939..79a999e63 100644 --- a/hermes/crates/cbork/cddl-parser/tests/rules.rs +++ b/hermes/crates/cbork/cddl-parser/tests/rules.rs @@ -6,68 +6,8 @@ use cddl_parser::{ cddl_test::{CDDLTestParser, Parser, Rule}, }; -#[path = "common/mod.rs"] -#[allow(clippy::duplicate_mod)] mod common; - -mod identifiers; -use identifiers::{ID_FAILS, ID_PASSES}; -mod type_declarations; -use type_declarations::{TYPE_FAILS, TYPE_PASSES}; - -pub(crate) const GENERICARG_PASSES: &[&str] = &[ - "", - "<{ foo: bar }>", - "<{ h'1234': uint }>", - "<1...10>", - "<\n1...10\t>", - "<{ foo: bar }, { foo: baz }>", - "<{ foo: bar }, 1..10>", -]; - -pub(crate) const GENERICARG_FAILS: &[&str] = - &["", "<>", "", "<( foo: bar )>", ""]; - -pub(crate) const GENERICPARM_PASSES: &[&str] = - &["", "", "", ""]; - -pub(crate) const GENERICPARM_FAILS: &[&str] = &[ - "", - "<>", - "", - "<{ foo: bar }>", - "<{ h'1234': uint }>", - "<1...10>", - "<\n1...10\t>", -]; - -pub(crate) const ASSIGNG_PASSES: &[&str] = &["=", "//="]; - -pub(crate) const ASSIGNG_FAILS: &[&str] = &["==", "/="]; - -pub(crate) const ASSIGNT_PASSES: &[&str] = &["=", "/="]; - -pub(crate) const ASSIGNT_FAILS: &[&str] = &["==", "//="]; - -pub(crate) const TYPENAME_PASSES: &[&str] = ID_PASSES; - -pub(crate) const TYPENAME_FAILS: &[&str] = ID_FAILS; - -pub(crate) const GROUPNAME_PASSES: &[&str] = ID_PASSES; - -pub(crate) const GROUPNAME_FAILS: &[&str] = ID_FAILS; - -pub(crate) const RULE_GROUP_PASSES: &[&str] = &[ - "foo = (bar: baz)", - "t //= (foo: bar)", - "t //= foo", - "t //= foo", - "t //= foo: bar", - "t //= 2*2 foo: bar", - "delivery //= ( lat: float, long: float, drone-type: tstr )", -]; - -pub(crate) const RULE_GROUP_FAILS: &[&str] = &["foo = bar: baz", "t /= (foo: bar)"]; +use common::{rules::*, type_declarations::*}; #[test] /// Test if the `genericarg` rule passes properly. diff --git a/hermes/crates/cbork/cddl-parser/tests/text_sequences.rs b/hermes/crates/cbork/cddl-parser/tests/text_sequences.rs index 56735bd2e..bd96aa657 100644 --- a/hermes/crates/cbork/cddl-parser/tests/text_sequences.rs +++ b/hermes/crates/cbork/cddl-parser/tests/text_sequences.rs @@ -1,13 +1,7 @@ use cddl_parser::{self, cddl_test::Rule}; -#[path = "common/mod.rs"] -#[allow(clippy::duplicate_mod)] mod common; - -pub(crate) const S_PASSES: &[&str] = &[" ", " ", " \t \t", " \t \r \n \r\n "]; -pub(crate) const S_FAILS: &[&str] = &[" a ", "zz", " \t d \t", " \t \r \n \t \r\n x"]; -pub(crate) const TEXT_PASSES: &[&str] = &[r#""""#, r#""abc""#, "\"abc\\n\""]; -pub(crate) const TEXT_FAILS: &[&str] = &["", "''", "\"abc\n\""]; +use common::text_sequences::*; #[test] /// Test if the `S` rule passes properly. diff --git a/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs b/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs index 786b20ace..571b95621 100644 --- a/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs +++ b/hermes/crates/cbork/cddl-parser/tests/type_declarations.rs @@ -6,118 +6,8 @@ use cddl_parser::{ cddl_test::{CDDLTestParser, Parser, Rule}, }; -#[path = "common/mod.rs"] -#[allow(clippy::duplicate_mod)] mod common; - -pub(crate) const CTLOP_PASSES: &[&str] = &[ - ".$", - ".@", - "._", - ".a", - ".z", - ".A", - ".Z", - ".$$", - ".@@", - ".__", - ".a$", - ".a@", - ".a_", - ".$0", - ".@9", - "._a", - ".abc", - ".aname", - ".@aname", - "._aname", - ".$aname", - ".a$name", - ".a.name", - ".@a.name", - ".$a.name", - "._a.name", - ".$$", - ".$$groupsocket", - ".$", - ".$typesocket", -]; - -pub(crate) const CTLOP_FAILS: &[&str] = &[ - "aname.", ".", "..", "aname.", "aname-", "aname%", "a%name4", "a^name5", "a name", "", -]; - -pub(crate) const RANGEOP_PASSES: &[&str] = &["..", "..."]; - -pub(crate) const RANGEOP_FAILS: &[&str] = &[".", "", "....", ".. .", ". .."]; - -pub(crate) const TYPE2_PASSES: &[&str] = &[ - "#", - "#1", - "#1.1", - "#1.1", - "#6", - "#6.11", - "#6.11(tstr)", - "#6.11(\tstr\n)", - "#6.11({ foo })", - "#6.11([ foo ])", - "#6.11(#3.1)", - "&foo", - "& foo", - "&((+ a) // (b / c))", - "&\t( foo: bar )", - "~foo", - "~ foo", - "foo", - "[ foo bar ]", - "{ foo bar }", - "(a)", - "(a / b)", - "(#)", - "((a))", - "1", - "h'1111'", - "true", - "foo", -]; - -pub(crate) const TYPE2_FAILS: &[&str] = &[ - "", - "##", - "#1.", - "#6.11 (tstr)", - "#6.11(( foo: uint ))", - "&", - "& foo ", - "(foo bar)", -]; - -pub(crate) const TYPE1_PASSES: &[&str] = &[ - "1..2", - "1 .. 2", - "1\t..\n2", - "1...2", - "0..10.0", // BAD range 1 - "0.0..10", // BAD range 2 - "0..max-byte", - "min-type..max-byte", - "1.0..2.0", - "1.0...2.0", - "foo.bar", -]; - -pub(crate) const TYPE1_FAILS: &[&str] = &[""]; - -pub(crate) const TYPE_PASSES: &[&str] = &[ - "1 / 2", - "1\n/\t2", - "1 / 2 / 3 / 4", - "1 / (2 / (3 / 4))", - "# / #", -]; - -pub(crate) const TYPE_FAILS: &[&str] = &["", "1 \\ 2", "1 // 2", "1 2", "1 / 2 3"]; +use common::type_declarations::*; #[test] /// Test if the `ctlop` rule passes properly. From 353fa21354c1db903baeaae37aa4468bc2a28451 Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Fri, 26 Jan 2024 17:06:38 +0700 Subject: [PATCH 42/43] fix: add tmp allow dead code --- .../crates/cbork/cddl-parser/tests/common/byte_sequences.rs | 6 +++++- hermes/crates/cbork/cddl-parser/tests/common/comments.rs | 4 +++- .../crates/cbork/cddl-parser/tests/common/group_elements.rs | 4 ++++ hermes/crates/cbork/cddl-parser/tests/common/identifiers.rs | 4 ++++ .../crates/cbork/cddl-parser/tests/common/literal_values.rs | 4 ++++ hermes/crates/cbork/cddl-parser/tests/common/rules.rs | 6 +++++- .../crates/cbork/cddl-parser/tests/common/text_sequences.rs | 2 ++ .../cbork/cddl-parser/tests/common/type_declarations.rs | 4 ++++ 8 files changed, 31 insertions(+), 3 deletions(-) diff --git a/hermes/crates/cbork/cddl-parser/tests/common/byte_sequences.rs b/hermes/crates/cbork/cddl-parser/tests/common/byte_sequences.rs index ae84b94ac..02fc02a66 100644 --- a/hermes/crates/cbork/cddl-parser/tests/common/byte_sequences.rs +++ b/hermes/crates/cbork/cddl-parser/tests/common/byte_sequences.rs @@ -1,3 +1,5 @@ +#![allow(dead_code)] // TODO: find a way to remove this. + pub(crate) const HEXPAIR_PASSES: &[&str] = &["00", "ab", "de", "0f", "f0"]; pub(crate) const HEXPAIR_FAILS: &[&str] = &["0", " 0", "0 ", "az", "0p"]; @@ -32,6 +34,8 @@ pub(crate) const BYTES_PASSES: &[&str] = &[ "'text\n that gets converted \\\' into a byte string...'", ]; +// cspell: words HEXPAIR rstuvw abcdefghijklmnopqrstuvwyz Xhhb Bhcm + pub(crate) const BYTES_FAILS: &[&str] = &[ "h64", "b64", @@ -42,4 +46,4 @@ pub(crate) const BYTES_FAILS: &[&str] = &[ "b64'abcdefghijklmnopq # rstuvw yz01\t23456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ~'", "b64'abcdefghijklmnopq & rstuvw yz01\t23456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ'", "'\u{7}'", -]; \ No newline at end of file +]; diff --git a/hermes/crates/cbork/cddl-parser/tests/common/comments.rs b/hermes/crates/cbork/cddl-parser/tests/common/comments.rs index 6c61f38e2..7ffcaad81 100644 --- a/hermes/crates/cbork/cddl-parser/tests/common/comments.rs +++ b/hermes/crates/cbork/cddl-parser/tests/common/comments.rs @@ -1,3 +1,5 @@ +#![allow(dead_code)] // TODO: find a way to remove this. + pub(crate) const COMMENT_PASSES: &[&str] = &["; A Comment \n", "; And another\r", ";more\r\n"]; pub(crate) const COMMENT_FAILS: &[&str] = &["not a comment\n"]; @@ -15,4 +17,4 @@ pub(crate) const WHITESPACE_COMMENT_PASSES: &[&str] = &[ "\t; A Comment \n ; Another Comment \t \r\n \t ; A Final Comment \r\n", ]; -pub(crate) const WHITESPACE_COMMENT_FAILS: &[&str] = &["not a comment"]; \ No newline at end of file +pub(crate) const WHITESPACE_COMMENT_FAILS: &[&str] = &["not a comment"]; diff --git a/hermes/crates/cbork/cddl-parser/tests/common/group_elements.rs b/hermes/crates/cbork/cddl-parser/tests/common/group_elements.rs index 5ac47cadc..e6fa05672 100644 --- a/hermes/crates/cbork/cddl-parser/tests/common/group_elements.rs +++ b/hermes/crates/cbork/cddl-parser/tests/common/group_elements.rs @@ -1,3 +1,7 @@ +// cspell: words OPTCOM MEMBERKEY bareword tstr GRPENT GRPCHOICE + +#![allow(dead_code)] // TODO: find a way to remove this. + pub(crate) const OCCUR_PASSES: &[&str] = &[ "*", "+", diff --git a/hermes/crates/cbork/cddl-parser/tests/common/identifiers.rs b/hermes/crates/cbork/cddl-parser/tests/common/identifiers.rs index fbac23ff1..65e186bb6 100644 --- a/hermes/crates/cbork/cddl-parser/tests/common/identifiers.rs +++ b/hermes/crates/cbork/cddl-parser/tests/common/identifiers.rs @@ -1,3 +1,7 @@ +// cspell: words aname groupsocket typesocket + +#![allow(dead_code)] // TODO: find a way to remove this. + pub(crate) const ID_PASSES: &[&str] = &[ "$", "@", diff --git a/hermes/crates/cbork/cddl-parser/tests/common/literal_values.rs b/hermes/crates/cbork/cddl-parser/tests/common/literal_values.rs index 8b4161b26..2289ed8d9 100644 --- a/hermes/crates/cbork/cddl-parser/tests/common/literal_values.rs +++ b/hermes/crates/cbork/cddl-parser/tests/common/literal_values.rs @@ -1,3 +1,7 @@ +// cspell: words xdog INTFLOAT HEXFLOAT xabcp defp + +#![allow(dead_code)] // TODO: find a way to remove this. + pub(crate) const UINT_PASSES: &[&str] = &[ "10", "101", diff --git a/hermes/crates/cbork/cddl-parser/tests/common/rules.rs b/hermes/crates/cbork/cddl-parser/tests/common/rules.rs index 257915012..c8eeb071c 100644 --- a/hermes/crates/cbork/cddl-parser/tests/common/rules.rs +++ b/hermes/crates/cbork/cddl-parser/tests/common/rules.rs @@ -1,4 +1,8 @@ -use super::identifiers::{ID_PASSES, ID_FAILS}; +// cspell: words GENERICARG bigfloat GENERICPARM ASSIGNG ASSIGNT GROUPNAME tstr + +#![allow(dead_code)] // TODO: find a way to remove this. + +use super::identifiers::{ID_FAILS, ID_PASSES}; pub(crate) const GENERICARG_PASSES: &[&str] = &[ "", diff --git a/hermes/crates/cbork/cddl-parser/tests/common/text_sequences.rs b/hermes/crates/cbork/cddl-parser/tests/common/text_sequences.rs index ccd171542..b38ae82f5 100644 --- a/hermes/crates/cbork/cddl-parser/tests/common/text_sequences.rs +++ b/hermes/crates/cbork/cddl-parser/tests/common/text_sequences.rs @@ -1,3 +1,5 @@ +#![allow(dead_code)] // TODO: find a way to remove this. + pub(crate) const S_PASSES: &[&str] = &[" ", " ", " \t \t", " \t \r \n \r\n "]; pub(crate) const S_FAILS: &[&str] = &[" a ", "zz", " \t d \t", " \t \r \n \t \r\n x"]; pub(crate) const TEXT_PASSES: &[&str] = &[r#""""#, r#""abc""#, "\"abc\\n\""]; diff --git a/hermes/crates/cbork/cddl-parser/tests/common/type_declarations.rs b/hermes/crates/cbork/cddl-parser/tests/common/type_declarations.rs index 374e978b1..5b77dd881 100644 --- a/hermes/crates/cbork/cddl-parser/tests/common/type_declarations.rs +++ b/hermes/crates/cbork/cddl-parser/tests/common/type_declarations.rs @@ -1,3 +1,7 @@ +// cspell: words CTLOP aname groupsocket typesocket RANGEOP tstr + +#![allow(dead_code)] // TODO: find a way to remove this. + pub(crate) const CTLOP_PASSES: &[&str] = &[ ".$", ".@", From ac3de3ac5bedf007dd97bc252444f298911495c3 Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Tue, 30 Jan 2024 15:38:01 +0700 Subject: [PATCH 43/43] fix: cspell --- .../crates/cbork/cddl-parser/tests/common/byte_sequences.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hermes/crates/cbork/cddl-parser/tests/common/byte_sequences.rs b/hermes/crates/cbork/cddl-parser/tests/common/byte_sequences.rs index 02fc02a66..afd944b15 100644 --- a/hermes/crates/cbork/cddl-parser/tests/common/byte_sequences.rs +++ b/hermes/crates/cbork/cddl-parser/tests/common/byte_sequences.rs @@ -1,3 +1,5 @@ +// cspell: words HEXPAIR rstuvw abcdefghijklmnopqrstuvwyz Xhhb Bhcm + #![allow(dead_code)] // TODO: find a way to remove this. pub(crate) const HEXPAIR_PASSES: &[&str] = &["00", "ab", "de", "0f", "f0"]; @@ -34,8 +36,6 @@ pub(crate) const BYTES_PASSES: &[&str] = &[ "'text\n that gets converted \\\' into a byte string...'", ]; -// cspell: words HEXPAIR rstuvw abcdefghijklmnopqrstuvwyz Xhhb Bhcm - pub(crate) const BYTES_FAILS: &[&str] = &[ "h64", "b64",