Skip to content

Commit 51ec94d

Browse files
committed
refactor character_sets tests
1 parent 4e4deec commit 51ec94d

File tree

2 files changed

+41
-106
lines changed

2 files changed

+41
-106
lines changed

rust/cbork-cddl-parser/tests/character_sets.rs

Lines changed: 36 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,24 @@ mod common;
44
use common::{CDDLTestParser, Rule};
55
use pest::Parser;
66

7-
#[test]
87
/// Test if the `WHITESPACE` rule passes properly.
8+
#[test]
99
fn check_whitespace() {
10-
let whitespace = vec![" ", "\t", "\r", "\n", "\r\n"];
11-
12-
let not_whitespace = "not";
13-
14-
for ws in whitespace {
15-
let parse = CDDLTestParser::parse(Rule::WHITESPACE, ws);
16-
assert!(parse.is_ok());
17-
}
18-
19-
let parse = CDDLTestParser::parse(Rule::WHITESPACE, not_whitespace);
20-
assert!(parse.is_err());
10+
common::check_tests_rule(Rule::WHITESPACE, &[" ", "\t", "\r", "\n", "\r\n"], &["not"]);
2111
}
2212

23-
#[test]
2413
/// Test if the `PCHAR` rule passes properly.
14+
#[test]
2515
fn check_pchar() {
26-
for x in ('\u{0}'..='\u{ff}').map(char::from) {
27-
let test = format!("{x}");
28-
let parse = CDDLTestParser::parse(Rule::PCHAR, &test);
29-
if x < ' ' || x == '\u{7f}' {
30-
assert!(parse.is_err());
31-
} else {
32-
assert!(parse.is_ok());
33-
}
34-
}
35-
36-
let parse = CDDLTestParser::parse(Rule::ASCII_VISIBLE, "\r");
37-
assert!(parse.is_err());
16+
let passes = ('\u{0}'..='\u{ff}')
17+
.filter(|x| x >= &' ' && x != &'\u{7f}')
18+
.map(String::from)
19+
.collect::<Vec<_>>();
20+
common::check_tests_rule(Rule::PCHAR, &passes, &["\u{7f}"]);
3821
}
3922

40-
#[test]
4123
/// Test if the `BCHAR` rule passes properly.
24+
#[test]
4225
fn check_bchar() {
4326
for x in ('\u{0}'..='\u{ff}').map(char::from) {
4427
let test = format!("{x}");
@@ -49,101 +32,51 @@ fn check_bchar() {
4932
assert!(parse.is_ok());
5033
}
5134
}
52-
53-
let parse = CDDLTestParser::parse(Rule::ASCII_VISIBLE, "\r");
54-
assert!(parse.is_err());
5535
}
5636

57-
#[test]
5837
/// Test if the `SESC` rule passes properly.
38+
#[test]
5939
fn check_sesc() {
60-
for x in (' '..='\u{ff}').map(char::from) {
61-
let test = format!("\\{x}");
62-
let parse = CDDLTestParser::parse(Rule::SESC, &test);
63-
if x == '\u{7f}' {
64-
assert!(parse.is_err());
65-
} else {
66-
assert!(parse.is_ok());
67-
}
68-
}
69-
70-
let parse = CDDLTestParser::parse(Rule::ASCII_VISIBLE, "\r");
71-
assert!(parse.is_err());
40+
let passes = (' '..='\u{ff}')
41+
.filter(|x| x != &'\u{7f}')
42+
.map(|x| format!("\\{x}"))
43+
.collect::<Vec<_>>();
44+
common::check_tests_rule(Rule::SESC, &passes, &["\u{7f}"]);
7245
}
7346

74-
#[test]
7547
/// Test if the `ASCII_VISIBLE` rule passes properly.
48+
#[test]
7649
fn check_ascii_visible() {
77-
for x in (b' '..=b'~').map(char::from) {
78-
let test = x.to_string();
79-
let parse = CDDLTestParser::parse(Rule::ASCII_VISIBLE, &test);
80-
assert!(parse.is_ok());
81-
}
82-
83-
let parse = CDDLTestParser::parse(Rule::ASCII_VISIBLE, "\r");
84-
assert!(parse.is_err());
85-
86-
let parse = CDDLTestParser::parse(Rule::ASCII_VISIBLE, "\u{80}");
87-
assert!(parse.is_err());
50+
let passes = (' '..='~').map(String::from).collect::<Vec<_>>();
51+
common::check_tests_rule(Rule::ASCII_VISIBLE, &passes, &["\r", "\u{80}"]);
8852
}
8953

