Debian Codegraph @

crates/mdbook-core/src/book/tests.rs

raw
1 use super::*;
2
3 #[test]
4 fn section_number_has_correct_dotted_representation() {
5 let inputs = vec![
6 (vec![0], "0."),
7 (vec![1, 3], "1.3."),
8 (vec![1, 2, 3], "1.2.3."),
9 ];
10
11 for (input, should_be) in inputs {
12 let section_number = SectionNumber(input).to_string();
13 assert_eq!(section_number, should_be);
14 }
15 }
16
17 #[test]
18 fn book_iter_iterates_over_sequential_items() {
19 let items = vec![
20 BookItem::Chapter(Chapter {
21 name: String::from("Chapter 1"),
22 content: String::from("# Chapter 1"),
23 ..Default::default()
24 }),
25 BookItem::Separator,
26 ];
27 let book = Book::new_with_items(items);
28
29 let should_be: Vec<_> = book.items.iter().collect();
30
31 let got: Vec<_> = book.iter().collect();
32
33 assert_eq!(got, should_be);
34 }
35
36 #[test]
37 fn for_each_mut_visits_all_items() {
38 let items = vec![
39 BookItem::Chapter(Chapter {
40 name: String::from("Chapter 1"),
41 content: String::from("# Chapter 1"),
42 number: None,
43 path: Some(PathBuf::from("Chapter_1/index.md")),
44 source_path: Some(PathBuf::from("Chapter_1/index.md")),
45 parent_names: Vec::new(),
46 sub_items: vec![
47 BookItem::Chapter(Chapter::new(
48 "Hello World",
49 String::new(),
50 "Chapter_1/hello.md",
51 Vec::new(),
52 )),
53 BookItem::Separator,
54 BookItem::Chapter(Chapter::new(
55 "Goodbye World",
56 String::new(),
57 "Chapter_1/goodbye.md",
58 Vec::new(),
59 )),
60 ],
61 }),
62 BookItem::Separator,
63 ];
64 let mut book = Book::new_with_items(items);
65
66 let num_items = book.iter().count();
67 let mut visited = 0;
68
69 book.for_each_mut(|_| visited += 1);
70
71 assert_eq!(visited, num_items);
72 }
73
74 #[test]
75 fn iterate_over_nested_book_items() {
76 let items = vec![
77 BookItem::Chapter(Chapter {
78 name: String::from("Chapter 1"),
79 content: String::from("# Chapter 1"),
80 number: None,
81 path: Some(PathBuf::from("Chapter_1/index.md")),
82 source_path: Some(PathBuf::from("Chapter_1/index.md")),
83 parent_names: Vec::new(),
84 sub_items: vec![
85 BookItem::Chapter(Chapter::new(
86 "Hello World",
87 String::new(),
88 "Chapter_1/hello.md",
89 Vec::new(),
90 )),
91 BookItem::Separator,
92 BookItem::Chapter(Chapter::new(
93 "Goodbye World",
94 String::new(),
95 "Chapter_1/goodbye.md",
96 Vec::new(),
97 )),
98 ],
99 }),
100 BookItem::Separator,
101 ];
102 let book = Book::new_with_items(items);
103
104 let got: Vec<_> = book.iter().collect();
105
106 assert_eq!(got.len(), 5);
107
108 // checking the chapter names are in the order should be sufficient here...
109 let chapter_names: Vec<String> = got
110 .into_iter()
111 .filter_map(|i| match *i {
112 BookItem::Chapter(ref ch) => Some(ch.name.clone()),
113 _ => None,
114 })
115 .collect();
116 let should_be: Vec<_> = vec![
117 String::from("Chapter 1"),
118 String::from("Hello World"),
119 String::from("Goodbye World"),
120 ];
121
122 assert_eq!(chapter_names, should_be);
123 }