Packets/80221Bundle: enhanced error message in MIHBaseTLVParser::validateType()
[senf.git] / senf / Utils / Console / Parse.test.cc
index 41b809f..1ebb7fe 100644 (file)
@@ -1,24 +1,29 @@
 // $Id$
 //
-// Copyright (C) 2008 
+// Copyright (C) 2008
 // Fraunhofer Institute for Open Communication Systems (FOKUS)
-// Competence Center NETwork research (NET), St. Augustin, GERMANY
-//     Stefan Bund <g0dil@berlios.de>
 //
-// This program is free software; you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation; either version 2 of the License, or
-// (at your option) any later version.
+// The contents of this file are subject to the Fraunhofer FOKUS Public License
+// Version 1.0 (the "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at 
+// http://senf.berlios.de/license.html
 //
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-// GNU General Public License for more details.
+// The Fraunhofer FOKUS Public License Version 1.0 is based on, 
+// but modifies the Mozilla Public License Version 1.1.
+// See the full license text for the amendments.
 //
-// You should have received a copy of the GNU General Public License
-// along with this program; if not, write to the
-// Free Software Foundation, Inc.,
-// 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+// Software distributed under the License is distributed on an "AS IS" basis, 
+// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
+// for the specific language governing rights and limitations under the License.
+//
+// The Original Code is Fraunhofer FOKUS code.
+//
+// The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
+// (registered association), Hansastraße 27 c, 80686 Munich, Germany.
+// All Rights Reserved.
+//
+// Contributor(s):
+//   Stefan Bund <g0dil@berlios.de>
 
 /** \file
     \brief Parse unit tests */
 #include <boost/test/test_tools.hpp>
 
 #define prefix_
-///////////////////////////////cc.p////////////////////////////////////////
+//-/////////////////////////////////////////////////////////////////////////////////////////////////
 
