Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow users to rebind expect! #26

Merged
merged 3 commits into from
Jan 3, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 31 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,30 @@
//! 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
//!
//! `expect!` returns an instance of `Expect` struct, which holds position
//! information and a string literal. Use `Expect::assert_eq` for string
//! comparison. Use `Expect::assert_debug_eq` for verbose debug comparison. Note
//! that leading indentation is automatically removed.
//! information and a string literal. Importing `expect!` under a different
//! name is supported; `UPDATE_EXPECT` will continue to work.
//!
//! Use `Expect::assert_eq` for string comparison. Use
//! `Expect::assert_debug_eq` for verbose debug comparison. Note that leading
//! indentation is automatically removed.
//!
//! ```
//! use expect_test::expect;
//! use expect_test::expect as ex; // Note the renaming
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not add this to the docs: this is a niche thing you shouldn't generally be doing. Documenting this helps for few people that do need this, but harms everyone else. Docs are like code -- the fewer lines you spend, the better!

//!
//! #[derive(Debug)]
//! struct Foo {
//! value: i32,
//! }
//!
//! let actual = Foo { value: 92 };
//! let expected = expect![["
//! let expected = ex![["
//! Foo {
//! value: 92,
//! }
Expand Down Expand Up @@ -141,6 +144,7 @@
//! bump.
use std::{
collections::HashMap,
convert::TryInto,
env, fmt, fs, mem,
ops::Range,
panic,
Expand Down Expand Up @@ -268,9 +272,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