123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- /*
- * Copyright Andrey Semashev 2007 - 2015.
- * Distributed under the Boost Software License, Version 1.0.
- * (See accompanying file LICENSE_1_0.txt or copy at
- * http://www.boost.org/LICENSE_1_0.txt)
- */
- /*!
- * \file setup_settings_parser.cpp
- * \author Andrey Semashev
- * \date 25.08.2013
- *
- * \brief This header contains tests for the settings parser.
- */
- #define BOOST_TEST_MODULE setup_settings_parser
- #include <string>
- #include <sstream>
- #include <boost/test/unit_test.hpp>
- #include <boost/log/utility/setup/settings_parser.hpp>
- #if !defined(BOOST_LOG_WITHOUT_SETTINGS_PARSERS)
- #include <boost/log/exceptions.hpp>
- namespace logging = boost::log;
- typedef logging::basic_settings< char > settings;
- // Tests for single-level settings
- BOOST_AUTO_TEST_CASE(single_level)
- {
- {
- std::istringstream strm
- (
- "[Section1]\n"
- "\n"
- "Param1 = Value1\n"
- "Param2 = \"hello, \\\"world\\\"\"\n"
- "\n"
- "[Section2]\n"
- "\n"
- "Param1 = 10\n"
- "Param2 = -2.2\n"
- );
- settings s = logging::parse_settings(strm);
- BOOST_CHECK(s.has_section("Section1"));
- BOOST_CHECK(s.has_section("Section2"));
- BOOST_CHECK(!s.has_section("Section3"));
- BOOST_CHECK(s.has_parameter("Section1", "Param1"));
- BOOST_CHECK(s.has_parameter("Section1", "Param2"));
- BOOST_CHECK(s.has_parameter("Section2", "Param1"));
- BOOST_CHECK(s.has_parameter("Section2", "Param2"));
- BOOST_CHECK_EQUAL(s["Section1"]["Param1"].or_default(std::string()), "Value1");
- BOOST_CHECK_EQUAL(s["Section1"]["Param2"].or_default(std::string()), "hello, \"world\"");
- BOOST_CHECK_EQUAL(s["Section2"]["Param1"].or_default(std::string()), "10");
- BOOST_CHECK_EQUAL(s["Section2"]["Param2"].or_default(std::string()), "-2.2");
- }
- }
- // Tests for multi-level settings
- BOOST_AUTO_TEST_CASE(multi_level)
- {
- {
- std::istringstream strm
- (
- " [Section1]\n"
- "\n"
- "Param1 = Value1 \n"
- "Param2=\"hello, \\\"world\\\"\" \n"
- "\n"
- "[Section1.Subsection2] \n"
- "\n"
- "Param1=10\n"
- "Param2=-2.2\n"
- );
- settings s = logging::parse_settings(strm);
- BOOST_CHECK(s.has_section("Section1"));
- BOOST_CHECK(s.has_section("Section1.Subsection2"));
- BOOST_CHECK(!s.has_section("Subsection2"));
- BOOST_CHECK(s.has_parameter("Section1", "Param1"));
- BOOST_CHECK(s.has_parameter("Section1", "Param2"));
- BOOST_CHECK(s.has_parameter("Section1.Subsection2", "Param1"));
- BOOST_CHECK(s.has_parameter("Section1.Subsection2", "Param2"));
- BOOST_CHECK(!s.has_parameter("Subsection2", "Param1"));
- BOOST_CHECK(!s.has_parameter("Subsection2", "Param2"));
- BOOST_CHECK_EQUAL(s["Section1"]["Param1"].or_default(std::string()), "Value1");
- BOOST_CHECK_EQUAL(s["Section1"]["Param2"].or_default(std::string()), "hello, \"world\"");
- BOOST_CHECK_EQUAL(s["Section1.Subsection2"]["Param1"].or_default(std::string()), "10");
- BOOST_CHECK_EQUAL(s["Section1.Subsection2"]["Param2"].or_default(std::string()), "-2.2");
- }
- }
- // Tests for comments
- BOOST_AUTO_TEST_CASE(comments)
- {
- {
- std::istringstream strm
- (
- "# Some comment\n"
- "[ Section1 ] # another comment\n"
- "\n"
- "Param1 = Value1 ### yet another comment \n"
- "Param2=\"hello, \\\"world\\\"\" # comment after a quoted string\n"
- "\n"
- "[ Section2 ]\n"
- "\n"
- "Param1=10#comment after a number\n"
- "Param2=-2.2#comment without a terminating newline"
- "\n"
- "#[Section3]\n"
- "#\n"
- "#Param1=10#comment after a number\n"
- "#Param2=-2.2#comment without a terminating newline"
- );
- settings s = logging::parse_settings(strm);
- BOOST_CHECK(s.has_section("Section1"));
- BOOST_CHECK(s.has_section("Section2"));
- BOOST_CHECK(!s.has_section("Section3"));
- BOOST_CHECK(s.has_parameter("Section1", "Param1"));
- BOOST_CHECK(s.has_parameter("Section1", "Param2"));
- BOOST_CHECK(s.has_parameter("Section2", "Param1"));
- BOOST_CHECK(s.has_parameter("Section2", "Param2"));
- BOOST_CHECK_EQUAL(s["Section1"]["Param1"].or_default(std::string()), "Value1");
- BOOST_CHECK_EQUAL(s["Section1"]["Param2"].or_default(std::string()), "hello, \"world\"");
- BOOST_CHECK_EQUAL(s["Section2"]["Param1"].or_default(std::string()), "10");
- BOOST_CHECK_EQUAL(s["Section2"]["Param2"].or_default(std::string()), "-2.2");
- }
- }
- // Tests for invalid settings
- BOOST_AUTO_TEST_CASE(invalid)
- {
- {
- std::istringstream strm
- (
- "Param1 = Value1\n" // parameters outside sections
- "Param2 = \"hello, \\\"world\\\"\"\n"
- );
- BOOST_CHECK_THROW(logging::parse_settings(strm), logging::parse_error);
- }
- {
- std::istringstream strm
- (
- "[Section1\n" // missing closing brace
- "\n"
- "Param1 = Value1\n"
- "Param2 = \"hello, \\\"world\\\"\"\n"
- "\n"
- "[Section2]\n"
- "\n"
- "Param1 = 10\n"
- "Param2 = -2.2\n"
- );
- BOOST_CHECK_THROW(logging::parse_settings(strm), logging::parse_error);
- }
- {
- std::istringstream strm
- (
- "Section1]\n" // missing opening brace
- "\n"
- "Param1 = Value1\n"
- "Param2 = \"hello, \\\"world\\\"\"\n"
- "\n"
- "[Section2]\n"
- "\n"
- "Param1 = 10\n"
- "Param2 = -2.2\n"
- );
- BOOST_CHECK_THROW(logging::parse_settings(strm), logging::parse_error);
- }
- {
- std::istringstream strm
- (
- "[Section1=xyz]\n" // invalid characters in the section name
- "\n"
- "Param1 = Value1\n"
- "Param2 = \"hello, \\\"world\\\"\"\n"
- "\n"
- "[Section2]\n"
- "\n"
- "Param1 = 10\n"
- "Param2 = -2.2\n"
- );
- BOOST_CHECK_THROW(logging::parse_settings(strm), logging::parse_error);
- }
- {
- std::istringstream strm
- (
- "[Section1# hello?]\n" // invalid characters in the section name
- "\n"
- "Param1 = Value1\n"
- "Param2 = \"hello, \\\"world\\\"\"\n"
- "\n"
- "[Section2]\n"
- "\n"
- "Param1 = 10\n"
- "Param2 = -2.2\n"
- );
- BOOST_CHECK_THROW(logging::parse_settings(strm), logging::parse_error);
- }
- {
- std::istringstream strm
- (
- "(Section1)\n" // invalid braces
- "\n"
- "Param1 = Value1\n"
- "Param2 = \"hello, \\\"world\\\"\"\n"
- "\n"
- "[Section2]\n"
- "\n"
- "Param1 = 10\n"
- "Param2 = -2.2\n"
- );
- BOOST_CHECK_THROW(logging::parse_settings(strm), logging::parse_error);
- }
- {
- std::istringstream strm
- (
- "[Section1]\n"
- "\n"
- "Param1 =\n" // no parameter value
- "Param2 = \"hello, \\\"world\\\"\"\n"
- "\n"
- "[Section2]\n"
- "\n"
- "Param1 = 10\n"
- "Param2 = -2.2\n"
- );
- BOOST_CHECK_THROW(logging::parse_settings(strm), logging::parse_error);
- }
- {
- std::istringstream strm
- (
- "[Section1]\n"
- "\n"
- "Param1\n" // no parameter value
- "Param2 = \"hello, \\\"world\\\"\"\n"
- "\n"
- "[Section2]\n"
- "\n"
- "Param1 = 10\n"
- "Param2 = -2.2\n"
- );
- BOOST_CHECK_THROW(logging::parse_settings(strm), logging::parse_error);
- }
- {
- std::istringstream strm
- (
- "[Section1]\n"
- "\n"
- "Param1 = Value1\n"
- "Param2 = \"hello, \\\"world\\\"\n" // unterminated quote
- "\n"
- "[Section2]\n"
- "\n"
- "Param1 = 10\n"
- "Param2 = -2.2\n"
- );
- BOOST_CHECK_THROW(logging::parse_settings(strm), logging::parse_error);
- }
- {
- std::istringstream strm
- (
- "[Section1]\n"
- "\n"
- "Param1 = Value1 Value2\n" // multi-word value
- "Param2 = \"hello, \\\"world\\\"\"\n"
- "\n"
- "[Section2]\n"
- "\n"
- "Param1 = 10\n"
- "Param2 = -2.2\n"
- );
- BOOST_CHECK_THROW(logging::parse_settings(strm), logging::parse_error);
- }
- }
- #endif // !defined(BOOST_LOG_WITHOUT_SETTINGS_PARSERS)
|