Skip to content

Commit

Permalink
Merge #26
Browse files Browse the repository at this point in the history
26: Allow users to rebind `expect!` r=matklad a=ecstatic-morse

Currently, `expect-test` checks for the string `expect![[` when updating test cases. As a result, setting `UPDATE_EXPECT=1` will cause a panic if `expect` is imported under a different name.

We have the `column` of the macro invocation for each `Expect`. Use that as a starting point instead.

Co-authored-by: Dylan MacKenzie <ecstaticmorse@gmail.com>
  • Loading branch information
bors[bot] and ecstatic-morse authored Jan 3, 2022
2 parents 4a5d796 + b727dae commit 8ad37b7
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
//! This becomes very useful when you have a lot of tests with verbose and
//! potentially changing expected output.
//!
//! Under the hood, `expect!` macro uses `file!` and `line!` to record source
//! position at compile time. At runtime, this position is used to patch the
//! file in-place, if `UPDATE_EXPECT` is set.
//! Under the hood, the `expect!` macro uses `file!`, `line!` and `column!` to
//! record source position at compile time. At runtime, this position is used
//! to patch the file in-place, if `UPDATE_EXPECT` is set.
//!
//! # Guide
//!
Expand Down Expand Up @@ -141,6 +141,7 @@
//! bump.
use std::{
collections::HashMap,
convert::TryInto,
env, fmt, fs, mem,
ops::Range,
panic,
Expand Down Expand Up @@ -268,9 +269,25 @@ impl Expect {
let mut line_start = 0;
for (i, line) in lines_with_ends(file).enumerate() {
if i == self.position.line as usize - 1 {
let pat = "expect![[";
let offset = line.find(pat).unwrap();
let literal_start = line_start + offset + pat.len();
// `column` points to the first character of the macro invocation:
//
// expect![[ ... ]]
// ^
//
// Seek past the exclam, then skip any whitespace and
// delimiters to get to our argument.
let byte_offset = line
.char_indices()
.skip((self.position.column - 1).try_into().unwrap())
.skip_while(|&(_, c)| c != '!')
.skip(1)
.skip_while(|&(_, c)| c.is_whitespace())
.skip_while(|&(_, c)| matches!(c, '[' | '(' | '{'))
.next()
.expect("Failed to parse macro invocation")
.0;

let literal_start = line_start + byte_offset;
let indent = line.chars().take_while(|&it| it == ' ').count();
target_line = Some((literal_start, indent));
break;
Expand Down

0 comments on commit 8ad37b7

Please sign in to comment.