view C++/tests/Parser/main.cpp @ 334:0b576ee64d45

* Create brand new hierarchy * Rename DynLib to Dynlib * Remove some warnings
author David Demelier <markand@malikania.fr>
date Sun, 08 Mar 2015 14:26:33 +0100
parents C++/Tests/Parser/main.cpp@b686a09fb9c6
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 <gtest/gtest.h>

#include <Parser.h>

TEST(Basic, simple)
{
	try {
		Parser parser("Parser/simple.conf");

		const auto &s = parser.getSection("general");
		ASSERT_EQ("general", s.getName());

		const auto &o1 = s.getOption<std::string>("option1");
		ASSERT_EQ("1", o1);

		const auto &o2 = s.getOption<std::string>("option2");
		ASSERT_EQ("2", o2);

		const auto &o3 = s.getOption<std::string>("option3");
		ASSERT_EQ("3", o3);
	} catch (const std::out_of_range &error) {
		FAIL();
	} catch (const std::runtime_error &error) {
		FAIL() << error.what();
	}
}

TEST(Basic, multi)
{
	try {
		Parser parser("Parser/multi.conf");
		int i(0);

		parser.findSections("entity", [&] (const Section &s) {
			if (i++ == 0) {
				ASSERT_EQ("Player", s.getOption<std::string>("name"));
				ASSERT_EQ("1.0", s.getOption<std::string>("version"));
			} else {
				ASSERT_EQ("Subwinner", s.getOption<std::string>("name"));
				ASSERT_EQ("2.0", s.getOption<std::string>("version"));
			}
		});

		ASSERT_EQ(2, i);
	} catch (const std::out_of_range &error) {
		FAIL();
	} catch (const std::runtime_error &error) {
		FAIL() << error.what();
	}
}

TEST(Basic, multiNoredef)
{
	try {
		Parser parser("Parser/multi.conf", Parser::DisableRedefinition);
		int i(0);

		parser.findSections("entity", [&] (const Section &s) {
			if (i++ == 0) {
				ASSERT_EQ("Player", s.getOption<std::string>("name"));
				ASSERT_EQ("1.0", s.getOption<std::string>("version"));
			}
		});

		ASSERT_EQ(1, i);
	} catch (const std::out_of_range &error) {
		FAIL();
	} catch (const std::runtime_error &error) {
		FAIL() << error.what();
	}
}

int main(int argc, char **argv)
{
	testing::InitGoogleTest(&argc, argv);

	return RUN_ALL_TESTS();
}