Mercurial > code
view C++/tests/Ini/main.cpp @ 426:cee5c74c1c83
Ini:
- Multiple fixes about using invalid iterators,
- Add more tests about line/position in ini::Error.
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 14 Oct 2015 10:04:24 +0200 |
parents | 0d004aba3ff6 |
children | aa9cc55338be |
line wrap: on
line source
/* * main.cpp -- main test file for Ini * * Copyright (c) 2013-2015 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.h> class BasicTest : public testing::Test { protected: ini::Document m_ini; public: BasicTest() : m_ini("Ini/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/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 defini::Documenttion * -------------------------------------------------------- */ class MultiTest : public testing::Test { protected: ini::Document m_ini; public: MultiTest() : m_ini("Ini/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/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/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/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/empty.conf"}; } catch (const ini::Error &error) { FAIL() << error.line() << ":" << error.position() << ": " << error.what(); } } /* -------------------------------------------------------- * Errors * -------------------------------------------------------- */ TEST(Errors, nosection) { // An option outside a section is not allowed try { ini::Document doc("Ini/error-nosection.conf"); FAIL() << "Failure expected, got success"; } catch (const ini::Error &ex) { ASSERT_EQ(3, ex.line()); ASSERT_EQ(7, ex.position()); } } TEST(Errors, lineassigment) { // The = assignment must be on the same line as the option key try { ini::Document doc("Ini/error-lineassigment.conf"); FAIL() << "Failure expected, got success"; } catch (const ini::Error &ex) { ASSERT_EQ(3, ex.line()); ASSERT_EQ(0, ex.position()); } } TEST(Errors, badcomment) { // Comment can't between option-key and = assigment try { ini::Document doc("Ini/error-badcomment.conf"); FAIL() << "Failure expected, got success"; } catch (const ini::Error &ex) { ASSERT_EQ(2, ex.line()); ASSERT_EQ(9, ex.position()); } } TEST(Errors, badsection) { // Bad section naming try { ini::Document doc("Ini/error-badsection.conf"); FAIL() << "Failure expected, got success"; } catch (const ini::Error &ex) { ASSERT_EQ(1, ex.line()); ASSERT_EQ(1, ex.position()); } } TEST(Errors, unterminatedsection) { // Section unfinished try { ini::Document doc("Ini/error-unterminatedsection.conf"); FAIL() << "Failure expected, got success"; } catch (const ini::Error &ex) { ASSERT_EQ(2, ex.line()); ASSERT_EQ(8, ex.position()); } } TEST(Errors, badinclude) { // Section unfinished try { ini::Document doc("Ini/error-badinclude.conf"); FAIL() << "Failure expected, got success"; } catch (const ini::Error &ex) { ASSERT_EQ(1, ex.line()); ASSERT_EQ(17, ex.position()); } } int main(int argc, char **argv) { testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }