Skip to content

Commit 07fe610

Browse files
committed
tests: fully test markdown.rs
1 parent 6613bd5 commit 07fe610

File tree

1 file changed

+152
-1
lines changed

1 file changed

+152
-1
lines changed

src/types/markdown.rs

Lines changed: 152 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ impl fmt::Display for Markdown {
202202
#[cfg(test)]
203203
mod tests {
204204
use super::*;
205-
use crate::ImageBuilder;
205+
use crate::{ImageBuilder, LinkBuilder, ListBuilder};
206206

207207
#[test]
208208
fn test_empty_document_using_default() {
@@ -218,6 +218,36 @@ mod tests {
218218
assert_eq!(doc.footers.len(), 0);
219219
}
220220

221+
#[test]
222+
fn test_create_document_with_empty_vecs() {
223+
assert_eq!(Markdown::with(vec![], vec![]).render(), "");
224+
}
225+
226+
#[test]
227+
fn test_create_document_with_paragraph() {
228+
assert_eq!(
229+
Markdown::with(vec![Box::new(Paragraph::from("Hello World"))], vec![]).render(),
230+
"Hello World\n"
231+
);
232+
}
233+
234+
#[test]
235+
fn test_create_document_with_paragraph_and_footer() {
236+
assert_eq!(
237+
Markdown::with(
238+
vec![Box::new(Paragraph::from("Hello World"))],
239+
vec![Box::new("[test]: https://example.com")]
240+
)
241+
.render(),
242+
"Hello World\n\n[test]: https://example.com\n"
243+
);
244+
}
245+
246+
#[test]
247+
fn test_empty_document_render() {
248+
assert_eq!(Markdown::new().render(), "");
249+
}
250+
221251
#[test]
222252
fn document_with_one_paragraph() {
223253
assert_eq!(
@@ -237,6 +267,75 @@ mod tests {
237267
);
238268
}
239269

270+
#[test]
271+
fn test_document_with_header_shorthands() {
272+
assert_eq!(
273+
Markdown::new()
274+
.h1("My document")
275+
.paragraph("I like pizza.")
276+
.h2("Heading 2")
277+
.h3("Heading 3")
278+
.h4("Heading 4")
279+
.h5("Heading 5")
280+
.h6("Heading 6")
281+
.render(),
282+
"# My document\n\nI like pizza.\n\n## Heading 2\n\n### Heading 3\n\n#### Heading 4\n\n##### Heading 5\n\n###### Heading 6\n"
283+
)
284+
}
285+
286+
#[test]
287+
fn test_document_with_header() {
288+
assert_eq!(
289+
Markdown::new()
290+
.header("My document", 1u16)
291+
.paragraph("I like pizza.")
292+
.header("Heading 2", 2usize)
293+
.header("Heading 6", 6usize)
294+
.render(),
295+
"# My document\n\nI like pizza.\n\n## Heading 2\n\n###### Heading 6\n"
296+
)
297+
}
298+
299+
#[test]
300+
#[should_panic]
301+
fn test_document_header_upper_range_panic() {
302+
Markdown::new().header("Invalid header", 7usize);
303+
}
304+
305+
#[test]
306+
#[should_panic]
307+
fn test_document_header_bottom_range_panic() {
308+
Markdown::new().header("Invalid header", 0usize);
309+
}
310+
311+
#[test]
312+
fn test_document_header_shorthand_matches_generic() {
313+
assert_eq!(
314+
Markdown::new().header("Header", 1usize).render(),
315+
Markdown::new().h1("Header").render()
316+
);
317+
assert_eq!(
318+
Markdown::new().header("Header", 2usize).render(),
319+
Markdown::new().h2("Header").render()
320+
);
321+
assert_eq!(
322+
Markdown::new().header("Header", 3usize).render(),
323+
Markdown::new().h3("Header").render()
324+
);
325+
assert_eq!(
326+
Markdown::new().header("Header", 4usize).render(),
327+
Markdown::new().h4("Header").render()
328+
);
329+
assert_eq!(
330+
Markdown::new().header("Header", 5usize).render(),
331+
Markdown::new().h5("Header").render()
332+
);
333+
assert_eq!(
334+
Markdown::new().header("Header", 6usize).render(),
335+
Markdown::new().h6("Header").render()
336+
);
337+
}
338+
240339
#[test]
241340
fn document_with_image() {
242341
let mut doc = Markdown::new();
@@ -266,4 +365,56 @@ mod tests {
266365

267366
assert_eq!(doc.render(), "![A cute picture of a sandcat][A cute picture of a sandcat]\n\n[A cute picture of a sandcat]: https://example.com/picture.png\n");
268367
}
368+
369+
#[test]
370+
fn test_document_with_link() {
371+
let mut doc = Markdown::new();
372+
doc.link(
373+
LinkBuilder::new()
374+
.url("https://example.com/picture.png")
375+
.text("A cute picture of a sandcat")
376+
.build(),
377+
);
378+
379+
assert_eq!(
380+
doc.render(),
381+
"[A cute picture of a sandcat](https://example.com/picture.png)\n"
382+
);
383+
}
384+
385+
#[test]
386+
fn test_document_with_link_footer() {
387+
let mut doc = Markdown::new();
388+
doc.link(
389+
LinkBuilder::new()
390+
.url("https://example.com/picture.png")
391+
.text("A cute picture of a sandcat")
392+
.footer()
393+
.build(),
394+
);
395+
396+
assert_eq!(doc.render(), "[A cute picture of a sandcat][A cute picture of a sandcat]\n\n[A cute picture of a sandcat]: https://example.com/picture.png\n");
397+
}
398+
399+
#[test]
400+
fn test_document_with_list() {
401+
let mut doc = Markdown::new();
402+
403+
doc.list(
404+
ListBuilder::new()
405+
.append("First do this")
406+
.append("Then do this")
407+
.ordered(),
408+
);
409+
410+
assert_eq!(doc.render(), "1. First do this\n2. Then do this\n")
411+
}
412+
413+
#[test]
414+
fn test_document_add() {
415+
assert_eq!(
416+
Markdown::new().add(Paragraph::from("Hello world")).render(),
417+
"Hello world\n"
418+
);
419+
}
269420
}

0 commit comments

Comments
 (0)