1 /* -*- Mode: c++ -*- */ 2 /*************************************************************************** 3 * configparsertest.cc 4 * 5 * Wed Jul 25 19:08:40 CEST 2018 6 * Copyright 2018 Bent Bisballe Nyeng 7 * deva@aasimon.org 8 ****************************************************************************/ 9 10 /* 11 * This file is part of DrumGizmo. 12 * 13 * DrumGizmo is free software; you can redistribute it and/or modify 14 * it under the terms of the GNU Lesser General Public License as published by 15 * the Free Software Foundation; either version 3 of the License, or 16 * (at your option) any later version. 17 * 18 * DrumGizmo is distributed in the hope that it will be useful, 19 * but WITHOUT ANY WARRANTY; without even the implied warranty of 20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21 * GNU Lesser General Public License for more details. 22 * 23 * You should have received a copy of the GNU Lesser General Public License 24 * along with DrumGizmo; if not, write to the Free Software 25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. 26 */ 27 #include <uunit.h> 28 29 #include <configparser.h> 30 31 class ConfigParserTest 32 : public uUnit 33 { 34 public: ConfigParserTest()35 ConfigParserTest() 36 { 37 uUNIT_TEST(ConfigParserTest::test); 38 uUNIT_TEST(ConfigParserTest::invalid); 39 } 40 test()41 void test() 42 { 43 std::string xml = 44 "<?xml version='1.0' encoding='UTF-8'?>\n" \ 45 "<config>\n" \ 46 " <value name=\"foo\">42</value>\n" \ 47 " <value name=\"bar\">true</value>\n" \ 48 " <value name=\"bas\">"<</value>\n" \ 49 "</config>"; 50 51 52 ConfigParser parser; 53 uUNIT_ASSERT(parser.parseString(xml)); 54 55 uUNIT_ASSERT_EQUAL(std::string("42"), parser.value("foo", "-")); 56 uUNIT_ASSERT_EQUAL(std::string("true"), parser.value("bar", "-")); 57 uUNIT_ASSERT_EQUAL(std::string("\"<"), parser.value("bas", "-")); 58 59 // Non-existing value 60 uUNIT_ASSERT_EQUAL(std::string("-"), parser.value("bas2", "-")); 61 } 62 invalid()63 void invalid() 64 { 65 std::string xml = 66 "<?xml version='1.0' encoding='UTF-8'?>\n" \ 67 "<config\n" \ 68 " <value name=\"foo\">42</value>\n" \ 69 " <value name=\"bar\">true</value>\n" \ 70 " <value name=\"bas\">"<</value>\n" \ 71 "</config>"; 72 73 74 ConfigParser parser; 75 // Epxect parser error (missing '>' in line 2) 76 uUNIT_ASSERT(!parser.parseString(xml)); 77 } 78 }; 79 80 // Registers the fixture into the 'registry' 81 static ConfigParserTest test; 82