diff options
-rw-r--r-- | src/core/model/Typesystem.cpp | 15 | ||||
-rw-r--r-- | test/core/model/TypesystemTest.cpp | 43 |
2 files changed, 27 insertions, 31 deletions
diff --git a/src/core/model/Typesystem.cpp b/src/core/model/Typesystem.cpp index 0078022..ed915ea 100644 --- a/src/core/model/Typesystem.cpp +++ b/src/core/model/Typesystem.cpp @@ -48,13 +48,14 @@ bool StringType::doBuild(Variant &data, Logger &logger) const } // Perform an implicit type conversion - if (!data.isString() || data.isMagic()) { + if (!data.isString()) { // Convert the variant value to a string and set it + const char *oldName = data.getTypeName(); data = data.toString().c_str(); // Log conversions as these may be potentially unwanted logger.note(std::string("Implicit conversion from ") + - data.getTypeName() + " to string."); + oldName + " to string."); } return true; } @@ -137,7 +138,7 @@ Rooted<EnumType> EnumType::createValidated( // uniqueness and insert them into the internal values map for (size_t i = 0; i < values.size(); i++) { if (!Utils::isIdentifier(values[i])) { - logger.error(values[i] + " is no valid identifier."); + logger.error(std::string("\"") + values[i] + "\" is no valid identifier."); } if (!(unique_values.insert(std::make_pair(values[i], i))).second) { @@ -215,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("Expected attribute \"") + attributes[a]->getName() + - std::string(", but no value given.")); + std::string("\", but no value given.")); } } } @@ -306,7 +307,7 @@ bool StructType::buildFromArrayOrMap(Variant &data, Logger &logger, return buildFromMap(data, logger, trim); } throw LoggableException( - "Expected array or map for building a struct type!"); + std::string("Expected array or map for building a struct type, but got ") + data.getTypeName()); } bool StructType::doBuild(Variant &data, Logger &logger) const @@ -404,7 +405,7 @@ ssize_t StructType::indexOf(const std::string &name) const bool ArrayType::doBuild(Variant &data, Logger &logger) const { if (!data.isArray()) { - throw LoggableException("Expected array!"); + throw LoggableException(std::string("Expected array, but got ") + data.getTypeName()); } bool res = true; for (auto &v : data.asArray()) { diff --git a/test/core/model/TypesystemTest.cpp b/test/core/model/TypesystemTest.cpp index 9cbc5bc..f396b66 100644 --- a/test/core/model/TypesystemTest.cpp +++ b/test/core/model/TypesystemTest.cpp @@ -26,6 +26,9 @@ namespace ousia { namespace model { +static TerminalLogger logger(std::cerr, true); +//static Logger logger; + /* Class StringType */ TEST(StringType, rtti) @@ -49,7 +52,6 @@ TEST(StringType, creation) TEST(StringType, conversion) { - Logger logger; Manager mgr; Rooted<StringType> strType{new StringType(mgr, nullptr)}; @@ -125,7 +127,6 @@ TEST(IntType, creation) TEST(IntType, conversion) { - Logger logger; Manager mgr; Rooted<IntType> intType{new IntType(mgr, nullptr)}; @@ -166,7 +167,6 @@ TEST(DoubleType, creation) TEST(DoubleType, conversion) { - Logger logger; Manager mgr; Rooted<DoubleType> doubleType{new DoubleType(mgr, nullptr)}; @@ -214,7 +214,6 @@ TEST(BoolType, creation) TEST(BoolType, conversion) { - Logger logger; Manager mgr; Rooted<BoolType> boolType{new BoolType(mgr, nullptr)}; @@ -244,7 +243,6 @@ TEST(BoolType, conversion) TEST(EnumType, rtti) { - Logger logger; Manager mgr; Rooted<EnumType> enumType{EnumType::createValidated( mgr, "enum", nullptr, {"a", "b", "c"}, logger)}; @@ -255,7 +253,6 @@ TEST(EnumType, rtti) TEST(EnumType, creation) { - Logger logger; Manager mgr; Rooted<EnumType> enumType{EnumType::createValidated( mgr, "enum", nullptr, {"a", "b", "c"}, logger)}; @@ -266,7 +263,6 @@ TEST(EnumType, creation) TEST(EnumType, conversion) { - Logger logger; Manager mgr; Rooted<EnumType> enumType{EnumType::createValidated( mgr, "enum", nullptr, {"a", "b", "c"}, logger)}; @@ -322,28 +318,28 @@ TEST(EnumType, createValidated) Manager mgr; { - Logger logger; + logger.resetMaxEncounteredSeverity(); Rooted<EnumType> enumType{EnumType::createValidated( mgr, "enum", nullptr, {"a", "b", "c"}, logger)}; ASSERT_EQ(Severity::DEBUG, logger.getMaxEncounteredSeverity()); } { - Logger logger; + logger.resetMaxEncounteredSeverity(); Rooted<EnumType> enumType{EnumType::createValidated( mgr, "enum", nullptr, {"a", "a", "c"}, logger)}; ASSERT_EQ(Severity::ERROR, logger.getMaxEncounteredSeverity()); } { - Logger logger; + logger.resetMaxEncounteredSeverity(); Rooted<EnumType> enumType{ EnumType::createValidated(mgr, "enum", nullptr, {}, logger)}; ASSERT_EQ(Severity::ERROR, logger.getMaxEncounteredSeverity()); } { - Logger logger; + logger.resetMaxEncounteredSeverity(); Rooted<EnumType> enumType{ EnumType::createValidated(mgr, "enum", nullptr, {"a a"}, logger)}; ASSERT_EQ(Severity::ERROR, logger.getMaxEncounteredSeverity()); @@ -352,7 +348,6 @@ TEST(EnumType, createValidated) TEST(EnumType, nameOf) { - Logger logger; Manager mgr; Rooted<EnumType> enumType{EnumType::createValidated( @@ -367,7 +362,6 @@ TEST(EnumType, nameOf) TEST(EnumType, valueOf) { - Logger logger; Manager mgr; Rooted<EnumType> enumType{EnumType::createValidated( @@ -414,7 +408,6 @@ static Rooted<StructType> createStructTypeWithParent(Handle<StructType> parent, TEST(StructType, rtti) { - Logger logger; Manager mgr; Rooted<StructType> structType = createStructType(mgr, logger); ASSERT_TRUE(structType->isa(RttiTypes::StructType)); @@ -424,7 +417,6 @@ TEST(StructType, rtti) TEST(StructType, creation) { - Logger logger; Manager mgr; Rooted<StructType> structType = createStructType(mgr, logger); Variant val = structType->create(); @@ -445,7 +437,6 @@ TEST(StructType, creation) TEST(StructType, creationWithParent) { - Logger logger; Manager mgr; Rooted<StructType> structType = createStructType(mgr, logger); Rooted<StructType> structWithParentType = @@ -474,7 +465,6 @@ TEST(StructType, creationWithParent) TEST(StructType, derivedFrom) { - Logger logger; Manager mgr; Rooted<StructType> structType = createStructType(mgr, logger); Rooted<StructType> structWithParentType = @@ -488,7 +478,6 @@ TEST(StructType, derivedFrom) TEST(StructType, cast) { - Logger logger; Manager mgr; Rooted<StructType> structType = createStructType(mgr, logger); Rooted<StructType> structWithParentType = @@ -513,7 +502,6 @@ TEST(StructType, cast) TEST(StructType, indexOf) { - Logger logger; Manager mgr; Rooted<StructType> structType = createStructType(mgr, logger); ASSERT_EQ(0, structType->indexOf("d")); @@ -525,7 +513,6 @@ TEST(StructType, indexOf) TEST(StructType, buildWithDefaults) { - Logger logger; Manager mgr; Rooted<StructType> structType = createStructType(mgr, logger); @@ -540,11 +527,22 @@ TEST(StructType, buildWithDefaults) ASSERT_EQ(3, arr[2].asInt()); ASSERT_EQ(5, arr[3].asInt()); } + + { + Variant var{Variant::mapType{{"a", 5}}}; + 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(5, arr[3].asInt()); + } } TEST(StructType, buildWithIndicesAndDefaults) { - Logger logger; Manager mgr; Rooted<StructType> structType = createStructType(mgr, logger); @@ -561,7 +559,6 @@ TEST(StructType, buildWithIndicesAndDefaults) } } - /* Class ArrayType */ TEST(ArrayType, rtti) @@ -587,7 +584,6 @@ TEST(ArrayType, creation) TEST(ArrayType, conversion) { - Logger logger; Manager mgr; Rooted<StringType> stringType{new StringType(mgr, nullptr)}; Rooted<ArrayType> arrayType{new ArrayType(mgr, stringType)}; @@ -638,7 +634,6 @@ TEST(UnknownType, creation) TEST(UnknownType, conversion) { - Logger logger; Manager mgr; Rooted<UnknownType> unknownType{new UnknownType(mgr, "unknown")}; |