]>
Commit | Line | Data |
---|---|---|
102a4884 RBR |
1 | pub struct Strategy {} |
2 | ||
5732d284 | 3 | use std::path::Path; |
102a4884 | 4 | |
5732d284 | 5 | use crate::file_handler::{File, FileHandlerStrategy, FileType}; |
102a4884 RBR |
6 | |
7 | impl FileHandlerStrategy for Strategy { | |
5732d284 RBR |
8 | fn is(&self, path: &Path) -> bool { |
9 | !path.is_dir() && path.ends_with("_layout.html") | |
102a4884 RBR |
10 | } |
11 | ||
12 | fn identify(&self) -> FileType { | |
13 | FileType::Layout | |
14 | } | |
15 | ||
4fd89b80 | 16 | fn can_handle(&self, file_type: &FileType) -> bool { |
5732d284 | 17 | matches!(file_type, FileType::Layout) |
102a4884 RBR |
18 | } |
19 | ||
ea529736 RBR |
20 | // We don't implement handling for layout, as we assume there's only one |
21 | // and it got handled before. | |
5732d284 RBR |
22 | fn handle_html(&self, _s: &Path, _d: &Path, _f: &File, _l: &str) {} |
23 | fn handle_gemini(&self, _s: &Path, _d: &Path, _f: &File) {} | |
102a4884 | 24 | } |
260e8ec6 RBR |
25 | |
26 | #[cfg(test)] | |
27 | mod tests { | |
2cbae13c | 28 | use std::fs::create_dir_all; |
5732d284 | 29 | use std::path::PathBuf; |
260e8ec6 | 30 | |
2cbae13c | 31 | use super::*; |
260e8ec6 | 32 | |
2cbae13c | 33 | use test_utilities::*; |
260e8ec6 | 34 | |
2cbae13c RBR |
35 | #[test] |
36 | fn identifies_layout_file() { | |
37 | let test_dir = setup_test_dir(); | |
38 | create_test_file(&test_dir.join("_layout.html"), ""); | |
39 | let strategy = Strategy {}; | |
40 | assert!(strategy.is(&test_dir.join("_layout.html"))); | |
41 | } | |
260e8ec6 | 42 | |
2cbae13c RBR |
43 | #[test] |
44 | fn rejects_non_layout_html() { | |
45 | let test_dir = setup_test_dir(); | |
46 | create_test_file(&test_dir.join("regular.html"), ""); | |
47 | let strategy = Strategy {}; | |
48 | assert!(!strategy.is(&test_dir.join("regular.html"))); | |
49 | } | |
260e8ec6 | 50 | |
2cbae13c RBR |
51 | #[test] |
52 | fn rejects_layout_with_different_extension() { | |
53 | let test_dir = setup_test_dir(); | |
54 | create_test_file(&test_dir.join("_layout.txt"), ""); | |
55 | let strategy = Strategy {}; | |
56 | assert!(!strategy.is(&test_dir.join("_layout.txt"))); | |
260e8ec6 RBR |
57 | } |
58 | ||
2cbae13c RBR |
59 | #[test] |
60 | fn rejects_layout_with_prefix() { | |
61 | let test_dir = setup_test_dir(); | |
62 | create_test_file(&test_dir.join("prefix_layout.txt"), ""); | |
63 | let strategy = Strategy {}; | |
64 | assert!(!strategy.is(&test_dir.join("prefix_layout.txt"))); | |
65 | } | |
260e8ec6 | 66 | |
2cbae13c RBR |
67 | #[test] |
68 | fn rejects_directory_named_layout() { | |
69 | let test_dir = setup_test_dir(); | |
70 | let layout_dir = test_dir.join("_layout"); | |
5732d284 | 71 | create_dir_all(&layout_dir).expect("Could not create _layout test directory"); |
2cbae13c RBR |
72 | let strategy = Strategy {}; |
73 | assert!(!strategy.is(&layout_dir)); | |
260e8ec6 RBR |
74 | } |
75 | ||
2cbae13c RBR |
76 | #[test] |
77 | fn identifies_layout_type() { | |
78 | let strategy = Strategy {}; | |
79 | assert!(matches!(strategy.identify(), FileType::Layout)); | |
80 | } | |
260e8ec6 | 81 | |
2cbae13c RBR |
82 | #[test] |
83 | fn handles_layout_type() { | |
84 | let strategy = Strategy {}; | |
85 | assert!(strategy.can_handle(&FileType::Layout)); | |
86 | } | |
260e8ec6 | 87 | |
2cbae13c RBR |
88 | #[test] |
89 | fn rejects_non_layout_types() { | |
90 | let strategy = Strategy {}; | |
91 | assert!(!strategy.can_handle(&FileType::File)); | |
92 | assert!(!strategy.can_handle(&FileType::Gemini)); | |
93 | assert!(!strategy.can_handle(&FileType::Unknown)); | |
260e8ec6 RBR |
94 | } |
95 | ||
2cbae13c RBR |
96 | #[test] |
97 | fn handle_html_does_nothing() { | |
98 | let strategy = Strategy {}; | |
99 | let file = File { | |
100 | path: PathBuf::from("_layout.html"), | |
101 | file_type: FileType::Layout, | |
102 | }; | |
103 | ||
104 | strategy.handle_html( | |
105 | &PathBuf::from("source"), | |
106 | &PathBuf::from("dest"), | |
107 | &file, | |
5732d284 | 108 | "layout content", |
2cbae13c RBR |
109 | ); |
110 | } | |
260e8ec6 | 111 | |
2cbae13c RBR |
112 | #[test] |
113 | fn handle_gemini_does_nothing() { | |
114 | let strategy = Strategy {}; | |
115 | let file = File { | |
116 | path: PathBuf::from("test.gmi"), | |
117 | file_type: FileType::Layout, | |
118 | }; | |
119 | ||
120 | // Should not panic | |
5732d284 | 121 | strategy.handle_gemini(&PathBuf::from("source"), &PathBuf::from("dest"), &file); |
260e8ec6 RBR |
122 | } |
123 | } |