Skip to content

Commit 2f995d7

Browse files
committed
refactor tests/rules.rs
1 parent 8e18b4a commit 2f995d7

File tree

1 file changed

+35
-28
lines changed

1 file changed

+35
-28
lines changed

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

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,18 @@
22
// cspell: words assigng assignt ASSIGNT GENERICPARM genericparm
33

44
mod common;
5-
use common::{rules::*, type_declarations::*, CDDLTestParser, Rule};
6-
use pest::Parser;
5+
use common::{rules::*, type_declarations::*, Rule};
76

8-
#[test]
97
/// Test if the `genericarg` rule passes properly.
108
/// This uses a special rule in the Grammar to test `genericarg` exhaustively.
9+
#[test]
1110
fn check_genericarg() {
1211
common::check_tests_rule(Rule::genericarg_TEST, GENERICARG_PASSES, GENERICARG_FAILS);
1312
}
1413

15-
#[test]
1614
/// Test if the `genericparm` rule passes properly.
1715
/// This uses a special rule in the Grammar to test `genericparm` exhaustively.
16+
#[test]
1817
fn check_genericparm() {
1918
common::check_tests_rule(
2019
Rule::genericparm_TEST,
@@ -23,61 +22,69 @@ fn check_genericparm() {
2322
);
2423
}
2524

26-
#[test]
2725
/// Test if the `assigng` rule passes properly.
2826
/// This uses a special rule in the Grammar to test `assigng` exhaustively.
27+
#[test]
2928
fn check_assigng() {
3029
common::check_tests_rule(Rule::assigng_TEST, ASSIGNG_PASSES, ASSIGNG_FAILS);
3130
}
3231

33-
#[test]
3432
/// Test if the `assignt` rule passes properly.
3533
/// This uses a special rule in the Grammar to test `assignt` exhaustively.
34+
#[test]
3635
fn check_assignt() {
3736
common::check_tests_rule(Rule::assignt_TEST, ASSIGNT_PASSES, ASSIGNT_FAILS);
3837
}
3938

40-
#[test]
4139
/// Test if the `typename` rule passes properly.
4240
/// This uses a special rule in the Grammar to test `typename` exhaustively.
41+
#[test]
4342
fn check_typename() {
4443
common::check_tests_rule(Rule::typename_TEST, TYPENAME_PASSES, TYPENAME_FAILS);
4544
}
4645

47-
#[test]
4846
/// Test if the `groupname` rule passes properly.
4947
/// This uses a special rule in the Grammar to test `groupname` exhaustively.
48+
#[test]
5049
fn check_groupname() {
5150
common::check_tests_rule(Rule::groupname_TEST, GROUPNAME_PASSES, GROUPNAME_FAILS);
5251
}
5352

54-
#[test]
5553
/// Test if the `rule` rule passes properly for type variant.
54+
#[test]
5655
fn check_rule_type_composition() {
57-
for (i, test_i) in [TYPENAME_PASSES, TYPENAME_FAILS]
56+
let typename_iter = [TYPENAME_PASSES, TYPENAME_FAILS]
5857
.into_iter()
5958
.flatten()
60-
.enumerate()
61-
{
62-
for (j, test_j) in [ASSIGNT_PASSES].into_iter().flatten().enumerate() {
63-
for (k, test_k) in [TYPE_PASSES, TYPE_FAILS].into_iter().flatten().enumerate() {
64-
let input = [test_i.to_owned(), test_j.to_owned(), test_k.to_owned()].join(" ");
65-
let parse = CDDLTestParser::parse(Rule::rule_TEST, &input);
66-
if (0..TYPENAME_PASSES.len()).contains(&i)
67-
&& (0..ASSIGNT_PASSES.len()).contains(&j)
68-
&& (0..TYPE_PASSES.len()).contains(&k)
69-
{
70-
assert!(parse.is_ok());
71-
} else {
72-
assert!(parse.is_err());
73-
}
74-
}
75-
}
76-
}
59+
.enumerate();
60+
61+
let assign_iter = ASSIGNT_PASSES.iter();
62+
let type_iter = [TYPE_PASSES, TYPE_FAILS].into_iter().flatten().enumerate();
63+
64+
let rules_iter = typename_iter.zip(assign_iter).zip(type_iter).map(
65+
|(((i, &test_i), &test_j), (k, &test_k))| {
66+
let is_passes = i < TYPENAME_PASSES.len() && k < TYPE_PASSES.len();
67+
let input = [test_i.to_string(), test_j.to_string(), test_k.to_string()].join(" ");
68+
(input, is_passes)
69+
},
70+
);
71+
72+
let passes = rules_iter
73+
.clone()
74+
.filter(|(_, is_passes)| *is_passes)
75+
.map(|(input, _)| input)
76+
.collect::<Vec<_>>();
77+
78+
let fails = rules_iter
79+
.filter(|(_, is_passes)| !*is_passes)
80+
.map(|(input, _)| input)
81+
.collect::<Vec<_>>();
82+
83+
common::check_tests_rule(Rule::rule_TEST, &passes, &fails);
7784
}
7885

79-
#[test]
8086
/// Test if the `rule` rule passes properly for group variant.
87+
#[test]
8188
fn check_rule_group() {
8289
common::check_tests_rule(Rule::rule_TEST, RULE_GROUP_PASSES, RULE_GROUP_FAILS);
8390
}

0 commit comments

Comments
 (0)