]> git.r.bdr.sh - rbdr/page/blame - src/file_handler/file_strategies/layout.rs
Update file strategy tests
[rbdr/page] / src / file_handler / file_strategies / layout.rs
CommitLineData
102a4884
RBR
1pub struct Strategy {}
2
3use std::path::PathBuf;
4
4fd89b80 5use crate::file_handler::{File, FileType, FileHandlerStrategy};
102a4884
RBR
6
7impl FileHandlerStrategy for Strategy {
8 fn is(&self, path: &PathBuf) -> bool {
9 return !path.is_dir() && path.ends_with("_layout.html");
10 }
11
12 fn identify(&self) -> FileType {
13 FileType::Layout
14 }
15
4fd89b80
RBR
16 fn can_handle(&self, file_type: &FileType) -> bool {
17 match file_type {
18 FileType::Layout => true,
19 _ => false,
20 }
102a4884
RBR
21 }
22
ea529736
RBR
23 // We don't implement handling for layout, as we assume there's only one
24 // and it got handled before.
260e8ec6 25 fn handle_html(&self, _s: &PathBuf, _d: &PathBuf, _f: &File, _l: &str) {}
dd0a540c 26 fn handle_gemini(&self, _s: &PathBuf, _d: &PathBuf, _f: &File) {}
102a4884 27}
260e8ec6
RBR
28
29#[cfg(test)]
30mod tests {
31 use super::*;
32 use std::path::PathBuf;
33
34 fn setup() -> Strategy {
35 Strategy {}
36 }
37
38 mod is_tests {
39 use super::*;
40
41 #[test]
42 fn identifies_layout_file() {
43 let strategy = setup();
44 let path = PathBuf::from("_layout.html");
45 assert!(strategy.is(&path));
46 }
47
48 #[test]
49 fn rejects_non_layout_html() {
50 let strategy = setup();
51 let path = PathBuf::from("regular.html");
52 assert!(!strategy.is(&path));
53 }
54
55 #[test]
56 fn rejects_layout_with_different_extension() {
57 let strategy = setup();
58 let path = PathBuf::from("_layout.txt");
59 assert!(!strategy.is(&path));
60 }
61
62 #[test]
63 fn rejects_layout_with_prefix() {
64 let strategy = setup();
65 let path = PathBuf::from("prefix_layout.html");
66 assert!(!strategy.is(&path));
67 }
68
69 #[test]
70 fn rejects_directory_named_layout() {
71 let strategy = setup();
72 let path = PathBuf::from("tests/fixtures");
73
74 assert!(!strategy.is(&path));
75 }
76 }
77
78 mod identify_tests {
79 use super::*;
80
81 #[test]
82 fn returns_layout_type() {
83 let strategy = setup();
84 assert!(matches!(strategy.identify(), FileType::Layout));
85 }
86 }
87
88 mod can_handle_tests {
89 use super::*;
90
91 #[test]
92 fn handles_layout_type() {
93 let strategy = setup();
94 assert!(strategy.can_handle(&FileType::Layout));
95 }
96
97 #[test]
98 fn rejects_non_layout_types() {
99 let strategy = setup();
100 assert!(!strategy.can_handle(&FileType::File));
101 assert!(!strategy.can_handle(&FileType::Gemini));
102 assert!(!strategy.can_handle(&FileType::Unknown));
103 }
104 }
105
106 mod handle_methods {
107 use super::*;
108
109 #[test]
110 fn handle_html_does_nothing() {
111 let strategy = setup();
112 let file = File {
113 path: PathBuf::from("test.html"),
114 file_type: FileType::Layout,
115 };
116
117 // Should not panic
118 strategy.handle_html(
119 &PathBuf::from("source"),
120 &PathBuf::from("dest"),
121 &file,
122 "layout content"
123 );
124 }
125
126 #[test]
127 fn handle_gemini_does_nothing() {
128 let strategy = setup();
129 let file = File {
130 path: PathBuf::from("test.gmi"),
131 file_type: FileType::Layout,
132 };
133
134 // Should not panic
135 strategy.handle_gemini(
136 &PathBuf::from("source"),
137 &PathBuf::from("dest"),
138 &file
139 );
140 }
141 }
142}