Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TheRealLorenz committed Jan 16, 2024
1 parent 6ff49bb commit 92bd67b
Showing 1 changed file with 66 additions and 1 deletion.
67 changes: 66 additions & 1 deletion rq-core/src/parser/variables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl FromStr for TemplateString {
}
}

#[derive(Debug, Error)]
#[derive(Debug, Error, PartialEq)]
#[error("missing field '{}'", .missing_variable.name)]
pub struct FillError {
missing_variable: Variable,
Expand Down Expand Up @@ -206,3 +206,68 @@ impl From<Pair<'_, Rule>> for HashTemplateMap {
Self(headers)
}
}

#[cfg(test)]
mod tests {
mod variable {}

mod template_string {
use std::collections::HashMap;

use crate::parser::variables::{FillError, Fragment, TemplateString, Variable};

#[test]
fn test_display() {
let ts = TemplateString::new(vec![Fragment::var("foo")]);
let ts2 = TemplateString::raw("barbar");
let ts_quoted = TemplateString::raw(" baz ");

assert_eq!(ts.to_string(), "{{foo}}");
assert_eq!(ts2.to_string(), "barbar");
assert_eq!(ts_quoted.to_string(), "\" baz \"");
}

#[test]
fn test_parse_str() {
let s = "' foo'{{bar}}baz";
let expected = TemplateString::new(vec![
Fragment::raw(" foo"),
Fragment::var("bar"),
Fragment::raw("baz"),
]);

assert_eq!(s.parse::<TemplateString>().unwrap(), expected);
}

#[test]
fn test_fill() {
let ts = TemplateString::new(vec![
Fragment::raw(" foo"),
Fragment::var("bar"),
Fragment::raw("baz"),
]);
let ts2 = TemplateString::raw("foobarbaz");
let ts3 = TemplateString::new(vec![Fragment::var("baz")]);
let values =
HashMap::from([("bar".into(), "FOOBAR".parse::<TemplateString>().unwrap())]);

assert_eq!(ts.fill(&values).unwrap(), " fooFOOBARbaz");
assert_eq!(ts2.fill(&values).unwrap(), "foobarbaz");
assert_eq!(
ts3.fill(&values),
Err(FillError::from(Variable::new("baz")))
)
}

#[test]
fn test_is_empty() {
let ts = TemplateString::new(vec![]);
let ts2 = TemplateString::raw("");
let ts3 = TemplateString::new(vec![Fragment::raw(""), Fragment::raw("")]);

assert!(ts.is_empty());
assert!(ts2.is_empty());
assert!(ts3.is_empty());
}
}
}

0 comments on commit 92bd67b

Please sign in to comment.