Changeset View
Changeset View
Standalone View
Standalone View
source/blender/blenlib/tests/BLI_serialize_test.cc
- This file was added.
| /* Apache License, Version 2.0 */ | |||||
| #include "testing/testing.h" | |||||
| #include "BLI_serialize.hh" | |||||
| /* -------------------------------------------------------------------- */ | |||||
| /* tests */ | |||||
| namespace blender::io::serialize::json::testing { | |||||
| TEST(serialize, string_to_json) | |||||
| { | |||||
| JsonFormatter json; | |||||
| std::stringstream out; | |||||
| StringValue test_value("Hello JSON"); | |||||
| json.serialize(out, test_value); | |||||
| EXPECT_EQ(out.str(), "\"Hello JSON\""); | |||||
| } | |||||
| TEST(serialize, int_to_json) | |||||
| { | |||||
| JsonFormatter json; | |||||
| std::stringstream out; | |||||
| IntValue test_value(42); | |||||
| json.serialize(out, test_value); | |||||
sybren: Also test with bigger & negative integers! | |||||
| EXPECT_EQ(out.str(), "42"); | |||||
| } | |||||
| TEST(serialize, float_to_json) | |||||
| { | |||||
| JsonFormatter json; | |||||
| std::stringstream out; | |||||
| FloatValue test_value(42.31); | |||||
| json.serialize(out, test_value); | |||||
Done Inline ActionsThe type is called FloatValue but you test with a double literal. Be sure to actually test the precision by which these are saved, if you want to actually test doubles. sybren: The type is called `FloatValue` but you test with a `double` literal. Be sure to actually test… | |||||
| EXPECT_EQ(out.str(), "42.31"); | |||||
| } | |||||
| TEST(serialize, null_to_json) | |||||
| { | |||||
| JsonFormatter json; | |||||
| std::stringstream out; | |||||
| NullValue test_value; | |||||
| json.serialize(out, test_value); | |||||
| EXPECT_EQ(out.str(), "null"); | |||||
| } | |||||
| TEST(serialize, false_to_json) | |||||
| { | |||||
| JsonFormatter json; | |||||
| std::stringstream out; | |||||
| BooleanValue value(false); | |||||
| json.serialize(out, value); | |||||
| EXPECT_EQ(out.str(), "false"); | |||||
| } | |||||
| TEST(serialize, true_to_json) | |||||
| { | |||||
| JsonFormatter json; | |||||
| std::stringstream out; | |||||
| BooleanValue value(true); | |||||
| json.serialize(out, value); | |||||
| EXPECT_EQ(out.str(), "true"); | |||||
| } | |||||
| TEST(serialize, array_to_json) | |||||
| { | |||||
| JsonFormatter json; | |||||
| std::stringstream out; | |||||
| ArrayValue value_array; | |||||
| ArrayValue::Items &array = value_array.elements(); | |||||
| array.append_as(new IntValue(42)); | |||||
| array.append_as(new StringValue("Hello JSON")); | |||||
| array.append_as(new NullValue); | |||||
| array.append_as(new BooleanValue(false)); | |||||
| array.append_as(new BooleanValue(true)); | |||||
| json.serialize(out, value_array); | |||||
| EXPECT_EQ(out.str(), "[42,\"Hello JSON\",null,false,true]"); | |||||
| } | |||||
| TEST(serialize, object_to_json) | |||||
| { | |||||
| JsonFormatter json; | |||||
| std::stringstream out; | |||||
| ObjectValue value_object; | |||||
| ObjectValue::Items &attributes = value_object.elements(); | |||||
| attributes.add_as(std::string("best_number"), new IntValue(42)); | |||||
| json.serialize(out, value_object); | |||||
| EXPECT_EQ(out.str(), "{\"best_number\":42}"); | |||||
| } | |||||
| } // namespace blender::io::serialize::json::testing | |||||
Done Inline ActionsWould prefer const. Severin: Would prefer `const`. | |||||
Also test with bigger & negative integers!