]>
Commit | Line | Data |
---|---|---|
1 | pub struct Strategy {} | |
2 | ||
3 | use std::path::PathBuf; | |
4 | ||
5 | use crate::file_handler::{File, FileType, FileHandlerStrategy}; | |
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 | ||
16 | fn can_handle(&self, file_type: &FileType) -> bool { | |
17 | match file_type { | |
18 | FileType::Layout => true, | |
19 | _ => false, | |
20 | } | |
21 | } | |
22 | ||
23 | // We don't implement handling for layout, as we assume there's only one | |
24 | // and it got handled before. | |
25 | fn handle_html(&self, _s: &PathBuf, _d: &PathBuf, _f: &File, _l: &str) {} | |
26 | fn handle_gemini(&self, _s: &PathBuf, _d: &PathBuf, _f: &File) {} | |
27 | } | |
28 | ||
29 | #[cfg(test)] | |
30 | mod tests { | |
31 | use std::fs::create_dir_all; | |
32 | ||
33 | use super::*; | |
34 | ||
35 | use test_utilities::*; | |
36 | ||
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 | } | |
44 | ||
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 | } | |
52 | ||
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"))); | |
59 | } | |
60 | ||
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 | } | |
68 | ||
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)); | |
77 | } | |
78 | ||
79 | #[test] | |
80 | fn identifies_layout_type() { | |
81 | let strategy = Strategy {}; | |
82 | assert!(matches!(strategy.identify(), FileType::Layout)); | |
83 | } | |
84 | ||
85 | #[test] | |
86 | fn handles_layout_type() { | |
87 | let strategy = Strategy {}; | |
88 | assert!(strategy.can_handle(&FileType::Layout)); | |
89 | } | |
90 | ||
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)); | |
97 | } | |
98 | ||
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 | } | |
114 | ||
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 | ); | |
129 | } | |
130 | } |