diff options
-rw-r--r-- | src/core/model/Typesystem.cpp | 30 | ||||
-rw-r--r-- | src/core/model/Typesystem.hpp | 40 | ||||
-rw-r--r-- | test/core/model/TypesystemTest.cpp | 44 |
3 files changed, 91 insertions, 23 deletions
diff --git a/src/core/model/Typesystem.cpp b/src/core/model/Typesystem.cpp index 36b6743..9be78d1 100644 --- a/src/core/model/Typesystem.cpp +++ b/src/core/model/Typesystem.cpp @@ -54,6 +54,16 @@ bool StringType::doBuild(Variant &var, Logger &logger) const return true; } +/* Class IntType */ + +bool IntType::doBuild(Variant &var, Logger &logger) const +{ + if (!var.isInt()) { + throw LoggableException{"Expected an integer value."}; + } + return true; +} + /* Class EnumType */ EnumType EnumType::createValidated(Manager &mgr, std::string name, @@ -79,19 +89,17 @@ EnumType EnumType::createValidated(Manager &mgr, std::string name, bool ArrayType::doBuild(Variant &var, Logger &logger) const { - if (!var.isArray()) { - throw LoggableException("Expected array!"); - } - bool res = true; - for (auto &v : var.asArray()) { - if (!innerType->build(v, logger)) { - res = false; - } + if (!var.isArray()) { + throw LoggableException("Expected array!"); + } + bool res = true; + for (auto &v : var.asArray()) { + if (!innerType->build(v, logger)) { + res = false; } - - return res; + } + return res; } - } /* RTTI type registrations */ diff --git a/src/core/model/Typesystem.hpp b/src/core/model/Typesystem.hpp index b05e132..195cf00 100644 --- a/src/core/model/Typesystem.hpp +++ b/src/core/model/Typesystem.hpp @@ -110,6 +110,9 @@ public: /** * Returns the underlying Typesystem instance. + * + * @return a Rooted reference pointing at the underlying typesystem + * instance. */ Rooted<Typesystem> getTypesystem() { @@ -127,13 +130,18 @@ protected: /** * If possible, converts the given variant to a string. Only works, if the * variant contains primitive objects (integers, strings, booleans, etc.). + * + * @param var is a variant containing the data that should be checked and + * converted to a string. + * @param logger is the Logger instance into which errors should be written. + * @return true if the conversion was successful, false otherwise. */ bool doBuild(Variant &var, Logger &logger) const override; public: /** * Constructor of the StringType class. Only one instance of StringType - * should exist per project. + * should exist per project graph. * * @param mgr is the Manager instance to be used for the Node. * @param name is the name of the type. @@ -152,22 +160,30 @@ public: Variant create() const override { return Variant{""}; } }; +/** + * The IntType class represents the primitive integer type. There should exactly + * be a single instance of this class available in a preloaded type system. + */ class IntType : public Type { protected: /** - * TODO: DOC + * Simply expects the given variant to be an integer. Does not perform any + * type conversion. + * + * @param var is a variant containing the data that should be checked. + * @param logger is the Logger instance into which errors should be written. + * @return true if the conversion was successful, false otherwise. */ - bool doBuild(Variant &var, Logger &logger) const override - { - if (!var.isInt()) { - throw LoggableException{"Expected an integer value."}; - } - return true; - } + bool doBuild(Variant &var, Logger &logger) const override; public: /** - * TODO: DOC + * Constructor of the IntType class. Only one instance of IntType should + * exist per project graph. + * + * @param mgr is the Manager instance to be used for the Node. + * @param name is the name of the type. + * @param system is a reference to the parent Typesystem instance. */ IntType(Manager &mgr, Handle<Typesystem> system) : Type(mgr, "int", system, true) @@ -175,7 +191,9 @@ public: } /** - * TODO: DOC + * Returns a variant containing the integer value zero. + * + * @return the integer value zero. */ Variant create() const override { return Variant{0}; } }; diff --git a/test/core/model/TypesystemTest.cpp b/test/core/model/TypesystemTest.cpp index 4c57431..b388fe0 100644 --- a/test/core/model/TypesystemTest.cpp +++ b/test/core/model/TypesystemTest.cpp @@ -103,6 +103,47 @@ TEST(StringType, conversion) } } +/* Class IntType */ + +TEST(IntType, rtti) +{ + Manager mgr; + Rooted<IntType> intType{new IntType(mgr, nullptr)}; + ASSERT_TRUE(intType->isa(RttiTypes::IntType)); + ASSERT_TRUE(intType->isa(typeOf<Type>())); + ASSERT_TRUE(intType->isa(typeOf<Node>())); +} + +TEST(IntType, creation) +{ + Manager mgr; + Rooted<IntType> intType{new IntType(mgr, nullptr)}; + Variant val = intType->create(); + ASSERT_TRUE(val.isInt()); + ASSERT_EQ(0, val.asInt()); +} + +TEST(IntType, conversion) +{ + Logger logger; + Manager mgr; + Rooted<IntType> intType{new IntType(mgr, nullptr)}; + + { + Variant val{314}; + ASSERT_TRUE(intType->build(val, logger)); + ASSERT_TRUE(val.isInt()); + ASSERT_EQ(314, val.asInt()); + } + + { + Variant val{"1"}; + ASSERT_FALSE(intType->build(val, logger)); + ASSERT_TRUE(val.isInt()); + ASSERT_EQ(0, val.asInt()); + } +} + /* Class ArrayType */ TEST(ArrayType, rtti) @@ -123,6 +164,7 @@ TEST(ArrayType, creation) Variant val = arrayType->create(); ASSERT_TRUE(val.isArray()); + ASSERT_TRUE(val.asArray().empty()); } TEST(ArrayType, conversion) @@ -152,7 +194,7 @@ TEST(ArrayType, conversion) Variant val{1}; ASSERT_FALSE(arrayType->build(val, logger)); ASSERT_TRUE(val.isArray()); - ASSERT_EQ(0U, val.asArray().size()); + ASSERT_TRUE(val.asArray().empty()); } } |