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