Add missing Build-Depends to debian/control
[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_exit()
70             { os_ << "builtin_exit()\n"; }
71         void builtin_help(std::vector<senf::console::Token> const & path)
72             { os_ << "builtin_help( " << senf::stringJoin(path, "/") << " )\n"; }
73     };
74 }
75
76 BOOST_AUTO_UNIT_TEST(commandGrammar)
77 {
78     senf::console::detail::CommandGrammar<TestParseDispatcher>::Context context;
79     std::stringstream ss;
80     TestParseDispatcher dispatcher (ss);
81     
82     typedef senf::console::detail::CommandGrammar<TestParseDispatcher> Grammar;
83     Grammar grammar (dispatcher, context);
84
85     {
86         static char text[] = 
87             "# Comment\n"
88             "doo / bii / doo arg"
89             "                flab::blub"
90             "                123.434>a"
91             "                (a,b;c (huhu/{haha}))"
92             "                \"foo\\\"bar\" #\n"
93             "                x\"01 02 # Inner comment\n"
94             "                   0304\";";
95
96         BOOST_CHECK( boost::spirit::parse( 
97                          text, 
98                          grammar.use_parser<Grammar::CommandParser>(), 
99                          grammar.use_parser<Grammar::SkipParser>() ) . full );
100         BOOST_CHECK_EQUAL( ss.str(), 
101                            "beginCommand( Word('doo')/Word('bii')/Word('doo') )\n"
102                            "pushToken( Word('arg') )\n"
103                            "pushToken( Word('flab::blub') )\n"
104                            "pushToken( Word('123.434>a') )\n"
105                            "pushToken( ArgumentGroupOpen('(') )\n"
106                            "pushToken( Word('a') )\n"
107                            "pushToken( OtherPunctuation(',') )\n"
108                            "pushToken( Word('b') )\n"
109                            "pushToken( CommandTerminator(';') )\n"
110                            "pushToken( Word('c') )\n"
111                            "pushToken( ArgumentGroupOpen('(') )\n"
112                            "pushToken( Word('huhu') )\n"
113                            "pushToken( PathSeparator('/') )\n"
114                            "pushToken( DirectoryGroupOpen('{') )\n"
115                            "pushToken( Word('haha') )\n"
116                            "pushToken( DirectoryGroupClose('}') )\n"
117                            "pushToken( ArgumentGroupClose(')') )\n"
118                            "pushToken( ArgumentGroupClose(')') )\n"
119                            "pushToken( BasicString('foo\"bar') )\n"
120                            "pushToken( HexString('\x01\x02\x03\x04') )\n"
121                            "endCommand()\n" );
122     }
123
124     {
125         ss.str("");
126         BOOST_CHECK( boost::spirit::parse( 
127                          "ls /foo/bar;", 
128                          grammar.use_parser<Grammar::CommandParser>(), 
129                          grammar.use_parser<Grammar::SkipParser>() ) . full );
130         BOOST_CHECK_EQUAL( ss.str(), "builtin_ls( None('')/Word('foo')/Word('bar') )\n" );
131     }
132
133     {
134         ss.str("");
135         BOOST_CHECK( boost::spirit::parse( 
136                          "cd /foo/bar;", 
137                          grammar.use_parser<Grammar::CommandParser>(), 
138                          grammar.use_parser<Grammar::SkipParser>() ) . full );
139         BOOST_CHECK_EQUAL( ss.str(), "builtin_cd( None('')/Word('foo')/Word('bar') )\n" );
140     }
141
142     {
143         ss.str("");
144         BOOST_CHECK( boost::spirit::parse( 
145                          "exit;", 
146                          grammar.use_parser<Grammar::CommandParser>(), 
147                          grammar.use_parser<Grammar::SkipParser>() ) . full );
148         BOOST_CHECK_EQUAL( ss.str(), "builtin_exit()\n" );
149     }
150
151     {
152         ss.str("");
153         BOOST_CHECK( boost::spirit::parse( 
154                          "foo/bar/ {", 
155                          grammar.use_parser<Grammar::CommandParser>(), 
156                          grammar.use_parser<Grammar::SkipParser>() ) . full );
157         BOOST_CHECK_EQUAL( ss.str(), 
158                            "beginCommand( Word('foo')/Word('bar')/None('') )\n"
159                            "pushDirectory()\n"
160                            "endCommand()\n" );
161     }
162
163     {
164         ss.str("");
165         BOOST_CHECK( boost::spirit::parse( 
166                          "}", 
167                          grammar.use_parser<Grammar::CommandParser>(), 
168                          grammar.use_parser<Grammar::SkipParser>() ) . full );
169         BOOST_CHECK_EQUAL( ss.str(), "popDirectory()\n" );
170     }
171
172     {
173         ss.str("");
174         BOOST_CHECK( boost::spirit::parse( 
175                          "help /foo/bar", 
176                          grammar.use_parser<Grammar::CommandParser>(), 
177                          grammar.use_parser<Grammar::SkipParser>() ) . full );
178         BOOST_CHECK_EQUAL( ss.str(), "builtin_help( None('')/Word('foo')/Word('bar') )\n" );
179     }
180 }
181
182 namespace {
183     std::vector<senf::console::ParseCommandInfo> commands;
184     void setInfo(senf::console::ParseCommandInfo const & i)
185     { commands.push_back(i); }
186 }
187
188 BOOST_AUTO_UNIT_TEST(commandParser)
189 {
190     senf::console::CommandParser parser;
191
192     char const text[] = 
193         "# Comment\n"
194         "doo / bii / doo arg"
195         "                flab::blub"
196         "                123.434>a"
197         "                (a,b,c (huhu))"
198         "                \"foo\\\"bar\" #\n"
199         "                x\"01 02 # Inner comment\n"
200         "                   0304\";"
201         "ls /foo/bar; ";
202
203     SENF_CHECK_NO_THROW( parser.parse(text, &setInfo) );
204     BOOST_CHECK_EQUAL( commands.size(), 2u );
205
206     {
207         senf::console::ParseCommandInfo const & info (commands.front());
208
209         senf::console::Token path[] = { 
210             senf::console::Token(senf::console::Token::Word, "doo"), 
211             senf::console::Token(senf::console::Token::Word, "bii"),
212             senf::console::Token(senf::console::Token::Word, "doo")
213         };
214
215         BOOST_CHECK_EQUAL_COLLECTIONS( info.commandPath().begin(), info.commandPath().end(),
216                                        path, path + sizeof(path)/sizeof(path[0]) );
217         BOOST_CHECK_EQUAL( boost::next(info.commandPath().begin())->index(), 16u );
218         BOOST_CHECK_EQUAL( unsigned(info.tokens().size()), 15u );
219         
220         char const * tokens[] = { "arg", 
221                                   "flab::blub", 
222                                   "123.434>a", 
223                                   "(", "a", ",", "b", ",", "c", "(", "huhu", ")", ")",
224                                   "foo\"bar",
225                                   "\x01\x02\x03\x04" };
226
227         senf::console::ParseCommandInfo::argument_iterator args (info.arguments().begin());
228         BOOST_REQUIRE( args != info.arguments().end() );
229         BOOST_REQUIRE_EQUAL( unsigned(args->size()), 1u );
230         BOOST_CHECK_EQUAL( args->begin()->value(), tokens[0] );
231         
232         ++ args;
233         BOOST_REQUIRE( args != info.arguments().end() );
234         BOOST_REQUIRE_EQUAL( args->size(), 1u );
235         BOOST_CHECK_EQUAL( args->begin()->value(), tokens[1] );
236         
237         ++ args;
238         BOOST_REQUIRE( args != info.arguments().end() );
239         BOOST_REQUIRE_EQUAL( args->size(), 1u );
240         BOOST_CHECK_EQUAL( args->begin()->value(), tokens[2] );
241         
242         ++ args;
243         BOOST_REQUIRE( args != info.arguments().end() );
244         BOOST_REQUIRE_EQUAL( args->size(), 8u );
245         for (unsigned i (0); i<8; ++i)
246             BOOST_CHECK_EQUAL( args->begin()[i].value(), tokens[4+i] );
247         BOOST_CHECK_EQUAL( info.tokens().begin()[3].index(), 96u );
248         BOOST_CHECK_EQUAL( info.tokens().begin()[5].index(), 98u );
249         BOOST_CHECK_EQUAL( info.tokens().begin()[12].index(), 109u );
250         
251         ++ args;
252         BOOST_REQUIRE( args != info.arguments().end() );
253         BOOST_REQUIRE_EQUAL( args->size(), 1u );
254         BOOST_CHECK_EQUAL( args->begin()->value(), tokens[13] );
255         BOOST_CHECK_EQUAL( args->begin()->index(), 126u );
256         
257         ++ args;
258         BOOST_REQUIRE( args != info.arguments().end() );
259         BOOST_REQUIRE_EQUAL( args->size(), 1u );
260         BOOST_CHECK_EQUAL( args->begin()->value(), tokens[14] );
261         
262         ++ args;
263         BOOST_CHECK( args == info.arguments().end() );
264     }
265
266     commands.clear();
267 }
268
269 namespace {
270     void parseArgs(senf::console::ParseCommandInfo::ArgumentsRange const & args)
271     {
272         senf::console::CheckedArgumentIteratorWrapper arg (args);
273         senf::console::ParseCommandInfo::TokensRange arg1 (*(arg++));
274         senf::console::ParseCommandInfo::TokensRange arg2 (*(arg++));
275     }
276 }
277
278 BOOST_AUTO_UNIT_TEST(checkedArgumentIterator)
279 {
280     senf::console::CommandParser parser;
281
282     SENF_CHECK_NO_THROW( parser.parse("foo a", &setInfo) );
283     BOOST_CHECK_THROW( parseArgs(commands.back().arguments()), 
284                        senf::console::SyntaxErrorException );
285
286     SENF_CHECK_NO_THROW( parser.parse("foo a b", &setInfo) );
287     SENF_CHECK_NO_THROW( parseArgs(commands.back().arguments()) );
288
289     SENF_CHECK_NO_THROW( parser.parse("foo a b c", &setInfo) );
290     BOOST_CHECK_THROW( parseArgs(commands.back().arguments()), 
291                        senf::console::SyntaxErrorException );
292     
293     senf::console::CheckedArgumentIteratorWrapper arg (commands.back().arguments());
294     BOOST_CHECK( arg == commands.back().arguments().begin() );
295     BOOST_CHECK( arg != commands.back().arguments().end() );
296     BOOST_CHECK( arg );
297     ++ arg;
298     BOOST_CHECK( arg );
299     arg.clear();
300     BOOST_CHECK( arg.done() );
301
302     senf::console::ParseCommandInfo::ArgumentIterator i (arg);
303     BOOST_CHECK( i == commands.back().arguments().end() );
304
305     commands.clear();
306 }
307
308 BOOST_AUTO_UNIT_TEST(parseIncremental)
309 {
310     senf::console::CommandParser parser;
311
312     BOOST_CHECK_EQUAL( parser.parseIncremental("foo a", &setInfo), 0u );
313     BOOST_CHECK_EQUAL( parser.parseIncremental("foo a; cd", &setInfo), 7u );
314     BOOST_CHECK_EQUAL( parser.parseIncremental("foo a; cd /bar", &setInfo), 7u );
315     BOOST_CHECK_EQUAL( parser.parseIncremental("foo a; cd /bar; ", &setInfo), 16u );
316     BOOST_CHECK_EQUAL( parser.parseIncremental(" ", &setInfo), 1u );
317     BOOST_CHECK_EQUAL( commands.size(), 4u );
318
319     commands.clear();
320 }
321
322 namespace {
323     std::string parseErrorMessage(std::string const & msg)
324     {
325         std::string::size_type i (msg.find("-- \n"));
326         return i == std::string::npos ? msg : msg.substr(i+4);
327     }
328 }
329
330 BOOST_AUTO_UNIT_TEST(parseExceptions)
331 {
332     senf::console::CommandParser parser;
333     std::string msg;
334
335 #   define CheckParseEx(c, e)                                                                     \
336         commands.clear();                                                                         \
337         msg.clear();                                                                              \
338         try { parser.parse(c, &setInfo); }                                                        \
339         catch (std::exception & ex) { msg = parseErrorMessage(ex.what()); }                       \
340         BOOST_CHECK_EQUAL( msg, e )
341     
342     CheckParseEx( "/foo/bar;\n  ()", "path expected\nat <unknown>:2:3" );
343     CheckParseEx( "cd /foo/bar foo/bar", "end of statement expected\nat <unknown>:1:13" );
344     CheckParseEx( "/foo/bar foo /", "end of statement expected\nat <unknown>:1:14" );
345     CheckParseEx( "cd \"foo\"", "path expected\nat <unknown>:1:4" );
346     CheckParseEx( "/foo/bar \"string", "'\"' expected\nat <unknown>:1:17" );
347     CheckParseEx( "/foo/bar x\"hi\"", "'\"' expected\nat <unknown>:1:12" );
348     CheckParseEx( "/foo/bar (", "')' expected\nat <unknown>:1:11" );
349 }
350
351 ///////////////////////////////cc.e////////////////////////////////////////
352 #undef prefix_
353
354 \f
355 // Local Variables:
356 // mode: c++
357 // fill-column: 100
358 // comment-column: 40
359 // c-file-style: "senf"
360 // indent-tabs-mode: nil
361 // ispell-local-dictionary: "american"
362 // compile-command: "scons -u test"
363 // End: