Fix parsing of AttrValues (fixes #37)
This commit is contained in:
parent
7f39205c92
commit
5f3e16e70c
2 changed files with 47 additions and 15 deletions
|
@ -96,6 +96,10 @@ impl AttrValue {
|
|||
cur_varref = Some(String::new())
|
||||
}
|
||||
} else {
|
||||
if curly_count == 1 {
|
||||
cur_word.push('{');
|
||||
}
|
||||
curly_count = 0;
|
||||
cur_word.push(c);
|
||||
}
|
||||
}
|
||||
|
@ -144,23 +148,24 @@ impl AttrValueElement {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(Test)]
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use pretty_assertions::assert_eq;
|
||||
#[test]
|
||||
fn test_parse_string_or_var_ref_list() {
|
||||
let input = "{{foo}}{{bar}}baz{{bat}}quok{{test}}";
|
||||
let output = parse_string_with_var_refs(input);
|
||||
let input = "{{foo}}{{bar}}b{}az{{bat}}{}quok{{test}}";
|
||||
let output = AttrValue::parse_string(input);
|
||||
assert_eq!(
|
||||
output,
|
||||
vec![
|
||||
StringOrVarRef::VarRef("foo".to_owned()),
|
||||
StringOrVarRef::VarRef("bar".to_owned()),
|
||||
StringOrVarRef::String("baz".to_owned()),
|
||||
StringOrVarRef::VarRef("bat".to_owned()),
|
||||
StringOrVarRef::String("quok".to_owned()),
|
||||
StringOrVarRef::VarRef("test".to_owned()),
|
||||
],
|
||||
AttrValue(vec![
|
||||
AttrValueElement::VarRef(VarName("foo".to_owned())),
|
||||
AttrValueElement::VarRef(VarName("bar".to_owned())),
|
||||
AttrValueElement::primitive("b{}az".to_owned()),
|
||||
AttrValueElement::VarRef(VarName("bat".to_owned())),
|
||||
AttrValueElement::primitive("{}quok".to_owned()),
|
||||
AttrValueElement::VarRef(VarName("test".to_owned())),
|
||||
]),
|
||||
)
|
||||
}
|
||||
#[test]
|
||||
|
@ -168,8 +173,8 @@ mod test {
|
|||
assert_eq!(
|
||||
AttrValue(
|
||||
vec![
|
||||
StringOrVarRef::VarRef(VarName("var".to_owned())),
|
||||
StringOrVarRef::primitive("something".to_owned())
|
||||
AttrValueElement::VarRef(VarName("var".to_owned())),
|
||||
AttrValueElement::primitive("something".to_owned())
|
||||
]
|
||||
.into()
|
||||
),
|
||||
|
|
|
@ -72,10 +72,37 @@ impl fmt::Debug for Coords {
|
|||
}
|
||||
|
||||
impl Coords {
|
||||
/// parse a string for x and a string for y into a [`Coords`] object.
|
||||
pub fn from_strs(x: &str, y: &str) -> Result<Coords> {
|
||||
Ok(Coords {
|
||||
x: x.parse()?,
|
||||
y: y.parse()?,
|
||||
x: x.parse().with_context(|| format!("Failed to parse '{}'", x))?,
|
||||
y: y.parse().with_context(|| format!("Failed to parse '{}'", y))?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
#[test]
|
||||
fn test_parse_num_with_unit() {
|
||||
assert_eq!(NumWithUnit::Pixels(55), NumWithUnit::from_str("55").unwrap());
|
||||
assert_eq!(NumWithUnit::Pixels(55), NumWithUnit::from_str("55px").unwrap());
|
||||
assert_eq!(NumWithUnit::Percent(55), NumWithUnit::from_str("55%").unwrap());
|
||||
assert!(NumWithUnit::from_str("55pp").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_coords() {
|
||||
assert_eq!(
|
||||
Coords {
|
||||
x: NumWithUnit::Pixels(50),
|
||||
y: NumWithUnit::Pixels(60)
|
||||
},
|
||||
Coords::from_str("50x60").unwrap()
|
||||
);
|
||||
assert!(Coords::from_str("5060").is_err());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue