diff options
-rw-r--r-- | src/core/model/Typesystem.cpp | 3 | ||||
-rw-r--r-- | test/core/model/TypesystemTest.cpp | 51 |
2 files changed, 53 insertions, 1 deletions
diff --git a/src/core/model/Typesystem.cpp b/src/core/model/Typesystem.cpp index 812d85c..8d5654c 100644 --- a/src/core/model/Typesystem.cpp +++ b/src/core/model/Typesystem.cpp @@ -244,7 +244,8 @@ bool StructType::buildFromArray(Variant &data, Logger &logger, bool trim) const } // Make sure the given attributes have to correct type - for (size_t a = 0; a < n; a++) { + const size_t len = std::min(n, N); + for (size_t a = 0; a < len; a++) { set[a] = attributes[a]->getType()->build(arr[a], logger); ok = ok && set[a]; } diff --git a/test/core/model/TypesystemTest.cpp b/test/core/model/TypesystemTest.cpp index f995222..fd6e38f 100644 --- a/test/core/model/TypesystemTest.cpp +++ b/test/core/model/TypesystemTest.cpp @@ -472,6 +472,57 @@ TEST(StructType, creationWithParent) ASSERT_EQ(42, arr[5].asInt()); } +TEST(StructType, derivedFrom) +{ + Logger logger; + Manager mgr; + Rooted<StructType> structType = createStructType(mgr, logger); + Rooted<StructType> structWithParentType = + createStructTypeWithParent(structType, mgr, logger); + + ASSERT_TRUE(structType->derivedFrom(structType)); + ASSERT_TRUE(structWithParentType->derivedFrom(structType)); + ASSERT_TRUE(structWithParentType->derivedFrom(structWithParentType)); + ASSERT_FALSE(structType->derivedFrom(structWithParentType)); +} + +TEST(StructType, cast) +{ + Logger logger; + Manager mgr; + Rooted<StructType> structType = createStructType(mgr, logger); + Rooted<StructType> structWithParentType = + createStructTypeWithParent(structType, mgr, logger); + + Variant val = structWithParentType->create(); + + Variant casted = structType->cast(val, logger); + 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()); +} + +TEST(StructType, indexOf) +{ + Logger logger; + Manager mgr; + Rooted<StructType> structType = createStructType(mgr, logger); + ASSERT_EQ(0, structType->indexOf("d")); + ASSERT_EQ(1, structType->indexOf("b")); + ASSERT_EQ(2, structType->indexOf("c")); + ASSERT_EQ(3, structType->indexOf("a")); + ASSERT_EQ(-1, structType->indexOf("#0")); +} + /* Class ArrayType */ TEST(ArrayType, rtti) |