4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
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.
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.
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.
24 \brief Parse unit tests */
26 //#include "Parse.test.hh"
27 //#include "Parse.test.ih"
29 // #define BOOST_SPIRIT_DEBUG
30 // #define BOOST_SPIRIT_DEBUG_TRACENODE 0
36 #include "../../Utils/String.hh"
38 #include "../../Utils/auto_unit_test.hh"
39 #include <boost/test/test_tools.hpp>
42 ///////////////////////////////cc.p////////////////////////////////////////
46 struct TestParseDispatcher
48 TestParseDispatcher(std::ostream & os) : os_ (os) {}
53 { os_ << "pushDirectory()\n"; }
55 { os_ << "popDirectory()\n"; }
57 void beginCommand(std::vector<senf::console::Token> const & command)
58 { os_ << "beginCommand( " << senf::stringJoin(command, "/") << " )\n"; }
60 { os_ << "endCommand()\n"; }
62 void pushToken(senf::console::Token token)
63 { os_ << "pushToken( " << token << " )\n"; }
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"; }
72 { os_ << "builtin_exit()\n"; }
73 void builtin_help(std::vector<senf::console::Token> const & path)
74 { os_ << "builtin_help( " << senf::stringJoin(path, "/") << " )\n"; }
78 BOOST_AUTO_UNIT_TEST(commandGrammar)
80 senf::console::detail::CommandGrammar<TestParseDispatcher>::Context context;
82 TestParseDispatcher dispatcher (ss);
84 typedef senf::console::detail::CommandGrammar<TestParseDispatcher> Grammar;
85 Grammar grammar (dispatcher, context);
93 " (a,b;c (huhu/{haha}))"
95 " x\"01 02 # Inner comment\n"
98 BOOST_CHECK( boost::spirit::parse(
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"
128 BOOST_CHECK( boost::spirit::parse(
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" );
137 BOOST_CHECK( boost::spirit::parse(
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" );
146 BOOST_CHECK( boost::spirit::parse(
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" );
155 BOOST_CHECK( boost::spirit::parse(
157 grammar.use_parser<Grammar::CommandParser>(),
158 grammar.use_parser<Grammar::SkipParser>() ) . full );
159 BOOST_CHECK_EQUAL( ss.str(), "builtin_exit()\n" );
164 BOOST_CHECK( boost::spirit::parse(
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"
176 BOOST_CHECK( boost::spirit::parse(
178 grammar.use_parser<Grammar::CommandParser>(),
179 grammar.use_parser<Grammar::SkipParser>() ) . full );
180 BOOST_CHECK_EQUAL( ss.str(), "popDirectory()\n" );
185 BOOST_CHECK( boost::spirit::parse(
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" );
194 std::vector<senf::console::ParseCommandInfo> commands;
195 void setInfo(senf::console::ParseCommandInfo const & i)
196 { commands.push_back(i); }
199 BOOST_AUTO_UNIT_TEST(commandParser)
201 senf::console::CommandParser parser;
205 "doo / bii / doo arg"
209 " \"foo\\\"bar\" #\n"
210 " x\"01 02 # Inner comment\n"
214 SENF_CHECK_NO_THROW( parser.parse(text, &setInfo) );
215 BOOST_CHECK_EQUAL( commands.size(), 2u );
218 senf::console::ParseCommandInfo const & info (commands.front());
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")
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 );
231 char const * tokens[] = { "arg",
234 "(", "a", ",", "b", ",", "c", "(", "huhu", ")", ")",
236 "\x01\x02\x03\x04" };
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] );
244 BOOST_REQUIRE( args != info.arguments().end() );
245 BOOST_REQUIRE_EQUAL( args->size(), 1u );
246 BOOST_CHECK_EQUAL( args->begin()->value(), tokens[1] );
249 BOOST_REQUIRE( args != info.arguments().end() );
250 BOOST_REQUIRE_EQUAL( args->size(), 1u );
251 BOOST_CHECK_EQUAL( args->begin()->value(), tokens[2] );
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 );
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 );
269 BOOST_REQUIRE( args != info.arguments().end() );
270 BOOST_REQUIRE_EQUAL( args->size(), 1u );
271 BOOST_CHECK_EQUAL( args->begin()->value(), tokens[14] );
274 BOOST_CHECK( args == info.arguments().end() );
281 void parseArgs(senf::console::ParseCommandInfo::ArgumentsRange const & args)
283 senf::console::CheckedArgumentIteratorWrapper arg (args);
284 senf::console::ParseCommandInfo::TokensRange arg1 (*(arg++));
285 senf::console::ParseCommandInfo::TokensRange arg2 (*(arg++));
289 BOOST_AUTO_UNIT_TEST(checkedArgumentIterator)
291 senf::console::CommandParser parser;
293 SENF_CHECK_NO_THROW( parser.parse("foo a", &setInfo) );
294 BOOST_CHECK_THROW( parseArgs(commands.back().arguments()),
295 senf::console::SyntaxErrorException );
297 SENF_CHECK_NO_THROW( parser.parse("foo a b", &setInfo) );
298 SENF_CHECK_NO_THROW( parseArgs(commands.back().arguments()) );
300 SENF_CHECK_NO_THROW( parser.parse("foo a b c", &setInfo) );
301 BOOST_CHECK_THROW( parseArgs(commands.back().arguments()),
302 senf::console::SyntaxErrorException );
304 senf::console::CheckedArgumentIteratorWrapper arg (commands.back().arguments());
305 BOOST_CHECK( arg == commands.back().arguments().begin() );
306 BOOST_CHECK( arg != commands.back().arguments().end() );
311 BOOST_CHECK( arg.done() );
313 senf::console::ParseCommandInfo::ArgumentIterator i (arg);
314 BOOST_CHECK( i == commands.back().arguments().end() );
319 BOOST_AUTO_UNIT_TEST(parseIncremental)
321 senf::console::CommandParser parser;
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 );
334 std::string parseErrorMessage(std::string const & msg)
336 std::string::size_type i (msg.find("-- \n"));
337 return i == std::string::npos ? msg : msg.substr(i+4);
341 BOOST_AUTO_UNIT_TEST(parseExceptions)
343 senf::console::CommandParser parser;
346 # define CheckParseEx(c, e) \
349 try { parser.parse(c, &setInfo); } \
350 catch (std::exception & ex) { msg = parseErrorMessage(ex.what()); } \
351 BOOST_CHECK_EQUAL( msg, e )
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" );
362 ///////////////////////////////cc.e////////////////////////////////////////
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"