Skip to content

Commit 3873330

Browse files
authored
Adds expect_file function mirroring expect_function! macro.
ref #15
1 parent 21f04f3 commit 3873330

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

src/lib.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,25 @@ macro_rules! expect_file {
204204
}};
205205
}
206206

207+
/// Creates an instance of `ExpectFile` from relative or absolute path:
208+
///
209+
/// ```
210+
/// # use expect_test::expect_file;
211+
/// expect_file("./test_data/bar.html");
212+
/// ```
213+
///
214+
/// This uses the `#[track_caller]` attribute to resolve relative file paths.
215+
/// They [won't work correctly][1] if this is called from a function which also uses `#[track_caller]`,
216+
/// or if the toolchain is [configured to strip caller information][2].
217+
/// In these cases you must use the macro form, [`expect_file!`].
218+
///
219+
/// [1]: https://github.com/rust-analyzer/expect-test/issues/15#issuecomment-939308821
220+
/// [2]: https://doc.rust-lang.org/beta/unstable-book/compiler-flags/location-detail.html
221+
#[track_caller]
222+
pub fn expect_file(path: impl Into<std::path::PathBuf>) -> ExpectFile {
223+
ExpectFile { path: path.into(), position: std::panic::Location::caller().file() }
224+
}
225+
207226
/// Self-updating string literal.
208227
#[derive(Debug)]
209228
pub struct Expect {
@@ -795,6 +814,11 @@ mod tests {
795814
expect_file!["./lib.rs"].assert_eq(include_str!("./lib.rs"))
796815
}
797816

817+
#[test]
818+
fn test_expect_file_fn() {
819+
expect_file("./lib.rs").assert_eq(include_str!("./lib.rs"))
820+
}
821+
798822
#[test]
799823
fn smoke_test_indent() {
800824
fn check_indented(input: &str, mut expect: Expect) {

0 commit comments

Comments
 (0)