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) {}
52 void pushDirectory(std::vector<std::string> const & path)
53 { os_ << "pushDirectory( " << senf::stringJoin(path,"/") << " )\n"; }
55 { os_ << "popDirectory()\n"; }
57 void beginCommand(std::vector<std::string> const & command)
58 { os_ << "beginCommand( " << senf::stringJoin(command, "/") << " )\n"; }
60 { os_ << "endCommand()\n"; }
62 void pushArgument(senf::console::ArgumentToken::TokenType type,
63 std::string const & argument)
64 { os_ << "pushArgument( " << argument << " )\n"; }
66 { os_ << "openGroup()\n"; }
68 { os_ << "closeGroup()\n"; }
69 void pushPunctuation(std::string const & token)
70 { os_ << "pushPunctuation( " << token << " )\n"; }
71 void pushWord(senf::console::ArgumentToken::TokenType type,
72 std::string const & token)
73 { os_ << "pushWord( " << token << " )\n"; }
75 void builtin_cd(std::vector<std::string> const & path)
76 { os_ << "builtin_cd( " << senf::stringJoin(path, "/") << " )\n"; }
77 void builtin_ls(std::vector<std::string> const & path)
78 { os_ << "builtin_ls( " << senf::stringJoin(path, "/") << " )\n"; }
80 { os_ << "builtin_exit()\n"; }
81 void builtin_help(std::vector<std::string> const & path)
82 { os_ << "builtin_help( " << senf::stringJoin(path, "/") << " )\n"; }
86 BOOST_AUTO_UNIT_TEST(commandGrammar)
88 senf::console::detail::CommandGrammar<TestParseDispatcher>::Context context;
90 TestParseDispatcher dispatcher (ss);
92 typedef senf::console::detail::CommandGrammar<TestParseDispatcher> Grammar;
93 Grammar grammar (dispatcher, context);
100 " (a,b;c (huhu/{haha}))"
101 " \"foo\\\"bar\" #\n"
102 " x\"01 02 # Inner comment\n"
110 BOOST_CHECK( boost::spirit::parse(
112 grammar.use_parser<Grammar::CommandParser>(),
113 grammar.use_parser<Grammar::SkipParser>() ) . full );
114 BOOST_CHECK_EQUAL( ss.str(),
115 "beginCommand( doo/bii/doo )\n"
116 "pushArgument( arg )\n"
117 "pushArgument( flab::blub )\n"
118 "pushArgument( 123.434>a )\n"
121 "pushPunctuation( , )\n"
123 "pushPunctuation( ; )\n"
125 "pushPunctuation( ( )\n"
127 "pushPunctuation( / )\n"
128 "pushPunctuation( { )\n"
130 "pushPunctuation( } )\n"
131 "pushPunctuation( ) )\n"
133 "pushArgument( foo\"bar )\n"
134 "pushArgument( \x01\x02\x03\x04 )\n"
136 "builtin_ls( /foo/bar )\n"
137 "builtin_cd( /foo/bar )\n"
139 "pushDirectory( foo/bar/ )\n"
142 "builtin_help( /foo/bar )\n" );
146 senf::console::ParseCommandInfo info;
147 void setInfo(senf::console::ParseCommandInfo const & i)
151 BOOST_AUTO_UNIT_TEST(commandParser)
153 senf::console::CommandParser parser;
157 "doo / bii / doo arg"
161 " \"foo\\\"bar\" #\n"
162 " x\"01 02 # Inner comment\n"
165 BOOST_CHECK( parser.parse(text, &setInfo) );
167 char const * path[] = { "doo", "bii", "doo" };
169 BOOST_CHECK_EQUAL_COLLECTIONS( info.commandPath().begin(), info.commandPath().end(),
170 path, path + sizeof(path)/sizeof(path[0]) );
171 BOOST_CHECK_EQUAL( info.tokens().size(), 15u );
173 char const * tokens[] = { "arg",
176 "(", "a", ",", "b", ",", "c", "(", "huhu", ")", ")",
178 "\x01\x02\x03\x04" };
180 senf::console::ParseCommandInfo::argument_iterator args (info.arguments().begin());
181 BOOST_REQUIRE( args != info.arguments().end() );
182 BOOST_REQUIRE_EQUAL( args->size(), 1u );
183 BOOST_CHECK_EQUAL( args->begin()->value(), tokens[0] );
186 BOOST_REQUIRE( args != info.arguments().end() );
187 BOOST_REQUIRE_EQUAL( args->size(), 1u );
188 BOOST_CHECK_EQUAL( args->begin()->value(), tokens[1] );
191 BOOST_REQUIRE( args != info.arguments().end() );
192 BOOST_REQUIRE_EQUAL( args->size(), 1u );
193 BOOST_CHECK_EQUAL( args->begin()->value(), tokens[2] );
196 BOOST_REQUIRE( args != info.arguments().end() );
197 BOOST_REQUIRE_EQUAL( args->size(), 8u );
198 for (unsigned i (0); i<8; ++i)
199 BOOST_CHECK_EQUAL( args->begin()[i].value(), tokens[4+i] );
202 BOOST_REQUIRE( args != info.arguments().end() );
203 BOOST_REQUIRE_EQUAL( args->size(), 1u );
204 BOOST_CHECK_EQUAL( args->begin()->value(), tokens[13] );
207 BOOST_REQUIRE( args != info.arguments().end() );
208 BOOST_REQUIRE_EQUAL( args->size(), 1u );
209 BOOST_CHECK_EQUAL( args->begin()->value(), tokens[14] );
212 BOOST_CHECK( args == info.arguments().end() );
216 void parseArgs(senf::console::ParseCommandInfo::ArgumentsRange const & args)
218 senf::console::CheckedArgumentIteratorWrapper arg (args);
219 senf::console::ParseCommandInfo::TokensRange arg1 (*(arg++));
220 senf::console::ParseCommandInfo::TokensRange arg2 (*(arg++));
224 BOOST_AUTO_UNIT_TEST(checkedArgumentIterator)
226 senf::console::CommandParser parser;
228 BOOST_CHECK( parser.parse("foo a", &setInfo) );
229 BOOST_CHECK_THROW( parseArgs(info.arguments()), senf::console::SyntaxErrorException );
231 BOOST_CHECK( parser.parse("foo a b", &setInfo) );
232 BOOST_CHECK_NO_THROW( parseArgs(info.arguments()) );
234 BOOST_CHECK( parser.parse("foo a b c", &setInfo) );
235 BOOST_CHECK_THROW( parseArgs(info.arguments()), senf::console::SyntaxErrorException );
237 senf::console::CheckedArgumentIteratorWrapper arg (info.arguments());
238 BOOST_CHECK( arg == info.arguments().begin() );
239 BOOST_CHECK( arg != info.arguments().end() );
244 BOOST_CHECK( arg.done() );
246 senf::console::ParseCommandInfo::ArgumentIterator i (arg);
247 BOOST_CHECK( i == info.arguments().end() );
250 ///////////////////////////////cc.e////////////////////////////////////////
257 // comment-column: 40
258 // c-file-style: "senf"
259 // indent-tabs-mode: nil
260 // ispell-local-dictionary: "american"
261 // compile-command: "scons -u test"