1 mod common;
2 
3 use common::ShortCode;
4 use rendering::Heading;
5 
6 #[derive(PartialEq, Debug)]
7 struct HelperHeader {
8     title: String,
9     children: Vec<HelperHeader>,
10 }
11 
12 impl PartialEq<Heading> for HelperHeader {
eq(&self, other: &Heading) -> bool13     fn eq(&self, other: &Heading) -> bool {
14         self.title == other.title && self.children == other.children
15     }
16 }
17 
18 macro_rules! hh {
19     ($title:literal, [$($children:expr),*]) => {{
20         HelperHeader {
21             title: $title.to_string(),
22             children: vec![$($children),*],
23         }
24     }}
25 }
26 
27 macro_rules! test_toc {
28     ($in_str:literal, $toc:expr, [$($shortcodes:ident),*]) => {
29         let config = config::Config::default_for_test();
30 
31         #[allow(unused_mut)]
32         let mut tera = tera::Tera::default();
33 
34         // Add all shortcodes
35         $(
36             tera.add_raw_template(
37                 &format!("shortcodes/{}", $shortcodes.filename()),
38                 $shortcodes.output
39             ).expect("Failed to add raw template");
40         )*
41 
42         let permalinks = std::collections::HashMap::new();
43         let mut context = rendering::RenderContext::new(
44             &tera,
45             &config,
46             &config.default_language,
47             "",
48             &permalinks,
49             front_matter::InsertAnchor::None,
50         );
51         let shortcode_def = utils::templates::get_shortcodes(&tera);
52         context.set_shortcode_definitions(&shortcode_def);
53 
54         let rendered = rendering::render_content($in_str, &context);
55         assert!(rendered.is_ok());
56 
57         let rendered = rendered.unwrap();
58         let toc = rendered.toc.clone();
59 
60         assert!($toc == toc);
61     }
62 }
63 
64 #[test]
basic_toc()65 fn basic_toc() {
66     test_toc!("Hello World!", <Vec<HelperHeader>>::new(), []);
67     test_toc!("# ABC\n## DEF", vec![hh!("ABC", [hh!("DEF", [])])], []);
68 }
69 
70 #[test]
all_layers()71 fn all_layers() {
72     test_toc!(
73         "# A\n## B\n### C\n#### D\n##### E\n###### F\n",
74         vec![hh!("A", [hh!("B", [hh!("C", [hh!("D", [hh!("E", [hh!("F", [])])])])])])],
75         []
76     );
77 }
78 
79 #[test]
multiple_on_layer()80 fn multiple_on_layer() {
81     test_toc!(
82         "# A\n## B\n## C\n### D\n## E\n### F\n",
83         vec![hh!("A", [hh!("B", []), hh!("C", [hh!("D", [])]), hh!("E", [hh!("F", [])])])],
84         []
85     );
86 }
87 
88 // const MD_SIMPLE1: ShortCode = ShortCode::new("simple", "Hello World!", true);
89 // const MD_SIMPLE2: ShortCode = ShortCode::new("simple2", "Wow, much cool!", true);
90 //
91 // #[test]
92 // fn with_shortcode_titles() {
93 //     test_toc!(
94 //         "# {{ simple() }}\n## {{ simple2() }}\n### ABC\n#### {{ simple() }}\n",
95 //         vec![hh!(
96 //             "Hello World!",
97 //             [hh!("Wow, much cool!", [hh!("ABC", [hh!("Hello World!", [])])])]
98 //         )],
99 //         [MD_SIMPLE1, MD_SIMPLE2]
100 //     );
101 // }
102 //
103 // const MD_MULTILINE: ShortCode = ShortCode::new("multiline", "<div>\n    Wow!\n</div>", false);
104 //
105 // #[test]
106 // fn with_multiline_shortcodes() {
107 //     test_toc!(
108 //         "# {{ multiline() }}\n{{ multiline() }}\n## {{ multiline()() }}\n",
109 //         vec![hh!("Wow!", [hh!("Wow!", [])])],
110 //         [MD_MULTILINE]
111 //     );
112 // }
113