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.test 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"; }
70 { os_ << "builtin_exit()\n"; }
71 void builtin_help(std::vector<senf::console::Token> const & path)
72 { os_ << "builtin_help( " << senf::stringJoin(path, "/") << " )\n"; }
76 BOOST_AUTO_UNIT_TEST(commandGrammar)
78 senf::console::detail::CommandGrammar<TestParseDispatcher>::Context context;
80 TestParseDispatcher dispatcher (ss);
82 typedef senf::console::detail::CommandGrammar<TestParseDispatcher> Grammar;
83 Grammar grammar (dispatcher, context);
91 " (a,b;c (huhu/{haha}))"
93 " x\"01 02 # Inner comment\n"
96 BOOST_CHECK( boost::spirit::parse(
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"
126 BOOST_CHECK( boost::spirit::parse(
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" );
135 BOOST_CHECK( boost::spirit::parse(
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" );
144 BOOST_CHECK( boost::spirit::parse(
146 grammar.use_parser<Grammar::CommandParser>(),
147 grammar.use_parser<Grammar::SkipParser>() ) . full );
148 BOOST_CHECK_EQUAL( ss.str(), "builtin_exit()\n" );
153 BOOST_CHECK( boost::spirit::parse(
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"
165 BOOST_CHECK( boost::spirit::parse(
167 grammar.use_parser<Grammar::CommandParser>(),
168 grammar.use_parser<Grammar::SkipParser>() ) . full );
169 BOOST_CHECK_EQUAL( ss.str(), "popDirectory()\n" );
174 BOOST_CHECK( boost::spirit::parse(
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" );
183 std::vector<senf::console::ParseCommandInfo> commands;
184 void setInfo(senf::console::ParseCommandInfo const & i)
185 { commands.push_back(i); }
188 BOOST_AUTO_UNIT_TEST(commandParser)
190 senf::console::CommandParser parser;
194 "doo / bii / doo arg"
198 " \"foo\\\"bar\" #\n"
199 " x\"01 02 # Inner comment\n"
203 SENF_CHECK_NO_THROW( parser.parse(text, &setInfo) );
204 BOOST_CHECK_EQUAL( commands.size(), 2u );
207 senf::console::ParseCommandInfo const & info (commands.front());
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")
215 BOOST_CHECK_EQUAL_COLLECTIONS( info.commandPath().begin(), info.commandPath().end(),
216 path, path + sizeof(path)/sizeof(path[0]) );
217 BOOST_CHECK_EQUAL( unsigned(info.tokens().size()), 15u );
219 char const * tokens[] = { "arg",
222 "(", "a", ",", "b", ",", "c", "(", "huhu", ")", ")",
224 "\x01\x02\x03\x04" };
226 senf::console::ParseCommandInfo::argument_iterator args (info.arguments().begin());
227 BOOST_REQUIRE( args != info.arguments().end() );
228 BOOST_REQUIRE_EQUAL( unsigned(args->size()), 1u );
229 BOOST_CHECK_EQUAL( args->begin()->value(), tokens[0] );
232 BOOST_REQUIRE( args != info.arguments().end() );
233 BOOST_REQUIRE_EQUAL( args->size(), 1u );
234 BOOST_CHECK_EQUAL( args->begin()->value(), tokens[1] );
237 BOOST_REQUIRE( args != info.arguments().end() );
238 BOOST_REQUIRE_EQUAL( args->size(), 1u );
239 BOOST_CHECK_EQUAL( args->begin()->value(), tokens[2] );
242 BOOST_REQUIRE( args != info.arguments().end() );
243 BOOST_REQUIRE_EQUAL( args->size(), 8u );
244 for (unsigned i (0); i<8; ++i)
245 BOOST_CHECK_EQUAL( args->begin()[i].value(), tokens[4+i] );
248 BOOST_REQUIRE( args != info.arguments().end() );
249 BOOST_REQUIRE_EQUAL( args->size(), 1u );
250 BOOST_CHECK_EQUAL( args->begin()->value(), tokens[13] );
253 BOOST_REQUIRE( args != info.arguments().end() );
254 BOOST_REQUIRE_EQUAL( args->size(), 1u );
255 BOOST_CHECK_EQUAL( args->begin()->value(), tokens[14] );
258 BOOST_CHECK( args == info.arguments().end() );
265 void parseArgs(senf::console::ParseCommandInfo::ArgumentsRange const & args)
267 senf::console::CheckedArgumentIteratorWrapper arg (args);
268 senf::console::ParseCommandInfo::TokensRange arg1 (*(arg++));
269 senf::console::ParseCommandInfo::TokensRange arg2 (*(arg++));
273 BOOST_AUTO_UNIT_TEST(checkedArgumentIterator)
275 senf::console::CommandParser parser;
277 SENF_CHECK_NO_THROW( parser.parse("foo a", &setInfo) );
278 BOOST_CHECK_THROW( parseArgs(commands.back().arguments()),
279 senf::console::SyntaxErrorException );
281 SENF_CHECK_NO_THROW( parser.parse("foo a b", &setInfo) );
282 SENF_CHECK_NO_THROW( parseArgs(commands.back().arguments()) );
284 SENF_CHECK_NO_THROW( parser.parse("foo a b c", &setInfo) );
285 BOOST_CHECK_THROW( parseArgs(commands.back().arguments()),
286 senf::console::SyntaxErrorException );
288 senf::console::CheckedArgumentIteratorWrapper arg (commands.back().arguments());
289 BOOST_CHECK( arg == commands.back().arguments().begin() );
290 BOOST_CHECK( arg != commands.back().arguments().end() );
295 BOOST_CHECK( arg.done() );
297 senf::console::ParseCommandInfo::ArgumentIterator i (arg);
298 BOOST_CHECK( i == commands.back().arguments().end() );
303 BOOST_AUTO_UNIT_TEST(parseIncremental)
305 senf::console::CommandParser parser;
307 BOOST_CHECK_EQUAL( parser.parseIncremental("foo a", &setInfo), 0u );
308 BOOST_CHECK_EQUAL( parser.parseIncremental("foo a; cd", &setInfo), 7u );
309 BOOST_CHECK_EQUAL( parser.parseIncremental("foo a; cd /bar", &setInfo), 7u );
310 BOOST_CHECK_EQUAL( parser.parseIncremental("foo a; cd /bar; ", &setInfo), 16u );
311 BOOST_CHECK_EQUAL( parser.parseIncremental(" ", &setInfo), 1u );
312 BOOST_CHECK_EQUAL( commands.size(), 4u );
318 std::string parseErrorMessage(std::string const & msg)
320 std::string::size_type i (msg.find("-- \n"));
321 return i == std::string::npos ? msg : msg.substr(i+4);
325 BOOST_AUTO_UNIT_TEST(parseExceptions)
327 senf::console::CommandParser parser;
330 # define CheckParseEx(c, e) \
333 try { parser.parse(c, &setInfo); } \
334 catch (std::exception & ex) { msg = parseErrorMessage(ex.what()); } \
335 BOOST_CHECK_EQUAL( msg, e )
337 CheckParseEx( "/foo/bar;\n ()", "path expected\nat <unknown>:2:3" );
338 CheckParseEx( "cd /foo/bar foo/bar", "end of statement expected\nat <unknown>:1:13" );
339 CheckParseEx( "/foo/bar foo /", "end of statement expected\nat <unknown>:1:14" );
340 CheckParseEx( "cd \"foo\"", "path expected\nat <unknown>:1:4" );
341 CheckParseEx( "/foo/bar \"string", "'\"' expected\nat <unknown>:1:17" );
342 CheckParseEx( "/foo/bar x\"hi\"", "'\"' expected\nat <unknown>:1:12" );
343 CheckParseEx( "/foo/bar (", "')' expected\nat <unknown>:1:11" );
346 ///////////////////////////////cc.e////////////////////////////////////////
353 // comment-column: 40
354 // c-file-style: "senf"
355 // indent-tabs-mode: nil
356 // ispell-local-dictionary: "american"
357 // compile-command: "scons -u test"