diff options
author | Aleksey Kladov <[email protected]> | 2020-07-09 09:58:56 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-07-09 09:58:56 +0100 |
commit | b660681a6becdcd33d4ed8cbb167c2a3dc170990 (patch) | |
tree | f249a5b4b21420dd833bc52e3705cd34a8b3c4f9 /crates/expect | |
parent | b9aab22d569c4ffe4d4f544a778bf07441ccf118 (diff) |
Unify tests
Diffstat (limited to 'crates/expect')
-rw-r--r-- | crates/expect/src/lib.rs | 27 |
1 files changed, 22 insertions, 5 deletions
diff --git a/crates/expect/src/lib.rs b/crates/expect/src/lib.rs index 7579a5027..408448eed 100644 --- a/crates/expect/src/lib.rs +++ b/crates/expect/src/lib.rs | |||
@@ -42,7 +42,9 @@ macro_rules! expect { | |||
42 | /// expect_file!["/crates/foo/test_data/bar.html"] | 42 | /// expect_file!["/crates/foo/test_data/bar.html"] |
43 | #[macro_export] | 43 | #[macro_export] |
44 | macro_rules! expect_file { | 44 | macro_rules! expect_file { |
45 | [$path:literal] => {$crate::ExpectFile { path: $path }}; | 45 | [$path:literal] => {$crate::ExpectFile { |
46 | path: $crate::ExpectFilePath::Static($path) | ||
47 | }}; | ||
46 | } | 48 | } |
47 | 49 | ||
48 | #[derive(Debug)] | 50 | #[derive(Debug)] |
@@ -53,7 +55,13 @@ pub struct Expect { | |||
53 | 55 | ||
54 | #[derive(Debug)] | 56 | #[derive(Debug)] |
55 | pub struct ExpectFile { | 57 | pub struct ExpectFile { |
56 | pub path: &'static str, | 58 | pub path: ExpectFilePath, |
59 | } | ||
60 | |||
61 | #[derive(Debug)] | ||
62 | pub enum ExpectFilePath { | ||
63 | Static(&'static str), | ||
64 | Dynamic(PathBuf), | ||
57 | } | 65 | } |
58 | 66 | ||
59 | #[derive(Debug)] | 67 | #[derive(Debug)] |
@@ -112,6 +120,9 @@ impl Expect { | |||
112 | } | 120 | } |
113 | 121 | ||
114 | impl ExpectFile { | 122 | impl ExpectFile { |
123 | pub fn new(path: PathBuf) -> ExpectFile { | ||
124 | ExpectFile { path: ExpectFilePath::Dynamic(path) } | ||
125 | } | ||
115 | pub fn assert_eq(&self, actual: &str) { | 126 | pub fn assert_eq(&self, actual: &str) { |
116 | let expected = self.read(); | 127 | let expected = self.read(); |
117 | if actual == expected { | 128 | if actual == expected { |
@@ -125,8 +136,14 @@ impl ExpectFile { | |||
125 | fn write(&self, contents: &str) { | 136 | fn write(&self, contents: &str) { |
126 | fs::write(self.abs_path(), contents).unwrap() | 137 | fs::write(self.abs_path(), contents).unwrap() |
127 | } | 138 | } |
139 | fn path(&self) -> &Path { | ||
140 | match &self.path { | ||
141 | ExpectFilePath::Static(it) => it.as_ref(), | ||
142 | ExpectFilePath::Dynamic(it) => it.as_path(), | ||
143 | } | ||
144 | } | ||
128 | fn abs_path(&self) -> PathBuf { | 145 | fn abs_path(&self) -> PathBuf { |
129 | workspace_root().join(self.path) | 146 | workspace_root().join(self.path()) |
130 | } | 147 | } |
131 | } | 148 | } |
132 | 149 | ||
@@ -154,11 +171,11 @@ impl Runtime { | |||
154 | fn fail_file(expect: &ExpectFile, expected: &str, actual: &str) { | 171 | fn fail_file(expect: &ExpectFile, expected: &str, actual: &str) { |
155 | let mut rt = RT.lock().unwrap_or_else(|poisoned| poisoned.into_inner()); | 172 | let mut rt = RT.lock().unwrap_or_else(|poisoned| poisoned.into_inner()); |
156 | if update_expect() { | 173 | if update_expect() { |
157 | println!("\x1b[1m\x1b[92mupdating\x1b[0m: {}", expect.path); | 174 | println!("\x1b[1m\x1b[92mupdating\x1b[0m: {}", expect.path().display()); |
158 | expect.write(actual); | 175 | expect.write(actual); |
159 | return; | 176 | return; |
160 | } | 177 | } |
161 | rt.panic(expect.path.to_string(), expected, actual); | 178 | rt.panic(expect.path().display().to_string(), expected, actual); |
162 | } | 179 | } |
163 | 180 | ||
164 | fn panic(&mut self, position: String, expected: &str, actual: &str) { | 181 | fn panic(&mut self, position: String, expected: &str, actual: &str) { |