Mercurial > code
changeset 224:ca69910b1407
Parser: add tests (and fix #270)
author | David Demelier <markand@malikania.fr> |
---|---|
date | Fri, 09 May 2014 09:15:52 +0200 |
parents | c6513d9c696b |
children | e01ee0c72c43 |
files | C++/Parser.cpp C++/Tests/Parser/CMakeLists.txt C++/Tests/Parser/TestParser.cpp C++/Tests/Parser/TestParser.h C++/Tests/Parser/configs/multi.conf C++/Tests/Parser/configs/simple.conf CMakeLists.txt |
diffstat | 7 files changed, 194 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/C++/Parser.cpp Thu May 08 23:51:07 2014 +0200 +++ b/C++/Parser.cpp Fri May 09 09:15:52 2014 +0200 @@ -144,7 +144,7 @@ size_t i, begin, last; char c; - key = line.substr(0, epos - 1); + key = line.substr(0, epos); value = line.substr(epos + 1); // clean option key
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/C++/Tests/Parser/CMakeLists.txt Fri May 09 09:15:52 2014 +0200 @@ -0,0 +1,37 @@ +# +# CMakeLists.txt -- tests for Parser +# +# Copyright (c) 2014 David Demelier <markand@malikania.fr> +# +# Permission to use, copy, modify, and/or distribute this software for any +# purpose with or without fee is hereby granted, provided that the above +# copyright notice and this permission notice appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +# + +set( + SOURCES + ${code_SOURCE_DIR}/C++/Parser.cpp + ${code_SOURCE_DIR}/C++/Parser.h + TestParser.cpp + TestParser.h + configs/simple.conf + configs/multi.conf +) + +define_test(parser "${SOURCES}") + +add_custom_command( + TARGET parser + POST_BUILD + COMMENT "Copying examples files" + COMMAND + ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/configs $<TARGET_FILE_DIR:parser> +) \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/C++/Tests/Parser/TestParser.cpp Fri May 09 09:15:52 2014 +0200 @@ -0,0 +1,102 @@ +/* + * TestParser.cpp -- test the config file parser + * + * Copyright (c) 2013, 2014 David Demelier <markand@malikania.fr> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <iostream> + +#include <cppunit/TextTestRunner.h> + +#include <Parser.h> + +#include "TestParser.h" + +void TestParser::simple() +{ + try { + Parser parser("simple.conf"); + + const auto &s = parser.getSection("general"); + CPPUNIT_ASSERT_EQUAL(std::string("general"), s.getName()); + + const auto &o1 = s.getOption<std::string>("option1"); + CPPUNIT_ASSERT_EQUAL(std::string("1"), o1); + + const auto &o2 = s.getOption<std::string>("option2"); + CPPUNIT_ASSERT_EQUAL(std::string("2"), o2); + + const auto &o3 = s.getOption<std::string>("option3"); + CPPUNIT_ASSERT_EQUAL(std::string("3"), o3); + } catch (const std::out_of_range &error) { + CPPUNIT_ASSERT_MESSAGE("unexpected exception", false); + } catch (const std::runtime_error &error) { + std::cout << "skipping because: " << error.what() << std::endl; + } +} + +void TestParser::multi() +{ + try { + Parser parser("multi.conf"); + int i(0); + + parser.findSections("entity", [&] (const Section &s) { + if (i++ == 0) { + CPPUNIT_ASSERT_EQUAL(std::string("Player"), s.getOption<std::string>("name")); + CPPUNIT_ASSERT_EQUAL(std::string("1.0"), s.getOption<std::string>("version")); + } else { + CPPUNIT_ASSERT_EQUAL(std::string("Subwinner"), s.getOption<std::string>("name")); + CPPUNIT_ASSERT_EQUAL(std::string("2.0"), s.getOption<std::string>("version")); + } + }); + + CPPUNIT_ASSERT_EQUAL(2, i); + } catch (const std::out_of_range &error) { + CPPUNIT_ASSERT_MESSAGE("unexpected exception", false); + } catch (const std::runtime_error &error) { + std::cout << "skipping because: " << error.what() << std::endl; + } +} + +void TestParser::multiNoredef() +{ + try { + Parser parser("multi.conf", Parser::DisableRedefinition); + int i(0); + + parser.findSections("entity", [&] (const Section &s) { + if (i++ == 0) { + CPPUNIT_ASSERT_EQUAL(std::string("Player"), s.getOption<std::string>("name")); + CPPUNIT_ASSERT_EQUAL(std::string("1.0"), s.getOption<std::string>("version")); + } + }); + + CPPUNIT_ASSERT_EQUAL(1, i); + } catch (const std::out_of_range &error) { + CPPUNIT_ASSERT_MESSAGE("unexpected exception", false); + } catch (const std::runtime_error &error) { + std::cout << "skipping because: " << error.what() << std::endl; + } +} + +int main() +{ + CppUnit::TextTestRunner runnerText; + + runnerText.addTest(TestParser::suite()); + + return runnerText.run("", false) == 1 ? 0 : 1; +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/C++/Tests/Parser/TestParser.h Fri May 09 09:15:52 2014 +0200 @@ -0,0 +1,39 @@ +/* + * TestParser.h -- test the config file parser + * + * Copyright (c) 2013, 2014 David Demelier <markand@malikania.fr> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef _TEST_PARSER_H_ +#define _TEST_PARSER_H_ + +#include <cppunit/extensions/HelperMacros.h> +#include <cppunit/TestFixture.h> + +class TestParser : public CppUnit::TestFixture { +private: + CPPUNIT_TEST_SUITE(TestParser); + CPPUNIT_TEST(simple); + CPPUNIT_TEST(multi); + CPPUNIT_TEST(multiNoredef); + CPPUNIT_TEST_SUITE_END(); + +public: + void simple(); + void multi(); + void multiNoredef(); +}; + +#endif // !_TEST_PARSER_H_ \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/C++/Tests/Parser/configs/multi.conf Fri May 09 09:15:52 2014 +0200 @@ -0,0 +1,7 @@ +[entity] +name = "Player" +version = 1.0 + +[entity] +name = "Subwinner" +version = 2.0 \ No newline at end of file