Skip to content

Commit

Permalink
fix unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
bridiver committed Oct 14, 2024
1 parent bec0d48 commit dbf9238
Showing 1 changed file with 31 additions and 38 deletions.
69 changes: 31 additions & 38 deletions components/json/json_parser_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <limits>
#include <string>

#include "brave/components/json/json_helper.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace brave_wallet {

TEST(JsonParser, ConvertUint64ToString) {
std::string json = "{\"a\": " + std::to_string(UINT64_MAX) + "}";
std::string json =
"{\"a\": " + std::to_string(std::numeric_limits<uint64_t>::max()) + "}";
EXPECT_EQ(json::convert_uint64_value_to_string("/a", json, false),
R"({"a":"18446744073709551615"})");

Expand All @@ -19,7 +23,7 @@ TEST(JsonParser, ConvertUint64ToString) {
EXPECT_TRUE(json::convert_uint64_value_to_string("/a", json, true).empty());

// INT64_MIN
json = "{\"a\": " + std::to_string(INT64_MIN) + "}";
json = "{\"a\": " + std::to_string(std::numeric_limits<int64_t>::min()) + "}";
EXPECT_TRUE(json::convert_uint64_value_to_string("/a", json, false).empty());
EXPECT_TRUE(json::convert_uint64_value_to_string("/a", json, true).empty());

Expand Down Expand Up @@ -84,7 +88,7 @@ TEST(JsonParser, ConvertInt64ToString) {

// INT64_MIN
json = "{\"a\": " + std::to_string(INT64_MIN) + "}";
json::convert_int64_value_to_string("/a", json, false),
EXPECT_EQ(json::convert_int64_value_to_string("/a", json, false),
R"({"a":"-9223372036854775808"})");

// INT64_MIN - 1
Expand Down Expand Up @@ -181,8 +185,7 @@ TEST(JsonParser, ConvertStringToUint64) {
R"({"a~c":{"b":1,"c":2},"d":"string"})");

json = R"({"a/d": { "b": [{"e":"1"}], "c": 2 }, "d": "string"})";
json::convert_string_value_to_uint64("/a~1d/b/0/e",
json, false)),
EXPECT_EQ(json::convert_string_value_to_uint64("/a~1d/b/0/e", json, false),
R"({"a/d":{"b":[{"e":1}],"c":2},"d":"string"})");

json = R"({"a": { "b": "1" }})";
Expand Down Expand Up @@ -232,13 +235,12 @@ TEST(JsonParser, ConvertStringToUint64) {
TEST(JsonParser, ConvertStringToInt64) {
// INT64_MIN
std::string json = R"({"a":"-9223372036854775808"})";
json::convert_string_value_to_int64("/a", json, false),
EXPECT_EQ(json::convert_string_value_to_int64("/a", json, false),
R"({"a":-9223372036854775808})");
// INT64_MIN - 1
json = R"({"a":"-9223372036854775809"})";
EXPECT_TRUE(json::convert_string_value_to_int64("/a", json, false).empty());
json::convert_string_value_to_int64("/a", json, true)
.empty());
EXPECT_TRUE(json::convert_string_value_to_int64("/a", json, true).empty());

// INT64_MAX
json = "{\"a\": \"" + std::to_string(INT64_MAX) + "\"}";
Expand All @@ -263,8 +265,7 @@ TEST(JsonParser, ConvertStringToInt64) {
R"({"a~e":{"b":1,"c":2},"d":"string"})");

json = R"({"a/e": { "b": [{"e":"1"}], "c": 2 }, "d": "string"})";
json::convert_string_value_to_int64("/a~1e/b/0/e", jso,
false)),
EXPECT_EQ(json::convert_string_value_to_int64("/a~1e/b/0/e", json, false),
R"({"a/e":{"b":[{"e":1}],"c":2},"d":"string"})");

json = R"({"a": { "b": "1" }})";
Expand Down Expand Up @@ -331,8 +332,7 @@ TEST(JsonParser, ConvertUint64InObjectArrayToString) {

json = R"({"a":[{"b":{"key":18446744073709551615}},{"b":{"key":null}}]})";
EXPECT_EQ(
json::convert_uint64_in_object_array_to_string("/a", "/b",
"key", json)),
json::convert_uint64_in_object_array_to_string("/a", "/b", "key", json),
R"({"a":[{"b":{"key":"18446744073709551615"}},{"b":{"key":null}}]})");

// Empty object array, nothing to convert.
Expand Down Expand Up @@ -360,8 +360,7 @@ TEST(JsonParser, ConvertUint64InObjectArrayToString) {

json = R"({"a":[{"b":{"key":1}},{"b":{"diff-key":1}},{"b":{"key":2}}]})";
EXPECT_EQ(
json::convert_uint64_in_object_array_to_string("/a", "/b",
"key", json)),
json::convert_uint64_in_object_array_to_string("/a", "/b", "key", json),
R"({"a":[{"b":{"key":"1"}},{"b":{"diff-key":1}},{"b":{"key":"2"}}]})");

std::vector<std::string> invalid_cases = {
Expand Down Expand Up @@ -402,16 +401,14 @@ TEST(JsonParser, ConvertUint64InObjectArrayToString) {
json =
R"({"a":[{"b":{"key":18446744073709551615}},{"b":{"key":2}},{"b":{"key":3}}]})";
EXPECT_EQ(
json::convert_uint64_in_object_array_to_string("/a", "/b",
"key", json)),
json::convert_uint64_in_object_array_to_string("/a", "/b", "key", json),
R"({"a":[{"b":{"key":"18446744073709551615"}},{"b":{"key":"2"}},{"b":{"key":"3"}}]})");

// Object array where key is nested deeper
json =
R"({"a":[{"b":{"c":{"key":18446744073709551615}}},{"b":{"c":{"key":2}}}]})";
EXPECT_EQ(
json::convert_uint64_in_object_array_to_string("/a", "/b/c",
"key", json)),
json::convert_uint64_in_object_array_to_string("/a", "/b/c", "key", json),
R"({"a":[{"b":{"c":{"key":"18446744073709551615"}}},{"b":{"c":{"key":"2"}}}]})");
}

Expand All @@ -425,7 +422,7 @@ TEST(JsonParser, ConvertAllNumbersToString) {

// OK: convert deeply nested value to string
json = R"({"some":[{"deeply":{"nested":[{"path":123}]}}]})";
json::convert_all_numbers_to_string(json, ""),
EXPECT_EQ(json::convert_all_numbers_to_string(json, ""),
R"({"some":[{"deeply":{"nested":[{"path":"123"}]}}]})");

// OK: values other than u64/f64/i64 are unchanged
Expand All @@ -436,29 +433,28 @@ TEST(JsonParser, ConvertAllNumbersToString) {

// OK: empty object array, nothing to convert
json = R"({"a":[]})";
json::convert_all_numbers_to_string(json, "")), json;
EXPECT_EQ(json::convert_all_numbers_to_string(json, ""), json);

// OK: empty array json, nothing to convert
json = R"([])";
json::convert_all_numbers_to_string(json, "")), json;
EXPECT_EQ(json::convert_all_numbers_to_string(json, ""), json);

// OK: floating point values in scientific notation are unchanged
json = R"({"a": 1.196568750220778e-7})";
json::convert_all_numbers_to_string(json, ""),
EXPECT_EQ(json::convert_all_numbers_to_string(json, ""),
R"({"a":"1.196568750220778e-7"})");

// OK: convert under specified JSON path only
json = R"({"a":1,"outer":{"inner": 2}})";
json::convert_all_numbers_to_string(json, "/outer"),
EXPECT_EQ(json::convert_all_numbers_to_string(json, "/outer"),
R"({"a":1,"outer":{"inner":"2"}})");
json::convert_all_numbers_to_string(json, "/a"),
EXPECT_EQ(json::convert_all_numbers_to_string(json, "/a"),
R"({"a":"1","outer":{"inner":2}})");

// KO: invalid path has no effect on the JSON
json = R"({"a":1,"outer":{"inner":2}})";
json::convert_all_numbers_to_string(json, "/invalid"),
json);
json::convert_all_numbers_to_string(json, "/")), json;
EXPECT_EQ(json::convert_all_numbers_to_string(json, "/invalid"), json);
EXPECT_EQ(json::convert_all_numbers_to_string(json, "/"), json);

