Skip to content

Commit

Permalink
additional tests and from implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
anweiss committed Oct 14, 2022
1 parent 677cdd4 commit 7a626dd
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 2 deletions.
42 changes: 42 additions & 0 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,54 @@ impl<'a, 'b: 'a> From<&'b TypeRule<'a>> for CDDLType<'a, 'b> {
}
}

impl<'a, 'b: 'a> From<&'b GroupRule<'a>> for CDDLType<'a, 'b> {
fn from(rule: &'b GroupRule<'a>) -> Self {
CDDLType::GroupRule(rule)
}
}

impl<'a, 'b: 'a> From<&'b Identifier<'a>> for CDDLType<'a, 'b> {
fn from(ident: &'b Identifier<'a>) -> Self {
CDDLType::Identifier(ident)
}
}

impl<'a, 'b: 'a> From<&'b Type<'a>> for CDDLType<'a, 'b> {
fn from(t: &'b Type<'a>) -> Self {
CDDLType::Type(t)
}
}

impl<'a, 'b: 'a> From<&'b TypeChoice<'a>> for CDDLType<'a, 'b> {
fn from(tc: &'b TypeChoice<'a>) -> Self {
CDDLType::TypeChoice(tc)
}
}

impl<'a, 'b: 'a> From<&'b Type1<'a>> for CDDLType<'a, 'b> {
fn from(t1: &'b Type1<'a>) -> Self {
CDDLType::Type1(t1)
}
}

impl<'a, 'b: 'a> From<&'b Type2<'a>> for CDDLType<'a, 'b> {
fn from(t2: &'b Type2<'a>) -> Self {
CDDLType::Type2(t2)
}
}

impl<'a, 'b: 'a> From<&'b Cow<'a, str>> for CDDLType<'a, 'b> {
fn from(text_value: &'b Cow<'a, str>) -> Self {
CDDLType::Value(Value::TEXT(Cow::Borrowed(text_value)))
}
}

impl<'a, 'b: 'a> From<&'b GroupEntry<'a>> for CDDLType<'a, 'b> {
fn from(ge: &'b GroupEntry<'a>) -> Self {
CDDLType::GroupEntry(ge)
}
}

#[cfg(feature = "ast-comments")]
#[derive(Default, Debug, PartialEq, Eq, Clone)]
#[doc(hidden)]
Expand Down
120 changes: 118 additions & 2 deletions src/validator/parent_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,8 @@ impl<'a, 'b: 'a> Visitor<'a, 'b, Error> for ParentVisitor<'a, 'b> {
mod tests {
#![allow(unused_imports)]

use core::any::Any;

use crate::cddl_from_str;

use super::*;
Expand All @@ -621,14 +623,128 @@ mod tests {
}

#[test]
fn type_rule_parent_is_rule() -> Result<()> {
let c = cddl_from_str(r#"a = "myrule""#, true).unwrap();
fn type_and_group_rule_parent_is_rule() -> Result<()> {
let c = cddl_from_str(
r#"
a = "myrule"
b = ( * tstr )
"#,
true,
)
.unwrap();
let t = ParentVisitor::new(&c).unwrap();

if let r @ Rule::Type { rule, .. } = c.rules.first().unwrap() {
assert_eq!(CDDLType::from(rule).parent(&t).unwrap(), &CDDLType::from(r));
}

if let r @ Rule::Group { rule, .. } = c.rules.get(1).unwrap() {
assert_eq!(
CDDLType::from(rule.as_ref()).parent(&t).unwrap(),
&CDDLType::from(r)
);
}

Ok(())
}

#[test]
fn type_parent_is_type_rule() -> Result<()> {
let c = cddl_from_str(r#"a = "myrule""#, true).unwrap();
let t = ParentVisitor::new(&c).unwrap();

if let Rule::Type { rule, .. } = c.rules.first().unwrap() {
assert_eq!(
CDDLType::from(&rule.value).parent(&t).unwrap(),
&CDDLType::from(rule)
);
}

Ok(())
}

#[test]
fn type_choice_parent_is_type() -> Result<()> {
let c = cddl_from_str(r#"a = "myrule""#, true).unwrap();
let t = ParentVisitor::new(&c).unwrap();

if let Rule::Type { rule, .. } = c.rules.first().unwrap() {
assert_eq!(
CDDLType::from(rule.value.type_choices.first().unwrap())
.parent(&t)
.unwrap(),
&CDDLType::from(&rule.value)
);
}

Ok(())
}

#[test]
fn type1_parent_is_type_choice() -> Result<()> {
let c = cddl_from_str(r#"a = "myrule""#, true).unwrap();
let t = ParentVisitor::new(&c).unwrap();

if let Rule::Type { rule, .. } = c.rules.first().unwrap() {
assert_eq!(
CDDLType::from(&rule.value.type_choices.first().unwrap().type1)
.parent(&t)
.unwrap(),
&CDDLType::from(rule.value.type_choices.first().unwrap())
);
}

Ok(())
}

#[test]
fn type2_parent_is_type1() -> Result<()> {
let c = cddl_from_str(r#"a = "myrule""#, true).unwrap();
let t = ParentVisitor::new(&c).unwrap();

if let Rule::Type { rule, .. } = c.rules.first().unwrap() {
assert_eq!(
CDDLType::from(&rule.value.type_choices.first().unwrap().type1.type2)
.parent(&t)
.unwrap(),
&CDDLType::from(&rule.value.type_choices.first().unwrap().type1)
);
}

Ok(())
}

#[test]
fn text_value_parent_is_type2() -> Result<()> {
let c = cddl_from_str(r#"a = "myrule""#, true).unwrap();
let t = ParentVisitor::new(&c).unwrap();

if let Rule::Type { rule, .. } = c.rules.first().unwrap() {
if let t2 @ Type2::TextValue { value, .. } =
&rule.value.type_choices.first().unwrap().type1.type2
{
assert_eq!(
CDDLType::from(value).parent(&t).unwrap(),
&CDDLType::from(t2)
);
}
}

Ok(())
}

#[test]
fn group_entry_parent_is_group_rule() -> Result<()> {
let c = cddl_from_str(r#"a = ( * tstr )"#, true).unwrap();
let t = ParentVisitor::new(&c).unwrap();

if let Rule::Group { rule, .. } = c.rules.first().unwrap() {
assert_eq!(
CDDLType::from(&rule.entry).parent(&t).unwrap(),
&CDDLType::from(rule.as_ref())
);
}

Ok(())
}
}

0 comments on commit 7a626dd

Please sign in to comment.