view modules/ini/test/main.cpp @ 521:b604d3dd45b7

Ini: resurrection
author David Demelier <markand@malikania.fr>
date Wed, 01 Jun 2016 16:44:55 +0200
parents
children f48bb09bccc7
line wrap: on
line source

/*
 * main.cpp -- main test file for Ini
 *
 * Copyright (c) 2013-2016 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 <ini.hpp>

class BasicTest : public testing::Test {
protected:
	ini::Document m_ini;

public:
	BasicTest()
		: m_ini(ini::readFile(DIRECTORY "simple.conf"))
	{
	}
};

TEST_F(BasicTest, simple)
{
	ASSERT_EQ(1, static_cast<int>(m_ini.size()));
}

TEST_F(BasicTest, operators)
{
	try {
		ASSERT_EQ(3, static_cast<int>(m_ini[0].size()));
		ASSERT_EQ("general", m_ini[0].key());
		ASSERT_EQ("general", m_ini["general"].key());
	} catch (const std::exception &ex) {
		FAIL() << ex.what();
	}
}

TEST_F(BasicTest, sectionOperators)
{
	try {
		// option1=1 (indexes).
		ASSERT_EQ("option1", m_ini[0][0].key());
		ASSERT_EQ("1", m_ini[0][0].value());

		// option1=1 (keys).
		ASSERT_EQ("option1", m_ini["general"]["option1"].key());
		ASSERT_EQ("1", m_ini["general"]["option1"].value());

		// option2 =2 (indexes).
		ASSERT_EQ("option2", m_ini[0][1].key());
		ASSERT_EQ("2", m_ini[0][1].value());

		// option2 =2 (keys).
		ASSERT_EQ("option2", m_ini["general"]["option2"].key());
		ASSERT_EQ("2", m_ini["general"]["option2"].value());

		// option3 = 3 (indexes).
		ASSERT_EQ("option3", m_ini[0][2].key());
		ASSERT_EQ("3", m_ini[0][2].value());

		// option3 = 3 (keys).
		ASSERT_EQ("option3", m_ini["general"]["option3"].key());
		ASSERT_EQ("3", m_ini["general"]["option3"].value());
	} catch (const std::exception &ex) {
		FAIL() << ex.what();
	}
}

/*
 * Reserved tokens in words.
 * ------------------------------------------------------------------
 */

TEST(Tokens, reserved)
{
	try {
		ini::Document doc = ini::readFile(DIRECTORY "tokens.conf");

		ASSERT_EQ("I have [brackets]", doc["tokens"]["bracket"].value());
		ASSERT_EQ("I have foo@at", doc["tokens"]["at"].value());
	} catch (const std::exception &ex) {
		FAIL() << ex.what();
	}
}

/*
 * Multiple definitions.
 * ------------------------------------------------------------------
 */

class MultiTest : public testing::Test {
protected:
	ini::Document m_ini;

public:
	MultiTest()
		: m_ini(ini::readFile(DIRECTORY "multi.conf"))
	{
	}
};

TEST_F(MultiTest, defined)
{
	ASSERT_EQ(2, static_cast<int>(m_ini.size()));
	ASSERT_EQ("name", m_ini[0]["name"].key());
	ASSERT_EQ("Player", m_ini[0]["name"].value());
	ASSERT_EQ("version", m_ini[0]["version"].key());
	ASSERT_EQ("1.0", m_ini[0]["version"].value());
	ASSERT_EQ("name", m_ini[1]["name"].key());
	ASSERT_EQ("Subwinner", m_ini[1]["name"].value());
	ASSERT_EQ("version", m_ini[1]["version"].key());
	ASSERT_EQ("2.0", m_ini[1]["version"].value());
}

/*
 * Option with no values.
 * ------------------------------------------------------------------
 */

class NoValueTest : public testing::Test {
protected:
	ini::Document m_ini;

public:
	NoValueTest()
		: m_ini(ini::readFile(DIRECTORY "novalue.conf"))
	{
	}
};

