aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Guillaume Gomez <guillaume1.gomez@gmail.com>2023-03-07 12:14:07 +0100
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2023-03-13 14:41:05 +0100
commit40be079d5d0ea988303cd70034d11c92fbb8fa39 (patch)
treede3ddcb2ec3a3c11d521e6bc5568f3ec2c8e546a
parent3ea521d6e88150dd4307ad610cabfe0fb8f30f9d (diff)
downloadaskama-40be079d5d0ea988303cd70034d11c92fbb8fa39.tar.gz
askama-40be079d5d0ea988303cd70034d11c92fbb8fa39.tar.bz2
askama-40be079d5d0ea988303cd70034d11c92fbb8fa39.zip
Add tests for whitespace argument in template derive proc-macro
-rw-r--r--askama_derive/src/config.rs30
-rw-r--r--testing/tests/whitespace.rs24
2 files changed, 54 insertions, 0 deletions
diff --git a/askama_derive/src/config.rs b/askama_derive/src/config.rs
index 22aada7..cf22a72 100644
--- a/askama_derive/src/config.rs
+++ b/askama_derive/src/config.rs
@@ -549,4 +549,34 @@ mod tests {
.unwrap();
assert_eq!(config.whitespace, WhitespaceHandling::Minimize);
}
+
+ #[cfg(feature = "toml")]
+ #[test]
+ fn test_whitespace_in_template() {
+ // Checking that template arguments have precedence over general configuration.
+ // So in here, in the template arguments, there is `whitespace = "minimize"` so
+ // the `WhitespaceHandling` should be `Minimize` as well.
+ let config = Config::new(
+ r#"
+ [general]
+ whitespace = "suppress"
+ "#,
+ Some(&"minimize".to_owned()),
+ )
+ .unwrap();
+ assert_eq!(config.whitespace, WhitespaceHandling::Minimize);
+
+ let config = Config::new(r#""#, Some(&"minimize".to_owned())).unwrap();
+ assert_eq!(config.whitespace, WhitespaceHandling::Minimize);
+ }
+
+ #[test]
+ fn test_config_whitespace_error() {
+ let config = Config::new(r#""#, Some(&"trim".to_owned()));
+ if let Err(err) = config {
+ assert_eq!(err.msg, "invalid value for `whitespace`: \"trim\"");
+ } else {
+ panic!("Config::new should have return an error");
+ }
+ }
}
diff --git a/testing/tests/whitespace.rs b/testing/tests/whitespace.rs
index 4793547..0e42f78 100644
--- a/testing/tests/whitespace.rs
+++ b/testing/tests/whitespace.rs
@@ -116,3 +116,27 @@ fn test_outer_whitespace() {
test_template!(" 1{# #} ", " 1 ");
test_template!("\n1{# #}\n\n\n", "\n1\n\n\n");
}
+
+macro_rules! test_template_ws_config {
+ ($config:literal, $ws:literal, $source:literal, $rendered: literal) => {{
+ #[derive(Template)]
+ #[template(source = $source, ext = "txt", config = $config, whitespace = $ws)]
+ struct CondWs;
+
+ assert_eq!(CondWs.render().unwrap(), $rendered);
+ }};
+}
+
+#[test]
+fn test_template_whitespace_config() {
+ test_template_ws_config!("test_trim.toml", "preserve", "\t1{# #}\t2", "\t1\t2");
+ test_template_ws_config!("test_trim.toml", "minimize", " 1{# #} 2", " 1 2");
+ test_template_ws_config!("test_trim.toml", "suppress", " 1{# #} 2", " 12");
+ test_template_ws_config!(
+ "test_minimize.toml",
+ "preserve",
+ "\n1{# #}\n\n\n2",
+ "\n1\n\n\n2"
+ );
+ test_template_ws_config!("test_minimize.toml", "suppress", "\n1{# #}\n\n\n2", "\n12");
+}