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; | |||||
| Value test_value(ValueType::String, std::string("Hello JSON")); | |||||
| json.serialize(out, test_value); | |||||
| EXPECT_EQ(out.str(), "\"Hello JSON\""); | |||||
| } | |||||
| TEST(serialize, int_to_json) | |||||
| { | |||||
| JsonFormatter json; | |||||
| std::stringstream out; | |||||
| Value test_value(ValueType::Int, 42); | |||||
| json.serialize(out, test_value); | |||||
sybren: Also test with bigger & negative integers! | |||||
| EXPECT_EQ(out.str(), "42"); | |||||
| } | |||||
| TEST(serialize, null_to_json) | |||||
| { | |||||
| JsonFormatter json; | |||||
| std::stringstream out; | |||||
| Value test_value(ValueType::Null); | |||||
| 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(), "null"); | |||||
| } | |||||
| TEST(serialize, false_to_json) | |||||
| { | |||||
| JsonFormatter json; | |||||
| std::stringstream out; | |||||
| Value value(ValueType::Boolean, false); | |||||
| json.serialize(out, value); | |||||
| EXPECT_EQ(out.str(), "false"); | |||||
| } | |||||
| TEST(serialize, true_to_json) | |||||
| { | |||||
| JsonFormatter json; | |||||
| std::stringstream out; | |||||
| Value value(ValueType::Boolean, true); | |||||
| json.serialize(out, value); | |||||
| EXPECT_EQ(out.str(), "true"); | |||||
| } | |||||
| TEST(serialize, array_to_json) | |||||
| { | |||||
| JsonFormatter json; | |||||
| std::stringstream out; | |||||
| Value value_array(ValueType::Array); | |||||
| Vector<Value *> &array = value_array.array_items(); | |||||
| array.append_as(new Value(ValueType::Int, 42)); | |||||
| array.append_as(new Value(ValueType::String, std::string("Hello JSON"))); | |||||
| array.append_as(new Value(ValueType::Null)); | |||||
| array.append_as(new Value(ValueType::Boolean, false)); | |||||
| array.append_as(new Value(ValueType::Boolean, 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; | |||||
| Value value_object(ValueType::Object); | |||||
| Map<std::string, Value *> &attributes = value_object.attributes(); | |||||
| attributes.add_as(std::string("best_number"), new Value(ValueType::Int, 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!