Add test for get_json_object_*_field.
This commit is contained in:
parent
6d05e8af6e
commit
44d4fcfa07
@ -706,7 +706,7 @@ Result<string> get_json_object_string_field(const JsonObject &object, Slice name
|
||||
}
|
||||
}
|
||||
if (is_optional) {
|
||||
return default_value;
|
||||
return std::move(default_value);
|
||||
}
|
||||
return Status::Error(400, PSLICE() << "Can't find field \"" << name << '"');
|
||||
}
|
||||
|
@ -89,3 +89,129 @@ TEST(JSON, kphp) {
|
||||
"{\"keyboard\":[[\"\\u2022 abcdefg\"],[\"\\u2022 hijklmnop\"],[\"\\u2022 "
|
||||
"qrstuvwxyz\"]],\"one_time_keyboard\":true}");
|
||||
}
|
||||
|
||||
TEST(JSON, get_json_object_field) {
|
||||
const td::string encoded_object =
|
||||
"{\"null\":null,\"bool\":true,\"int\":\"1\",\"int2\":2,\"long\":\"123456789012\",\"long2\":2123456789012,"
|
||||
"\"double\":12345678901.1,\"string\":\"string\",\"string2\":12345e+1,\"array\":[],\"object\":{}}";
|
||||
{
|
||||
td::string encoded_object_copy = encoded_object;
|
||||
auto value = td::json_decode(encoded_object_copy).move_as_ok();
|
||||
auto &object = value.get_object();
|
||||
ASSERT_EQ(td::json_encode<td::string>(td::get_json_object_field_force(object, "null")), "null");
|
||||
ASSERT_EQ(td::json_encode<td::string>(td::get_json_object_field_force(object, "bool")), "true");
|
||||
ASSERT_EQ(td::json_encode<td::string>(td::get_json_object_field_force(object, "bool")), "null");
|
||||
ASSERT_EQ(td::json_encode<td::string>(td::get_json_object_field_force(object, "int")), "\"1\"");
|
||||
ASSERT_EQ(td::json_encode<td::string>(td::get_json_object_field_force(object, "int2")), "2");
|
||||
ASSERT_EQ(td::json_encode<td::string>(td::get_json_object_field_force(object, "int3")), "null");
|
||||
ASSERT_EQ(td::json_encode<td::string>(td::get_json_object_field_force(object, "long")), "\"123456789012\"");
|
||||
ASSERT_EQ(td::json_encode<td::string>(td::get_json_object_field_force(object, "long2")), "2123456789012");
|
||||
ASSERT_EQ(td::json_encode<td::string>(td::get_json_object_field_force(object, "double")), "12345678901.1");
|
||||
ASSERT_EQ(td::json_encode<td::string>(td::get_json_object_field_force(object, "string")), "\"string\"");
|
||||
ASSERT_EQ(td::json_encode<td::string>(td::get_json_object_field_force(object, "string2")), "12345e+1");
|
||||
ASSERT_EQ(td::json_encode<td::string>(td::get_json_object_field_force(object, "array")), "[]");
|
||||
ASSERT_EQ(td::json_encode<td::string>(td::get_json_object_field_force(object, "object")), "{}");
|
||||
ASSERT_EQ(td::json_encode<td::string>(td::get_json_object_field_force(object, "")), "null");
|
||||
}
|
||||
|
||||
{
|
||||
td::string encoded_object_copy = encoded_object;
|
||||
auto value = td::json_decode(encoded_object_copy).move_as_ok();
|
||||
auto &object = value.get_object();
|
||||
ASSERT_TRUE(td::get_json_object_field(object, "int", td::JsonValue::Type::Number).is_error());
|
||||
ASSERT_TRUE(td::get_json_object_field(object, "int", td::JsonValue::Type::Number).is_error());
|
||||
ASSERT_TRUE(td::get_json_object_field(object, "int2", td::JsonValue::Type::Number).is_ok());
|
||||
ASSERT_TRUE(td::get_json_object_field(object, "int2", td::JsonValue::Type::Number).is_error());
|
||||
ASSERT_TRUE(td::get_json_object_field(object, "int3", td::JsonValue::Type::Number).is_ok());
|
||||
ASSERT_TRUE(td::get_json_object_field(object, "int3", td::JsonValue::Type::Null).is_ok());
|
||||
ASSERT_EQ(td::get_json_object_field(object, "int", td::JsonValue::Type::String).ok().get_string(), "1");
|
||||
ASSERT_TRUE(td::get_json_object_field(object, "int", td::JsonValue::Type::Number).is_error());
|
||||
ASSERT_EQ(td::get_json_object_field(object, "int", td::JsonValue::Type::Null).ok().type(),
|
||||
td::JsonValue::Type::Null);
|
||||
|
||||
ASSERT_TRUE(td::get_json_object_field(object, "long", td::JsonValue::Type::Number, false).is_error());
|
||||
ASSERT_TRUE(td::get_json_object_field(object, "long", td::JsonValue::Type::Number, false).is_error());
|
||||
ASSERT_TRUE(td::get_json_object_field(object, "long2", td::JsonValue::Type::Number, false).is_ok());
|
||||
ASSERT_TRUE(td::get_json_object_field(object, "long2", td::JsonValue::Type::Number, false).is_error());
|
||||
ASSERT_TRUE(td::get_json_object_field(object, "long3", td::JsonValue::Type::Number, false).is_error());
|
||||
ASSERT_TRUE(td::get_json_object_field(object, "long3", td::JsonValue::Type::Null, false).is_error());
|
||||
ASSERT_EQ(td::get_json_object_field(object, "long", td::JsonValue::Type::String, false).ok().get_string(),
|
||||
"123456789012");
|
||||
ASSERT_TRUE(td::get_json_object_field(object, "long", td::JsonValue::Type::Number, false).is_error());
|
||||
ASSERT_EQ(td::get_json_object_field(object, "long", td::JsonValue::Type::Null, false).ok().type(),
|
||||
td::JsonValue::Type::Null);
|
||||
}
|
||||
|
||||
td::string encoded_object_copy = encoded_object;
|
||||
auto value = td::json_decode(encoded_object_copy).move_as_ok();
|
||||
auto &object = value.get_object();
|
||||
ASSERT_TRUE(td::has_json_object_field(object, "null"));
|
||||
ASSERT_TRUE(td::has_json_object_field(object, "object"));
|
||||
ASSERT_TRUE(!td::has_json_object_field(object, ""));
|
||||
ASSERT_TRUE(!td::has_json_object_field(object, "objec"));
|
||||
ASSERT_TRUE(!td::has_json_object_field(object, "object2"));
|
||||
|
||||
ASSERT_TRUE(td::get_json_object_bool_field(object, "int").is_error());
|
||||
ASSERT_EQ(td::get_json_object_bool_field(object, "bool").ok(), true);
|
||||
ASSERT_EQ(td::get_json_object_bool_field(object, "bool").ok(), true);
|
||||
ASSERT_EQ(td::get_json_object_bool_field(object, "bool", true).ok(), true);
|
||||
ASSERT_EQ(td::get_json_object_bool_field(object, "bool3").ok(), false);
|
||||
ASSERT_EQ(td::get_json_object_bool_field(object, "bool4", true, true).ok(), true);
|
||||
ASSERT_TRUE(td::get_json_object_bool_field(object, "bool5", false, true).is_error());
|
||||
|
||||
ASSERT_TRUE(td::get_json_object_int_field(object, "null").is_error());
|
||||
ASSERT_EQ(td::get_json_object_int_field(object, "int").ok(), 1);
|
||||
ASSERT_EQ(td::get_json_object_int_field(object, "int").ok(), 1);
|
||||
ASSERT_EQ(td::get_json_object_int_field(object, "int", true).ok(), 1);
|
||||
ASSERT_EQ(td::get_json_object_int_field(object, "int2").ok(), 2);
|
||||
ASSERT_EQ(td::get_json_object_int_field(object, "int2").ok(), 2);
|
||||
ASSERT_EQ(td::get_json_object_int_field(object, "int2", true).ok(), 2);
|
||||
ASSERT_EQ(td::get_json_object_int_field(object, "int3").ok(), 0);
|
||||
ASSERT_EQ(td::get_json_object_int_field(object, "int4", true, 5).ok(), 5);
|
||||
ASSERT_TRUE(td::get_json_object_int_field(object, "int5", false, 5).is_error());
|
||||
ASSERT_EQ(td::get_json_object_int_field(object, "long").is_error(), true);
|
||||
ASSERT_EQ(td::get_json_object_int_field(object, "long2").is_error(), true);
|
||||
|
||||
ASSERT_TRUE(td::get_json_object_long_field(object, "null").is_error());
|
||||
ASSERT_EQ(td::get_json_object_long_field(object, "long").ok(), 123456789012ll);
|
||||
ASSERT_EQ(td::get_json_object_long_field(object, "long").ok(), 123456789012ll);
|
||||
ASSERT_EQ(td::get_json_object_long_field(object, "long", true).ok(), 123456789012ll);
|
||||
ASSERT_EQ(td::get_json_object_long_field(object, "long2").ok(), 2123456789012ll);
|
||||
ASSERT_EQ(td::get_json_object_long_field(object, "long2").ok(), 2123456789012ll);
|
||||
ASSERT_EQ(td::get_json_object_long_field(object, "long2", true).ok(), 2123456789012ll);
|
||||
ASSERT_EQ(td::get_json_object_long_field(object, "long3").ok(), 0);
|
||||
ASSERT_EQ(td::get_json_object_long_field(object, "long4", true, 5).ok(), 5);
|
||||
ASSERT_TRUE(td::get_json_object_long_field(object, "long5", false, 5).is_error());
|
||||
ASSERT_EQ(td::get_json_object_long_field(object, "int").ok(), 1);
|
||||
ASSERT_EQ(td::get_json_object_long_field(object, "int2").ok(), 2);
|
||||
|
||||
auto are_equal_double = [](double lhs, double rhs) {
|
||||
return rhs - 1e-3 < lhs && lhs < rhs + 1e-3;
|
||||
};
|
||||
|
||||
ASSERT_TRUE(td::get_json_object_double_field(object, "null").is_error());
|
||||
ASSERT_TRUE(are_equal_double(td::get_json_object_double_field(object, "double").ok(), 12345678901.1));
|
||||
ASSERT_TRUE(are_equal_double(td::get_json_object_double_field(object, "double").ok(), 12345678901.1));
|
||||
ASSERT_TRUE(are_equal_double(td::get_json_object_double_field(object, "double", true).ok(), 12345678901.1));
|
||||
ASSERT_TRUE(are_equal_double(td::get_json_object_double_field(object, "long2").ok(), 2123456789012.0));
|
||||
ASSERT_TRUE(are_equal_double(td::get_json_object_double_field(object, "long2").ok(), 2123456789012.0));
|
||||
ASSERT_TRUE(are_equal_double(td::get_json_object_double_field(object, "long2", true).ok(), 2123456789012.0));
|
||||
ASSERT_TRUE(are_equal_double(td::get_json_object_double_field(object, "double3").ok(), 0.0));
|
||||
ASSERT_TRUE(are_equal_double(td::get_json_object_double_field(object, "double4", true, -5.23).ok(), -5.23));
|
||||
ASSERT_TRUE(td::get_json_object_double_field(object, "double5", false, 5).is_error());
|
||||
ASSERT_TRUE(td::get_json_object_double_field(object, "int").is_error());
|
||||
ASSERT_TRUE(are_equal_double(td::get_json_object_double_field(object, "int2").ok(), 2));
|
||||
|
||||
ASSERT_TRUE(td::get_json_object_string_field(object, "null").is_error());
|
||||
ASSERT_EQ(td::get_json_object_string_field(object, "string").ok(), "string");
|
||||
ASSERT_EQ(td::get_json_object_string_field(object, "string").ok(), "string");
|
||||
ASSERT_EQ(td::get_json_object_string_field(object, "string", true).ok(), "string");
|
||||
ASSERT_EQ(td::get_json_object_string_field(object, "string2").ok(), "12345e+1");
|
||||
ASSERT_EQ(td::get_json_object_string_field(object, "string2").ok(), "12345e+1");
|
||||
ASSERT_EQ(td::get_json_object_string_field(object, "string2", true).ok(), "12345e+1");
|
||||
ASSERT_EQ(td::get_json_object_string_field(object, "string3").ok(), td::string());
|
||||
ASSERT_EQ(td::get_json_object_string_field(object, "string4", true, "abacaba").ok(), "abacaba");
|
||||
ASSERT_TRUE(td::get_json_object_string_field(object, "string5", false, "test").is_error());
|
||||
ASSERT_EQ(td::get_json_object_string_field(object, "int").ok(), "1");
|
||||
ASSERT_EQ(td::get_json_object_string_field(object, "int2").ok(), "2");
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user