From d0d0f978f9dcfad9e009392600b4ac94d25efb16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Wed, 7 Sep 2022 14:06:37 +0200 Subject: [PATCH] Test: start testing code in various command line utilities. Initially, if possible, by extracting the code into separate files and testing those, instead of having to execute a binary and deal with output redirection and other crazy things. The option propagation is already extracted, so do that first. --- src/Magnum/Test/CMakeLists.txt | 3 + src/Magnum/Test/ConverterUtilitiesTest.cpp | 174 +++++++++++++++++++++ 2 files changed, 177 insertions(+) create mode 100644 src/Magnum/Test/ConverterUtilitiesTest.cpp diff --git a/src/Magnum/Test/CMakeLists.txt b/src/Magnum/Test/CMakeLists.txt index cab58ee1a..e22f69069 100644 --- a/src/Magnum/Test/CMakeLists.txt +++ b/src/Magnum/Test/CMakeLists.txt @@ -27,6 +27,9 @@ # property that would have to be set on each target separately. set(CMAKE_FOLDER "Magnum/Test") +find_package(Corrade REQUIRED PluginManager) + +corrade_add_test(ConverterUtilitiesTest ConverterUtilitiesTest.cpp LIBRARIES Magnum Corrade::PluginManager) corrade_add_test(FileCallbackTest FileCallbackTest.cpp LIBRARIES Magnum) corrade_add_test(ImageTest ImageTest.cpp LIBRARIES MagnumTestLib) corrade_add_test(ImageFlagsTest ImageFlagsTest.cpp LIBRARIES Magnum) diff --git a/src/Magnum/Test/ConverterUtilitiesTest.cpp b/src/Magnum/Test/ConverterUtilitiesTest.cpp new file mode 100644 index 000000000..e825673f9 --- /dev/null +++ b/src/Magnum/Test/ConverterUtilitiesTest.cpp @@ -0,0 +1,174 @@ +/* + This file is part of Magnum. + + Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, + 2020, 2021, 2022 Vladimír Vondruš + + Permission is hereby granted, free of charge, to any person obtaining a + copy of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + DEALINGS IN THE SOFTWARE. +*/ + +#include +#include +#include + +#include "Magnum/Implementation/converterUtilities.h" + +namespace Magnum { namespace Test { namespace { + +struct ConverterUtilitiesTest: TestSuite::Tester { + explicit ConverterUtilitiesTest(); + + void setOptions(); +}; + +const struct { + const char* name; + const char* options; + const char* anyPluginName; + const char* expectedConfig; + const char* expectedWarning; +} SetOptionsData[]{ + {"", "option=value", "AnyPlugin", R"([configuration] +option=value +another= +[configuration/group] +option= +[configuration/group/nested] +option= +)", nullptr}, + {"two options", "option=value,another=yes", "AnyPlugin", R"([configuration] +option=value +another=yes +[configuration/group] +option= +[configuration/group/nested] +option= +)", nullptr}, + {"implicit true", "option=value,another", "AnyPlugin", R"([configuration] +option=value +another=true +[configuration/group] +option= +[configuration/group/nested] +option= +)", nullptr}, + {"group", "group/option=value", "AnyPlugin", R"([configuration] +option= +another= +[configuration/group] +option=value +[configuration/group/nested] +option= +)", nullptr}, + {"nested group + root option after", "group/nested/option=value,another=yes", "AnyPlugin", R"([configuration] +option= +another=yes +[configuration/group] +option= +[configuration/group/nested] +option=value +)", nullptr}, + {"unrecognized option", "notFound=value", "AnyPlugin", R"([configuration] +option= +another= +notFound=value +[configuration/group] +option= +[configuration/group/nested] +option= +)", + /* The trailing space is there because the plugin name is empty */ + "Option notFound not recognized by \n"}, + {"unrecognized option in Any plugin", "notFound=value", "", R"([configuration] +option= +another= +notFound=value +[configuration/group] +option= +[configuration/group/nested] +option= +)", nullptr}, + {"unrecognized group", "notFound/option=value", "AnyPlugin", R"([configuration] +option= +another= +[configuration/group] +option= +[configuration/group/nested] +option= +[configuration/notFound] +option=value +)", + /* The trailing space is there because the plugin name is empty */ + "Option notFound/option not recognized by \n"}, + {"unrecognized nested group", "group/notFound/nested/option=value", "AnyPlugin", R"([configuration] +option= +another= +[configuration/group] +option= +[configuration/group/nested] +option= +[configuration/group/notFound/nested] +option=value +)", + /* The trailing space is there because the plugin name is empty */ + "Option group/notFound/nested/option not recognized by \n"} +}; + +ConverterUtilitiesTest::ConverterUtilitiesTest() { + addInstancedTests({&ConverterUtilitiesTest::setOptions}, + Containers::arraySize(SetOptionsData)); +} + +void ConverterUtilitiesTest::setOptions() { + auto&& data = SetOptionsData[testCaseInstanceId()]; + setTestCaseDescription(data.name); + + struct Plugin: PluginManager::AbstractPlugin { + explicit Plugin() { + /* Populate default config */ + configuration().setValue("option", ""); + configuration().setValue("another", ""); + Utility::ConfigurationGroup& group = *configuration().addGroup("group"); + group.setValue("option", ""); + group.addGroup("nested")->setValue("option", ""); + } + } plugin; + + { + std::ostringstream out; + Warning redirectWarning{&out}; + Implementation::setOptions(plugin, data.anyPluginName, data.options); + if(data.expectedWarning) + CORRADE_COMPARE(out.str(), data.expectedWarning); + else + CORRADE_COMPARE(out.str(), ""); + } + + Utility::Configuration conf; + /** @todo ugh, is there no better way to serialize a ConfigurationGroup? */ + conf.addGroup("configuration", new Utility::ConfigurationGroup{plugin.configuration()}); + CORRADE_COMPARE(conf.group("configuration")->configuration(), &conf); + std::ostringstream out; + conf.save(out); + CORRADE_COMPARE(out.str(), data.expectedConfig); +} + +}}} + +CORRADE_TEST_MAIN(Magnum::Test::ConverterUtilitiesTest)