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"
34 #include <boost/test/auto_unit_test.hpp>
35 #include <boost/test/test_tools.hpp>
38 ///////////////////////////////cc.p////////////////////////////////////////
42 struct TestParseDispatcher
44 TestParseDispatcher(std::ostream & os) : os_ (os) {}
48 void beginCommand(std::string const & command)
49 { os_ << "beginCommand( " << command << " )\n"; }
51 { os_ << "endCommand()\n"; }
53 void pushArgument(std::string const & argument)
54 { os_ << "pushArgument( " << argument << " )\n"; }
56 { os_ << "openGroup()\n"; }
58 { os_ << "closeGroup()\n"; }
59 void pushPunctuation(std::string const & token)
60 { os_ << "pushPunctuation( " << token << " )\n"; }
61 void pushWord(std::string const & token)
62 { os_ << "pushWord( " << token << " )\n"; }
66 BOOST_AUTO_UNIT_TEST(commandParser)
68 senf::console::detail::CommandGrammar<TestParseDispatcher>::Context context;
70 TestParseDispatcher dispatcher (ss);
71 senf::console::detail::CommandGrammar<TestParseDispatcher> grammar (dispatcher, context);
72 senf::console::detail::SkipGrammar skipGrammar;
76 "doo / bii / doo arg/../path"
81 " x\"01 02 # Inner comment\n"
84 BOOST_CHECK( boost::spirit::parse(
87 skipGrammar ) . full );
88 BOOST_CHECK_EQUAL( ss.str(),
89 "beginCommand( doo/bii/doo )\n"
90 "pushArgument( arg/../path )\n"
91 "pushArgument( flab::blub )\n"
92 "pushArgument( 123.434>a )\n"
95 "pushPunctuation( , )\n"
97 "pushPunctuation( , )\n"
99 "pushPunctuation( ( )\n"
101 "pushPunctuation( ) )\n"
103 "pushArgument( foo\"bar )\n"
104 "pushArgument( \x01\x02\x03\x04 )\n"
108 BOOST_AUTO_UNIT_TEST(singleCommandParser)
110 senf::console::SingleCommandParser parser;
114 "doo / bii / doo arg/../path"
118 " \"foo\\\"bar\" #\n"
119 " x\"01 02 # Inner comment\n"
122 senf::console::ParseCommandInfo info;
123 BOOST_CHECK( parser.parseCommand(text, info) );
125 BOOST_CHECK_EQUAL( info.commandPath(), "doo/bii/doo" );
126 BOOST_REQUIRE_EQUAL( info.arguments(), 6u );
127 BOOST_REQUIRE_EQUAL( info.tokens(), 13u );
129 char const * tokens[] = { "arg/../path",
132 "a", ",", "b", ",", "c", "(", "huhu", ")",
134 "\x01\x02\x03\x04" };
136 BOOST_REQUIRE_EQUAL( info.begin_arguments()[0].size(), 1u );
137 BOOST_CHECK_EQUAL( info.begin_arguments()[0].begin()->value(), tokens[0] );
139 BOOST_REQUIRE_EQUAL( info.begin_arguments()[1].size(), 1u );
140 BOOST_CHECK_EQUAL( info.begin_arguments()[1].begin()->value(), tokens[1] );
142 BOOST_REQUIRE_EQUAL( info.begin_arguments()[2].size(), 1u );
143 BOOST_CHECK_EQUAL( info.begin_arguments()[2].begin()->value(), tokens[2] );
145 BOOST_REQUIRE_EQUAL( info.begin_arguments()[3].size(), 8u );
146 for (unsigned i (0); i<8; ++i)
147 BOOST_CHECK_EQUAL( info.begin_arguments()[3].begin()[i].value(), tokens[3+i] );
149 BOOST_REQUIRE_EQUAL( info.begin_arguments()[4].size(), 1u );
150 BOOST_CHECK_EQUAL( info.begin_arguments()[4].begin()->value(), tokens[11] );
152 BOOST_REQUIRE_EQUAL( info.begin_arguments()[5].size(), 1u );
153 BOOST_CHECK_EQUAL( info.begin_arguments()[5].begin()->value(), tokens[12] );
156 ///////////////////////////////cc.e////////////////////////////////////////
163 // comment-column: 40
164 // c-file-style: "senf"
165 // indent-tabs-mode: nil
166 // ispell-local-dictionary: "american"
167 // compile-command: "scons -u test"