Fix documentation
[senf.git] / Console / Parse.test.cc
index a6d170b..9d465b7 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,8 +49,13 @@ 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"; }
         
@@ -60,58 +69,90 @@ namespace
             { os_ << "pushPunctuation( " << token << " )\n"; }
         void pushWord(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 +160,40 @@ 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_REQUIRE_EQUAL( info.arguments().size(), 6u );
+    BOOST_REQUIRE_EQUAL( info.tokens().size(), 13u );
 
-    char const * tokens[] = { "arg/../path", 
+    char const * tokens[] = { "arg", 
                               "flab::blub", 
                               "123.434>a", 
                               "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] );
+    BOOST_REQUIRE_EQUAL( info.arguments().begin()[0].size(), 1u );
+    BOOST_CHECK_EQUAL( info.arguments().begin()[0].begin()->value(), tokens[0] );
 
-    BOOST_REQUIRE_EQUAL( info.begin_arguments()[1].size(), 1u );
-    BOOST_CHECK_EQUAL( info.begin_arguments()[1].begin()->value(), tokens[1] );
+    BOOST_REQUIRE_EQUAL( info.arguments().begin()[1].size(), 1u );
+    BOOST_CHECK_EQUAL( info.arguments().begin()[1].begin()->value(), tokens[1] );
 
-    BOOST_REQUIRE_EQUAL( info.begin_arguments()[2].size(), 1u );
-    BOOST_CHECK_EQUAL( info.begin_arguments()[2].begin()->value(), tokens[2] );
+    BOOST_REQUIRE_EQUAL( info.arguments().begin()[2].size(), 1u );
+    BOOST_CHECK_EQUAL( info.arguments().begin()[2].begin()->value(), tokens[2] );
 
-    BOOST_REQUIRE_EQUAL( info.begin_arguments()[3].size(), 8u );
+    BOOST_REQUIRE_EQUAL( info.arguments().begin()[3].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( info.arguments().begin()[3].begin()[i].value(), tokens[3+i] );
 
-    BOOST_REQUIRE_EQUAL( info.begin_arguments()[4].size(), 1u );
-    BOOST_CHECK_EQUAL( info.begin_arguments()[4].begin()->value(), tokens[11] );
+    BOOST_REQUIRE_EQUAL( info.arguments().begin()[4].size(), 1u );
+    BOOST_CHECK_EQUAL( info.arguments().begin()[4].begin()->value(), tokens[11] );
 
-    BOOST_REQUIRE_EQUAL( info.begin_arguments()[5].size(), 1u );
-    BOOST_CHECK_EQUAL( info.begin_arguments()[5].begin()->value(), tokens[12] );
+    BOOST_REQUIRE_EQUAL( info.arguments().begin()[5].size(), 1u );
+    BOOST_CHECK_EQUAL( info.arguments().begin()[5].begin()->value(), tokens[12] );
 }
 
 ///////////////////////////////cc.e////////////////////////////////////////