Skip to content

Commit 757389c

Browse files
committed
tests
1 parent cb31e07 commit 757389c

File tree

1 file changed

+65
-58
lines changed

1 file changed

+65
-58
lines changed

src/template_helpers.rs

Lines changed: 65 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -304,13 +304,13 @@ impl CanHelp for MarkdownHelper {
304304
fn call(&self, args: &[PathAndJson]) -> Result<JsonValue, String> {
305305
let (markdown_src_value, preset_name) = match args {
306306
[v] => (v.value(), "default"),
307-
[v, preset] => (
308-
v.value(),
309-
preset
310-
.value()
311-
.as_str()
312-
.ok_or("markdown template helper expects a string as preset name")?,
313-
),
307+
[v, preset] => {
308+
let value = v.value();
309+
let preset_name_value = preset.value();
310+
let preset = preset_name_value.as_str()
311+
.ok_or_else(|| format!("markdown template helper expects a string as preset name. Got: {preset_name_value}"))?;
312+
(value, preset)
313+
}
314314
_ => return Err("markdown template helper expects one or two arguments".to_string()),
315315
};
316316
let markdown_src = match markdown_src_value {
@@ -626,60 +626,67 @@ mod tests {
626626

627627
const UNSAFE_MARKUP: &'static str = "<table><tr><td>";
628628
const ESCAPED_UNSAFE_MARKUP: &'static str = "&lt;table&gt;&lt;tr&gt;&lt;td&gt;";
629-
630-
#[test]
631-
fn test_html_blocks_are_not_allowed_by_default() {
632-
let helper = MarkdownHelper::default();
633-
let actual = helper.call(&as_args(&contents())).unwrap();
634-
635-
assert_eq!(Some(ESCAPED_UNSAFE_MARKUP), actual.as_str());
636-
}
637-
638629
#[test]
639-
fn test_html_blocks_are_not_allowed_when_allow_unsafe_is_undefined() {
630+
fn test_html_blocks_with_various_settings() {
640631
let helper = MarkdownHelper::default();
641-
let allow_unsafe = Value::Null;
642-
let actual = helper
643-
.call(&as_args_with_unsafe(&contents(), &allow_unsafe))
644-
.unwrap();
645-
646-
assert_eq!(Some(ESCAPED_UNSAFE_MARKUP), actual.as_str());
647-
}
632+
let content = contents();
648633

649-
#[test]
650-
fn test_html_blocks_are_not_allowed_when_allow_unsafe_is_false() {
651-
let helper = MarkdownHelper::default();
652-
let allow_unsafe = Value::Bool(false);
653-
let actual = helper
654-
.call(&as_args_with_unsafe(&contents(), &allow_unsafe))
655-
.unwrap();
656-
657-
assert_eq!(Some(ESCAPED_UNSAFE_MARKUP), actual.as_str());
658-
}
659-
660-
#[test]
661-
fn test_html_blocks_are_not_allowed_when_allow_unsafe_option_is_missing() {
662-
let helper = MarkdownHelper::default();
663-
let allow_unsafe = ScopedJson::Missing;
664-
let actual = helper
665-
.call(&[
666-
as_helper_arg(CONTENT_KEY, &contents()),
667-
to_path_and_json(MarkdownHelper::ALLOW_UNSAFE, allow_unsafe),
668-
])
669-
.unwrap();
670-
671-
assert_eq!(Some(ESCAPED_UNSAFE_MARKUP), actual.as_str());
672-
}
673-
674-
#[test]
675-
fn test_html_blocks_are_allowed_when_allow_unsafe_is_true() {
676-
let helper = MarkdownHelper::default();
677-
let allow_unsafe = Value::String(String::from(MarkdownHelper::ALLOW_UNSAFE));
678-
let actual = helper
679-
.call(&as_args_with_unsafe(&contents(), &allow_unsafe))
680-
.unwrap();
634+
struct TestCase {
635+
name: &'static str,
636+
preset: Option<Value>,
637+
expected_output: Result<&'static str, String>,
638+
}
681639

682-
assert_eq!(Some(UNSAFE_MARKUP), actual.as_str());
640+
let test_cases = [
641+
TestCase {
642+
name: "default settings",
643+
preset: Some(Value::String("default".to_string())),
644+
expected_output: Ok(ESCAPED_UNSAFE_MARKUP),
645+
},
646+
TestCase {
647+
name: "allow_unsafe preset",
648+
preset: Some(Value::String("allow_unsafe".to_string())),
649+
expected_output: Ok(UNSAFE_MARKUP),
650+
},
651+
TestCase {
652+
name: "undefined allow_unsafe",
653+
preset: Some(Value::Null),
654+
expected_output: Err(
655+
"markdown template helper expects a string as preset name. Got: null"
656+
.to_string(),
657+
),
658+
},
659+
TestCase {
660+
name: "allow_unsafe is false",
661+
preset: Some(Value::Bool(false)),
662+
expected_output: Err(
663+
"markdown template helper expects a string as preset name. Got: false"
664+
.to_string(),
665+
),
666+
},
667+
];
668+
669+
for case in test_cases {
670+
let args = match case.preset {
671+
None => &as_args(&content)[..],
672+
Some(ref preset) => &as_args_with_unsafe(&content, preset)[..],
673+
};
674+
675+
match helper.call(&args) {
676+
Ok(actual) => assert_eq!(
677+
case.expected_output.unwrap(),
678+
actual.as_str().unwrap(),
679+
"Failed on case: {}",
680+
case.name
681+
),
682+
Err(e) => assert_eq!(
683+
case.expected_output.unwrap_err(),
684+
e,
685+
"Failed on case: {}",
686+
case.name
687+
),
688+
}
689+
}
683690
}
684691

685692
fn as_args_with_unsafe<'a>(
@@ -688,7 +695,7 @@ mod tests {
688695
) -> [PathAndJson<'a>; 2] {
689696
[
690697
as_helper_arg(CONTENT_KEY, contents),
691-
as_helper_arg(MarkdownHelper::ALLOW_UNSAFE, allow_unsafe),
698+
as_helper_arg("allow_unsafe", allow_unsafe),
692699
]
693700
}
694701

0 commit comments

Comments
 (0)