Skip to content

Commit 84b76ac

Browse files
committed
Add HtmlSafe reference wrapper tests
1 parent 216836e commit 84b76ac

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

testing/tests/simple.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
#![allow(clippy::disallowed_names)] // For the use of `foo` in test cases
22

3+
use std::cell::{RefCell, RefMut};
34
use std::collections::HashMap;
5+
use std::fmt;
6+
use std::pin::Pin;
7+
use std::sync::{Arc, Mutex, MutexGuard};
48

59
use rinja::Template;
10+
use rinja::filters::HtmlSafe;
611

712
#[derive(Template)]
813
#[template(path = "simple.html")]
@@ -560,3 +565,31 @@ struct SplitTemplateDeclaration;
560565
fn test_split_template_declaration() {
561566
assert_eq!(SplitTemplateDeclaration.to_string(), "🙂");
562567
}
568+
569+
#[test]
570+
fn test_ref_custom_type() {
571+
const TEXT: &str = "&this <is >not 'safe";
572+
573+
struct MySafeType;
574+
575+
impl HtmlSafe for MySafeType {}
576+
577+
impl fmt::Display for MySafeType {
578+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
579+
f.write_str(TEXT)
580+
}
581+
}
582+
583+
#[derive(Template)]
584+
#[template(ext = "html", source = "{{ data }}")]
585+
struct MyTemplate<'a, 'b, 'c> {
586+
data: &'a mut RefMut<'b, Pin<Arc<MutexGuard<'c, MySafeType>>>>,
587+
}
588+
589+
let mutex = Mutex::new(MySafeType);
590+
let cell = RefCell::new(Arc::pin(mutex.try_lock().unwrap()));
591+
let tmpl = MyTemplate {
592+
data: &mut cell.try_borrow_mut().unwrap(),
593+
};
594+
assert_eq!(tmpl.render().unwrap(), TEXT);
595+
}

0 commit comments

Comments
 (0)