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