]>
Commit | Line | Data |
---|---|---|
102a4884 RBR |
1 | pub struct Strategy {} |
2 | ||
3 | use std::path::PathBuf; | |
4 | ||
4fd89b80 | 5 | use crate::file_handler::{File, FileType, FileHandlerStrategy}; |
102a4884 RBR |
6 | |
7 | impl 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)] | |
30 | mod tests { | |
2cbae13c | 31 | use std::fs::create_dir_all; |
260e8ec6 | 32 | |
2cbae13c | 33 | use super::*; |
260e8ec6 | 34 | |
2cbae13c | 35 | use test_utilities::*; |
260e8ec6 | 36 | |
2cbae13c RBR |
37 | #[test] |
38 | fn identifies_layout_file() { | |
39 | let test_dir = setup_test_dir(); | |
40 | create_test_file(&test_dir.join("_layout.html"), ""); | |
41 | let strategy = Strategy {}; | |
42 | assert!(strategy.is(&test_dir.join("_layout.html"))); | |
43 | } | |
260e8ec6 | 44 | |
2cbae13c RBR |
45 | #[test] |
46 | fn rejects_non_layout_html() { | |
47 | let test_dir = setup_test_dir(); | |
48 | create_test_file(&test_dir.join("regular.html"), ""); | |
49 | let strategy = Strategy {}; | |
50 | assert!(!strategy.is(&test_dir.join("regular.html"))); | |
51 | } | |
260e8ec6 | 52 | |
2cbae13c RBR |
53 | #[test] |
54 | fn rejects_layout_with_different_extension() { | |
55 | let test_dir = setup_test_dir(); | |
56 | create_test_file(&test_dir.join("_layout.txt"), ""); | |
57 | let strategy = Strategy {}; | |
58 | assert!(!strategy.is(&test_dir.join("_layout.txt"))); | |
260e8ec6 RBR |
59 | } |
60 | ||
2cbae13c RBR |
61 | #[test] |
62 | fn rejects_layout_with_prefix() { | |
63 | let test_dir = setup_test_dir(); | |
64 | create_test_file(&test_dir.join("prefix_layout.txt"), ""); | |
65 | let strategy = Strategy {}; | |
66 | assert!(!strategy.is(&test_dir.join("prefix_layout.txt"))); | |
67 | } | |
260e8ec6 | 68 | |
2cbae13c RBR |
69 | #[test] |
70 | fn rejects_directory_named_layout() { | |
71 | let test_dir = setup_test_dir(); | |
72 | let layout_dir = test_dir.join("_layout"); | |
73 | create_dir_all(&layout_dir) | |
74 | .expect("Could not create _layout test directory"); | |
75 | let strategy = Strategy {}; | |
76 | assert!(!strategy.is(&layout_dir)); | |
260e8ec6 RBR |
77 | } |
78 | ||
2cbae13c RBR |
79 | #[test] |
80 | fn identifies_layout_type() { | |
81 | let strategy = Strategy {}; | |
82 | assert!(matches!(strategy.identify(), FileType::Layout)); | |
83 | } | |
260e8ec6 | 84 | |
2cbae13c RBR |
85 | #[test] |
86 | fn handles_layout_type() { | |
87 | let strategy = Strategy {}; | |
88 | assert!(strategy.can_handle(&FileType::Layout)); | |
89 | } | |
260e8ec6 | 90 | |
2cbae13c RBR |
91 | #[test] |
92 | fn rejects_non_layout_types() { | |
93 | let strategy = Strategy {}; | |
94 | assert!(!strategy.can_handle(&FileType::File)); | |
95 | assert!(!strategy.can_handle(&FileType::Gemini)); | |
96 | assert!(!strategy.can_handle(&FileType::Unknown)); | |
260e8ec6 RBR |
97 | } |
98 | ||
2cbae13c RBR |
99 | #[test] |
100 | fn handle_html_does_nothing() { | |
101 | let strategy = Strategy {}; | |
102 | let file = File { | |
103 | path: PathBuf::from("_layout.html"), | |
104 | file_type: FileType::Layout, | |
105 | }; | |
106 | ||
107 | strategy.handle_html( | |
108 | &PathBuf::from("source"), | |
109 | &PathBuf::from("dest"), | |
110 | &file, | |
111 | "layout content" | |
112 | ); | |
113 | } | |
260e8ec6 | 114 | |
2cbae13c RBR |
115 | #[test] |
116 | fn handle_gemini_does_nothing() { | |
117 | let strategy = Strategy {}; | |
118 | let file = File { | |
119 | path: PathBuf::from("test.gmi"), | |
120 | file_type: FileType::Layout, | |
121 | }; | |
122 | ||
123 | // Should not panic | |
124 | strategy.handle_gemini( | |
125 | &PathBuf::from("source"), | |
126 | &PathBuf::from("dest"), | |
127 | &file | |
128 | ); | |
260e8ec6 RBR |
129 | } |
130 | } |