Utils/Logger: Remove dependency on libboost_datetime
[senf.git] / Console / Parse.test.cc
index a6d170b..46ad986 100644 (file)
 //#include "Parse.test.hh"
 //#include "Parse.test.ih"
 
+// #define BOOST_SPIRIT_DEBUG
+// #define BOOST_SPIRIT_DEBUG_TRACENODE 0
+
 // Custom includes
 #include <sstream>
 #include "Parse.hh"
 #include "Parse.ih"
+#include "../Utils/String.hh"
 
-#include <boost/test/auto_unit_test.hpp>
+#include "../Utils/auto_unit_test.hh"
 #include <boost/test/test_tools.hpp>
 
 #define prefix_
@@ -45,12 +49,18 @@ namespace
 
         std::ostream & os_;
 
-        void beginCommand(std::string const & command) 
-            { os_ << "beginCommand( " << command << " )\n"; }
+        void pushDirectory(std::vector<std::string> const & path)
+            { os_ << "pushDirectory( " << senf::stringJoin(path,"/") << " )\n"; }
+        void popDirectory()
+            { os_ << "popDirectory()\n"; }
+
+        void beginCommand(std::vector<std::string> const & command) 
+            { os_ << "beginCommand( " << senf::stringJoin(command, "/") << " )\n"; }
         void endCommand() 
             { os_ << "endCommand()\n"; }
         
-        void pushArgument(std::string const & argument)
+        void pushArgument(senf::console::ArgumentToken::TokenType type, 
+                          std::string const & argument)
             { os_ << "pushArgument( " << argument << " )\n"; }
         void openGroup()
             { os_ << "openGroup()\n"; }
@@ -58,60 +68,93 @@ namespace
             { os_ << "closeGroup()\n"; }
         void pushPunctuation(std::string const & token)
             { os_ << "pushPunctuation( " << token << " )\n"; }
-        void pushWord(std::string const & token)
+        void pushWord(senf::console::ArgumentToken::TokenType type, 
+                      std::string const & token)
             { os_ << "pushWord( " << token << " )\n"; }
+
+        void builtin_cd(std::vector<std::string> const & path)
+            { os_ << "builtin_cd( " << senf::stringJoin(path, "/") << " )\n"; }
+        void builtin_ls(std::vector<std::string> const & path)
+            { os_ << "builtin_ls( " << senf::stringJoin(path, "/") << " )\n"; }
+        void builtin_exit()
+            { os_ << "builtin_exit()\n"; }
+        void builtin_help(std::vector<std::string> const & path)
+            { os_ << "builtin_help( " << senf::stringJoin(path, "/") << " )\n"; }
     };
 }
 
-BOOST_AUTO_UNIT_TEST(commandParser)
+BOOST_AUTO_UNIT_TEST(commandGrammar)
 {
     senf::console::detail::CommandGrammar<TestParseDispatcher>::Context context;
     std::stringstream ss;
     TestParseDispatcher dispatcher (ss);
-    senf::console::detail::CommandGrammar<TestParseDispatcher> grammar (dispatcher, context);
-    senf::console::detail::SkipGrammar skipGrammar;
+    
+    typedef senf::console::detail::CommandGrammar<TestParseDispatcher> Grammar;
+    Grammar grammar (dispatcher, context);
 
     char text[] = 
         "# Comment\n"
-        "doo / bii / doo arg/../path"
+        "doo / bii / doo arg"
         "                flab::blub"
         "                123.434>a"
-        "                (a,b,c (huhu))"
+        "                (a,b;c (huhu/{haha}))"
         "                \"foo\\\"bar\" #\n"
         "                x\"01 02 # Inner comment\n"
-        "                   0304\"";
+        "                   0304\";"
+        "ls /foo/bar;"
+        "cd /foo/bar;"
+        "exit;"
+        "foo/bar/ { ls; }"
+        "help /foo/bar";
 
     BOOST_CHECK( boost::spirit::parse( 
                      text, 
-                     grammar, 
-                     skipGrammar ) . full );
+                     grammar.use_parser<Grammar::CommandParser>(), 
+                     grammar.use_parser<Grammar::SkipParser>() ) . full );
     BOOST_CHECK_EQUAL( ss.str(), 
                        "beginCommand( doo/bii/doo )\n"
-                       "pushArgument( arg/../path )\n"
+                       "pushArgument( arg )\n"
                        "pushArgument( flab::blub )\n"
                        "pushArgument( 123.434>a )\n"
                        "openGroup()\n"
                        "pushWord( a )\n"
                        "pushPunctuation( , )\n"
                        "pushWord( b )\n"
-                       "pushPunctuation( , )\n"
+                       "pushPunctuation( ; )\n"
                        "pushWord( c )\n"
                        "pushPunctuation( ( )\n"
                        "pushWord( huhu )\n"
+                       "pushPunctuation( / )\n"
+                       "pushPunctuation( { )\n"
+                       "pushWord( haha )\n"
+                       "pushPunctuation( } )\n"
                        "pushPunctuation( ) )\n"
                        "closeGroup()\n"
                        "pushArgument( foo\"bar )\n"
                        "pushArgument( \x01\x02\x03\x04 )\n"
-                       "endCommand()\n" );
+                       "endCommand()\n"
+                       "builtin_ls( /foo/bar )\n"
+                       "builtin_cd( /foo/bar )\n"
+                       "builtin_exit()\n"
+                       "pushDirectory( foo/bar/ )\n"
+                       "builtin_ls(  )\n"
+                       "popDirectory()\n"
+                       "builtin_help( /foo/bar )\n" );
 }
 
