| 1 | #[macro_use] |
| 2 | extern crate serde_json; |
| 3 | |
| 4 | #[test] |
| 5 | #[cfg(feature = "rust-embed")] |
| 6 | fn test_embed() { |
| 7 | use rust_embed::RustEmbed; |
| 8 | |
| 9 | #[derive(RustEmbed)] |
| 10 | #[folder = "tests/templates/"] |
| 11 | #[include = "*.hbs"] |
| 12 | struct Templates; |
| 13 | |
| 14 | let mut hbs = handlebars::Handlebars::new(); |
| 15 | hbs.register_embed_templates::<Templates>().unwrap(); |
| 16 | |
| 17 | assert_eq!(1, hbs.get_templates().len()); |
| 18 | |
| 19 | let data = json!({ |
| 20 | "name": "Andy" |
| 21 | }); |
| 22 | |
| 23 | assert_eq!( |
| 24 | hbs.render("hello.hbs", &data).unwrap().trim(), |
| 25 | "Hello, Andy" |
| 26 | ); |
| 27 | } |
| 28 | |
| 29 | #[test] |
| 30 | #[cfg(feature = "rust-embed")] |
| 31 | fn test_embed_with_extension() { |
| 32 | use rust_embed::RustEmbed; |
| 33 | |
| 34 | #[derive(RustEmbed)] |
| 35 | #[folder = "tests/templates/"] |
| 36 | struct Templates; |
| 37 | |
| 38 | let mut hbs = handlebars::Handlebars::new(); |
| 39 | hbs.register_embed_templates_with_extension::<Templates>(".hbs") |
| 40 | .unwrap(); |
| 41 | |
| 42 | assert_eq!(1, hbs.get_templates().len()); |
| 43 | |
| 44 | let data = json!({ |
| 45 | "name": "Andy" |
| 46 | }); |
| 47 | |
| 48 | assert_eq!(hbs.render("hello", &data).unwrap().trim(), "Hello, Andy"); |
| 49 | } |
| 50 | |
| 51 | #[test] |
| 52 | #[cfg(feature = "rust-embed")] |
| 53 | fn test_embed_with_extension_and_tests_struct_root() { |
| 54 | use rust_embed::RustEmbed; |
| 55 | |
| 56 | #[derive(RustEmbed)] |
| 57 | #[folder = "tests/"] |
| 58 | struct Templates; |
| 59 | |
| 60 | let mut hbs = handlebars::Handlebars::new(); |
| 61 | hbs.register_embed_templates_with_extension::<Templates>(".hbs") |
| 62 | .unwrap(); |
| 63 | |
| 64 | assert_eq!(1, hbs.get_templates().len()); |
| 65 | |
| 66 | let data = json!({ |
| 67 | "name": "Andy" |
| 68 | }); |
| 69 | |
| 70 | assert_eq!( |
| 71 | hbs.render("templates/hello", &data).unwrap().trim(), |
| 72 | "Hello, Andy" |
| 73 | ); |
| 74 | } |