diff --git a/model/XmlUtil.cpp b/model/XmlUtil.cpp index 0d4a5fe..94cc263 100644 --- a/model/XmlUtil.cpp +++ b/model/XmlUtil.cpp @@ -87,7 +87,7 @@ namespace glabels if ( valueString != "" ) { bool ok; - double value = valueString.toDouble(& ok ); + double value = valueString.toDouble( &ok ); if ( !ok ) { @@ -112,12 +112,13 @@ namespace glabels QString valueString = node.attribute( name, "" ); if ( valueString != "" ) { - int intValue = valueString.toInt(); + bool ok; + int intValue = valueString.toInt( &ok ); if ( (valueString == "True") || (valueString == "TRUE") || (valueString == "true") || - (intValue == 1) ) + (ok && (intValue == 1) ) ) { return true; } @@ -125,7 +126,7 @@ namespace glabels if ( (valueString == "False") || (valueString == "FALSE") || (valueString == "false") || - (intValue == 0) ) + (ok && (intValue == 0) ) ) { return false; } @@ -149,7 +150,7 @@ namespace glabels if ( valueString != "" ) { bool ok; - int value = valueString.toInt(& ok ); + int value = valueString.toInt( &ok ); if ( !ok ) { @@ -175,7 +176,7 @@ namespace glabels if ( valueString != "" ) { bool ok; - uint32_t value = valueString.toUInt(& ok, 0 ); + uint32_t value = valueString.toUInt( &ok, 0 ); if ( !ok ) { @@ -227,6 +228,7 @@ namespace glabels { qWarning() << "Error: bad length value in attribute " << node.tagName() << ":" << name << "=" << valueString; + return default_value; } return Distance( value, unitsString ); @@ -253,6 +255,12 @@ namespace glabels { return QFont::Normal; } + else + { + qWarning() << "Error: bad weight value in attribute " + << node.tagName() << ":" << name << "=" << valueString; + return default_value; + } } return default_value; @@ -292,6 +300,12 @@ namespace glabels { return Qt::AlignTop; } + else + { + qWarning() << "Error: bad alignment value in attribute " + << node.tagName() << ":" << name << "=" << valueString; + return default_value; + } } return default_value; @@ -319,6 +333,12 @@ namespace glabels { return QTextOption::NoWrap; } + else + { + qWarning() << "Error: bad wrap mode value in attribute " + << node.tagName() << ":" << name << "=" << valueString; + return default_value; + } } return default_value; diff --git a/model/unit_tests/CMakeLists.txt b/model/unit_tests/CMakeLists.txt index 16adeed..5edddd8 100644 --- a/model/unit_tests/CMakeLists.txt +++ b/model/unit_tests/CMakeLists.txt @@ -8,4 +8,12 @@ if (Qt5Test_FOUND) target_link_libraries (TestSubstitutionField Model Qt5::Test) add_test (NAME SubstitutionField COMMAND TestSubstitutionField) + #======================================= + # Test XmlUtil class + #======================================= + qt5_wrap_cpp (TestXmlUtil_moc_sources TestXmlUtil.h) + add_executable (TestXmlUtil TestXmlUtil.cpp ${TestXmlUtil_moc_sources}) + target_link_libraries (TestXmlUtil Model Qt5::Test) + add_test (NAME XmlUtil COMMAND TestXmlUtil) + endif (Qt5Test_FOUND) diff --git a/model/unit_tests/TestXmlUtil.cpp b/model/unit_tests/TestXmlUtil.cpp new file mode 100644 index 0000000..dc64f7d --- /dev/null +++ b/model/unit_tests/TestXmlUtil.cpp @@ -0,0 +1,298 @@ +/* TestXmlUtil.cpp + * + * Copyright (C) 2018 Jim Evins + * + * This file is part of gLabels-qt. + * + * gLabels-qt is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * gLabels-qt is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with gLabels-qt. If not, see . + */ + +#include "TestXmlUtil.h" + +#include "model/XmlUtil.h" + + +QTEST_MAIN(TestXmlUtil) + + +void TestXmlUtil::getStringAttr() +{ + using namespace glabels::model; + + // Test XML + QString xml = ""; + + // Setup document and extract root node + QDomDocument doc; + QCOMPARE( doc.setContent( xml, false ), true ); + QDomElement node = doc.documentElement(); + QCOMPARE( node.tagName(), QString("root") ); + + // + // Tests + // + QCOMPARE( XmlUtil::getStringAttr( node, "a", "Default" ), QString( "A test string" ) ); + + // non-existant attribute, use default + QCOMPARE( XmlUtil::getStringAttr( node, "b", "Default" ), QString( "Default" ) ); +} + + +void TestXmlUtil::getDoubleAttr() +{ + using namespace glabels::model; + + // Test XML + QString xml = ""; + + // Setup document and extract root node + QDomDocument doc; + QCOMPARE( doc.setContent( xml, false ), true ); + QDomElement node = doc.documentElement(); + QCOMPARE( node.tagName(), QString("root") ); + + // + // Tests + // + QCOMPARE( XmlUtil::getDoubleAttr( node, "a", 3.14 ), 0.0 ); + QCOMPARE( XmlUtil::getDoubleAttr( node, "b", 3.14 ), 0.0 ); + QCOMPARE( XmlUtil::getDoubleAttr( node, "c", 3.14 ), 1.0 ); + QCOMPARE( XmlUtil::getDoubleAttr( node, "d", 3.14 ), 1.5 ); + QCOMPARE( XmlUtil::getDoubleAttr( node, "e", 3.14 ), -0.15 ); + + // bad value, use default + QCOMPARE( XmlUtil::getDoubleAttr( node, "f", 3.14 ), 3.14 ); + + // non-existant attribute, use default + QCOMPARE( XmlUtil::getDoubleAttr( node, "g", 3.14 ), 3.14 ); +} + + +void TestXmlUtil::getBoolAttr() +{ + using namespace glabels::model; + + // Test XML + QString xml = ""; + + // Setup document and extract root node + QDomDocument doc; + QCOMPARE( doc.setContent( xml, false ), true ); + QDomElement node = doc.documentElement(); + QCOMPARE( node.tagName(), QString("root") ); + + // + // Tests + // + QCOMPARE( XmlUtil::getBoolAttr( node, "a", false ), false ); + QCOMPARE( XmlUtil::getBoolAttr( node, "a", true ), false ); + QCOMPARE( XmlUtil::getBoolAttr( node, "b", false ), true ); + QCOMPARE( XmlUtil::getBoolAttr( node, "b", true ), true ); + QCOMPARE( XmlUtil::getBoolAttr( node, "c", false ), true ); + QCOMPARE( XmlUtil::getBoolAttr( node, "c", true ), true ); + QCOMPARE( XmlUtil::getBoolAttr( node, "d", false ), false ); + QCOMPARE( XmlUtil::getBoolAttr( node, "d", true ), false ); + + // bad value, use default + QCOMPARE( XmlUtil::getBoolAttr( node, "e", false ), false ); + QCOMPARE( XmlUtil::getBoolAttr( node, "e", true ), true ); + + // non-existant attribute, use default + QCOMPARE( XmlUtil::getBoolAttr( node, "f", false ), false ); + QCOMPARE( XmlUtil::getBoolAttr( node, "f", true ), true ); +} + + +void TestXmlUtil::getIntAttr() +{ + using namespace glabels::model; + + // Test XML + QString xml = ""; + + // Setup document and extract root node + QDomDocument doc; + QCOMPARE( doc.setContent( xml, false ), true ); + QDomElement node = doc.documentElement(); + QCOMPARE( node.tagName(), QString("root") ); + + // + // Tests + // + QCOMPARE( XmlUtil::getIntAttr( node, "a", 456 ), 0 ); + QCOMPARE( XmlUtil::getIntAttr( node, "b", 456 ), 1 ); + QCOMPARE( XmlUtil::getIntAttr( node, "c", 456 ), -11 ); + QCOMPARE( XmlUtil::getIntAttr( node, "d", 456 ), 1234567890 ); + + // bad value, use default + QCOMPARE( XmlUtil::getIntAttr( node, "e", 456 ), 456 ); + + // non-existant attribute, use default + QCOMPARE( XmlUtil::getIntAttr( node, "f", 456 ), 456 ); +} + + +void TestXmlUtil::getUIntAttr() +{ + using namespace glabels::model; + + // Test XML + QString xml = ""; + + // Setup document and extract root node + QDomDocument doc; + QCOMPARE( doc.setContent( xml, false ), true ); + QDomElement node = doc.documentElement(); + QCOMPARE( node.tagName(), QString("root") ); + + // + // Tests + // + QCOMPARE( XmlUtil::getUIntAttr( node, "a", 0xdefa17 ), 0u ); + QCOMPARE( XmlUtil::getUIntAttr( node, "b", 0xdefa17 ), 1u ); + QCOMPARE( XmlUtil::getUIntAttr( node, "c", 0xdefa17 ), 1234567890u ); + QCOMPARE( XmlUtil::getUIntAttr( node, "d", 0xdefa17 ), 0xbaadbeef ); + QCOMPARE( XmlUtil::getUIntAttr( node, "e", 0xdefa17 ), 0xc001000f ); + + // bad value, use default + QCOMPARE( XmlUtil::getUIntAttr( node, "f", 0xdefa17 ), 0xdefa17u ); + + // non-existant attribute, use default + QCOMPARE( XmlUtil::getUIntAttr( node, "g", 0xdefa17 ), 0xdefa17u ); +} + + +void TestXmlUtil::getLengthAttr() +{ + using namespace glabels::model; + + // Test XML + QString xml = ""; + + // Setup document and extract root node + QDomDocument doc; + QCOMPARE( doc.setContent( xml, false ), true ); + QDomElement node = doc.documentElement(); + QCOMPARE( node.tagName(), QString("root") ); + + // + // Tests + // + QCOMPARE( XmlUtil::getLengthAttr( node, "a", Distance::pt(1234) ), Distance::pt(0.0) ); + QCOMPARE( XmlUtil::getLengthAttr( node, "b", Distance::pt(1234) ), Distance::pt(-1.0) ); + QCOMPARE( XmlUtil::getLengthAttr( node, "c", Distance::pt(1234) ), Distance::pt(720) ); + QCOMPARE( XmlUtil::getLengthAttr( node, "d", Distance::pt(1234) ), Distance::mm(1.5) ); + QCOMPARE( XmlUtil::getLengthAttr( node, "e", Distance::pt(1234) ), Distance::cm(-0.15) ); + QCOMPARE( XmlUtil::getLengthAttr( node, "f", Distance::pt(1234) ), Distance::pt(3) ); + + // bad value, use default + QCOMPARE( XmlUtil::getLengthAttr( node, "g", Distance::pt(1234) ), Distance::pt(1234) ); + QCOMPARE( XmlUtil::getLengthAttr( node, "h", Distance::pt(1234) ), Distance::pt(1234) ); + + // non-existant attribute, use default + QCOMPARE( XmlUtil::getLengthAttr( node, "i", Distance::pt(1234) ), Distance::pt(1234) ); +} + + +void TestXmlUtil::getWeightAttr() +{ + using namespace glabels::model; + + // Test XML + QString xml = ""; + + // Setup document and extract root node + QDomDocument doc; + QCOMPARE( doc.setContent( xml, false ), true ); + QDomElement node = doc.documentElement(); + QCOMPARE( node.tagName(), QString("root") ); + + // + // Tests + // + QCOMPARE( XmlUtil::getWeightAttr( node, "a", QFont::Normal ), QFont::Bold ); + QCOMPARE( XmlUtil::getWeightAttr( node, "a", QFont::Bold ), QFont::Bold ); + QCOMPARE( XmlUtil::getWeightAttr( node, "b", QFont::Normal ), QFont::Normal ); + QCOMPARE( XmlUtil::getWeightAttr( node, "b", QFont::Bold ), QFont::Normal ); + + // bad value, use default + QCOMPARE( XmlUtil::getWeightAttr( node, "c", QFont::Normal ), QFont::Normal ); + QCOMPARE( XmlUtil::getWeightAttr( node, "c", QFont::Bold ), QFont::Bold ); + + // non-existant attribute, use default + QCOMPARE( XmlUtil::getWeightAttr( node, "d", QFont::Normal ), QFont::Normal ); + QCOMPARE( XmlUtil::getWeightAttr( node, "d", QFont::Bold ), QFont::Bold ); +} + + +void TestXmlUtil::getAlignmentAttr() +{ + using namespace glabels::model; + + // Test XML + QString xml = ""; + + // Setup document and extract root node + QDomDocument doc; + QCOMPARE( doc.setContent( xml, false ), true ); + QDomElement node = doc.documentElement(); + QCOMPARE( node.tagName(), QString("root") ); + + // + // Tests + // + QCOMPARE( XmlUtil::getAlignmentAttr( node, "a", Qt::AlignRight ), Qt::AlignLeft ); + QCOMPARE( XmlUtil::getAlignmentAttr( node, "b", Qt::AlignRight ), Qt::AlignHCenter ); + QCOMPARE( XmlUtil::getAlignmentAttr( node, "c", Qt::AlignLeft ), Qt::AlignRight ); + QCOMPARE( XmlUtil::getAlignmentAttr( node, "d", Qt::AlignTop ), Qt::AlignBottom ); + QCOMPARE( XmlUtil::getAlignmentAttr( node, "e", Qt::AlignTop ), Qt::AlignVCenter ); + QCOMPARE( XmlUtil::getAlignmentAttr( node, "f", Qt::AlignBottom ), Qt::AlignTop ); + + // bad value, use default + QCOMPARE( XmlUtil::getAlignmentAttr( node, "g", Qt::AlignTop ), Qt::AlignTop ); + QCOMPARE( XmlUtil::getAlignmentAttr( node, "g", Qt::AlignLeft ), Qt::AlignLeft ); + + // non-existant attribute, use default + QCOMPARE( XmlUtil::getAlignmentAttr( node, "h", Qt::AlignBottom ), Qt::AlignBottom ); +} + + +void TestXmlUtil::getWrapModeAttr() +{ + using namespace glabels::model; + + // Test XML + QString xml = ""; + + // Setup document and extract root node + QDomDocument doc; + QCOMPARE( doc.setContent( xml, false ), true ); + QDomElement node = doc.documentElement(); + QCOMPARE( node.tagName(), QString("root") ); + + // + // Tests + // + QCOMPARE( XmlUtil::getWrapModeAttr( node, "a", QTextOption::NoWrap ), QTextOption::WordWrap ); + QCOMPARE( XmlUtil::getWrapModeAttr( node, "b", QTextOption::NoWrap ), QTextOption::WrapAnywhere ); + QCOMPARE( XmlUtil::getWrapModeAttr( node, "c", QTextOption::WordWrap ), QTextOption::NoWrap ); + + // bad value, use default + QCOMPARE( XmlUtil::getWrapModeAttr( node, "d", QTextOption::NoWrap ), QTextOption::NoWrap ); + QCOMPARE( XmlUtil::getWrapModeAttr( node, "d", QTextOption::WordWrap ), QTextOption::WordWrap ); + + // non-existant attribute, use default + QCOMPARE( XmlUtil::getWrapModeAttr( node, "e", QTextOption::NoWrap ), QTextOption::NoWrap ); + QCOMPARE( XmlUtil::getWrapModeAttr( node, "e", QTextOption::WrapAnywhere ), QTextOption::WrapAnywhere ); +} diff --git a/model/unit_tests/TestXmlUtil.h b/model/unit_tests/TestXmlUtil.h new file mode 100644 index 0000000..f104b53 --- /dev/null +++ b/model/unit_tests/TestXmlUtil.h @@ -0,0 +1,42 @@ +/* TestXmlUtil.h + * + * Copyright (C) 2018 Jim Evins + * + * This file is part of gLabels-qt. + * + * gLabels-qt is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * gLabels-qt is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with gLabels-qt. If not, see . + */ + +#include + + +class TestXmlUtil : public QObject +{ + Q_OBJECT + +private slots: + void getStringAttr(); + void getDoubleAttr(); + void getBoolAttr(); + void getIntAttr(); + void getUIntAttr(); + void getLengthAttr(); + void getWeightAttr(); + void getAlignmentAttr(); + void getWrapModeAttr(); + + // TODO: test setters +}; + +