Utils/Console: Console UDPServer
[senf.git] / Utils / Console / Parse.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2008 
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief Parse unit tests */
25
26 //#include "Parse.test.hh"
27 //#include "Parse.test.ih"
28
29 // #define BOOST_SPIRIT_DEBUG
30 // #define BOOST_SPIRIT_DEBUG_TRACENODE 0
31
32 // Custom includes
33 #include <sstream>
34 #include "Parse.hh"
35 #include "Parse.ih"
36 #include "../../Utils/String.hh"
37
38 #include "../../Utils/auto_unit_test.hh"
39 #include <boost/test/test_tools.hpp>
40
41 #define prefix_
42 ///////////////////////////////cc.p////////////////////////////////////////
43
44 namespace 
45 {
46     struct TestParseDispatcher 
47     {
48         TestParseDispatcher(std::ostream & os) : os_ (os) {}
49
50         std::ostream & os_;
51
52         void pushDirectory()
53             { os_ << "pushDirectory()\n"; }
54         void popDirectory()
55             { os_ << "popDirectory()\n"; }
56
57         void beginCommand(std::vector<senf::console::Token> const & command) 
58             { os_ << "beginCommand( " << senf::stringJoin(command, "/") << " )\n"; }
59         void endCommand() 
60             { os_ << "endCommand()\n"; }
61         
62         void pushToken(senf::console::Token token)
63             { os_ << "pushToken( " << token << " )\n"; }
64
65         void builtin_cd(std::vector<senf::console::Token> const & path)
66             { os_ << "builtin_cd( " << senf::stringJoin(path, "/") << " )\n"; }
67         void builtin_ls(std::vector<senf::console::Token> const & path)
68             { os_ << "builtin_ls( " << senf::stringJoin(path, "/") << " )\n"; }
69         void builtin_lr(std::vector<senf::console::Token> const & path)
70             { os_ << "builtin_lr( " << senf::stringJoin(path, "/") << " )\n"; }
71         void builtin_exit()
72             { os_ << "builtin_exit()\n"; }
73         void builtin_help(std::vector<senf::console::Token> const & path)
74             { os_ << "builtin_help( " << senf::stringJoin(path, "/") << " )\n"; }
75     };
76 }
77
78 BOOST_AUTO_UNIT_TEST(commandGrammar)
79 {
80     senf::console::detail::CommandGrammar<TestParseDispatcher>::Context context;
81     std::stringstream ss;
82     TestParseDispatcher dispatcher (ss);
83     
84     typedef senf::console::detail::CommandGrammar<TestParseDispatcher> Grammar;
85     Grammar grammar (dispatcher, context);
86
87     {
88         static char text[] = 
89             "# Comment\n"
90             "doo / bii / doo arg"
91             "                flab::blub"
92             "                123.434>a"
93             "                (a,b;c (huhu/{haha}))"
94             "                \"foo\\\"bar\" #\n"
95             "                x\"01 02 # Inner comment\n"
96             "                   0304\";";
97
98         BOOST_CHECK( boost::spirit::parse( 
99                          text, 
100                          grammar.use_parser<Grammar::CommandParser>(), 
101                          grammar.use_parser<Grammar::SkipParser>() ) . full );
102         BOOST_CHECK_EQUAL( ss.str(), 
103                            "beginCommand( Word('doo')/Word('bii')/Word('doo') )\n"
104                            "pushToken( Word('arg') )\n"
105                            "pushToken( Word('flab::blub') )\n"
106                            "pushToken( Word('123.434>a') )\n"
107                            "pushToken( ArgumentGroupOpen('(') )\n"
108                            "pushToken( Word('a') )\n"
109                            "pushToken( OtherPunctuation(',') )\n"
110                            "pushToken( Word('b') )\n"
111                            "pushToken( CommandTerminator(';') )\n"
112                            "pushToken( Word('c') )\n"
113                            "pushToken( ArgumentGroupOpen('(') )\n"
114                            "pushToken( Word('huhu') )\n"
115                            "pushToken( PathSeparator('/') )\n"
116                            "pushToken( DirectoryGroupOpen('{') )\n"
117                            "pushToken( Word('haha') )\n"
118                            "pushToken( DirectoryGroupClose('}') )\n"
119                            "pushToken( ArgumentGroupClose(')') )\n"
120                            "pushToken( ArgumentGroupClose(')') )\n"
121                            "pushToken( BasicString('foo\"bar') )\n"
122                            "pushToken( HexString('\x01\x02\x03\x04') )\n"
123                            "endCommand()\n" );
124     }
125
126     {
127         ss.str("");
128         BOOST_CHECK( boost::spirit::parse( 
129                          "ls /foo/bar;", 
130                          grammar.use_parser<Grammar::CommandParser>(), 
131                          grammar.use_parser<Grammar::SkipParser>() ) . full );
132         BOOST_CHECK_EQUAL( ss.str(), "builtin_ls( None('')/Word('foo')/Word('bar') )\n" );
133     }
134
135     {
136         ss.str("");
137         BOOST_CHECK( boost::spirit::parse( 
138                          "lr /foo/bar;", 
139                          grammar.use_parser<Grammar::CommandParser>(), 
140                          grammar.use_parser<Grammar::SkipParser>() ) . full );
141         BOOST_CHECK_EQUAL( ss.str(), "builtin_lr( None('')/Word('foo')/Word('bar') )\n" );
142     }
143
144     {
145         ss.str("");
146         BOOST_CHECK( boost::spirit::parse( 
147                          "cd /foo/bar;", 
148                          grammar.use_parser<Grammar::CommandParser>(), 
149                          grammar.use_parser<Grammar::SkipParser>() ) . full );
150         BOOST_CHECK_EQUAL( ss.str(), "builtin_cd( None('')/Word('foo')/Word('bar') )\n" );
151     }
152
153     {
154         ss.str("");
155         BOOST_CHECK( boost::spirit::parse( 
156                          "exit;", 
157                          grammar.use_parser<Grammar::CommandParser>(), 
158                          grammar.use_parser<Grammar::SkipParser>() ) . full );
159         BOOST_CHECK_EQUAL( ss.str(), "builtin_exit()\n" );
160     }
161
162     {
163         ss.str("");
164         BOOST_CHECK( boost::spirit::parse( 
165                          "foo/bar/ {", 
166                          grammar.use_parser<Grammar::CommandParser>(), 
167                          grammar.use_parser<Grammar::SkipParser>() ) . full );
168         BOOST_CHECK_EQUAL( ss.str(), 
169                            "beginCommand( Word('foo')/Word('bar')/None('') )\n"
170                            "pushDirectory()\n"
171                            "endCommand()\n" );
172     }
173
174     {
175         ss.str("");
176         BOOST_CHECK( boost::spirit::parse( 
177                          "}", 
178                          grammar.use_parser<Grammar::CommandParser>(), 
179                          grammar.use_parser<Grammar::SkipParser>() ) . full );
180         BOOST_CHECK_EQUAL( ss.str(), "popDirectory()\n" );
181     }
182
183     {
184         ss.str("");
185         BOOST_CHECK( boost::spirit::parse( 
186                          "help /foo/bar", 
187                          grammar.use_parser<Grammar::CommandParser>(), 
188                          grammar.use_parser<Grammar::SkipParser>() ) . full );
189         BOOST_CHECK_EQUAL( ss.str(), "builtin_help( None('')/Word('foo')/Word('bar') )\n" );
190     }
191 }
192
193 namespace {
194     std::vector<senf::console::ParseCommandInfo> commands;
195     void setInfo(senf::console::ParseCommandInfo const & i)
196     { commands.push_back(i); }
197 }
198
199 BOOST_AUTO_UNIT_TEST(commandParser)
200 {
201     senf::console::CommandParser parser;
202
203     char const text[] = 
204         "# Comment\n"
205         "doo / bii / doo arg"
206         "                flab::blub"
207         "                123.434>a"
208         "                (a,b,c (huhu))"
209         "                \"foo\\\"bar\" #\n"
210         "                x\"01 02 # Inner comment\n"
211         "                   0304\";"
212         "ls /foo/bar; ";
213
214     SENF_CHECK_NO_THROW( parser.parse(text, &setInfo) );
215     BOOST_CHECK_EQUAL( commands.size(), 2u );
216
217     {
218         senf::console::ParseCommandInfo const & info (commands.front());
219
220         senf::console::Token path[] = { 
221             senf::console::Token(senf::console::Token::Word, "doo"), 
222             senf::console::Token(senf::console::Token::Word, "bii"),
223             senf::console::Token(senf::console::Token::Word, "doo")
224         };
225
226         BOOST_CHECK_EQUAL_COLLECTIONS( info.commandPath().begin(), info.commandPath().end(),
227                                        path, path + sizeof(path)/sizeof(path[0]) );
228         BOOST_CHECK_EQUAL( boost::next(info.commandPath().begin())->index(), 16u );
229         BOOST_CHECK_EQUAL( unsigned(info.tokens().size()), 15u );
230         
231         char const * tokens[] = { "arg", 
232                                   "flab::blub", 
233                                   "123.434>a", 
234                                   "(", "a", ",", "b", ",", "c", "(", "huhu", ")", ")",
235                                   "foo\"bar",
236                                   "\x01\x02\x03\x04" };
237
238         senf::console::ParseCommandInfo::argument_iterator args (info.arguments().begin());
239         BOOST_REQUIRE( args != info.arguments().end() );
240         BOOST_REQUIRE_EQUAL( unsigned(args->size()), 1u );
241         BOOST_CHECK_EQUAL( args->begin()->value(), tokens[0] );
242         
243         ++ args;
244         BOOST_REQUIRE( args != info.arguments().end() );
245         BOOST_REQUIRE_EQUAL( args->size(), 1u );
246         BOOST_CHECK_EQUAL( args->begin()->value(), tokens[1] );
247         
248         ++ args;
249         BOOST_REQUIRE( args != info.arguments().end() );
250         BOOST_REQUIRE_EQUAL( args->size(), 1u );
251         BOOST_CHECK_EQUAL( args->begin()->value(), tokens[2] );
252         
253         ++ args;
254         BOOST_REQUIRE( args != info.arguments().end() );
255         BOOST_REQUIRE_EQUAL( args->size(), 8u );
256         for (unsigned i (0); i<8; ++i)
257             BOOST_CHECK_EQUAL( args->begin()[i].value(), tokens[4+i] );
258         BOOST_CHECK_EQUAL( info.tokens().begin()[3].index(), 96u );
259         BOOST_CHECK_EQUAL( info.tokens().begin()[5].index(), 98u );
260         BOOST_CHECK_EQUAL( info.tokens().begin()[12].index(), 109u );
261         
262         ++ args;
263         BOOST_REQUIRE( args != info.arguments().end() );
264         BOOST_REQUIRE_EQUAL( args->size(), 1u );
265         BOOST_CHECK_EQUAL( args->begin()->value(), tokens[13] );
266         BOOST_CHECK_EQUAL( args->begin()->index(), 126u );
267         
268         ++ args;
269         BOOST_REQUIRE( args != info.arguments().end() );
270         BOOST_REQUIRE_EQUAL( args->size(), 1u );
271         BOOST_CHECK_EQUAL( args->begin()->value(), tokens[14] );
272         
273         ++ args;
274         BOOST_CHECK( args == info.arguments().end() );
275     }
276
277     commands.clear();
278 }
279
280 namespace {
281     void parseArgs(senf::console::ParseCommandInfo::ArgumentsRange const & args)
282     {
283         senf::console::CheckedArgumentIteratorWrapper arg (args);
284         senf::console::ParseCommandInfo::TokensRange arg1 (*(arg++));
285         senf::console::ParseCommandInfo::TokensRange arg2 (*(arg++));
286     }
287 }
288
289 BOOST_AUTO_UNIT_TEST(checkedArgumentIterator)
290 {
291     senf::console::CommandParser parser;
292
293     SENF_CHECK_NO_THROW( parser.parse("foo a", &setInfo) );
294     BOOST_CHECK_THROW( parseArgs(commands.back().arguments()), 
295                        senf::console::SyntaxErrorException );
296
297     SENF_CHECK_NO_THROW( parser.parse("foo a b", &setInfo) );
298     SENF_CHECK_NO_THROW( parseArgs(commands.back().arguments()) );
299
300     SENF_CHECK_NO_THROW( parser.parse("foo a b c", &setInfo) );
301     BOOST_CHECK_THROW( parseArgs(commands.back().arguments()), 
302                        senf::console::SyntaxErrorException );
303     
304     senf::console::CheckedArgumentIteratorWrapper arg (commands.back().arguments());
305     BOOST_CHECK( arg == commands.back().arguments().begin() );
306     BOOST_CHECK( arg != commands.back().arguments().end() );
307     BOOST_CHECK( arg );
308     ++ arg;
309     BOOST_CHECK( arg );
310     arg.clear();
311     BOOST_CHECK( arg.done() );
312
313     senf::console::ParseCommandInfo::ArgumentIterator i (arg);
314     BOOST_CHECK( i == commands.back().arguments().end() );
315
316     commands.clear();
317 }
318
319 BOOST_AUTO_UNIT_TEST(parseIncremental)
320 {
321     senf::console::CommandParser parser;
322
323     BOOST_CHECK_EQUAL( parser.parseIncremental("foo a", &setInfo), 0u );
324     BOOST_CHECK_EQUAL( parser.parseIncremental("foo a; cd", &setInfo), 7u );
325     BOOST_CHECK_EQUAL( parser.parseIncremental("foo a; cd /bar", &setInfo), 7u );
326     BOOST_CHECK_EQUAL( parser.parseIncremental("foo a; cd /bar; ", &setInfo), 16u );
327     BOOST_CHECK_EQUAL( parser.parseIncremental(" ", &setInfo), 1u );
328     BOOST_CHECK_EQUAL( commands.size(), 4u );
329
330     commands.clear();
331 }
332
333 namespace {
334     std::string parseErrorMessage(std::string const & msg)
335     {
336         std::string::size_type i (msg.find("-- \n"));
337         return i == std::string::npos ? msg : msg.substr(i+4);
338     }
339 }
340
341 BOOST_AUTO_UNIT_TEST(parseExceptions)
342 {
343     senf::console::CommandParser parser;
344     std::string msg;
345
346 #   define CheckParseEx(c, e)                                                                     \
347         commands.clear();                                                                         \
348         msg.clear();                                                                              \
349         try { parser.parse(c, &setInfo); }                                                        \
350         catch (std::exception & ex) { msg = parseErrorMessage(ex.what()); }                       \
351         BOOST_CHECK_EQUAL( msg, e )
352     
353     CheckParseEx( "/foo/bar;\n  ()", "path expected\nat <unknown>:2:3" );
354     CheckParseEx( "cd /foo/bar foo/bar", "end of statement expected\nat <unknown>:1:13" );
355     CheckParseEx( "/foo/bar foo /", "end of statement expected\nat <unknown>:1:14" );
356     CheckParseEx( "cd \"foo\"", "path expected\nat <unknown>:1:4" );
357     CheckParseEx( "/foo/bar \"string", "'\"' expected\nat <unknown>:1:17" );
358     CheckParseEx( "/foo/bar x\"hi\"", "'\"' expected\nat <unknown>:1:12" );
359     CheckParseEx( "/foo/bar (", "')' expected\nat <unknown>:1:11" );
360 }
361
362 ///////////////////////////////cc.e////////////////////////////////////////
363 #undef prefix_
364
365 \f
366 // Local Variables:
367 // mode: c++
368 // fill-column: 100
369 // comment-column: 40
370 // c-file-style: "senf"
371 // indent-tabs-mode: nil
372 // ispell-local-dictionary: "american"
373 // compile-command: "scons -u test"
374 // End: