test: Add STInteger and STParsedJSON tests (#5726)

This change is to improve code coverage (and to simplify #5720 and #5725); there is otherwise no change in functionality. The change adds basic tests for `STInteger` and `STParsedJSON`, so it becomes easier to test smaller changes to the types, as well as removes `STParsedJSONArray`, since it is not used anywhere (including in Clio).
This commit is contained in:
Mayukha Vadari
2025-09-22 14:00:31 -04:00
committed by GitHub
parent ffeabc9642
commit 6b8a589447
7 changed files with 2162 additions and 131 deletions

View File

@@ -83,7 +83,8 @@ constexpr std::
return static_cast<U1>(value);
}
static std::string
// LCOV_EXCL_START
static inline std::string
make_name(std::string const& object, std::string const& field)
{
if (field.empty())
@@ -92,7 +93,7 @@ make_name(std::string const& object, std::string const& field)
return object + "." + field;
}
static Json::Value
static inline Json::Value
not_an_object(std::string const& object, std::string const& field)
{
return RPC::make_error(
@@ -100,20 +101,20 @@ not_an_object(std::string const& object, std::string const& field)
"Field '" + make_name(object, field) + "' is not a JSON object.");
}
static Json::Value
static inline Json::Value
not_an_object(std::string const& object)
{
return not_an_object(object, "");
}
static Json::Value
static inline Json::Value
not_an_array(std::string const& object)
{
return RPC::make_error(
rpcINVALID_PARAMS, "Field '" + object + "' is not a JSON array.");
}
static Json::Value
static inline Json::Value
unknown_field(std::string const& object, std::string const& field)
{
return RPC::make_error(
@@ -121,7 +122,7 @@ unknown_field(std::string const& object, std::string const& field)
"Field '" + make_name(object, field) + "' is unknown.");
}
static Json::Value
static inline Json::Value
out_of_range(std::string const& object, std::string const& field)
{
return RPC::make_error(
@@ -129,7 +130,7 @@ out_of_range(std::string const& object, std::string const& field)
"Field '" + make_name(object, field) + "' is out of range.");
}
static Json::Value
static inline Json::Value
bad_type(std::string const& object, std::string const& field)
{
return RPC::make_error(
@@ -137,7 +138,7 @@ bad_type(std::string const& object, std::string const& field)
"Field '" + make_name(object, field) + "' has bad type.");
}
static Json::Value
static inline Json::Value
invalid_data(std::string const& object, std::string const& field)
{
return RPC::make_error(
@@ -145,13 +146,13 @@ invalid_data(std::string const& object, std::string const& field)
"Field '" + make_name(object, field) + "' has invalid data.");
}
static Json::Value
static inline Json::Value
invalid_data(std::string const& object)
{
return invalid_data(object, "");
}
static Json::Value
static inline Json::Value
array_expected(std::string const& object, std::string const& field)
{
return RPC::make_error(
@@ -159,7 +160,7 @@ array_expected(std::string const& object, std::string const& field)
"Field '" + make_name(object, field) + "' must be a JSON array.");
}
static Json::Value
static inline Json::Value
string_expected(std::string const& object, std::string const& field)
{
return RPC::make_error(
@@ -167,7 +168,7 @@ string_expected(std::string const& object, std::string const& field)
"Field '" + make_name(object, field) + "' must be a string.");
}
static Json::Value
static inline Json::Value
too_deep(std::string const& object)
{
return RPC::make_error(
@@ -175,7 +176,7 @@ too_deep(std::string const& object)
"Field '" + object + "' exceeds nesting depth limit.");
}
static Json::Value
static inline Json::Value
singleton_expected(std::string const& object, unsigned int index)
{
return RPC::make_error(
@@ -184,7 +185,7 @@ singleton_expected(std::string const& object, unsigned int index)
"]' must be an object with a single key/object value.");
}
static Json::Value
static inline Json::Value
template_mismatch(SField const& sField)
{
return RPC::make_error(
@@ -193,7 +194,7 @@ template_mismatch(SField const& sField)
"' contents did not meet requirements for that type.");
}
static Json::Value
static inline Json::Value
non_object_in_array(std::string const& item, Json::UInt index)
{
return RPC::make_error(
@@ -201,6 +202,7 @@ non_object_in_array(std::string const& item, Json::UInt index)
"Item '" + item + "' at index " + std::to_string(index) +
" is not an object. Arrays may only contain objects.");
}
// LCOV_EXCL_STOP
template <class STResult, class Integer>
static std::optional<detail::STVar>
@@ -385,10 +387,13 @@ parseLeaf(
auto const& field = SField::getField(fieldName);
// checked in parseObject
if (field == sfInvalid)
{
// LCOV_EXCL_START
error = unknown_field(json_name, fieldName);
return ret;
// LCOV_EXCL_STOP
}
switch (field.fieldType)
@@ -760,6 +765,12 @@ parseLeaf(
AccountID uAccount, uIssuer;
Currency uCurrency;
if (!account && !currency && !issuer)
{
error = invalid_data(element_name);
return ret;
}
if (account)
{
// human account id
@@ -1153,24 +1164,4 @@ STParsedJSONObject::STParsedJSONObject(
object = parseObject(name, json, sfGeneric, 0, error);
}
//------------------------------------------------------------------------------
STParsedJSONArray::STParsedJSONArray(
std::string const& name,
Json::Value const& json)
{
using namespace STParsedJSONDetail;
auto arr = parseArray(name, json, sfGeneric, 0, error);
if (!arr)
array.reset();
else
{
auto p = dynamic_cast<STArray*>(&arr->get());
if (p == nullptr)
array.reset();
else
array = std::move(*p);
}
}
} // namespace ripple