diff options
-rw-r--r-- | src/core/model/Typesystem.cpp | 12 | ||||
-rw-r--r-- | test/core/model/TypesystemTest.cpp | 94 |
2 files changed, 94 insertions, 12 deletions
diff --git a/src/core/model/Typesystem.cpp b/src/core/model/Typesystem.cpp index ed915ea..58afd27 100644 --- a/src/core/model/Typesystem.cpp +++ b/src/core/model/Typesystem.cpp @@ -44,7 +44,7 @@ bool StringType::doBuild(Variant &data, Logger &logger) const { // Cannot convert non-primitive values to strings if (!data.isPrimitive()) { - throw LoggableException{"Expected a string or primitive input."}; + throw LoggableException{"Expected string or primitive input."}; } // Perform an implicit type conversion @@ -65,7 +65,7 @@ bool StringType::doBuild(Variant &data, Logger &logger) const bool IntType::doBuild(Variant &data, Logger &logger) const { if (!data.isInt()) { - throw LoggableException{"Expected an integer value."}; + throw LoggableException{"Expected integer value."}; } return true; } @@ -75,7 +75,7 @@ bool IntType::doBuild(Variant &data, Logger &logger) const bool DoubleType::doBuild(Variant &data, Logger &logger) const { if (!data.isInt() && !data.isDouble()) { - throw LoggableException{"Expected a double value."}; + throw LoggableException{"Expected double value."}; } data = Variant{data.toDouble()}; return true; @@ -216,9 +216,9 @@ bool StructType::insertDefaults(Variant &data, const std::vector<bool> &set, } else { ok = false; arr[a] = attributes[a]->getType()->create(); - logger.error(std::string("Expected attribute \"") + + logger.error(std::string("No value given for mandatory attribute \"") + attributes[a]->getName() + - std::string("\", but no value given.")); + std::string("\"")); } } } @@ -335,7 +335,7 @@ Rooted<StructType> StructType::createValidated(Manager &mgr, std::string name, for (size_t idx = 0; idx < attributes.size(); idx++) { // Check for valid attribute names const std::string &attrName = attributes[idx]->getName(); - if (!Utils::isIdentifier(name)) { + if (!Utils::isIdentifier(attrName)) { logger.error(std::string("Invalid attribute name \"") + attrName + std::string("\"")); } diff --git a/test/core/model/TypesystemTest.cpp b/test/core/model/TypesystemTest.cpp index f396b66..b2f2600 100644 --- a/test/core/model/TypesystemTest.cpp +++ b/test/core/model/TypesystemTest.cpp @@ -476,6 +476,52 @@ TEST(StructType, derivedFrom) ASSERT_FALSE(structType->derivedFrom(structWithParentType)); } +TEST(StructType, createValidated) +{ + Manager mgr; + Rooted<StringType> stringType{new StringType(mgr, nullptr)}; + Rooted<IntType> intType{new IntType(mgr, nullptr)}; + + { + logger.resetMaxEncounteredSeverity(); + Rooted<StructType> structType{StructType::createValidated( + mgr, "struct", nullptr, nullptr, + NodeVector<Attribute>{ + new Attribute{mgr, "d", stringType, "attr1default"}, + new Attribute{mgr, "b", stringType}, + new Attribute{mgr, "c", intType, 3}, + new Attribute{mgr, "a", intType}}, + logger)}; + ASSERT_EQ(Severity::DEBUG, logger.getMaxEncounteredSeverity()); + } + + { + logger.resetMaxEncounteredSeverity(); + Rooted<StructType> structType{StructType::createValidated( + mgr, "struct", nullptr, nullptr, + NodeVector<Attribute>{ + new Attribute{mgr, "d", stringType, "attr1default"}, + new Attribute{mgr, "b", stringType}, + new Attribute{mgr, "a", intType, 3}, + new Attribute{mgr, "a", intType}}, + logger)}; + ASSERT_EQ(Severity::ERROR, logger.getMaxEncounteredSeverity()); + } + + { + logger.resetMaxEncounteredSeverity(); + Rooted<StructType> structType{StructType::createValidated( + mgr, "struct", nullptr, nullptr, + NodeVector<Attribute>{ + new Attribute{mgr, "d", stringType, "attr1default"}, + new Attribute{mgr, "b", stringType}, + new Attribute{mgr, "a", intType, 3}, + new Attribute{mgr, "a a", intType}}, + logger)}; + ASSERT_EQ(Severity::ERROR, logger.getMaxEncounteredSeverity()); + } +} + TEST(StructType, cast) { Manager mgr; @@ -511,11 +557,12 @@ TEST(StructType, indexOf) ASSERT_EQ(-1, structType->indexOf("#0")); } -TEST(StructType, buildWithDefaults) +TEST(StructType, build) { Manager mgr; Rooted<StructType> structType = createStructType(mgr, logger); + // All mandatory attributes given as map { Variant var{{{"b", 42}, {"a", 5}}}; ASSERT_TRUE(structType->build(var, logger)); @@ -528,6 +575,21 @@ TEST(StructType, buildWithDefaults) ASSERT_EQ(5, arr[3].asInt()); } + + // All mandatory attributes given as array + { + Variant var{{"v1", 2, 3, 4}}; + ASSERT_TRUE(structType->build(var, logger)); + + const auto &arr = var.asArray(); + ASSERT_EQ(4U, arr.size()); + ASSERT_EQ("v1", arr[0].asString()); + ASSERT_EQ("2", arr[1].asString()); + ASSERT_EQ(3, arr[2].asInt()); + ASSERT_EQ(4, arr[3].asInt()); + } + + // Too few attributes { Variant var{Variant::mapType{{"a", 5}}}; ASSERT_FALSE(structType->build(var, logger)); @@ -539,12 +601,32 @@ TEST(StructType, buildWithDefaults) ASSERT_EQ(3, arr[2].asInt()); ASSERT_EQ(5, arr[3].asInt()); } -} -TEST(StructType, buildWithIndicesAndDefaults) -{ - Manager mgr; - Rooted<StructType> structType = createStructType(mgr, logger); + // Too few attributes + { + Variant var{Variant::arrayType{}}; + ASSERT_FALSE(structType->build(var, logger)); + + const auto &arr = var.asArray(); + ASSERT_EQ(4U, arr.size()); + ASSERT_EQ("attr1default", arr[0].asString()); + ASSERT_EQ("", arr[1].asString()); + ASSERT_EQ(3, arr[2].asInt()); + ASSERT_EQ(0, arr[3].asInt()); + } + + // Too few attributes + { + Variant var{{"v1", 2}}; + ASSERT_FALSE(structType->build(var, logger)); + + const auto &arr = var.asArray(); + ASSERT_EQ(4U, arr.size()); + ASSERT_EQ("v1", arr[0].asString()); + ASSERT_EQ("2", arr[1].asString()); + ASSERT_EQ(3, arr[2].asInt()); + ASSERT_EQ(0, arr[3].asInt()); + } { Variant var{{{"b", 42}, {"#3", 5}, {"#0", "foo"}}}; |