view C++/Tests/Parser/TestParser.cpp @ 236:ff2db0ed78f1

* Import GoogleTest * Start testing of OptionParser
author David Demelier <markand@malikania.fr>
date Fri, 04 Jul 2014 22:16:04 +0200
parents ca69910b1407
children
line wrap: on
line source

/*
 * 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;
}