// KO: invalid cases
std::vector<std::string> invalid_cases = {
Expand All @@ -473,7 +469,7 @@ TEST(JsonParser, ConvertAllNumbersToString) {
// DBL_MAX
R"("{a":[{"key":)" + std::to_string(DBL_MAX + 1) + "}]}"};
for (const auto& invalid_case : invalid_cases) {
EXPECT_EQ(json::convert_all_numbers_to_string(invalid_case, ""))
EXPECT_EQ("", json::convert_all_numbers_to_string(invalid_case, ""))
<< invalid_case;
}
}
Expand Down Expand Up @@ -504,20 +500,17 @@ TEST(JsonParser, ConvertAllNumbersToStringAndRemoveNullValues) {
// OK: convert under specified JSON path only
json = R"({"a":1,"outer":{"inner": 2, "nullprop1": null}, "nulprop2": null})";

EXPECT_EQ(
json::convert_all_numbers_to_string_and_remove_null_value(
json, "/outer")),
R"({"a":1,"nulprop2":null,"outer":{"inner":"2"}})");
EXPECT_EQ(json::convert_all_numbers_to_string_and_remove_null_values(
json, "/outer"),
R"({"a":1,"nulprop2":null,"outer":{"inner":"2"}})");

// OK: invalid path has no effect on the JSON
json = R"({"a":1,"outer":{"inner":2}})";
EXPECT_EQ(json::convert_all_numbers_to_string_and_remove_null_values(
json, "/invalid"),
json);
EXPECT_EQ(
json::convert_all_numbers_to_string_and_remove_null_value(
json, "/invalid")),
json);
EXPECT_EQ(
json::convert_all_numbers_to_string_and_remove_null_value(
json, "/")),
json::convert_all_numbers_to_string_and_remove_null_values(json, "/"),
json);
}

Expand Down

0 comments on commit dbf9238

Please sign in to comment.