-
Notifications
You must be signed in to change notification settings - Fork 1.6k
feat(test): add file/line to Env #6276
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -32,17 +32,46 @@ | |||||||||||||||||||||
| #include <xrpl/protocol/STTx.h> | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| #include <functional> | ||||||||||||||||||||||
| #include <source_location> | ||||||||||||||||||||||
| #include <string> | ||||||||||||||||||||||
| #include <tuple> | ||||||||||||||||||||||
| #include <type_traits> | ||||||||||||||||||||||
| #include <unordered_map> | ||||||||||||||||||||||
| #include <utility> | ||||||||||||||||||||||
| #include <variant> | ||||||||||||||||||||||
| #include <vector> | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| namespace xrpl { | ||||||||||||||||||||||
| namespace test { | ||||||||||||||||||||||
| namespace jtx { | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| class Env; // Forward declaration | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /** Wrapper that captures source_location when implicitly constructed. | ||||||||||||||||||||||
| This solves the problem of combining std::source_location with variadic | ||||||||||||||||||||||
| templates. The source_location default argument is evaluated at the | ||||||||||||||||||||||
| call site when the wrapper is constructed via implicit conversion. | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| This is a non-template struct that uses std::variant to hold either | ||||||||||||||||||||||
| Json::Value or JTx, allowing implicit conversion without template | ||||||||||||||||||||||
| argument deduction issues. | ||||||||||||||||||||||
| */ | ||||||||||||||||||||||
| struct WithSourceLoc | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| std::variant<Json::Value, JTx> value; | ||||||||||||||||||||||
| std::source_location loc; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // Non-explicit constructors allow implicit conversion. | ||||||||||||||||||||||
| // The default argument for loc is evaluated at the call site. | ||||||||||||||||||||||
| WithSourceLoc(Json::Value v, std::source_location l = std::source_location::current()) : value(std::move(v)), loc(l) | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| WithSourceLoc(JTx v, std::source_location l = std::source_location::current()) : value(std::move(v)), loc(l) | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
Comment on lines
+66
to
+72
|
||||||||||||||||||||||
| WithSourceLoc(Json::Value v, std::source_location l = std::source_location::current()) : value(std::move(v)), loc(l) | |
| { | |
| } | |
| WithSourceLoc(JTx v, std::source_location l = std::source_location::current()) : value(std::move(v)), loc(l) | |
| { | |
| } | |
| WithSourceLoc(Json::Value v, std::source_location l = std::source_location::current()) : value(std::move(v)), loc(l) {} | |
| WithSourceLoc(JTx v, std::source_location l = std::source_location::current()) : value(std::move(v)), loc(l) {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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); | ||
mvadari marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| 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); | ||
mvadari marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| 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) + ")" : ""; | ||
| auto const file = std::string("(") + loc.file_name() + ":" + to_string(loc.line()) + ")"; | ||
mvadari marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| bool bad = !test.expect(parsed.ter, "apply: 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 " + file + ": 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 " + file + ": 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)) || | ||
|
|
@@ -424,7 +426,7 @@ Env::postconditions(JTx const& jt, ParsedResult const& parsed, Json::Value const | |
| (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 " + | ||
| "apply " + file + ": Got RPC result "s + parsed.rpcError + " (" + parsed.rpcException + "); Expected " + | ||
| jt.rpcException->first + " (" + jt.rpcException->second.value_or("n/a") + ")" + line)) || | ||
| bad; | ||
| if (bad) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
<variant>header is included but the standard library already provides all necessary functionality. Consider documenting whystd::variantis required here, as it adds complexity to theWithSourceLocimplementation. A simpler approach might be to use two separate wrapper types or template the wrapper class.