Blender V5.3
idprop_serialize_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2021 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "testing/testing.h"
6
7#include "BLI_listbase.hh"
8#include "BLI_serialize.hh"
9
10#include "BKE_gtest_base.hh"
11
12#include "DNA_ID.h"
13
14#include "BKE_idprop.hh"
15
17
18using namespace blender::io::serialize;
19
21
23{
24 ASSERT_NE(value, nullptr);
25 ASSERT_EQ(value->type(), eValueType::Array);
26 const Span<std::shared_ptr<Value>> elements = value->elements();
27 EXPECT_FALSE(elements.is_empty());
28 EXPECT_EQ(elements.size(), 1);
29
30 const std::shared_ptr<Value> &item = value->elements()[0];
31 ASSERT_EQ(item->type(), eValueType::Dictionary);
32}
33
35 const std::string expected_key,
36 const std::string expected_value)
37{
38 EXPECT_TRUE(lookup.contains(expected_key));
39 const std::shared_ptr<Value> &element = *lookup.lookup_ptr(expected_key);
40 ASSERT_EQ(element->type(), eValueType::String);
41 EXPECT_EQ(element->as_string_value()->value(), expected_value);
42}
43
45 const std::string expected_key,
46 const int32_t expected_value)
47{
48 EXPECT_TRUE(lookup.contains(expected_key));
49 const std::shared_ptr<Value> &element = *lookup.lookup_ptr(expected_key);
50 ASSERT_EQ(element->type(), eValueType::Int);
51 EXPECT_EQ(element->as_int_value()->value(), expected_value);
52}
53
55 const std::string expected_key,
56 const float expected_value)
57{
58 EXPECT_TRUE(lookup.contains(expected_key));
59 const std::shared_ptr<Value> &element = *lookup.lookup_ptr(expected_key);
60 ASSERT_EQ(element->type(), eValueType::Double);
61 EXPECT_EQ(element->as_double_value()->value(), expected_value);
62}
63
65 const std::string expected_key,
66 const double expected_value)
67{
68 EXPECT_TRUE(lookup.contains(expected_key));
69 const std::shared_ptr<Value> &element = *lookup.lookup_ptr(expected_key);
70 ASSERT_EQ(element->type(), eValueType::Double);
71 EXPECT_EQ(element->as_double_value()->value(), expected_value);
72}
73
74static void test_string_to_value(const StringRefNull prop_name, const StringRefNull prop_content)
75{
76 std::unique_ptr<IDProperty, IDPropertyDeleter> property = create(prop_name, prop_content);
77
78 std::unique_ptr<ArrayValue> value = convert_to_serialize_values(property.get());
79 check_container_value(value.get());
80 const std::shared_ptr<Value> &item = value->elements()[0];
81 const DictionaryValue *object = item->as_dictionary_value();
82 const DictionaryValue::Lookup lookup = object->create_lookup();
83
84 EXPECT_EQ(lookup.size(), 3);
85 check_object_attribute(lookup, "name", prop_name);
86 check_object_attribute(lookup, "type", "IDP_STRING");
87 check_object_attribute(lookup, "value", prop_content);
88}
89
90TEST_F(IDPropertySerializeTest, convert_idp_string_to_value)
91{
92 test_string_to_value("mykey", "mycontent");
93}
94
95static void test_int_to_value(const StringRefNull prop_name, int32_t prop_content)
96{
97 std::unique_ptr<IDProperty, IDPropertyDeleter> property = create(prop_name, prop_content);
98
99 std::unique_ptr<ArrayValue> value = convert_to_serialize_values(property.get());
100 check_container_value(value.get());
101 const std::shared_ptr<Value> &item = value->elements()[0];
102 const DictionaryValue *object = item->as_dictionary_value();
103 const DictionaryValue::Lookup lookup = object->create_lookup();
104
105 EXPECT_EQ(lookup.size(), 3);
106 check_object_attribute(lookup, "name", prop_name);
107 check_object_attribute(lookup, "type", "IDP_INT");
108 check_object_attribute(lookup, "value", prop_content);
109}
110
111TEST_F(IDPropertySerializeTest, convert_idp_int_to_value)
112{
113 test_int_to_value("mykey", 0);
114}
115
116static void test_float_to_value(const StringRefNull prop_name, float prop_content)
117{
118 std::unique_ptr<IDProperty, IDPropertyDeleter> property = create(prop_name, prop_content);
119
120 std::unique_ptr<ArrayValue> value = convert_to_serialize_values(property.get());
121 check_container_value(value.get());
122 const std::shared_ptr<Value> &item = value->elements()[0];
123 const DictionaryValue *object = item->as_dictionary_value();
124 const DictionaryValue::Lookup lookup = object->create_lookup();
125
126 EXPECT_EQ(lookup.size(), 3);
127 check_object_attribute(lookup, "name", prop_name);
128 check_object_attribute(lookup, "type", "IDP_FLOAT");
129 check_object_attribute(lookup, "value", prop_content);
130}
131
132TEST_F(IDPropertySerializeTest, convert_idp_float_to_value)
133{
134 test_float_to_value("mykey", 0.2f);
135}
136
137static void test_double_to_value(const StringRefNull prop_name, double prop_content)
138{
139 std::unique_ptr<IDProperty, IDPropertyDeleter> property = create(prop_name, prop_content);
140
141 std::unique_ptr<ArrayValue> value = convert_to_serialize_values(property.get());
142 check_container_value(value.get());
143 const std::shared_ptr<Value> &item = value->elements()[0];
144 const DictionaryValue *object = item->as_dictionary_value();
145 const DictionaryValue::Lookup lookup = object->create_lookup();
146
147 EXPECT_EQ(lookup.size(), 3);
148 check_object_attribute(lookup, "name", prop_name);
149 check_object_attribute(lookup, "type", "IDP_DOUBLE");
150 check_object_attribute(lookup, "value", prop_content);
151}
152
153TEST_F(IDPropertySerializeTest, convert_idp_double_to_value)
154{
155 test_double_to_value("mykey", 0.2);
156}
157
158template<typename PrimitiveType, typename ValueType>
159static void test_array_to_value(const StringRefNull prop_name, Vector<PrimitiveType> prop_content)
160{
161 std::unique_ptr<IDProperty, IDPropertyDeleter> property = create(prop_name, prop_content);
162 std::unique_ptr<ArrayValue> value = convert_to_serialize_values(property.get());
163
164 check_container_value(value.get());
165 const std::shared_ptr<Value> &item = value->elements()[0];
166 const DictionaryValue *object = item->as_dictionary_value();
167 const DictionaryValue::Lookup lookup = object->create_lookup();
168
169 EXPECT_EQ(lookup.size(), 4);
170 check_object_attribute(lookup, "name", prop_name);
171 check_object_attribute(lookup, "type", "IDP_ARRAY");
172
173 const std::shared_ptr<Value> &element = *lookup.lookup_ptr("value");
174 const ArrayValue *subvalues = element->as_array_value();
175 ASSERT_NE(subvalues, nullptr);
176 const Span<std::shared_ptr<Value>> subitems = subvalues->elements();
177 ASSERT_EQ(subitems.size(), prop_content.size());
178
179 for (size_t i = 0; i < prop_content.size(); i++) {
180 EXPECT_EQ(static_cast<ValueType *>(subitems[i].get())->value(), prop_content[i]);
181 }
182}
183
184TEST_F(IDPropertySerializeTest, convert_idp_int_array_to_value)
185{
187 {-16, -8, -4, -2, -1, 0, 1, 2, 4, 8, 16});
188}
189
190TEST_F(IDPropertySerializeTest, convert_idp_float_array_to_value)
191{
193 "my_float_array", {-16.8f, -8.4f, -4.2f, -2.1f, -1.0f, 0.0f, 1.0f, 2.1f, 4.2f, 8.4f, 16.8f});
194}
195
196TEST_F(IDPropertySerializeTest, convert_idp_double_array_to_value)
197{
199 "my_double_array", {-16.8, -8.4, -4.2, -2.1, -1.0, 0.0, 1.0, 2.1, 4.2, 8.4, 16.8});
200}
201
202static std::unique_ptr<Value> parse_json(StringRef input)
203{
204 std::stringstream is(input);
206 std::unique_ptr<Value> value = json.deserialize(is);
207 return value;
208}
209
210static std::string to_json(const Value &value)
211{
212 std::stringstream out;
214 json.serialize(out, value);
215 return out.str();
216}
217
218static void test_idprop(const IDProperty *id_property,
219 StringRef expected_name,
220 StringRef expected_value)
221{
222 ASSERT_NE(id_property, nullptr);
223 EXPECT_EQ(id_property->type, IDP_STRING);
224 EXPECT_EQ(id_property->name, expected_name);
225 EXPECT_EQ(IDP_string_get(id_property), expected_value);
226}
227
228static void test_idprop(const IDProperty *id_property,
229 StringRef expected_name,
230 int32_t expected_value)
231{
232 ASSERT_NE(id_property, nullptr);
233 EXPECT_EQ(id_property->type, IDP_INT);
234 EXPECT_EQ(id_property->name, expected_name);
235 EXPECT_EQ(IDP_int_get(id_property), expected_value);
236}
237
238static void test_idprop(const IDProperty *id_property,
239 StringRef expected_name,
240 float expected_value)
241{
242 ASSERT_NE(id_property, nullptr);
243 EXPECT_EQ(id_property->type, IDP_FLOAT);
244 EXPECT_EQ(id_property->name, expected_name);
245 EXPECT_EQ(IDP_float_get(id_property), expected_value);
246}
247
248static void test_idprop(const IDProperty *id_property,
249 StringRef expected_name,
250 double expected_value)
251{
252 ASSERT_NE(id_property, nullptr);
253 EXPECT_EQ(id_property->type, IDP_DOUBLE);
254 EXPECT_EQ(id_property->name, expected_name);
255 EXPECT_EQ(IDP_double_get(id_property), expected_value);
256}
257
258static void test_idprop(const IDProperty *id_property,
259 StringRef expected_name,
260 const Span<int32_t> values)
261{
262 ASSERT_NE(id_property, nullptr);
263 EXPECT_EQ(id_property->type, IDP_ARRAY);
264 EXPECT_EQ(id_property->subtype, IDP_INT);
265 EXPECT_EQ(id_property->len, values.size());
266 EXPECT_EQ(id_property->name, expected_name);
267 int32_t *idprop_values = IDP_array_int_get(id_property);
268 for (int i = 0; i < values.size(); i++) {
269 EXPECT_EQ(idprop_values[i], values[i]);
270 }
271}
272
273static void test_idprop(const IDProperty *id_property,
274 StringRef expected_name,
275 const Span<float> values)
276{
277 ASSERT_NE(id_property, nullptr);
278 EXPECT_EQ(id_property->type, IDP_ARRAY);
279 EXPECT_EQ(id_property->subtype, IDP_FLOAT);
280 EXPECT_EQ(id_property->len, values.size());
281 EXPECT_EQ(id_property->name, expected_name);
282 float *idprop_values = IDP_array_float_get(id_property);
283 for (int i = 0; i < values.size(); i++) {
284 EXPECT_EQ(idprop_values[i], values[i]);
285 }
286}
287
288static void test_idprop(const IDProperty *id_property,
289 StringRef expected_name,
290 const Span<double> values)
291{
292 ASSERT_NE(id_property, nullptr);
293 EXPECT_EQ(id_property->type, IDP_ARRAY);
294 EXPECT_EQ(id_property->subtype, IDP_DOUBLE);
295 EXPECT_EQ(id_property->len, values.size());
296 EXPECT_EQ(id_property->name, expected_name);
297 double *idprop_values = IDP_array_double_get(id_property);
298 for (int i = 0; i < values.size(); i++) {
299 EXPECT_EQ(idprop_values[i], values[i]);
300 }
301}
302
303template<typename Type>
305 StringRef expected_name,
306 Type expected_value)
307{
308 std::unique_ptr<Value> value = parse_json(input);
309 IDProperty *id_property = convert_from_serialize_value(*value);
310 test_idprop(id_property, expected_name, expected_value);
311 IDP_FreeProperty(id_property);
312}
313
314TEST_F(IDPropertySerializeTest, convert_idp_string_from_value)
315{
317 R"([{"name":"MyStringName","type":"IDP_STRING","value":"MyString"}])",
318 "MyStringName",
319 "MyString");
320}
321
322TEST_F(IDPropertySerializeTest, convert_idp_int_from_value)
323{
325 R"([{"name":"MyIntegerName","type":"IDP_INT","value":42}])", "MyIntegerName", 42);
326}
327
328TEST_F(IDPropertySerializeTest, convert_idp_float_from_value)
329{
331 R"([{"name":"MyFloatName","type":"IDP_FLOAT","value":42.24}])", "MyFloatName", 42.24f);
332}
333
334TEST_F(IDPropertySerializeTest, convert_idp_double_from_value)
335{
337 R"([{"name":"MyDoubleName","type":"IDP_DOUBLE","value":42.24}])", "MyDoubleName", 42.24);
338}
339
340TEST_F(IDPropertySerializeTest, convert_idp_array_int_from_value)
341{
343 R"([{"name":"MyArrayName","type":"IDP_ARRAY","subtype":"IDP_INT","value":[42, 24, 35]}])",
344 "MyArrayName",
345 Vector<int32_t>{42, 24, 35});
346}
347
348TEST_F(IDPropertySerializeTest, convert_idp_array_float_from_value)
349{
351 R"([{"name":"MyArrayName","type":"IDP_ARRAY","subtype":"IDP_FLOAT","value":[42.0, 24.4, 35.2]}])",
352 "MyArrayName",
353 Vector<float>{42.0f, 24.4f, 35.2f});
354}
355
356TEST_F(IDPropertySerializeTest, convert_idp_array_double_from_value)
357{
359 R"([{"name":"MyArrayName","type":"IDP_ARRAY","subtype":"IDP_DOUBLE","value":[42.43,24.5,35.8]}])",
360 "MyArrayName",
361 Vector<double>{42.43, 24.5, 35.8});
362}
363
364TEST_F(IDPropertySerializeTest, convert_idp_multiple_from_value)
365{
366 static const std::string input_json =
367 R"([{"name":"MyIntegerName","type":"IDP_INT","value":42},{"name":"MyStringName","type":"IDP_STRING","value":"MyString"},{"name":"MyFloatName","type":"IDP_FLOAT","value":42.24},{"name":"MyDoubleName","type":"IDP_DOUBLE","value":42.24}])";
368 std::unique_ptr<Value> value = parse_json(input_json);
369
370 IDProperty *id_property = convert_from_serialize_value(*value);
371 IDProperty *id_property_1 = id_property;
372 ASSERT_NE(id_property_1, nullptr);
373 IDProperty *id_property_2 = id_property_1->next;
374 ASSERT_NE(id_property_2, nullptr);
375 IDProperty *id_property_3 = id_property_2->next;
376 ASSERT_NE(id_property_3, nullptr);
377 IDProperty *id_property_4 = id_property_3->next;
378 ASSERT_NE(id_property_4, nullptr);
379
380 EXPECT_EQ(id_property_1->prev, nullptr);
381 EXPECT_EQ(id_property_2->prev, id_property_1);
382 EXPECT_EQ(id_property_3->prev, id_property_2);
383 EXPECT_EQ(id_property_4->prev, id_property_3);
384 EXPECT_EQ(id_property_4->next, nullptr);
385
386 test_idprop(id_property_1, "MyIntegerName", 42);
387 test_idprop(id_property_2, "MyStringName", "MyString");
388 test_idprop(id_property_3, "MyFloatName", 42.24f);
389 test_idprop(id_property_4, "MyDoubleName", 42.24);
390
391 IDP_FreeProperty(id_property_1);
392 IDP_FreeProperty(id_property_2);
393 IDP_FreeProperty(id_property_3);
394 IDP_FreeProperty(id_property_4);
395}
396
397TEST_F(IDPropertySerializeTest, convert_idp_multiple_roundtrip)
398{
399 static const std::string input_json =
400 R"([{"name":"MyIntegerName","type":"IDP_INT","value":42},{"name":"MyStringName","type":"IDP_STRING","value":"MyString"},{"name":"MyFloatName","type":"IDP_FLOAT","value":42.2400016784668},{"name":"MyDoubleName","type":"IDP_DOUBLE","value":42.24}])";
401 std::unique_ptr<Value> value = parse_json(input_json);
402
403 IDProperty *id_property = convert_from_serialize_value(*value);
404 IDProperty *id_property_1 = id_property;
405 ASSERT_NE(id_property_1, nullptr);
406 IDProperty *id_property_2 = id_property_1->next;
407 ASSERT_NE(id_property_2, nullptr);
408 IDProperty *id_property_3 = id_property_2->next;
409 ASSERT_NE(id_property_3, nullptr);
410 IDProperty *id_property_4 = id_property_3->next;
411 ASSERT_NE(id_property_4, nullptr);
412
413 std::unique_ptr<Value> value_from_id_properties = convert_to_serialize_values(id_property);
414 std::string output_json = to_json(*value_from_id_properties);
415 EXPECT_EQ(input_json, output_json);
416
417 IDP_FreeProperty(id_property_1);
418 IDP_FreeProperty(id_property_2);
419 IDP_FreeProperty(id_property_3);
420 IDP_FreeProperty(id_property_4);
421}
422
423TEST_F(IDPropertySerializeTest, convert_idp_group_from_value)
424{
425 static const std::string input_json =
426 R"([{"name":"AssetMetaData.properties","type":"IDP_GROUP","value":[{"name":"dimensions","type":"IDP_ARRAY","subtype":"IDP_FLOAT","value":[2.0,2.0,2.0]}]}])";
427 std::unique_ptr<Value> value = parse_json(input_json);
428
429 IDProperty *id_property = convert_from_serialize_value(*value);
430 ASSERT_NE(id_property, nullptr);
431 EXPECT_EQ(id_property->type, IDP_GROUP);
432 EXPECT_EQ(id_property->data.group.count(), 1);
433
434 test_idprop(id_property->data.group.first(), "dimensions", Vector<float>{2.0f, 2.0f, 2.0f});
435
436 IDP_FreeProperty(id_property);
437}
438
439} // namespace blender::bke::idprop::tests
#define IDP_float_get(prop)
#define IDP_int_get(prop)
#define IDP_array_double_get(prop)
#define IDP_string_get(prop)
#define IDP_double_get(prop)
#define IDP_array_int_get(prop)
#define IDP_array_float_get(prop)
ID and Library types, which are fundamental for SDNA.
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr bool is_empty() const
Definition BLI_span.hh:264
int64_t size() const
Span< std::shared_ptr< Value > > elements() const
Map< std::string, std::shared_ptr< Value > > Lookup
const DictionaryValue * as_dictionary_value() const
Definition serialize.cc:64
#define EXPECT_FALSE(result)
#define EXPECT_TRUE(result)
static void test_float_to_value(const StringRefNull prop_name, float prop_content)
static void test_string_to_value(const StringRefNull prop_name, const StringRefNull prop_content)
static void test_convert_idprop_from_value(StringRef input, StringRef expected_name, Type expected_value)
static void test_idprop(const IDProperty *id_property, StringRef expected_name, StringRef expected_value)
static void test_array_to_value(const StringRefNull prop_name, Vector< PrimitiveType > prop_content)
static void check_container_value(ArrayValue *value)
static void test_int_to_value(const StringRefNull prop_name, int32_t prop_content)
static void check_object_attribute(const DictionaryValue::Lookup &lookup, const std::string expected_key, const std::string expected_value)
TEST_F(IDPropertySerializeTest, convert_idp_string_to_value)
static std::unique_ptr< Value > parse_json(StringRef input)
static void test_double_to_value(const StringRefNull prop_name, double prop_content)
static std::string to_json(const Value &value)
std::unique_ptr< IDProperty, IDPropertyDeleter > create(StringRef prop_name, int32_t value, eIDPropertyFlag flags={})
Allocate a new IDProperty of type IDP_INT, set its name and value.
IDProperty * convert_from_serialize_value(const io::serialize::Value &value)
Convert the given value (which must be an array) to an IDProperty.
std::unique_ptr< io::serialize::ArrayValue > convert_to_serialize_values(const IDProperty *properties)
Convert the given properties to Value objects for serialization.
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
void IDP_FreeProperty(IDProperty *prop)
Definition idprop.cc:1403
signed int int32_t
Definition BKE_text.h:78
ATTR_WARN_UNUSED_RESULT const void * element
ListBaseT< struct IDProperty > group
Definition DNA_ID.h:133
eIDPropertyType type
Definition DNA_ID.h:145
struct IDProperty * prev
Definition DNA_ID.h:144
struct IDProperty * next
Definition DNA_ID.h:144
IDPropertyData data
Definition DNA_ID.h:157