Mercurial > code
comparison C++/Tests/Parser/TestParser.cpp @ 224:ca69910b1407
Parser: add tests (and fix #270)
author | David Demelier <markand@malikania.fr> |
---|---|
date | Fri, 09 May 2014 09:15:52 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
223:c6513d9c696b | 224:ca69910b1407 |
---|---|
1 /* | |
2 * TestParser.cpp -- test the config file parser | |
3 * | |
4 * Copyright (c) 2013, 2014 David Demelier <markand@malikania.fr> | |
5 * | |
6 * Permission to use, copy, modify, and/or distribute this software for any | |
7 * purpose with or without fee is hereby granted, provided that the above | |
8 * copyright notice and this permission notice appear in all copies. | |
9 * | |
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
17 */ | |
18 | |
19 #include <iostream> | |
20 | |
21 #include <cppunit/TextTestRunner.h> | |
22 | |
23 #include <Parser.h> | |
24 | |
25 #include "TestParser.h" | |
26 | |
27 void TestParser::simple() | |
28 { | |
29 try { | |
30 Parser parser("simple.conf"); | |
31 | |
32 const auto &s = parser.getSection("general"); | |
33 CPPUNIT_ASSERT_EQUAL(std::string("general"), s.getName()); | |
34 | |
35 const auto &o1 = s.getOption<std::string>("option1"); | |
36 CPPUNIT_ASSERT_EQUAL(std::string("1"), o1); | |
37 | |
38 const auto &o2 = s.getOption<std::string>("option2"); | |
39 CPPUNIT_ASSERT_EQUAL(std::string("2"), o2); | |
40 | |
41 const auto &o3 = s.getOption<std::string>("option3"); | |
42 CPPUNIT_ASSERT_EQUAL(std::string("3"), o3); | |
43 } catch (const std::out_of_range &error) { | |
44 CPPUNIT_ASSERT_MESSAGE("unexpected exception", false); | |
45 } catch (const std::runtime_error &error) { | |
46 std::cout << "skipping because: " << error.what() << std::endl; | |
47 } | |
48 } | |
49 | |
50 void TestParser::multi() | |
51 { | |
52 try { | |
53 Parser parser("multi.conf"); | |
54 int i(0); | |
55 | |
56 parser.findSections("entity", [&] (const Section &s) { | |
57 if (i++ == 0) { | |
58 CPPUNIT_ASSERT_EQUAL(std::string("Player"), s.getOption<std::string>("name")); | |
59 CPPUNIT_ASSERT_EQUAL(std::string("1.0"), s.getOption<std::string>("version")); | |
60 } else { | |
61 CPPUNIT_ASSERT_EQUAL(std::string("Subwinner"), s.getOption<std::string>("name")); | |
62 CPPUNIT_ASSERT_EQUAL(std::string("2.0"), s.getOption<std::string>("version")); | |
63 } | |
64 }); | |
65 | |
66 CPPUNIT_ASSERT_EQUAL(2, i); | |
67 } catch (const std::out_of_range &error) { | |
68 CPPUNIT_ASSERT_MESSAGE("unexpected exception", false); | |
69 } catch (const std::runtime_error &error) { | |
70 std::cout << "skipping because: " << error.what() << std::endl; | |
71 } | |
72 } | |
73 | |
74 void TestParser::multiNoredef() | |
75 { | |
76 try { | |
77 Parser parser("multi.conf", Parser::DisableRedefinition); | |
78 int i(0); | |
79 | |
80 parser.findSections("entity", [&] (const Section &s) { | |
81 if (i++ == 0) { | |
82 CPPUNIT_ASSERT_EQUAL(std::string("Player"), s.getOption<std::string>("name")); | |
83 CPPUNIT_ASSERT_EQUAL(std::string("1.0"), s.getOption<std::string>("version")); | |
84 } | |
85 }); | |
86 | |
87 CPPUNIT_ASSERT_EQUAL(1, i); | |
88 } catch (const std::out_of_range &error) { | |
89 CPPUNIT_ASSERT_MESSAGE("unexpected exception", false); | |
90 } catch (const std::runtime_error &error) { | |
91 std::cout << "skipping because: " << error.what() << std::endl; | |
92 } | |
93 } | |
94 | |
95 int main() | |
96 { | |
97 CppUnit::TextTestRunner runnerText; | |
98 | |
99 runnerText.addTest(TestParser::suite()); | |
100 | |
101 return runnerText.run("", false) == 1 ? 0 : 1; | |
102 } |