Skip to content

Commit

Permalink
[FOLD] Minor refactor in unit-test utils
Browse files Browse the repository at this point in the history
  • Loading branch information
gregtatcam committed Oct 26, 2023
1 parent 51af941 commit 38a0eaa
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
15 changes: 1 addition & 14 deletions src/test/app/Oracle_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,6 @@ namespace test {
struct Oracle_test : public beast::unit_test::suite
{
private:
static auto
ledgerEntryOracle(
jtx::Env& env,
AccountID const& account,
std::uint32_t const& sequence)
{
Json::Value jvParams;
jvParams[jss::oracle][jss::account] = to_string(account);
jvParams[jss::oracle][jss::oracle_sequence] = sequence;
return env.rpc(
"json", "ledger_entry", to_string(jvParams))[jss::result];
}

void
testInvalidSet()
{
Expand Down Expand Up @@ -403,7 +390,7 @@ struct Oracle_test : public beast::unit_test::suite
}
for (int i = 0; i < accounts.size(); ++i)
{
auto const jv = ledgerEntryOracle(env, accounts[i], oracles[i]);
auto const jv = Oracle::ledgerEntry(env, accounts[i], oracles[i]);
try
{
BEAST_EXPECT(
Expand Down
15 changes: 14 additions & 1 deletion src/test/jtx/Oracle.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ using DataSeries = std::vector<std::tuple<
std::optional<std::uint32_t>,
std::optional<std::uint8_t>>>;

// Typical defaults for Create
struct CreateArg
{
std::optional<AccountID> owner = std::nullopt;
Expand All @@ -49,6 +50,7 @@ struct CreateArg
std::optional<ter> ter = std::nullopt;
};

// Typical defaults for Update
struct UpdateArg
{
std::optional<AccountID> owner = std::nullopt;
Expand Down Expand Up @@ -82,6 +84,7 @@ struct RemoveArg
class Oracle
{
private:
// Global fee if not 0
static inline std::uint32_t fee = 0;
Env& env_;
AccountID owner_;
Expand Down Expand Up @@ -134,8 +137,18 @@ class Oracle
bool
expectPrice(DataSeries const& pricess) const;

static Json::Value
ledgerEntry(
Env& env,
AccountID const& account,
std::uint32_t sequence,
std::optional<std::string> const& index = std::nullopt);

Json::Value
ledgerEntry(std::optional<std::string> const& index = std::nullopt) const;
ledgerEntry(std::optional<std::string> const& index = std::nullopt) const
{
return Oracle::ledgerEntry(env_, owner_, sequence_, index);
}

static void
setFee(std::uint32_t f)
Expand Down
18 changes: 10 additions & 8 deletions src/test/jtx/impl/Oracle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@
*/
//==============================================================================

#include <ripple/basics/safe_cast.h>
#include <ripple/protocol/digest.h>
#include <ripple/protocol/jss.h>
#include <test/jtx/Oracle.h>

#include <boost/lexical_cast/try_lexical_convert.hpp>

#include <format>
#include <vector>

namespace ripple {
Expand Down Expand Up @@ -99,8 +97,8 @@ Oracle::expectPrice(DataSeries const& series) const
{
if (auto const sle = env_.le(keylet::oracle(owner_, sequence_)))
{
auto const leSeries = sle->getFieldArray(sfPriceDataSeries);
if (leSeries.size() != series.size())
auto const& leSeries = sle->getFieldArray(sfPriceDataSeries);
if (leSeries.size() == 0 || leSeries.size() != series.size())
return false;
for (auto const& data : series)
{
Expand Down Expand Up @@ -234,11 +232,15 @@ Oracle::set(CreateArg const& arg)
}

Json::Value
Oracle::ledgerEntry(std::optional<std::string> const& index) const
Oracle::ledgerEntry(
Env& env,
AccountID const& account,
std::uint32_t sequence,
std::optional<std::string> const& index)
{
Json::Value jvParams;
jvParams[jss::oracle][jss::account] = to_string(owner_);
jvParams[jss::oracle][jss::oracle_sequence] = sequence_;
jvParams[jss::oracle][jss::account] = to_string(account);
jvParams[jss::oracle][jss::oracle_sequence] = sequence;
if (index)
{
std::uint32_t i;
Expand All @@ -247,7 +249,7 @@ Oracle::ledgerEntry(std::optional<std::string> const& index) const
else
jvParams[jss::oracle][jss::ledger_index] = *index;
}
return env_.rpc("json", "ledger_entry", to_string(jvParams))[jss::result];
return env.rpc("json", "ledger_entry", to_string(jvParams))[jss::result];
}

} // namespace jtx
Expand Down

0 comments on commit 38a0eaa

Please sign in to comment.