Skip to content

Commit 9d79c66

Browse files
committed
refactor tests/type_declaration
1 parent 2f995d7 commit 9d79c66

File tree

3 files changed

+75
-46
lines changed

3 files changed

+75
-46
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ pub(crate) mod type_declarations;
1818
pub struct CDDLTestParser;
1919

2020
/// # Panics
21-
#[allow(dead_code)]
2221
pub(crate) fn check_tests_rule(
2322
rule_type: Rule, passes: &[impl AsRef<str>], fails: &[impl AsRef<str>],
2423
) {

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,23 @@ fn check_rule_type_composition() {
5959
.enumerate();
6060

6161
let assign_iter = ASSIGNT_PASSES.iter();
62-
let type_iter = [TYPE_PASSES, TYPE_FAILS].into_iter().flatten().enumerate();
62+
let type_iter = [
63+
TYPE_PASSES,
64+
TYPE1_PASSES,
65+
TYPE2_PASSES,
66+
TYPE_FAILS,
67+
TYPE1_FAILS,
68+
TYPE2_FAILS,
69+
]
70+
.into_iter()
71+
.flatten()
72+
.enumerate();
6373

6474
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(" ");
75+
|(((i, typename), assign), (k, r#type))| {
76+
let is_passes = i < TYPENAME_PASSES.len()
77+
&& k < TYPE_PASSES.len() + TYPE1_PASSES.len() + TYPE2_PASSES.len();
78+
let input = [typename.to_owned(), assign.to_owned(), r#type.to_owned()].join(" ");
6879
(input, is_passes)
6980
},
7081
);

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

Lines changed: 60 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,84 +2,103 @@
22
// cspell: words rangeop RANGEOP
33

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

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

15-
#[test]
1614
/// Test if the `rangeop` rule passes properly.
1715
/// This uses a special rule in the Grammar to test `rangeop` exhaustively.
16+
#[test]
1817
fn check_rangeop() {
1918
common::check_tests_rule(Rule::rangeop_TEST, RANGEOP_PASSES, RANGEOP_FAILS);
2019
}
2120

22-
#[test]
2321
/// Test if the `type2` rule passes properly.
2422
/// This uses a special rule in the Grammar to test `type2` exhaustively.
23+
#[test]
2524
fn check_type2() {
2625
common::check_tests_rule(Rule::type2_TEST, TYPE2_PASSES, TYPE2_FAILS);
2726
}
2827

29-
#[test]
3028
/// Test if the `type1` rule passes properly.
3129
/// This uses a special rule in the Grammar to test `type1` exhaustively.
30+
#[test]
3231
fn check_type1() {
3332
common::check_tests_rule(Rule::type1_TEST, TYPE1_PASSES, TYPE1_FAILS);
3433
}
3534

36-
#[test]
3735
/// Test if the `type1` rule passes properly based on composition of type2 test cases.
36+
#[test]
3837
fn check_type1_composition() {
39-
let j_len = CTLOP_PASSES.len() + RANGEOP_PASSES.len();
40-
for (i, test_i) in [TYPE2_PASSES, TYPE_FAILS].into_iter().flatten().enumerate() {
41-
for (j, test_j) in [CTLOP_PASSES, RANGEOP_PASSES]
42-
.into_iter()
43-
.flatten()
44-
.enumerate()
45-
{
46-
for (k, test_k) in [TYPE2_PASSES, TYPE_FAILS].into_iter().flatten().enumerate() {
47-
let input = [test_i.to_owned(), test_j.to_owned(), test_k.to_owned()].join(" ");
48-
let parse = CDDLTestParser::parse(Rule::type1_TEST, &input);
49-
if (0..TYPE2_PASSES.len()).contains(&i)
50-
&& (0..j_len).contains(&j)
51-
&& (0..TYPE2_PASSES.len()).contains(&k)
52-
{
53-
assert!(parse.is_ok());
54-
} else {
55-
assert!(parse.is_err());
56-
}
57-
}
58-
}
59-
}
38+
let separator_iter = [CTLOP_PASSES, RANGEOP_PASSES].into_iter().flatten();
39+
40+
let type_iter = [TYPE2_PASSES, TYPE_FAILS, TYPE1_FAILS, TYPE2_FAILS]
41+
.into_iter()
42+
.flatten()
43+
.enumerate();
44+
45+
let rules_iter = type_iter.clone().zip(separator_iter).zip(type_iter).map(
46+
|(((i, type_1), separator), (j, type_2))| {
47+
let is_passed = i < TYPE2_PASSES.len() && j < TYPE2_PASSES.len();
48+
let input = [type_1.to_owned(), separator.to_owned(), type_2.to_owned()].join(" ");
49+
(input, is_passed)
50+
},
51+
);
52+
53+
let passes = rules_iter
54+
.clone()
55+
.filter(|(_, is_passes)| *is_passes)
56+
.map(|(input, _)| input)
57+
.collect::<Vec<_>>();
58+
59+
let fails = rules_iter
60+
.filter(|(_, is_passes)| !*is_passes)
61+
.map(|(input, _)| input)
62+
.collect::<Vec<_>>();
63+
64+
common::check_tests_rule(Rule::type1_TEST, &passes, &fails);
6065
}
6166

62-
#[test]
6367
/// Test if the `type` rule passes properly.
6468
/// This uses a special rule in the Grammar to test `type` exhaustively.
69+
#[test]
6570
fn check_type() {
6671
common::check_tests_rule(Rule::type_TEST, TYPE_PASSES, TYPE_FAILS);
6772
}
6873

69-
#[test]
7074
/// Test if the `type` rule passes properly based on composition of type2 test cases.
75+
#[test]
7176
fn check_type_composition() {
7277
// type2 composition testing
73-
for (i, test_i) in [TYPE2_PASSES, TYPE_FAILS].into_iter().flatten().enumerate() {
74-
for (j, test_j) in [TYPE2_PASSES, TYPE_FAILS].into_iter().flatten().enumerate() {
78+
let type_iter = [TYPE2_PASSES, TYPE_FAILS, TYPE1_FAILS, TYPE2_FAILS]
79+
.into_iter()
80+
.flatten()
81+
.enumerate();
82+
83+
let rules_iter = type_iter
84+
.clone()
85+
.zip(type_iter)
86+
.map(|((i, test_i), (j, test_j))| {
87+
let is_passed = i < TYPE2_PASSES.len() && j < TYPE2_PASSES.len();
7588
let input = [test_i.to_owned(), "/", test_j.to_owned()].join(" ");
76-
let parse = CDDLTestParser::parse(Rule::type_TEST, &input);
77-
78-
if (0..TYPE2_PASSES.len()).contains(&i) && (0..TYPE2_PASSES.len()).contains(&j) {
79-
assert!(parse.is_ok());
80-
} else {
81-
assert!(parse.is_err());
82-
}
83-
}
84-
}
89+
(input, is_passed)
90+
});
91+
92+
let passes = rules_iter
93+
.clone()
94+
.filter(|(_, is_passes)| *is_passes)
95+
.map(|(input, _)| input)
96+
.collect::<Vec<_>>();
97+
98+
let fails = rules_iter
99+
.filter(|(_, is_passes)| !*is_passes)
100+
.map(|(input, _)| input)
101+
.collect::<Vec<_>>();
102+
103+
common::check_tests_rule(Rule::type_TEST, &passes, &fails);
85104
}

0 commit comments

Comments
 (0)