Mercurial > code
changeset 329:43b4163470c2
Ini: improve tests
author | David Demelier <markand@malikania.fr> |
---|---|
date | Fri, 06 Mar 2015 21:57:21 +0100 |
parents | 02e5ff7b9890 |
children | 14d9c7a4f358 |
files | C++/Ini.cpp C++/Tests/Ini/CMakeLists.txt C++/Tests/Ini/configs/compact.conf C++/Tests/Ini/configs/error-badcomment.conf C++/Tests/Ini/configs/error-badsection.conf C++/Tests/Ini/configs/error-lineassigment.conf C++/Tests/Ini/configs/error-nosection.conf C++/Tests/Ini/configs/tokens.conf C++/Tests/Ini/main.cpp |
diffstat | 9 files changed, 109 insertions(+), 7 deletions(-) [+] |
line wrap: on
line diff
--- a/C++/Ini.cpp Wed Mar 04 13:18:00 2015 +0100 +++ b/C++/Ini.cpp Fri Mar 06 21:57:21 2015 +0100 @@ -19,6 +19,7 @@ #include <cctype> #include <cerrno> #include <cstring> +#include <fstream> #include <iostream> #include <iterator> #include <memory> @@ -229,7 +230,7 @@ } return path; - } + } #if defined(_WIN32) bool isAbsolute(const std::string &path) @@ -364,17 +365,17 @@ readSpace(it, end); - if (it == end || it++->type() != TokenType::Assign) { - throw IniError(it->line(), it->position(), "expected '=' after option declaration, got " + it->toString()); + if (it == end || it->type() != TokenType::Assign) { + throw IniError(it->line(), it->position(), "expected '=' after option declaration, got " + it++->toString()); } - readSpace(it, end); + readSpace(++it, end); std::ostringstream oss; if (it->type() == TokenType::QuoteSimple || it->type() == TokenType::QuoteDouble) { TokenStack::iterator save = it++; - + while (it != end && it->type() != save->type()) { oss << it++->value(); } @@ -389,7 +390,7 @@ // No value requested, must be NewLine or comment throw IniError(it->line(), it->position(), "expected option value after '=', got " + it->toString()); } - + return IniOption(std::move(key), oss.str()); } @@ -437,7 +438,7 @@ public: IniBuilder(Ini &ini, std::string path) : m_path(path) - , m_base(base(std::move(path))) + , m_base(base(std::move(path))) , m_ini(ini) { std::ifstream file(m_path);
--- a/C++/Tests/Ini/CMakeLists.txt Wed Mar 04 13:18:00 2015 +0100 +++ b/C++/Tests/Ini/CMakeLists.txt Fri Mar 06 21:57:21 2015 +0100 @@ -24,6 +24,11 @@ configs/simple.conf configs/multi.conf configs/novalue.conf + configs/compact.conf + configs/includes.conf + configs/error-badcomment.conf + configs/error-lineassigment.conf + configs/error-nosection.conf ) define_test(ini "${SOURCES}")
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/C++/Tests/Ini/configs/compact.conf Fri Mar 06 21:57:21 2015 +0100 @@ -0,0 +1,1 @@ +[general]verbose=true foreground=false[server]host=google.fr
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/C++/Tests/Ini/configs/error-badcomment.conf Fri Mar 06 21:57:21 2015 +0100 @@ -0,0 +1,2 @@ +[general] +verbose #hello = xyz
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/C++/Tests/Ini/configs/error-badsection.conf Fri Mar 06 21:57:21 2015 +0100 @@ -0,0 +1,2 @@ +[[general] +verbose = false
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/C++/Tests/Ini/configs/error-lineassigment.conf Fri Mar 06 21:57:21 2015 +0100 @@ -0,0 +1,4 @@ +[general] +host += +google.fr
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/C++/Tests/Ini/configs/error-nosection.conf Fri Mar 06 21:57:21 2015 +0100 @@ -0,0 +1,1 @@ +option = value
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/C++/Tests/Ini/configs/tokens.conf Fri Mar 06 21:57:21 2015 +0100 @@ -0,0 +1,3 @@ +[tokens] +bracket = "I have [brackets]" +at = "I have foo@at"
--- a/C++/Tests/Ini/main.cpp Wed Mar 04 13:18:00 2015 +0100 +++ b/C++/Tests/Ini/main.cpp Fri Mar 06 21:57:21 2015 +0100 @@ -82,6 +82,22 @@ } /* -------------------------------------------------------- + * Reserved tokens in words + * -------------------------------------------------------- */ + +TEST(Tokens, iniReserved) +{ + try { + Ini ini("tokens.conf"); + + ASSERT_EQ("I have [brackets]", ini["tokens"]["bracket"].value()); + ASSERT_EQ("I have foo@at", ini["tokens"]["at"].value()); + } catch (const std::exception &ex) { + FAIL() << ex.what(); + } +} + +/* -------------------------------------------------------- * Multiple definition * -------------------------------------------------------- */ @@ -160,6 +176,73 @@ ASSERT_EQ("false", m_ini[1][0].value()); } +/* -------------------------------------------------------- + * Compact + * -------------------------------------------------------- */ + +TEST(Compact, test) +{ + try { + Ini ini("compact.conf"); + + ASSERT_EQ(2, static_cast<int>(ini.size())); + ASSERT_EQ("true", ini["general"]["verbose"].value()); + ASSERT_EQ("false", ini["general"]["foreground"].value()); + ASSERT_EQ("google.fr", ini["server"]["host"].value()); + } catch (const std::exception &ex) { + FAIL() << ex.what(); + } +} + +/* -------------------------------------------------------- + * Errors + * -------------------------------------------------------- */ + +TEST(Errors, nosection) +{ + // An option outside a section is not allowed + try { + Ini ini("error-nosection.conf"); + + FAIL() << "Failure expected, got success"; + } catch (const std::exception &ex) { + } +} + +TEST(Errors, lineassigment) +{ + // The = assignment must be on the same line as the option key + try { + Ini ini("error-lineassigment.conf"); + + FAIL() << "Failure expected, got success"; + } catch (const std::exception &ex) { + } +} + +TEST(Errors, badcomment) +{ + // Comment can't between option-key and = assigment + try { + Ini ini("error-badcomment.conf"); + + FAIL() << "Failure expected, got success"; + } catch (const std::exception &ex) { + } +} + +TEST(Errors, badsection) +{ + // Bad section naming + try { + Ini ini("error-badsection.conf"); + + FAIL() << "Failure expected, got success"; + } catch (const std::exception &ex) { + std::cout << ex.what() << std::endl; + } +} + int main(int argc, char **argv) { testing::InitGoogleTest(&argc, argv);