TEST_F(NoValueTest, isDefined)
{
	ASSERT_EQ("plugins", m_ini[0].key());
	ASSERT_EQ("", m_ini["plugins"]["histedit"].value());
	ASSERT_EQ("", m_ini["plugins"]["highlight"].value());
	ASSERT_EQ("", m_ini["plugins"]["general"].value());
}

/*
 * Include tests.
 * ------------------------------------------------------------------
 */

class IncludeTest : public testing::Test {
protected:
	ini::Document m_ini;

public:
	IncludeTest()
		: m_ini(ini::readFile(DIRECTORY "includes.conf"))
	{
	}
};

TEST_F(IncludeTest, all)
{
	ASSERT_EQ(2, static_cast<int>(m_ini.size()));

	// from include.
	ASSERT_EQ("1", m_ini[0][0].value());
	ASSERT_EQ("2", m_ini[0][1].value());
	ASSERT_EQ("3", m_ini[0][2].value());

	// from standard.
	ASSERT_EQ("false", m_ini[1][0].value());
}

/*
 * Compact.
 * ------------------------------------------------------------------
 */

TEST(Compact, test)
{
	try {
		ini::Document doc = ini::readFile(DIRECTORY "compact.conf");

		ASSERT_EQ(2, static_cast<int>(doc.size()));
		ASSERT_EQ("true", doc["general"]["verbose"].value());
		ASSERT_EQ("false", doc["general"]["foreground"].value());
		ASSERT_EQ("google.fr", doc["server"]["host"].value());
	} catch (const std::exception &ex) {
		FAIL() << ex.what();
	}
}

/*
 * Empty.
 * ------------------------------------------------------------------
 */

TEST(Empty, test)
{
	try {
		ini::Document doc = ini::readFile(DIRECTORY "empty.conf");
	} catch (const ini::Error &error) {
		FAIL() << error.line() << ":" << error.column() << ": " << error.what();
	}
}

/*
 * List.
 * ------------------------------------------------------------------
 */

TEST(List, test)
{
	try {
		std::vector<std::string> rule1{"abc", "bcd"};
		std::vector<std::string> rule2{"xyz", "poi"};
		ini::Document doc = ini::readFile(DIRECTORY "lists.conf");

		ASSERT_EQ(rule1, doc[0][0]);
		ASSERT_EQ(rule2, doc[1][0]);
	} catch (const ini::Error &error) {
		FAIL() << error.line() << ":" << error.column() << ": " << error.what();
	}
}

/*
 * Errors.
 * ------------------------------------------------------------------
 */

TEST(Errors, nosection)
{
	// An option outside a section is not allowed.
	try {
		ini::Document doc = ini::readFile(DIRECTORY "error-nosection.conf");

		FAIL() << "Failure expected, got success";
	} catch (const ini::Error &ex) {
		ASSERT_EQ(3, ex.line());
		ASSERT_EQ(0, ex.column());
	}
}

TEST(Errors, badcomment)
{
	// Comment can't between option-key and = assigment.
	try {
		ini::Document doc = ini::readFile(DIRECTORY "error-badcomment.conf");

		FAIL() << "Failure expected, got success";
	} catch (const ini::Error &ex) {
		ASSERT_EQ(2, ex.line());
		ASSERT_EQ(0, ex.column());
	}
}

TEST(Errors, badsection)
{
	// Bad section naming
	try {
		ini::Document doc = ini::readFile(DIRECTORY "error-badsection.conf");

		FAIL() << "Failure expected, got success";
	} catch (const ini::Error &ex) {
		ASSERT_EQ(1, ex.line());
		ASSERT_EQ(0, ex.column());
	}
}

TEST(Errors, unterminatedsection)
{
	// Section unfinished.
	try {
		ini::Document doc = ini::readFile(DIRECTORY "error-unterminatedsection.conf");

		FAIL() << "Failure expected, got success";
	} catch (const ini::Error &ex) {
		ASSERT_EQ(2, ex.line());
		ASSERT_EQ(6, ex.column());
	}
}

TEST(Errors, notFound)
{
	ASSERT_ANY_THROW(ini::readFile("does not exists"));
}

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

	return RUN_ALL_TESTS();
}