mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-30 21:50:47 +01:00
* Allow set enum as json value (#4363) * fix issue #4341 add testEnum if you set enum as json value then it will be cast to int * add behavour for enum into VarHolderImpl add tests for enum classes with differnent underline integral types * replace static_cast with convertTo* methods, they can chaeck types and limits * fix missing types for convert methods * fix code style add testEnumType to the VarTest.cpp, extract() works * enh(VarHolder): replace ::value and ::type with shortcuts --------- Co-authored-by: Alexander B <ale.bychuk@gmail.com>
This commit is contained in:
committed by
GitHub
parent
03444bdcea
commit
669be63134
@@ -2369,7 +2369,178 @@ void JSONTest::testRemove()
|
||||
assertTrue(nl[1] == "baz");
|
||||
|
||||
}
|
||||
|
||||
void JSONTest::testEnum()
|
||||
{
|
||||
enum SAMPLE_ENUM
|
||||
{
|
||||
SE_VALUE = 42
|
||||
};
|
||||
|
||||
enum class SAMPLE_ENUM_CLASS
|
||||
{
|
||||
VALUE = 42
|
||||
};
|
||||
|
||||
enum class SAMPLE_ENUM_CLASS_I8: Poco::Int8
|
||||
{
|
||||
VALUE = 42
|
||||
};
|
||||
|
||||
enum class SAMPLE_ENUM_CLASS_I16: Poco::Int16
|
||||
{
|
||||
VALUE = 42
|
||||
};
|
||||
|
||||
enum class SAMPLE_ENUM_CLASS_I32: Poco::Int32
|
||||
{
|
||||
VALUE = 42
|
||||
};
|
||||
|
||||
enum class SAMPLE_ENUM_CLASS_I64: Poco::Int64
|
||||
{
|
||||
VALUE = 42
|
||||
};
|
||||
|
||||
enum class SAMPLE_ENUM_CLASS_UI8: Poco::UInt8
|
||||
{
|
||||
VALUE = 42
|
||||
};
|
||||
|
||||
enum class SAMPLE_ENUM_CLASS_UI16: Poco::UInt16
|
||||
{
|
||||
VALUE = 42
|
||||
};
|
||||
|
||||
enum class SAMPLE_ENUM_CLASS_UI32: Poco::UInt32
|
||||
{
|
||||
VALUE = 42
|
||||
};
|
||||
|
||||
enum class SAMPLE_ENUM_CLASS_UI64: Poco::UInt64
|
||||
{
|
||||
VALUE = 42
|
||||
};
|
||||
|
||||
{
|
||||
Poco::JSON::Object::Ptr obj = new Poco::JSON::Object();
|
||||
obj->set("simple_enum", SE_VALUE);
|
||||
Poco::Dynamic::Var var(obj);
|
||||
std::string expected = "{\"simple_enum\":42}";
|
||||
std::string result = var.convert<std::string>();
|
||||
assertEquals(expected, result);
|
||||
|
||||
SAMPLE_ENUM se = obj->get("simple_enum").extract<SAMPLE_ENUM>();
|
||||
assertTrue(se == SE_VALUE);
|
||||
}
|
||||
|
||||
{
|
||||
Poco::JSON::Object::Ptr obj = new Poco::JSON::Object();
|
||||
obj->set("enum_class", SAMPLE_ENUM_CLASS::VALUE);
|
||||
Poco::Dynamic::Var var(obj);
|
||||
std::string expected = "{\"enum_class\":42}";
|
||||
std::string result = var.convert<std::string>();
|
||||
assertEquals(expected, result);
|
||||
|
||||
SAMPLE_ENUM_CLASS se = obj->get("enum_class").extract<SAMPLE_ENUM_CLASS>();
|
||||
assertTrue(se == SAMPLE_ENUM_CLASS::VALUE);
|
||||
}
|
||||
|
||||
{
|
||||
Poco::JSON::Object::Ptr obj = new Poco::JSON::Object();
|
||||
obj->set("enum_class_i8", SAMPLE_ENUM_CLASS_I8::VALUE);
|
||||
Poco::Dynamic::Var var(obj);
|
||||
std::string expected = "{\"enum_class_i8\":42}";
|
||||
std::string result = var.convert<std::string>();
|
||||
assertEquals(expected, result);
|
||||
|
||||
SAMPLE_ENUM_CLASS_I8 se = obj->get("enum_class_i8").extract<SAMPLE_ENUM_CLASS_I8>();
|
||||
assertTrue(se == SAMPLE_ENUM_CLASS_I8::VALUE);
|
||||
}
|
||||
|
||||
{
|
||||
Poco::JSON::Object::Ptr obj = new Poco::JSON::Object();
|
||||
obj->set("enum_class_i16", SAMPLE_ENUM_CLASS_I16::VALUE);
|
||||
Poco::Dynamic::Var var(obj);
|
||||
std::string expected = "{\"enum_class_i16\":42}";
|
||||
std::string result = var.convert<std::string>();
|
||||
assertEquals(expected, result);
|
||||
|
||||
SAMPLE_ENUM_CLASS_I16 se = obj->get("enum_class_i16").extract<SAMPLE_ENUM_CLASS_I16>();
|
||||
assertTrue(se == SAMPLE_ENUM_CLASS_I16::VALUE);
|
||||
}
|
||||
|
||||
{
|
||||
Poco::JSON::Object::Ptr obj = new Poco::JSON::Object();
|
||||
obj->set("enum_class_i32", SAMPLE_ENUM_CLASS_I32::VALUE);
|
||||
Poco::Dynamic::Var var(obj);
|
||||
std::string expected = "{\"enum_class_i32\":42}";
|
||||
std::string result = var.convert<std::string>();
|
||||
assertEquals(expected, result);
|
||||
|
||||
SAMPLE_ENUM_CLASS_I32 se = obj->get("enum_class_i32").extract<SAMPLE_ENUM_CLASS_I32>();
|
||||
assertTrue(se == SAMPLE_ENUM_CLASS_I32::VALUE);
|
||||
}
|
||||
|
||||
{
|
||||
Poco::JSON::Object::Ptr obj = new Poco::JSON::Object();
|
||||
obj->set("enum_class_i64", SAMPLE_ENUM_CLASS_I64::VALUE);
|
||||
Poco::Dynamic::Var var(obj);
|
||||
std::string expected = "{\"enum_class_i64\":42}";
|
||||
std::string result = var.convert<std::string>();
|
||||
assertEquals(expected, result);
|
||||
|
||||
SAMPLE_ENUM_CLASS_I64 se = obj->get("enum_class_i64").extract<SAMPLE_ENUM_CLASS_I64>();
|
||||
assertTrue(se == SAMPLE_ENUM_CLASS_I64::VALUE);
|
||||
}
|
||||
|
||||
{
|
||||
Poco::JSON::Object::Ptr obj = new Poco::JSON::Object();
|
||||
obj->set("enum_class_ui8", SAMPLE_ENUM_CLASS_UI8::VALUE);
|
||||
Poco::Dynamic::Var var(obj);
|
||||
std::string expected = "{\"enum_class_ui8\":42}";
|
||||
std::string result = var.convert<std::string>();
|
||||
assertEquals(expected, result);
|
||||
|
||||
SAMPLE_ENUM_CLASS_UI8 se = obj->get("enum_class_ui8").extract<SAMPLE_ENUM_CLASS_UI8>();
|
||||
assertTrue(se == SAMPLE_ENUM_CLASS_UI8::VALUE);
|
||||
}
|
||||
|
||||
{
|
||||
Poco::JSON::Object::Ptr obj = new Poco::JSON::Object();
|
||||
obj->set("enum_class_ui16", SAMPLE_ENUM_CLASS_UI16::VALUE);
|
||||
Poco::Dynamic::Var var(obj);
|
||||
std::string expected = "{\"enum_class_ui16\":42}";
|
||||
std::string result = var.convert<std::string>();
|
||||
assertEquals(expected, result);
|
||||
|
||||
SAMPLE_ENUM_CLASS_UI16 se = obj->get("enum_class_ui16").extract<SAMPLE_ENUM_CLASS_UI16>();
|
||||
assertTrue(se == SAMPLE_ENUM_CLASS_UI16::VALUE);
|
||||
}
|
||||
|
||||
{
|
||||
Poco::JSON::Object::Ptr obj = new Poco::JSON::Object();
|
||||
obj->set("enum_class_ui32", SAMPLE_ENUM_CLASS_UI32::VALUE);
|
||||
Poco::Dynamic::Var var(obj);
|
||||
std::string expected = "{\"enum_class_ui32\":42}";
|
||||
std::string result = var.convert<std::string>();
|
||||
assertEquals(expected, result);
|
||||
|
||||
SAMPLE_ENUM_CLASS_UI32 se = obj->get("enum_class_ui32").extract<SAMPLE_ENUM_CLASS_UI32>();
|
||||
assertTrue(se == SAMPLE_ENUM_CLASS_UI32::VALUE);
|
||||
}
|
||||
|
||||
{
|
||||
Poco::JSON::Object::Ptr obj = new Poco::JSON::Object();
|
||||
obj->set("enum_class_ui64", SAMPLE_ENUM_CLASS_UI64::VALUE);
|
||||
Poco::Dynamic::Var var(obj);
|
||||
std::string expected = "{\"enum_class_ui64\":42}";
|
||||
std::string result = var.convert<std::string>();
|
||||
assertEquals(expected, result);
|
||||
|
||||
SAMPLE_ENUM_CLASS_UI64 se = obj->get("enum_class_ui64").extract<SAMPLE_ENUM_CLASS_UI64>();
|
||||
assertTrue(se == SAMPLE_ENUM_CLASS_UI64::VALUE);
|
||||
}
|
||||
}
|
||||
|
||||
CppUnit::Test* JSONTest::suite()
|
||||
{
|
||||
@@ -2424,6 +2595,7 @@ CppUnit::Test* JSONTest::suite()
|
||||
CppUnit_addTest(pSuite, JSONTest, testCopy);
|
||||
CppUnit_addTest(pSuite, JSONTest, testMove);
|
||||
CppUnit_addTest(pSuite, JSONTest, testRemove);
|
||||
CppUnit_addTest(pSuite, JSONTest, testEnum);
|
||||
|
||||
return pSuite;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user