test: Add file and line location to Env (#6276)

This change uses `std::source_location` to output the file and line location of the call that triggered a failed transaction.
This commit is contained in:
Mayukha Vadari
2026-02-06 13:37:01 -05:00
committed by GitHub
parent 2305bc98a4
commit f5208fc850
5 changed files with 88 additions and 34 deletions

View File

@@ -27,6 +27,7 @@
#include <xrpl/protocol/jss.h>
#include <memory>
#include <source_location>
namespace xrpl {
namespace test {
@@ -330,7 +331,7 @@ Env::parseResult(Json::Value const& jr)
}
void
Env::submit(JTx const& jt)
Env::submit(JTx const& jt, std::source_location const& loc)
{
ParsedResult parsedResult;
auto const jr = [&]() {
@@ -356,11 +357,11 @@ Env::submit(JTx const& jt)
return Json::Value();
}
}();
return postconditions(jt, parsedResult, jr);
return postconditions(jt, parsedResult, jr, loc);
}
void
Env::sign_and_submit(JTx const& jt, Json::Value params)
Env::sign_and_submit(JTx const& jt, Json::Value params, std::source_location const& loc)
{
auto const account = lookup(jt.jv[jss::Account].asString());
auto const& passphrase = account.name();
@@ -393,25 +394,26 @@ Env::sign_and_submit(JTx const& jt, Json::Value params)
test.expect(parsedResult.ter, "ter uninitialized!");
ter_ = parsedResult.ter.value_or(telENV_RPC_FAILED);
return postconditions(jt, parsedResult, jr);
return postconditions(jt, parsedResult, jr, loc);
}
void
Env::postconditions(JTx const& jt, ParsedResult const& parsed, Json::Value const& jr)
Env::postconditions(JTx const& jt, ParsedResult const& parsed, Json::Value const& jr, std::source_location const& loc)
{
auto const line = jt.testLine ? " (" + to_string(*jt.testLine) + ")" : "";
bool bad = !test.expect(parsed.ter, "apply: No ter result!" + line);
auto const locStr = std::string("(") + loc.file_name() + ":" + to_string(loc.line()) + ")";
bool bad = !test.expect(parsed.ter, "apply " + locStr + ": No ter result!" + line);
bad =
(jt.ter && parsed.ter &&
!test.expect(
*parsed.ter == *jt.ter,
"apply: Got " + transToken(*parsed.ter) + " (" + transHuman(*parsed.ter) + "); Expected " +
"apply " + locStr + ": Got " + transToken(*parsed.ter) + " (" + transHuman(*parsed.ter) + "); Expected " +
transToken(*jt.ter) + " (" + transHuman(*jt.ter) + ")" + line));
using namespace std::string_literals;
bad = (jt.rpcCode &&
!test.expect(
parsed.rpcCode == jt.rpcCode->first && parsed.rpcMessage == jt.rpcCode->second,
"apply: Got RPC result "s +
"apply " + locStr + ": Got RPC result "s +
(parsed.rpcCode ? RPC::get_error_info(*parsed.rpcCode).token.c_str() : "NO RESULT") + " (" +
parsed.rpcMessage + "); Expected " + RPC::get_error_info(jt.rpcCode->first).token.c_str() + " (" +
jt.rpcCode->second + ")" + line)) ||
@@ -419,13 +421,14 @@ Env::postconditions(JTx const& jt, ParsedResult const& parsed, Json::Value const
// If we have an rpcCode (just checked), then the rpcException check is
// optional - the 'error' field may not be defined, but if it is, it must
// match rpcError.
bad = (jt.rpcException &&
!test.expect(
(jt.rpcCode && parsed.rpcError.empty()) ||
(parsed.rpcError == jt.rpcException->first &&
(!jt.rpcException->second || parsed.rpcException == *jt.rpcException->second)),
"apply: Got RPC result "s + parsed.rpcError + " (" + parsed.rpcException + "); Expected " +
jt.rpcException->first + " (" + jt.rpcException->second.value_or("n/a") + ")" + line)) ||
bad =
(jt.rpcException &&
!test.expect(
(jt.rpcCode && parsed.rpcError.empty()) ||
(parsed.rpcError == jt.rpcException->first &&
(!jt.rpcException->second || parsed.rpcException == *jt.rpcException->second)),
"apply " + locStr + ": Got RPC result "s + parsed.rpcError + " (" + parsed.rpcException + "); Expected " +
jt.rpcException->first + " (" + jt.rpcException->second.value_or("n/a") + ")" + line)) ||
bad;
if (bad)
{