Skip to content

Commit

Permalink
refactor character_sets tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Leshiy committed Nov 18, 2024
1 parent 4e4deec commit 51ec94d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 106 deletions.
139 changes: 36 additions & 103 deletions rust/cbork-cddl-parser/tests/character_sets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,24 @@ mod common;
use common::{CDDLTestParser, Rule};
use pest::Parser;

#[test]
/// Test if the `WHITESPACE` rule passes properly.
#[test]
fn check_whitespace() {
let whitespace = vec![" ", "\t", "\r", "\n", "\r\n"];

let not_whitespace = "not";

for ws in whitespace {
let parse = CDDLTestParser::parse(Rule::WHITESPACE, ws);
assert!(parse.is_ok());
}

let parse = CDDLTestParser::parse(Rule::WHITESPACE, not_whitespace);
assert!(parse.is_err());
common::check_tests_rule(Rule::WHITESPACE, &[" ", "\t", "\r", "\n", "\r\n"], &["not"]);
}

#[test]
/// Test if the `PCHAR` rule passes properly.
#[test]
fn check_pchar() {
for x in ('\u{0}'..='\u{ff}').map(char::from) {
let test = format!("{x}");
let parse = CDDLTestParser::parse(Rule::PCHAR, &test);
if x < ' ' || x == '\u{7f}' {
assert!(parse.is_err());
} else {
assert!(parse.is_ok());
}
}

let parse = CDDLTestParser::parse(Rule::ASCII_VISIBLE, "\r");
assert!(parse.is_err());
let passes = ('\u{0}'..='\u{ff}')
.filter(|x| x >= &' ' && x != &'\u{7f}')
.map(String::from)
.collect::<Vec<_>>();
common::check_tests_rule(Rule::PCHAR, &passes, &["\u{7f}"]);
}

#[test]
/// Test if the `BCHAR` rule passes properly.
#[test]
fn check_bchar() {
for x in ('\u{0}'..='\u{ff}').map(char::from) {
let test = format!("{x}");
Expand All @@ -49,101 +32,51 @@ fn check_bchar() {
assert!(parse.is_ok());
}
}

let parse = CDDLTestParser::parse(Rule::ASCII_VISIBLE, "\r");
assert!(parse.is_err());
}

#[test]
/// Test if the `SESC` rule passes properly.
#[test]
fn check_sesc() {
for x in (' '..='\u{ff}').map(char::from) {
let test = format!("\\{x}");
let parse = CDDLTestParser::parse(Rule::SESC, &test);
if x == '\u{7f}' {
assert!(parse.is_err());
} else {
assert!(parse.is_ok());
}
}

let parse = CDDLTestParser::parse(Rule::ASCII_VISIBLE, "\r");
assert!(parse.is_err());
let passes = (' '..='\u{ff}')
.filter(|x| x != &'\u{7f}')
.map(|x| format!("\\{x}"))
.collect::<Vec<_>>();
common::check_tests_rule(Rule::SESC, &passes, &["\u{7f}"]);
}

#[test]
/// Test if the `ASCII_VISIBLE` rule passes properly.
#[test]
fn check_ascii_visible() {
for x in (b' '..=b'~').map(char::from) {
let test = x.to_string();
let parse = CDDLTestParser::parse(Rule::ASCII_VISIBLE, &test);
assert!(parse.is_ok());
}

let parse = CDDLTestParser::parse(Rule::ASCII_VISIBLE, "\r");
assert!(parse.is_err());

let parse = CDDLTestParser::parse(Rule::ASCII_VISIBLE, "\u{80}");
assert!(parse.is_err());
let passes = (' '..='~').map(String::from).collect::<Vec<_>>();
common::check_tests_rule(Rule::ASCII_VISIBLE, &passes, &["\r", "\u{80}"]);
}

#[test]
/// Test if the `SCHAR_ASCII_VISIBLE` rule passes properly.
#[test]
fn check_schar_ascii_visible() {
let invalids = "\"\\";
for x in (b' '..=b'~').map(char::from) {
let test = x.to_string();
let parse = CDDLTestParser::parse(Rule::SCHAR_ASCII_VISIBLE, &test);
if invalids.contains(x) {
assert!(parse.is_err());
} else {
assert!(parse.is_ok());
}
}

let parse = CDDLTestParser::parse(Rule::SCHAR_ASCII_VISIBLE, "\r");
assert!(parse.is_err());

let parse = CDDLTestParser::parse(Rule::SCHAR_ASCII_VISIBLE, "\u{80}");
assert!(parse.is_err());
let passes = (' '..='~')
.filter(|c| c != &'"' && c != &'\\')
.map(String::from)
.collect::<Vec<_>>();
common::check_tests_rule(Rule::SCHAR_ASCII_VISIBLE, &passes, &["\r", "\u{80}"]);
}

#[test]
/// Test if the `BCHAR_ASCII_VISIBLE` rule passes properly.
#[test]
fn check_bchar_ascii_visible() {
let invalids = "'\\";
for x in (b' '..=b'~').map(char::from) {
let test = x.to_string();
let parse = CDDLTestParser::parse(Rule::BCHAR_ASCII_VISIBLE, &test);
if invalids.contains(x) {
assert!(parse.is_err());
} else {
assert!(parse.is_ok());
}
}

let parse = CDDLTestParser::parse(Rule::BCHAR_ASCII_VISIBLE, "\r");
assert!(parse.is_err());

let parse = CDDLTestParser::parse(Rule::BCHAR_ASCII_VISIBLE, "\u{80}");
assert!(parse.is_err());
let passes = (' '..='~')
.filter(|c| c != &'\'' && c != &'\\')
.map(String::from)
.collect::<Vec<_>>();
common::check_tests_rule(Rule::BCHAR_ASCII_VISIBLE, &passes, &["\r", "\u{80}"]);
}

#[test]
/// Test if the `UNICODE_CHAR` rule passes properly.
#[test]
fn check_unicode() {
let parse = CDDLTestParser::parse(Rule::UNICODE_CHAR, "\r");
assert!(parse.is_err());

let parse = CDDLTestParser::parse(Rule::UNICODE_CHAR, "\u{80}");
assert!(parse.is_ok());

let parse = CDDLTestParser::parse(Rule::UNICODE_CHAR, "\u{10fffd}");
assert!(parse.is_ok());

let parse = CDDLTestParser::parse(Rule::UNICODE_CHAR, "\u{7ffff}");
assert!(parse.is_ok());

let parse = CDDLTestParser::parse(Rule::UNICODE_CHAR, "\u{10fffe}");
assert!(parse.is_err());
common::check_tests_rule(
Rule::UNICODE_CHAR,
&["\u{80}", "\u{10fffd}", "\u{7ffff}"],
&["\r", "\u{10fffe}"],
);
}
8 changes: 5 additions & 3 deletions rust/cbork-cddl-parser/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ pub struct CDDLTestParser;

/// # Panics
#[allow(dead_code)]
pub(crate) fn check_tests_rule(rule_type: Rule, passes: &[&str], fails: &[&str]) {
pub(crate) fn check_tests_rule(
rule_type: Rule, passes: &[impl AsRef<str>], fails: &[impl AsRef<str>],
) {
for test in passes {
let parse = CDDLTestParser::parse(rule_type, test);
let parse = CDDLTestParser::parse(rule_type, test.as_ref());
assert!(parse.is_ok());
}

for test in fails {
let parse = CDDLTestParser::parse(rule_type, test);
let parse = CDDLTestParser::parse(rule_type, test.as_ref());
assert!(parse.is_err());
}
}

0 comments on commit 51ec94d

Please sign in to comment.