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 ParsedCommand.test unit tests */
26 //#include "ParsedCommand.test.hh"
27 //#include "ParsedCommand.test.ih"
32 #include "ParsedCommand.hh"
33 #include "Executor.hh"
35 #include "ScopedDirectory.hh"
37 #include "../Utils/auto_unit_test.hh"
38 #include <boost/test/test_tools.hpp>
41 ///////////////////////////////cc.p////////////////////////////////////////
45 int cb1(int a, double b) { return int(a+b); }
46 double cb2(){ return 1.2; }
48 std::string cb4(std::ostream & os) { os << "text\n"; return "value"; }
49 void cb5(std::ostream & os, std::string const & v) { os << "Value: " << v << "\n"; }
53 typedef senf::console::ParseCommandInfo::TokensRange const & first_argument_type;
54 typedef std::string & second_argument_type;
55 typedef void result_type;
57 result_type operator()(first_argument_type, second_argument_type out) const
63 BOOST_AUTO_UNIT_TEST(parsedCommand)
65 senf::console::Executor executor;
66 senf::console::CommandParser parser;
67 senf::console::ScopedDirectory<> dir;
68 senf::console::root().add("test", dir);
73 parser.parse("test/cb1 2 3.2",
74 boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 ));
75 BOOST_CHECK_EQUAL( ss.str(), "5\n" );
81 parser.parse("test/cb2",
82 boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 ));
83 BOOST_CHECK_EQUAL( ss.str(), "1.2\n" );
89 parser.parse("test/cb3 234",
90 boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 ));
91 BOOST_CHECK_EQUAL( ss.str(), "" );
97 parser.parse("test/cb4",
98 boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 ));
99 BOOST_CHECK_EQUAL( ss.str(), "text\n" "value\n" );
103 std::stringstream ss;
104 dir.add("cb5", &cb5);
105 parser.parse("test/cb5 1234",
106 boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 ));
107 BOOST_CHECK_EQUAL( ss.str(), "Value: 1234\n" );
111 std::stringstream ss;
114 parser.parse("test/cb1 2 3.2 foo",
115 boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )),
116 senf::console::SyntaxErrorException );
119 parser.parse("test/cb1 2",
120 boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )),
121 senf::console::SyntaxErrorException );
124 parser.parse("test/cb1 2 foo",
125 boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )),
126 senf::console::SyntaxErrorException );
130 namespace kw = senf::console::kw;
131 std::stringstream ss;
133 // Just for the fun of it, use a functor and not a function pointer as parser ...
135 .arg( kw::parser = TestParser() );
136 BOOST_CHECK_NO_THROW(
137 parser.parse("test/cb6 false",
138 boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )) );
139 BOOST_CHECK_EQUAL( ss.str(), "Value: true\n" );
144 std::stringstream ss;
146 using namespace senf::console::kw;
150 "Ops fortunate, ops me ut orgia vociferatio contumax per, rudo re loco emitto\n"
151 "intolerabiliter ita iugo. Subcribo gravo. Devenio luna fonticulus Castanea\n"
152 "horum fascino Os interpretor non ipse conjuratio hora, qui filius denuntio ait\n"
153 "sono te odium Anhelo. Dum Cedo audax celox alius una Agnosco hic, ibi retineo\n"
154 "lux sto ioco. Per Re dono. Copiose reus scitus jus diligens sis scapulare\n"
155 "Servitium transi." )
158 "Lo nam balnearius Opprimo Pennatus, no decentia sui, dicto esse se pulchritudo,\n"
159 "pupa Sive res indifferenter. Captivo pa." )
161 .arg( description = "Bar didelfrump di desgorb. Nu widsoflar brimeldrgf." )
163 .arg( name = "checkup",
164 type_name = "number",
165 description = "Florgel, dargel and durgel",
167 default_doc = "(double) 2.1" );
169 senf::console::OverloadedCommandNode & cbNode (
172 "Uus Primordia fundo falsidicus corium, diurnitas humo pro leto. Sui Ueraciter\n"
173 "hio eruca lenis qua Agalmate ut fors penitentia. Iugum obdormio anxio nuncupo\n"
174 "iam, in vos nam Custodi." )
175 .arg( "text", default_value = "" ) );
179 BOOST_CHECK_NO_THROW(
180 parser.parse("test/cb 111 222.4",
181 boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )) );
182 BOOST_CHECK_NO_THROW(
183 parser.parse("test/cb 222",
184 boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )) );
185 BOOST_CHECK_NO_THROW(
186 parser.parse("test/cb foo",
187 boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )) );
188 BOOST_CHECK_NO_THROW(
189 parser.parse("test/cb",
190 boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )) );
192 BOOST_CHECK_EQUAL( ss.str(), "333\n" "224\n" "Value: foo\n" "Value: \n" );
196 std::stringstream ss;
197 senf::console::root()["test"]("cb").help(ss);
201 " 1- cb arg11:int [checkup:number]\n"
202 " 2- cb [text:string]\n"
205 " arg11 Bar didelfrump di desgorb. Nu widsoflar brimeldrgf.\n"
206 " checkup Florgel, dargel and durgel\n"
207 " default: (double) 2.1\n"
209 " default: (empty)\n"
211 "Ops fortunate, ops me ut orgia vociferatio contumax per, rudo re loco emitto\n"
212 "intolerabiliter ita iugo. Subcribo gravo. Devenio luna fonticulus Castanea\n"
213 "horum fascino Os interpretor non ipse conjuratio hora, qui filius denuntio ait\n"
214 "sono te odium Anhelo. Dum Cedo audax celox alius una Agnosco hic, ibi retineo\n"
215 "lux sto ioco. Per Re dono. Copiose reus scitus jus diligens sis scapulare\n"
216 "Servitium transi.\n"
219 "Lo nam balnearius Opprimo Pennatus, no decentia sui, dicto esse se pulchritudo,\n"
220 "pupa Sive res indifferenter. Captivo pa.\n"
223 "Uus Primordia fundo falsidicus corium, diurnitas humo pro leto. Sui Ueraciter\n"
224 "hio eruca lenis qua Agalmate ut fors penitentia. Iugum obdormio anxio nuncupo\n"
225 "iam, in vos nam Custodi.\n" );
233 senf::console::ScopedDirectory<Test> dir;
236 Test(std::string const & name) : dir(this), name_ (name) {
237 dir.add("name", &Test::name);
240 std::string name(std::string const & suffix) {
241 return name_ + suffix;
247 BOOST_AUTO_UNIT_TEST(memberParsedCommand)
249 senf::console::Executor executor;
250 senf::console::CommandParser parser;
251 senf::console::ScopedDirectory<> dir;
252 senf::console::root().add("test", dir);
256 dir.add("obj", obj.dir);
258 std::stringstream ss;
259 parser.parse("test/obj/name \": foo\"",
260 boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 ));
261 BOOST_CHECK_EQUAL( ss.str(), "bar: foo\n" );
267 COMPILE_FAIL(argParser)
269 senf::console::ScopedDirectory<> dir;
271 // Fails, since there are only two arguments defined
278 COMPILE_FAIL(defaultDoc)
280 senf::console::ScopedDirectory<> dir;
281 using namespace senf::console::kw;
283 // Fails, since default_value is missing but default_doc is given
285 .arg(default_doc = "doc");
290 ///////////////////////////////cc.e////////////////////////////////////////
297 // comment-column: 40
298 // c-file-style: "senf"
299 // indent-tabs-mode: nil
300 // ispell-local-dictionary: "american"
301 // compile-command: "scons -u test"