-namespace 
+namespace
 {
-    struct TestParseDispatcher 
+    struct TestParseDispatcher
     {
         TestParseDispatcher(std::ostream & os) : os_ (os) {}
 
@@ -54,11 +59,11 @@ namespace
         void popDirectory()
             { os_ << "popDirectory()\n"; }
 
-        void beginCommand(std::vector<senf::console::Token> const & command) 
+        void beginCommand(std::vector<senf::console::Token> const & command)
             { os_ << "beginCommand( " << senf::stringJoin(command, "/") << " )\n"; }
-        void endCommand() 
+        void endCommand()
             { os_ << "endCommand()\n"; }
-        
+
         void pushToken(senf::console::Token token)
             { os_ << "pushToken( " << token << " )\n"; }
 
@@ -82,12 +87,12 @@ SENF_AUTO_UNIT_TEST(commandGrammar)
     senf::console::detail::CommandGrammar<TestParseDispatcher>::Context context;
     std::stringstream ss;
     TestParseDispatcher dispatcher (ss);
-    
+
     typedef senf::console::detail::CommandGrammar<TestParseDispatcher> Grammar;
     Grammar grammar (dispatcher, context);
 
     {
-        static char text[] = 
+        static char text[] =
             "# Comment\n"
             "doo / bii / // doo arg"
             "                flab::blub"
@@ -97,11 +102,11 @@ SENF_AUTO_UNIT_TEST(commandGrammar)
             "                x\"01 02 # Inner comment\n"
             "                   0304\";";
 
-        BOOST_CHECK( boost::spirit::parse( 
-                         text, 
-                         grammar.use_parser<Grammar::CommandParser>(), 
+        BOOST_CHECK( senf::console::detail::boost_spirit::parse(
+                         text,
+                         grammar.use_parser<Grammar::CommandParser>(),
                          grammar.use_parser<Grammar::SkipParser>() ) . full );
-        BOOST_CHECK_EQUAL( ss.str(), 
+        BOOST_CHECK_EQUAL( ss.str(),
                            "beginCommand( Word('doo')/Word('bii')/Word('doo') )\n"
                            "pushToken( Word('arg') )\n"
                            "pushToken( Word('flab::blub') )\n"
@@ -127,66 +132,66 @@ SENF_AUTO_UNIT_TEST(commandGrammar)
 
     {
         ss.str("");
-        BOOST_CHECK( boost::spirit::parse( 
-                         "ls //foo/bar;", 
-                         grammar.use_parser<Grammar::CommandParser>(), 
+        BOOST_CHECK( senf::console::detail::boost_spirit::parse(
+                         "ls //foo/\"bar\";",
+                         grammar.use_parser<Grammar::CommandParser>(),
                          grammar.use_parser<Grammar::SkipParser>() ) . full );
-        BOOST_CHECK_EQUAL( ss.str(), "builtin_ls( None('')/Word('foo')/Word('bar') )\n" );
+        BOOST_CHECK_EQUAL( ss.str(), "builtin_ls( None('')/Word('foo')/BasicString('bar') )\n" );
     }
 
     {
         ss.str("");
-        BOOST_CHECK( boost::spirit::parse( 
-                         "lr /foo/bar;", 
-                         grammar.use_parser<Grammar::CommandParser>(), 
+        BOOST_CHECK( senf::console::detail::boost_spirit::parse(
+                         "lr /foo/bar;",
+                         grammar.use_parser<Grammar::CommandParser>(),
                          grammar.use_parser<Grammar::SkipParser>() ) . full );
         BOOST_CHECK_EQUAL( ss.str(), "builtin_lr( None('')/Word('foo')/Word('bar') )\n" );
     }
 
     {
         ss.str("");
-        BOOST_CHECK( boost::spirit::parse( 
-                         "cd /foo/bar;", 
-                         grammar.use_parser<Grammar::CommandParser>(), 
+        BOOST_CHECK( senf::console::detail::boost_spirit::parse(
+                         "cd /foo/bar;",
+                         grammar.use_parser<Grammar::CommandParser>(),
                          grammar.use_parser<Grammar::SkipParser>() ) . full );
         BOOST_CHECK_EQUAL( ss.str(), "builtin_cd( None('')/Word('foo')/Word('bar') )\n" );
     }
 
     {
         ss.str("");
-        BOOST_CHECK( boost::spirit::parse( 
-                         "exit;", 
-                         grammar.use_parser<Grammar::CommandParser>(), 
+        BOOST_CHECK( senf::console::detail::boost_spirit::parse(
+                         "exit;",
+                         grammar.use_parser<Grammar::CommandParser>(),
                          grammar.use_parser<Grammar::SkipParser>() ) . full );
         BOOST_CHECK_EQUAL( ss.str(), "builtin_exit()\n" );
     }
 
     {
         ss.str("");
-        BOOST_CHECK( boost::spirit::parse( 
-                         "foo/bar// {", 
-                         grammar.use_parser<Grammar::CommandParser>(), 
+        BOOST_CHECK( senf::console::detail::boost_spirit::parse(
+                         "foo/\"bar baz\"// {",
+                         grammar.use_parser<Grammar::CommandParser>(),
                          grammar.use_parser<Grammar::SkipParser>() ) . full );
-        BOOST_CHECK_EQUAL( ss.str(), 
-                           "beginCommand( Word('foo')/Word('bar')/None('') )\n"
+        BOOST_CHECK_EQUAL( ss.str(),
+                           "beginCommand( Word('foo')/BasicString('bar baz')/None('') )\n"
                            "pushDirectory()\n"
                            "endCommand()\n" );
     }
 
     {
         ss.str("");
-        BOOST_CHECK( boost::spirit::parse( 
-                         "}", 
-                         grammar.use_parser<Grammar::CommandParser>(), 
+        BOOST_CHECK( senf::console::detail::boost_spirit::parse(
+                         "}",
+                         grammar.use_parser<Grammar::CommandParser>(),
                          grammar.use_parser<Grammar::SkipParser>() ) . full );
         BOOST_CHECK_EQUAL( ss.str(), "popDirectory()\n" );
     }
 
     {
         ss.str("");
-        BOOST_CHECK( boost::spirit::parse( 
-                         "help /foo/bar", 
-                         grammar.use_parser<Grammar::CommandParser>(), 
+        BOOST_CHECK( senf::console::detail::boost_spirit::parse(
+                         "help /foo/bar",
+                         grammar.use_parser<Grammar::CommandParser>(),
                          grammar.use_parser<Grammar::SkipParser>() ) . full );
         BOOST_CHECK_EQUAL( ss.str(), "builtin_help( None('')/Word('foo')/Word('bar') )\n" );
     }
@@ -202,7 +207,7 @@ SENF_AUTO_UNIT_TEST(commandParser)
 {
     senf::console::CommandParser parser;
 
-    char const text[] = 
+    char const text[] =
         "# Comment\n"
         "doo / bii / doo arg"
         "                flab::blub"
@@ -219,8 +224,8 @@ SENF_AUTO_UNIT_TEST(commandParser)
     {
         senf::console::ParseCommandInfo const & info (commands.front());
 
-        senf::console::Token path[] = { 
-            senf::console::Token(senf::console::Token::Word, "doo"), 
+        senf::console::Token path[] = {
+            senf::console::Token(senf::console::Token::Word, "doo"),
             senf::console::Token(senf::console::Token::Word, "bii"),
             senf::console::Token(senf::console::Token::Word, "doo")
         };
@@ -229,10 +234,10 @@ SENF_AUTO_UNIT_TEST(commandParser)
                                        path, path + sizeof(path)/sizeof(path[0]) );
         BOOST_CHECK_EQUAL( boost::next(info.commandPath().begin())->index(), 16u );
         BOOST_CHECK_EQUAL( unsigned(info.tokens().size()), 15u );
-        
-        char const * tokens[] = { "arg", 
-                                  "flab::blub", 
-                                  "123.434>a", 
+
+        char const * tokens[] = { "arg",
+                                  "flab::blub",
+                                  "123.434>a",
                                   "(", "a", ",", "b", ",", "c", "(", "huhu", ")", ")",
                                   "foo\"bar",
                                   "\x01\x02\x03\x04" };
@@ -241,17 +246,17 @@ SENF_AUTO_UNIT_TEST(commandParser)
         BOOST_REQUIRE( args != info.arguments().end() );
         BOOST_REQUIRE_EQUAL( unsigned(args->size()), 1u );
         BOOST_CHECK_EQUAL( args->begin()->value(), tokens[0] );
-        
+
         ++ args;
         BOOST_REQUIRE( args != info.arguments().end() );
         BOOST_REQUIRE_EQUAL( args->size(), 1u );
         BOOST_CHECK_EQUAL( args->begin()->value(), tokens[1] );
-        
+
         ++ args;
         BOOST_REQUIRE( args != info.arguments().end() );
         BOOST_REQUIRE_EQUAL( args->size(), 1u );
         BOOST_CHECK_EQUAL( args->begin()->value(), tokens[2] );
-        
+
         ++ args;
         BOOST_REQUIRE( args != info.arguments().end() );
         BOOST_REQUIRE_EQUAL( args->size(), 8u );
@@ -260,18 +265,18 @@ SENF_AUTO_UNIT_TEST(commandParser)
         BOOST_CHECK_EQUAL( info.tokens().begin()[3].index(), 96u );
         BOOST_CHECK_EQUAL( info.tokens().begin()[5].index(), 98u );
         BOOST_CHECK_EQUAL( info.tokens().begin()[12].index(), 109u );
-        
+
         ++ args;
         BOOST_REQUIRE( args != info.arguments().end() );
         BOOST_REQUIRE_EQUAL( args->size(), 1u );
         BOOST_CHECK_EQUAL( args->begin()->value(), tokens[13] );
         BOOST_CHECK_EQUAL( args->begin()->index(), 126u );
-        
+
         ++ args;
         BOOST_REQUIRE( args != info.arguments().end() );
         BOOST_REQUIRE_EQUAL( args->size(), 1u );
         BOOST_CHECK_EQUAL( args->begin()->value(), tokens[14] );
-        
+
         ++ args;
         BOOST_CHECK( args == info.arguments().end() );
     }
@@ -293,16 +298,16 @@ SENF_AUTO_UNIT_TEST(checkedArgumentIterator)
     senf::console::CommandParser parser;
 
     SENF_CHECK_NO_THROW( parser.parse("foo a", &setInfo) );
-    BOOST_CHECK_THROW( parseArgs(commands.back().arguments()), 
+    BOOST_CHECK_THROW( parseArgs(commands.back().arguments()),
                        senf::console::SyntaxErrorException );
 
     SENF_CHECK_NO_THROW( parser.parse("foo a b", &setInfo) );
     SENF_CHECK_NO_THROW( parseArgs(commands.back().arguments()) );
 
     SENF_CHECK_NO_THROW( parser.parse("foo a b c", &setInfo) );
-    BOOST_CHECK_THROW( parseArgs(commands.back().arguments()), 
+    BOOST_CHECK_THROW( parseArgs(commands.back().arguments()),
                        senf::console::SyntaxErrorException );
-    
+
     senf::console::CheckedArgumentIteratorWrapper arg (commands.back().arguments());
     BOOST_CHECK( arg == commands.back().arguments().begin() );
     BOOST_CHECK( arg != commands.back().arguments().end() );
@@ -351,17 +356,17 @@ SENF_AUTO_UNIT_TEST(parseExceptions)
         try { parser.parse(c, &setInfo); }                                                        \
         catch (std::exception & ex) { msg = parseErrorMessage(ex.what()); }                       \
         BOOST_CHECK_EQUAL( msg, e )
-    
+
     CheckParseEx( "/foo/bar;\n  ()", "path expected\nat <unknown>:2:3" );
     CheckParseEx( "cd /foo/bar foo/bar", "end of statement expected\nat <unknown>:1:13" );
     CheckParseEx( "/foo/bar foo /", "end of statement expected\nat <unknown>:1:14" );
-    CheckParseEx( "cd \"foo\"", "path expected\nat <unknown>:1:4" );
+    CheckParseEx( "cd (foo)", "path expected\nat <unknown>:1:4" );
     CheckParseEx( "/foo/bar \"string", "'\"' expected\nat <unknown>:1:17" );
     CheckParseEx( "/foo/bar x\"hi\"", "'\"' expected\nat <unknown>:1:12" );
     CheckParseEx( "/foo/bar (", "')' expected\nat <unknown>:1:11" );
 }
 
-///////////////////////////////cc.e////////////////////////////////////////
+//-/////////////////////////////////////////////////////////////////////////////////////////////////
 #undef prefix_
 
 \f