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////////////////////////////////////////
48 struct TestParseDispatcher
50 TestParseDispatcher(std::ostream & os) : os_ (os) {}
54 void pushDirectory(std::vector<std::string> const & path)
55 { os_ << "pushDirectory( " << senf::stringJoin(path,"/") << " )\n"; }
57 { os_ << "popDirectory()\n"; }
59 void beginCommand(std::vector<std::string> const & command)
60 { os_ << "beginCommand( " << senf::stringJoin(command, "/") << " )\n"; }
62 { os_ << "endCommand()\n"; }
64 void pushArgument(std::string const & argument)
65 { os_ << "pushArgument( " << argument << " )\n"; }
67 { os_ << "openGroup()\n"; }
69 { os_ << "closeGroup()\n"; }
70 void pushPunctuation(std::string const & token)
71 { os_ << "pushPunctuation( " << token << " )\n"; }
72 void pushWord(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_cd( " << senf::stringJoin(path, "/") << " )\n"; }
80 { os_ << "builtin_exit()\n"; }
84 BOOST_AUTO_UNIT_TEST(commandGrammar)
86 senf::console::detail::CommandGrammar<TestParseDispatcher>::Context context;
88 TestParseDispatcher dispatcher (ss);
90 typedef senf::console::detail::CommandGrammar<TestParseDispatcher> Grammar;
91 Grammar grammar (dispatcher, context);
100 " x\"01 02 # Inner comment\n"
103 BOOST_CHECK( boost::spirit::parse(
105 grammar.use_parser<Grammar::CommandParser>(),
106 grammar.use_parser<Grammar::SkipParser>() ) . full );
107 BOOST_CHECK_EQUAL( ss.str(),
108 "beginCommand( doo/bii/doo )\n"
109 "pushArgument( arg )\n"
110 "pushArgument( flab::blub )\n"
111 "pushArgument( 123.434>a )\n"
114 "pushPunctuation( , )\n"
116 "pushPunctuation( , )\n"
118 "pushPunctuation( ( )\n"
120 "pushPunctuation( ) )\n"
122 "pushArgument( foo\"bar )\n"
123 "pushArgument( \x01\x02\x03\x04 )\n"
128 senf::console::ParseCommandInfo info;
129 void setInfo(senf::console::ParseCommandInfo const & i)
133 BOOST_AUTO_UNIT_TEST(commandParser)
135 senf::console::CommandParser parser;
139 "doo / bii / doo arg"
143 " \"foo\\\"bar\" #\n"
144 " x\"01 02 # Inner comment\n"
147 BOOST_CHECK( parser.parse(text, &setInfo) );
149 char const * path[] = { "doo", "bii", "doo" };
151 BOOST_CHECK_EQUAL_COLLECTIONS( info.commandPath().begin(), info.commandPath().end(),
152 path, path + sizeof(path)/sizeof(path[0]) );
153 BOOST_REQUIRE_EQUAL( info.arguments().size(), 6u );
154 BOOST_REQUIRE_EQUAL( info.tokens().size(), 13u );
156 char const * tokens[] = { "arg",
159 "a", ",", "b", ",", "c", "(", "huhu", ")",
161 "\x01\x02\x03\x04" };
163 BOOST_REQUIRE_EQUAL( info.arguments().begin()[0].size(), 1u );
164 BOOST_CHECK_EQUAL( info.arguments().begin()[0].begin()->value(), tokens[0] );
166 BOOST_REQUIRE_EQUAL( info.arguments().begin()[1].size(), 1u );
167 BOOST_CHECK_EQUAL( info.arguments().begin()[1].begin()->value(), tokens[1] );
169 BOOST_REQUIRE_EQUAL( info.arguments().begin()[2].size(), 1u );
170 BOOST_CHECK_EQUAL( info.arguments().begin()[2].begin()->value(), tokens[2] );
172 BOOST_REQUIRE_EQUAL( info.arguments().begin()[3].size(), 8u );
173 for (unsigned i (0); i<8; ++i)
174 BOOST_CHECK_EQUAL( info.arguments().begin()[3].begin()[i].value(), tokens[3+i] );
176 BOOST_REQUIRE_EQUAL( info.arguments().begin()[4].size(), 1u );
177 BOOST_CHECK_EQUAL( info.arguments().begin()[4].begin()->value(), tokens[11] );
179 BOOST_REQUIRE_EQUAL( info.arguments().begin()[5].size(), 1u );
180 BOOST_CHECK_EQUAL( info.arguments().begin()[5].begin()->value(), tokens[12] );
183 ///////////////////////////////cc.e////////////////////////////////////////
190 // comment-column: 40
191 // c-file-style: "senf"
192 // indent-tabs-mode: nil
193 // ispell-local-dictionary: "american"
194 // compile-command: "scons -u test"