diff options
-rw-r--r-- | src/core/model/Typesystem.cpp | 6 | ||||
-rw-r--r-- | test/core/model/TypesystemTest.cpp | 29 |
2 files changed, 30 insertions, 5 deletions
diff --git a/src/core/model/Typesystem.cpp b/src/core/model/Typesystem.cpp index 4a2f4eb..561692a 100644 --- a/src/core/model/Typesystem.cpp +++ b/src/core/model/Typesystem.cpp @@ -344,7 +344,11 @@ Variant StructType::create() const Variant::arrayType arr; arr.resize(attributes.size()); for (size_t idx = 0; idx < attributes.size(); idx++) { - arr[idx] = attributes[idx]->getType()->create(); + if (attributes[idx]->optional) { + arr[idx] = attributes[idx]->defaultValue; + } else { + arr[idx] = attributes[idx]->getType()->create(); + } } return arr; } diff --git a/test/core/model/TypesystemTest.cpp b/test/core/model/TypesystemTest.cpp index 5757213..4a37707 100644 --- a/test/core/model/TypesystemTest.cpp +++ b/test/core/model/TypesystemTest.cpp @@ -389,10 +389,10 @@ static Rooted<StructType> createStructType(Manager &mgr, Logger &logger) Rooted<StructType> structType{StructType::createValidated( mgr, "struct", nullptr, nullptr, NodeVector<Attribute>{ - new Attribute{mgr, "attr1", stringType, "attr1default"}, - new Attribute{mgr, "attr2", stringType}, - new Attribute{mgr, "attr3", intType, 3}, - new Attribute{mgr, "attr4", intType}}, + new Attribute{mgr, "d", stringType, "attr1default"}, + new Attribute{mgr, "b", stringType}, + new Attribute{mgr, "c", intType, 3}, + new Attribute{mgr, "a", intType}}, logger)}; return structType; } @@ -407,6 +407,27 @@ TEST(StructType, rtti) ASSERT_TRUE(structType->isa(typeOf<Node>())); } +TEST(StructType, creation) +{ + Logger logger; + Manager mgr; + Rooted<StructType> structType = createStructType(mgr, logger); + Variant val = structType->create(); + ASSERT_TRUE(val.isArray()); + ASSERT_EQ(4U, val.asArray().size()); + + const auto &arr = val.asArray(); + ASSERT_TRUE(arr[0].isString()); + ASSERT_TRUE(arr[1].isString()); + ASSERT_TRUE(arr[2].isInt()); + ASSERT_TRUE(arr[3].isInt()); + + ASSERT_EQ("attr1default", arr[0].asString()); + ASSERT_EQ("", arr[1].asString()); + ASSERT_EQ(3, arr[2].asInt()); + ASSERT_EQ(0, arr[3].asInt()); +} + /* Class ArrayType */ TEST(ArrayType, rtti) |