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 <senf/Utils/String.hh>
38 #include <senf/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_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"; }
74 { os_ << "builtin_exit()\n"; }
75 void builtin_help(std::vector<senf::console::Token> const & path)
76 { os_ << "builtin_help( " << senf::stringJoin(path, "/") << " )\n"; }
80 SENF_AUTO_UNIT_TEST(commandGrammar)
82 senf::console::detail::CommandGrammar<TestParseDispatcher>::Context context;
84 TestParseDispatcher dispatcher (ss);
86 typedef senf::console::detail::CommandGrammar<TestParseDispatcher> Grammar;
87 Grammar grammar (dispatcher, context);
92 "doo / bii / // doo arg"
95 " (a,b;c (huhu/{haha}))"
97 " x\"01 02 # Inner comment\n"
100 BOOST_CHECK( senf::console::detail::boost_spirit::parse(
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"
130 BOOST_CHECK( senf::console::detail::boost_spirit::parse(
132 grammar.use_parser<Grammar::CommandParser>(),
133 grammar.use_parser<Grammar::SkipParser>() ) . full );
134 BOOST_CHECK_EQUAL( ss.str(), "builtin_ls( None('')/Word('foo')/Word('bar') )\n" );
139 BOOST_CHECK( senf::console::detail::boost_spirit::parse(
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" );
148 BOOST_CHECK( senf::console::detail::boost_spirit::parse(
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" );
157 BOOST_CHECK( senf::console::detail::boost_spirit::parse(
159 grammar.use_parser<Grammar::CommandParser>(),
160 grammar.use_parser<Grammar::SkipParser>() ) . full );
161 BOOST_CHECK_EQUAL( ss.str(), "builtin_exit()\n" );
166 BOOST_CHECK( senf::console::detail::boost_spirit::parse(
168 grammar.use_parser<Grammar::CommandParser>(),
169 grammar.use_parser<Grammar::SkipParser>() ) . full );
170 BOOST_CHECK_EQUAL( ss.str(),
171 "beginCommand( Word('foo')/Word('bar')/None('') )\n"
178 BOOST_CHECK( senf::console::detail::boost_spirit::parse(
180 grammar.use_parser<Grammar::CommandParser>(),
181 grammar.use_parser<Grammar::SkipParser>() ) . full );
182 BOOST_CHECK_EQUAL( ss.str(), "popDirectory()\n" );
187 BOOST_CHECK( senf::console::detail::boost_spirit::parse(
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" );
196 std::vector<senf::console::ParseCommandInfo> commands;
197 void setInfo(senf::console::ParseCommandInfo const & i)
198 { commands.push_back(i); }
201 SENF_AUTO_UNIT_TEST(commandParser)
203 senf::console::CommandParser parser;
207 "doo / bii / doo arg"
211 " \"foo\\\"bar\" #\n"
212 " x\"01 02 # Inner comment\n"
216 SENF_CHECK_NO_THROW( parser.parse(text, &setInfo) );
217 BOOST_CHECK_EQUAL( commands.size(), 2u );
220 senf::console::ParseCommandInfo const & info (commands.front());
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")
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 );
233 char const * tokens[] = { "arg",
236 "(", "a", ",", "b", ",", "c", "(", "huhu", ")", ")",
238 "\x01\x02\x03\x04" };
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] );
246 BOOST_REQUIRE( args != info.arguments().end() );
247 BOOST_REQUIRE_EQUAL( args->size(), 1u );
248 BOOST_CHECK_EQUAL( args->begin()->value(), tokens[1] );
251 BOOST_REQUIRE( args != info.arguments().end() );
252 BOOST_REQUIRE_EQUAL( args->size(), 1u );
253 BOOST_CHECK_EQUAL( args->begin()->value(), tokens[2] );
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 );
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 );
271 BOOST_REQUIRE( args != info.arguments().end() );
272 BOOST_REQUIRE_EQUAL( args->size(), 1u );
273 BOOST_CHECK_EQUAL( args->begin()->value(), tokens[14] );
276 BOOST_CHECK( args == info.arguments().end() );
283 void parseArgs(senf::console::ParseCommandInfo::ArgumentsRange const & args)
285 senf::console::CheckedArgumentIteratorWrapper arg (args);
286 senf::console::ParseCommandInfo::TokensRange arg1 (*(arg++));
287 senf::console::ParseCommandInfo::TokensRange arg2 (*(arg++));
291 SENF_AUTO_UNIT_TEST(checkedArgumentIterator)
293 senf::console::CommandParser parser;
295 SENF_CHECK_NO_THROW( parser.parse("foo a", &setInfo) );
296 BOOST_CHECK_THROW( parseArgs(commands.back().arguments()),
297 senf::console::SyntaxErrorException );
299 SENF_CHECK_NO_THROW( parser.parse("foo a b", &setInfo) );
300 SENF_CHECK_NO_THROW( parseArgs(commands.back().arguments()) );
302 SENF_CHECK_NO_THROW( parser.parse("foo a b c", &setInfo) );
303 BOOST_CHECK_THROW( parseArgs(commands.back().arguments()),
304 senf::console::SyntaxErrorException );
306 senf::console::CheckedArgumentIteratorWrapper arg (commands.back().arguments());
307 BOOST_CHECK( arg == commands.back().arguments().begin() );
308 BOOST_CHECK( arg != commands.back().arguments().end() );
313 BOOST_CHECK( arg.done() );
315 senf::console::ParseCommandInfo::ArgumentIterator i (arg);
316 BOOST_CHECK( i == commands.back().arguments().end() );
321 SENF_AUTO_UNIT_TEST(parseIncremental)
323 senf::console::CommandParser parser;
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 );
336 std::string parseErrorMessage(std::string const & msg)
338 std::string::size_type i (msg.find("-- \n"));
339 return i == std::string::npos ? msg : msg.substr(i+4);
343 SENF_AUTO_UNIT_TEST(parseExceptions)
345 senf::console::CommandParser parser;
348 # define CheckParseEx(c, e) \
351 try { parser.parse(c, &setInfo); } \
352 catch (std::exception & ex) { msg = parseErrorMessage(ex.what()); } \
353 BOOST_CHECK_EQUAL( msg, e )
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" );
364 ///////////////////////////////cc.e////////////////////////////////////////
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"