90-
#[test]
9154
/// Test if the `SCHAR_ASCII_VISIBLE` rule passes properly.
55+
#[test]
9256
fn check_schar_ascii_visible() {
93-
let invalids = "\"\\";
94-
for x in (b' '..=b'~').map(char::from) {
95-
let test = x.to_string();
96-
let parse = CDDLTestParser::parse(Rule::SCHAR_ASCII_VISIBLE, &test);
97-
if invalids.contains(x) {
98-
assert!(parse.is_err());
99-
} else {
100-
assert!(parse.is_ok());
101-
}
102-
}
103-
104-
let parse = CDDLTestParser::parse(Rule::SCHAR_ASCII_VISIBLE, "\r");
105-
assert!(parse.is_err());
106-
107-
let parse = CDDLTestParser::parse(Rule::SCHAR_ASCII_VISIBLE, "\u{80}");
108-
assert!(parse.is_err());
57+
let passes = (' '..='~')
58+
.filter(|c| c != &'"' && c != &'\\')
59+
.map(String::from)
60+
.collect::<Vec<_>>();
61+
common::check_tests_rule(Rule::SCHAR_ASCII_VISIBLE, &passes, &["\r", "\u{80}"]);
10962
}
11063

111-
#[test]
11264
/// Test if the `BCHAR_ASCII_VISIBLE` rule passes properly.
65+
#[test]
11366
fn check_bchar_ascii_visible() {
114-
let invalids = "'\\";
115-
for x in (b' '..=b'~').map(char::from) {
116-
let test = x.to_string();
117-
let parse = CDDLTestParser::parse(Rule::BCHAR_ASCII_VISIBLE, &test);
118-
if invalids.contains(x) {
119-
assert!(parse.is_err());
120-
} else {
121-
assert!(parse.is_ok());
122-
}
123-
}
124-
125-
let parse = CDDLTestParser::parse(Rule::BCHAR_ASCII_VISIBLE, "\r");
126-
assert!(parse.is_err());
127-
128-
let parse = CDDLTestParser::parse(Rule::BCHAR_ASCII_VISIBLE, "\u{80}");
129-
assert!(parse.is_err());
67+
let passes = (' '..='~')
68+
.filter(|c| c != &'\'' && c != &'\\')
69+
.map(String::from)
70+
.collect::<Vec<_>>();
71+
common::check_tests_rule(Rule::BCHAR_ASCII_VISIBLE, &passes, &["\r", "\u{80}"]);
13072
}
13173

132-
#[test]
13374
/// Test if the `UNICODE_CHAR` rule passes properly.
75+
#[test]
13476
fn check_unicode() {
135-
let parse = CDDLTestParser::parse(Rule::UNICODE_CHAR, "\r");
136-
assert!(parse.is_err());
137-
138-
let parse = CDDLTestParser::parse(Rule::UNICODE_CHAR, "\u{80}");
139-
assert!(parse.is_ok());
140-
141-
let parse = CDDLTestParser::parse(Rule::UNICODE_CHAR, "\u{10fffd}");
142-
assert!(parse.is_ok());
143-
144-
let parse = CDDLTestParser::parse(Rule::UNICODE_CHAR, "\u{7ffff}");
145-
assert!(parse.is_ok());
146-
147-
let parse = CDDLTestParser::parse(Rule::UNICODE_CHAR, "\u{10fffe}");
148-
assert!(parse.is_err());
77+
common::check_tests_rule(
78+
Rule::UNICODE_CHAR,
79+
&["\u{80}", "\u{10fffd}", "\u{7ffff}"],
80+
&["\r", "\u{10fffe}"],
81+
);
14982
}

rust/cbork-cddl-parser/tests/common/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,16 @@ pub struct CDDLTestParser;
1919

2020
/// # Panics
2121
#[allow(dead_code)]
22-
pub(crate) fn check_tests_rule(rule_type: Rule, passes: &[&str], fails: &[&str]) {
22+
pub(crate) fn check_tests_rule(
23+
rule_type: Rule, passes: &[impl AsRef<str>], fails: &[impl AsRef<str>],
24+
) {
2325
for test in passes {
24-
let parse = CDDLTestParser::parse(rule_type, test);
26+
let parse = CDDLTestParser::parse(rule_type, test.as_ref());
2527
assert!(parse.is_ok());
2628
}
2729

2830
for test in fails {
29-
let parse = CDDLTestParser::parse(rule_type, test);
31+
let parse = CDDLTestParser::parse(rule_type, test.as_ref());
3032
assert!(parse.is_err());
3133
}
3234
}

0 commit comments

Comments
 (0)