Skip to content

Commit

Permalink
Dynamically obtain line number of panic! expression
Browse files Browse the repository at this point in the history
  • Loading branch information
Bromeon committed Nov 5, 2024
1 parent 2ac8d82 commit bb1c53f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
5 changes: 4 additions & 1 deletion itest/rust/src/object_tests/dynamic_call_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,14 @@ fn dynamic_call_with_panic() {
#[cfg(not(target_os = "windows"))]
let path = "itest/rust/src/object_tests/object_test.rs";

// Obtain line number dynamically, avoids tedious maintenance on code reorganization.
let line = ObjPayload::get_do_panic_line();

let expected_error_message = format!(
"godot-rust function call failed: Object::call(&\"do_panic\")\
\n Source: ObjPayload::do_panic()\
\n Reason: [panic] do_panic exploded\
\n at {path}:893"
\n at {path}:{line}"
);

assert_eq!(call_error.to_string(), expected_error_message);
Expand Down
5 changes: 5 additions & 0 deletions itest/rust/src/object_tests/object_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,11 @@ impl ObjPayload {
fn do_panic(&self) {
panic!("do_panic exploded");
}

// Obtain the line number of the panic!() call above; keep equidistant to do_panic() method.
pub fn get_do_panic_line() -> u32 {
line!() - 5
}
}

// ----------------------------------------------------------------------------------------------------------------------------------------------
Expand Down

0 comments on commit bb1c53f

Please sign in to comment.