diff options
author | Aleksey Kladov <[email protected]> | 2020-07-01 10:19:40 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-07-01 10:19:40 +0100 |
commit | adf624b433277a0106f5354bb7d62ab1a04f216b (patch) | |
tree | 82b7095484d457ae8f0d89960cc94291ee53c909 /crates/expect/src | |
parent | 82838f5eda6ee98cebb9574ceef36544f1a45a4d (diff) |
Add file support to expect
Diffstat (limited to 'crates/expect/src')
-rw-r--r-- | crates/expect/src/lib.rs | 60 |
1 files changed, 52 insertions, 8 deletions
diff --git a/crates/expect/src/lib.rs b/crates/expect/src/lib.rs index 3a92b36e2..3f293f5d5 100644 --- a/crates/expect/src/lib.rs +++ b/crates/expect/src/lib.rs | |||
@@ -14,7 +14,7 @@ use once_cell::sync::Lazy; | |||
14 | use stdx::{lines_with_ends, trim_indent}; | 14 | use stdx::{lines_with_ends, trim_indent}; |
15 | 15 | ||
16 | const HELP: &str = " | 16 | const HELP: &str = " |
17 | You can update all `expect![[]]` tests by: | 17 | You can update all `expect![[]]` tests by running: |
18 | 18 | ||
19 | env UPDATE_EXPECT=1 cargo test | 19 | env UPDATE_EXPECT=1 cargo test |
20 | 20 | ||
@@ -25,20 +25,26 @@ fn update_expect() -> bool { | |||
25 | env::var("UPDATE_EXPECT").is_ok() | 25 | env::var("UPDATE_EXPECT").is_ok() |
26 | } | 26 | } |
27 | 27 | ||
28 | /// expect![[""]] | 28 | /// expect![[r#"inline snapshot"#]] |
29 | #[macro_export] | 29 | #[macro_export] |
30 | macro_rules! expect { | 30 | macro_rules! expect { |
31 | [[$lit:literal]] => {$crate::Expect { | 31 | [[$data:literal]] => {$crate::Expect { |
32 | position: $crate::Position { | 32 | position: $crate::Position { |
33 | file: file!(), | 33 | file: file!(), |
34 | line: line!(), | 34 | line: line!(), |
35 | column: column!(), | 35 | column: column!(), |
36 | }, | 36 | }, |
37 | data: $lit, | 37 | data: $data, |
38 | }}; | 38 | }}; |
39 | [[]] => { $crate::expect![[""]] }; | 39 | [[]] => { $crate::expect![[""]] }; |
40 | } | 40 | } |
41 | 41 | ||
42 | /// expect_file!["/crates/foo/test_data/foo.rs"] | ||
43 | #[macro_export] | ||
44 | macro_rules! expect_file { | ||
45 | [$path:literal] => {$crate::ExpectFile { path: $path }}; | ||
46 | } | ||
47 | |||
42 | #[derive(Debug)] | 48 | #[derive(Debug)] |
43 | pub struct Expect { | 49 | pub struct Expect { |
44 | pub position: Position, | 50 | pub position: Position, |
@@ -46,6 +52,11 @@ pub struct Expect { | |||
46 | } | 52 | } |
47 | 53 | ||
48 | #[derive(Debug)] | 54 | #[derive(Debug)] |
55 | pub struct ExpectFile { | ||
56 | pub path: &'static str, | ||
57 | } | ||
58 | |||
59 | #[derive(Debug)] | ||
49 | pub struct Position { | 60 | pub struct Position { |
50 | pub file: &'static str, | 61 | pub file: &'static str, |
51 | pub line: u32, | 62 | pub line: u32, |
@@ -64,7 +75,7 @@ impl Expect { | |||
64 | if &trimmed == actual { | 75 | if &trimmed == actual { |
65 | return; | 76 | return; |
66 | } | 77 | } |
67 | Runtime::fail(self, &trimmed, actual); | 78 | Runtime::fail_expect(self, &trimmed, actual); |
68 | } | 79 | } |
69 | pub fn assert_debug_eq(&self, actual: &impl fmt::Debug) { | 80 | pub fn assert_debug_eq(&self, actual: &impl fmt::Debug) { |
70 | let actual = format!("{:#?}\n", actual); | 81 | let actual = format!("{:#?}\n", actual); |
@@ -100,6 +111,25 @@ impl Expect { | |||
100 | } | 111 | } |
101 | } | 112 | } |
102 | 113 | ||
114 | impl ExpectFile { | ||
115 | pub fn assert_eq(&self, actual: &str) { | ||
116 | let expected = self.read(); | ||
117 | if actual == expected { | ||
118 | return; | ||
119 | } | ||
120 | Runtime::fail_file(self, &expected, actual); | ||
121 | } | ||
122 | fn read(&self) -> String { | ||
123 | fs::read_to_string(self.abs_path()).unwrap_or_default().replace("\r\n", "\n") | ||
124 | } | ||
125 | fn write(&self, contents: &str) { | ||
126 | fs::write(self.abs_path(), contents).unwrap() | ||
127 | } | ||
128 | fn abs_path(&self) -> PathBuf { | ||
129 | workspace_root().join(self.path) | ||
130 | } | ||
131 | } | ||
132 | |||
103 | #[derive(Default)] | 133 | #[derive(Default)] |
104 | struct Runtime { | 134 | struct Runtime { |
105 | help_printed: bool, | 135 | help_printed: bool, |
@@ -108,7 +138,7 @@ struct Runtime { | |||
108 | static RT: Lazy<Mutex<Runtime>> = Lazy::new(Default::default); | 138 | static RT: Lazy<Mutex<Runtime>> = Lazy::new(Default::default); |
109 | 139 | ||
110 | impl Runtime { | 140 | impl Runtime { |
111 | fn fail(expect: &Expect, expected: &str, actual: &str) { | 141 | fn fail_expect(expect: &Expect, expected: &str, actual: &str) { |
112 | let mut rt = RT.lock().unwrap_or_else(|poisoned| poisoned.into_inner()); | 142 | let mut rt = RT.lock().unwrap_or_else(|poisoned| poisoned.into_inner()); |
113 | if update_expect() { | 143 | if update_expect() { |
114 | println!("\x1b[1m\x1b[92mupdating\x1b[0m: {}", expect.position); | 144 | println!("\x1b[1m\x1b[92mupdating\x1b[0m: {}", expect.position); |
@@ -118,7 +148,21 @@ impl Runtime { | |||
118 | .update(expect, actual); | 148 | .update(expect, actual); |
119 | return; | 149 | return; |
120 | } | 150 | } |
121 | let print_help = !mem::replace(&mut rt.help_printed, true); | 151 | rt.panic(expect.position.to_string(), expected, actual); |
152 | } | ||
153 | |||
154 | fn fail_file(expect: &ExpectFile, expected: &str, actual: &str) { | ||
155 | let mut rt = RT.lock().unwrap_or_else(|poisoned| poisoned.into_inner()); | ||
156 | if update_expect() { | ||
157 | println!("\x1b[1m\x1b[92mupdating\x1b[0m: {}", expect.path); | ||
158 | expect.write(actual); | ||
159 | return; | ||
160 | } | ||
161 | rt.panic(expect.path.to_string(), expected, actual); | ||
162 | } | ||
163 | |||
164 | fn panic(&mut self, position: String, expected: &str, actual: &str) { | ||
165 | let print_help = !mem::replace(&mut self.help_printed, true); | ||
122 | let help = if print_help { HELP } else { "" }; | 166 | let help = if print_help { HELP } else { "" }; |
123 | 167 | ||
124 | let diff = Changeset::new(actual, expected, "\n"); | 168 | let diff = Changeset::new(actual, expected, "\n"); |
@@ -143,7 +187,7 @@ impl Runtime { | |||
143 | {} | 187 | {} |
144 | ---- | 188 | ---- |
145 | ", | 189 | ", |
146 | expect.position, help, expected, actual, diff | 190 | position, help, expected, actual, diff |
147 | ); | 191 | ); |
148 | // Use resume_unwind instead of panic!() to prevent a backtrace, which is unnecessary noise. | 192 | // Use resume_unwind instead of panic!() to prevent a backtrace, which is unnecessary noise. |
149 | panic::resume_unwind(Box::new(())); | 193 | panic::resume_unwind(Box::new(())); |