-BOOST_AUTO_UNIT_TEST(singleCommandParser)
+namespace {
+    senf::console::ParseCommandInfo info;
+    void setInfo(senf::console::ParseCommandInfo const & i)
+    { info = i; }
+}
+
+BOOST_AUTO_UNIT_TEST(commandParser)
 {
-    senf::console::SingleCommandParser parser;
+    senf::console::CommandParser parser;
 
     char const text[] = 
         "# Comment\n"
-        "doo / bii / doo arg/../path"
+        "doo / bii / doo arg"
         "                flab::blub"
         "                123.434>a"
         "                (a,b,c (huhu))"
@@ -119,38 +162,89 @@ BOOST_AUTO_UNIT_TEST(singleCommandParser)
         "                x\"01 02 # Inner comment\n"
         "                   0304\"";
 
-    senf::console::ParseCommandInfo info;
-    BOOST_CHECK( parser.parseCommand(text, info) );
+    BOOST_CHECK( parser.parse(text, &setInfo) );
+
+    char const * path[] = { "doo", "bii", "doo" };
 
-    BOOST_CHECK_EQUAL( info.commandPath(), "doo/bii/doo" );
-    BOOST_REQUIRE_EQUAL( info.arguments(), 6u );
-    BOOST_REQUIRE_EQUAL( info.tokens(), 13u );
+    BOOST_CHECK_EQUAL_COLLECTIONS( info.commandPath().begin(), info.commandPath().end(),
+                                   path, path + sizeof(path)/sizeof(path[0]) );
+    BOOST_CHECK_EQUAL( info.tokens().size(), 15u );
 
-    char const * tokens[] = { "arg/../path", 
+    char const * tokens[] = { "arg", 
                               "flab::blub", 
                               "123.434>a", 
-                              "a", ",", "b", ",", "c", "(", "huhu", ")",
+                              "(", "a", ",", "b", ",", "c", "(", "huhu", ")", ")",
                               "foo\"bar",
                               "\x01\x02\x03\x04" };
 
-    BOOST_REQUIRE_EQUAL( info.begin_arguments()[0].size(), 1u );
-    BOOST_CHECK_EQUAL( info.begin_arguments()[0].begin()->value(), tokens[0] );
+    senf::console::ParseCommandInfo::argument_iterator args (info.arguments().begin());
+    BOOST_REQUIRE( args != info.arguments().end() );
+    BOOST_REQUIRE_EQUAL( args->size(), 1u );
+    BOOST_CHECK_EQUAL( args->begin()->value(), tokens[0] );
 
-    BOOST_REQUIRE_EQUAL( info.begin_arguments()[1].size(), 1u );
-    BOOST_CHECK_EQUAL( info.begin_arguments()[1].begin()->value(), tokens[1] );
+    ++ 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] );
 
-    BOOST_REQUIRE_EQUAL( info.begin_arguments()[2].size(), 1u );
-    BOOST_CHECK_EQUAL( info.begin_arguments()[2].begin()->value(), tokens[2] );
-
-    BOOST_REQUIRE_EQUAL( info.begin_arguments()[3].size(), 8u );
+    ++ args;
+    BOOST_REQUIRE( args != info.arguments().end() );
+    BOOST_REQUIRE_EQUAL( args->size(), 8u );
     for (unsigned i (0); i<8; ++i)
-        BOOST_CHECK_EQUAL( info.begin_arguments()[3].begin()[i].value(), tokens[3+i] );
+        BOOST_CHECK_EQUAL( args->begin()[i].value(), tokens[4+i] );
+
+    ++ args;
+    BOOST_REQUIRE( args != info.arguments().end() );
+    BOOST_REQUIRE_EQUAL( args->size(), 1u );
+    BOOST_CHECK_EQUAL( args->begin()->value(), tokens[13] );
+
+    ++ 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() );
+}
+
+namespace {
+    void parseArgs(senf::console::ParseCommandInfo::ArgumentsRange const & args)
+    {
+        senf::console::CheckedArgumentIteratorWrapper arg (args);
+        senf::console::ParseCommandInfo::TokensRange arg1 (*(arg++));
+        senf::console::ParseCommandInfo::TokensRange arg2 (*(arg++));
+    }
+}
+
+BOOST_AUTO_UNIT_TEST(checkedArgumentIterator)
+{
+    senf::console::CommandParser parser;
+
+    BOOST_CHECK( parser.parse("foo a", &setInfo) );
+    BOOST_CHECK_THROW( parseArgs(info.arguments()), senf::console::SyntaxErrorException );
+
+    BOOST_CHECK( parser.parse("foo a b", &setInfo) );
+    BOOST_CHECK_NO_THROW( parseArgs(info.arguments()) );
 
-    BOOST_REQUIRE_EQUAL( info.begin_arguments()[4].size(), 1u );
-    BOOST_CHECK_EQUAL( info.begin_arguments()[4].begin()->value(), tokens[11] );
+    BOOST_CHECK( parser.parse("foo a b c", &setInfo) );
+    BOOST_CHECK_THROW( parseArgs(info.arguments()), senf::console::SyntaxErrorException );
+    
+    senf::console::CheckedArgumentIteratorWrapper arg (info.arguments());
+    BOOST_CHECK( arg == info.arguments().begin() );
+    BOOST_CHECK( arg != info.arguments().end() );
+    BOOST_CHECK( arg );
+    ++ arg;
+    BOOST_CHECK( arg );
+    arg.clear();
+    BOOST_CHECK( arg.done() );
 
-    BOOST_REQUIRE_EQUAL( info.begin_arguments()[5].size(), 1u );
-    BOOST_CHECK_EQUAL( info.begin_arguments()[5].begin()->value(), tokens[12] );
+    senf::console::ParseCommandInfo::ArgumentIterator i (arg);
+    BOOST_CHECK( i == info.arguments().end() );
 }
 
 ///////////////////////////////cc.e////////////////////////////////////////