diff options
-rw-r--r-- | crates/expect/src/lib.rs | 36 |
1 files changed, 23 insertions, 13 deletions
diff --git a/crates/expect/src/lib.rs b/crates/expect/src/lib.rs index d678b1817..3a92b36e2 100644 --- a/crates/expect/src/lib.rs +++ b/crates/expect/src/lib.rs | |||
@@ -29,9 +29,11 @@ fn update_expect() -> bool { | |||
29 | #[macro_export] | 29 | #[macro_export] |
30 | macro_rules! expect { | 30 | macro_rules! expect { |
31 | [[$lit:literal]] => {$crate::Expect { | 31 | [[$lit:literal]] => {$crate::Expect { |
32 | file: file!(), | 32 | position: $crate::Position { |
33 | line: line!(), | 33 | file: file!(), |
34 | column: column!(), | 34 | line: line!(), |
35 | column: column!(), | ||
36 | }, | ||
35 | data: $lit, | 37 | data: $lit, |
36 | }}; | 38 | }}; |
37 | [[]] => { $crate::expect![[""]] }; | 39 | [[]] => { $crate::expect![[""]] }; |
@@ -39,10 +41,21 @@ macro_rules! expect { | |||
39 | 41 | ||
40 | #[derive(Debug)] | 42 | #[derive(Debug)] |
41 | pub struct Expect { | 43 | pub struct Expect { |
44 | pub position: Position, | ||
45 | pub data: &'static str, | ||
46 | } | ||
47 | |||
48 | #[derive(Debug)] | ||
49 | pub struct Position { | ||
42 | pub file: &'static str, | 50 | pub file: &'static str, |
43 | pub line: u32, | 51 | pub line: u32, |
44 | pub column: u32, | 52 | pub column: u32, |
45 | pub data: &'static str, | 53 | } |
54 | |||
55 | impl fmt::Display for Position { | ||
56 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
57 | write!(f, "{}:{}:{}", self.file, self.line, self.column) | ||
58 | } | ||
46 | } | 59 | } |
47 | 60 | ||
48 | impl Expect { | 61 | impl Expect { |
@@ -69,7 +82,7 @@ impl Expect { | |||
69 | let mut target_line = None; | 82 | let mut target_line = None; |
70 | let mut line_start = 0; | 83 | let mut line_start = 0; |
71 | for (i, line) in lines_with_ends(file).enumerate() { | 84 | for (i, line) in lines_with_ends(file).enumerate() { |
72 | if i == self.line as usize - 1 { | 85 | if i == self.position.line as usize - 1 { |
73 | let pat = "expect![["; | 86 | let pat = "expect![["; |
74 | let offset = line.find(pat).unwrap(); | 87 | let offset = line.find(pat).unwrap(); |
75 | let literal_start = line_start + offset + pat.len(); | 88 | let literal_start = line_start + offset + pat.len(); |
@@ -98,12 +111,9 @@ impl Runtime { | |||
98 | fn fail(expect: &Expect, expected: &str, actual: &str) { | 111 | fn fail(expect: &Expect, expected: &str, actual: &str) { |
99 | let mut rt = RT.lock().unwrap_or_else(|poisoned| poisoned.into_inner()); | 112 | let mut rt = RT.lock().unwrap_or_else(|poisoned| poisoned.into_inner()); |
100 | if update_expect() { | 113 | if update_expect() { |
101 | println!( | 114 | println!("\x1b[1m\x1b[92mupdating\x1b[0m: {}", expect.position); |
102 | "\x1b[1m\x1b[92mupdating\x1b[0m: {}:{}:{}", | ||
103 | expect.file, expect.line, expect.column | ||
104 | ); | ||
105 | rt.per_file | 115 | rt.per_file |
106 | .entry(expect.file) | 116 | .entry(expect.position.file) |
107 | .or_insert_with(|| FileRuntime::new(expect)) | 117 | .or_insert_with(|| FileRuntime::new(expect)) |
108 | .update(expect, actual); | 118 | .update(expect, actual); |
109 | return; | 119 | return; |
@@ -116,7 +126,7 @@ impl Runtime { | |||
116 | println!( | 126 | println!( |
117 | "\n | 127 | "\n |
118 | \x1b[1m\x1b[91merror\x1b[97m: expect test failed\x1b[0m | 128 | \x1b[1m\x1b[91merror\x1b[97m: expect test failed\x1b[0m |
119 | \x1b[1m\x1b[34m-->\x1b[0m {}:{}:{} | 129 | \x1b[1m\x1b[34m-->\x1b[0m {} |
120 | {} | 130 | {} |
121 | \x1b[1mExpect\x1b[0m: | 131 | \x1b[1mExpect\x1b[0m: |
122 | ---- | 132 | ---- |
@@ -133,7 +143,7 @@ impl Runtime { | |||
133 | {} | 143 | {} |
134 | ---- | 144 | ---- |
135 | ", | 145 | ", |
136 | expect.file, expect.line, expect.column, help, expected, actual, diff | 146 | expect.position, help, expected, actual, diff |
137 | ); | 147 | ); |
138 | // Use resume_unwind instead of panic!() to prevent a backtrace, which is unnecessary noise. | 148 | // Use resume_unwind instead of panic!() to prevent a backtrace, which is unnecessary noise. |
139 | panic::resume_unwind(Box::new(())); | 149 | panic::resume_unwind(Box::new(())); |
@@ -148,7 +158,7 @@ struct FileRuntime { | |||
148 | 158 | ||
149 | impl FileRuntime { | 159 | impl FileRuntime { |
150 | fn new(expect: &Expect) -> FileRuntime { | 160 | fn new(expect: &Expect) -> FileRuntime { |
151 | let path = workspace_root().join(expect.file); | 161 | let path = workspace_root().join(expect.position.file); |
152 | let original_text = fs::read_to_string(&path).unwrap(); | 162 | let original_text = fs::read_to_string(&path).unwrap(); |
153 | let patchwork = Patchwork::new(original_text.clone()); | 163 | let patchwork = Patchwork::new(original_text.clone()); |
154 | FileRuntime { path, original_text, patchwork } | 164 | FileRuntime { path, original_text, patchwork } |