From dd824ef36463da28bdeff4e367934617e985c592 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Thu, 27 Oct 2016 18:01:07 -0700 Subject: [PATCH] Test that evaluated recipe lines are correct Saves the value in each expression fragment when that fragment is evaluated so that we can print the value in Display. This allows us to check fragment values in tests. --- notes | 2 -- src/lib.rs | 23 ++++++++++++++--------- src/tests.rs | 2 +- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/notes b/notes index b9c2ea4..d4d8665 100644 --- a/notes +++ b/notes @@ -1,8 +1,6 @@ notes ----- -- test evaluated recipe lines - - properly handle state stack at EOF - implement string parsing diff --git a/src/lib.rs b/src/lib.rs index d7816a7..cabb76c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -66,7 +66,7 @@ struct Recipe<'a> { #[derive(PartialEq, Debug)] enum Fragment<'a> { Text{text: Token<'a>}, - Expression{expression: Expression<'a>}, + Expression{expression: Expression<'a>, value: Option}, } #[derive(PartialEq, Debug)] @@ -240,7 +240,8 @@ impl<'a> Display for Recipe<'a> { } match piece { &Fragment::Text{ref text} => try!(write!(f, "{}", text.lexeme)), - &Fragment::Expression{ref expression} => try!(write!(f, "{}{}{}", "{{", expression, "}}")), + &Fragment::Expression{ref expression, value: None} => try!(write!(f, "{}{} # ? {}", "{{", expression, "}}")), + &Fragment::Expression{ref expression, value: Some(ref string)} => try!(write!(f, "{}{} # \"{}\"{}", "{{", expression, string, "}}")), } } if i + 1 < self.lines.len() { @@ -324,13 +325,15 @@ fn evaluate<'a>( } for recipe in recipes.values_mut() { - for fragments in &recipe.lines { + for mut fragments in recipe.lines.iter_mut() { let mut line = String::new(); - for fragment in fragments { + for mut fragment in fragments.iter_mut() { match fragment { - &Fragment::Text{ref text} => line += text.lexeme, - &Fragment::Expression{ref expression} => { - line += &try!(evaluator.evaluate_expression(&expression)); + &mut Fragment::Text{ref text} => line += text.lexeme, + &mut Fragment::Expression{ref expression, ref mut value} => { + let evaluated = &try!(evaluator.evaluate_expression(&expression)); + *value = Some(evaluated.clone()); + line += evaluated; } } } @@ -1110,7 +1113,9 @@ impl<'a> Parser<'a> { } else if let Some(token) = self.expect(InterpolationStart) { return Err(self.unexpected_token(&token, &[Text, InterpolationStart, Eol])); } else { - pieces.push(Fragment::Expression{expression: try!(self.expression(true))}); + pieces.push(Fragment::Expression{ + expression: try!(self.expression(true)), value: None + }); if let Some(token) = self.expect(InterpolationEnd) { return Err(self.unexpected_token(&token, &[InterpolationEnd])); } @@ -1318,7 +1323,7 @@ impl<'a> Parser<'a> { for line in &recipe.lines { for piece in line { - if let &Fragment::Expression{ref expression} = piece { + if let &Fragment::Expression{ref expression, ..} = piece { for variable in expression.variables() { let name = variable.lexeme; if !(assignments.contains_key(&name) || recipe.arguments.contains(&name)) { diff --git a/src/tests.rs b/src/tests.rs index 256b99d..15a8fba 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -251,7 +251,7 @@ goodbye = \"y\" # \"y\" hello a b c: x y z #! blah #blarg - {{foo + bar}}abc{{goodbye + \"x\"}}xyz + {{foo + bar # \"xx\"}}abc{{goodbye + \"x\" # \"yx\"}}xyz 1 2 3