94ab8fabaa9e45b7edefedee94ad5edb9f5cb140
[senf.git] / Console / ParsedCommand.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2008 
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
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.
12 //
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.
17 //
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.
22
23 /** \file
24     \brief ParsedCommand.test unit tests */
25
26 //#include "ParsedCommand.test.hh"
27 //#include "ParsedCommand.test.ih"
28
29 // Custom includes
30 #include <iostream>
31 #include <sstream>
32 #include "ParsedCommand.hh"
33 #include "Executor.hh"
34 #include "Parse.hh"
35 #include "ScopedDirectory.hh"
36
37 #include "../Utils/auto_unit_test.hh"
38 #include <boost/test/test_tools.hpp>
39
40 #define prefix_
41 ///////////////////////////////cc.p////////////////////////////////////////
42
43 namespace {
44
45     int cb1(int a, double b) { return int(a+b); }
46     double cb2(){ return 1.2; }
47     void cb3(int i) { }
48     std::string cb4(std::ostream & os) { os << "text\n"; return "value"; }
49     void cb5(std::ostream & os, std::string v) { os << "Value: " << v << "\n"; }
50 }
51
52 BOOST_AUTO_UNIT_TEST(parsedCommand)
53 {
54     senf::console::Executor executor;
55     senf::console::CommandParser parser;
56     senf::console::ScopedDirectory<> dir;
57     senf::console::root().add("test", dir);
58
59     {
60         std::stringstream ss;
61         dir.add("cb1", &cb1);
62         parser.parse("test/cb1 2 3.2", 
63                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 ));
64         BOOST_CHECK_EQUAL( ss.str(), "5\n" );
65     }
66
67     {
68         std::stringstream ss;
69         dir.add("cb2", &cb2);
70         parser.parse("test/cb2",
71                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 ));
72         BOOST_CHECK_EQUAL( ss.str(), "1.2\n" );
73     }
74
75     {
76         std::stringstream ss;
77         dir.add("cb3", &cb3);
78         parser.parse("test/cb3 234",
79                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 ));
80         BOOST_CHECK_EQUAL( ss.str(), "" );
81     }
82
83     {
84         std::stringstream ss;
85         dir.add("cb4", &cb4);
86         parser.parse("test/cb4",
87                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 ));
88         BOOST_CHECK_EQUAL( ss.str(), "text\n" "value\n" );
89     }
90
91     {
92         std::stringstream ss;
93         dir.add("cb5", &cb5);
94         parser.parse("test/cb5 1234",
95                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 ));
96         BOOST_CHECK_EQUAL( ss.str(), "Value: 1234\n" );
97     }
98
99     {
100         std::stringstream ss;
101
102         BOOST_CHECK_THROW( 
103             parser.parse("test/cb1 2 3.2 foo", 
104                          boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )),
105             senf::console::SyntaxErrorException );
106
107         BOOST_CHECK_THROW(
108             parser.parse("test/cb1 2", 
109                          boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )),
110             senf::console::SyntaxErrorException );
111
112         BOOST_CHECK_THROW(
113             parser.parse("test/cb1 2 foo", 
114                          boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )),
115             senf::console::SyntaxErrorException );
116     }
117
118     {
119         std::stringstream ss;
120
121         using namespace senf::console::tag;
122
123         dir.add("cb", &cb1)
124             .doc(
125                 "Ops fortunate, ops me ut orgia vociferatio contumax per, rudo re loco emitto\n"
126                 "intolerabiliter ita iugo. Subcribo gravo. Devenio luna fonticulus Castanea\n"
127                 "horum fascino Os interpretor non ipse conjuratio hora, qui filius denuntio ait\n"
128                 "sono te odium Anhelo. Dum Cedo audax celox alius una Agnosco hic, ibi retineo\n"
129                 "lux sto ioco. Per Re dono. Copiose reus scitus jus diligens sis scapulare\n"
130                 "Servitium transi." )
131
132             .overloadDoc(
133                 "Lo nam balnearius Opprimo Pennatus, no decentia sui, dicto esse se pulchritudo,\n"
134                 "pupa Sive res indifferenter. Captivo pa." )
135
136             .arg( description_   = "Bar didelfrump di desgorb. Nu widsoflar brimeldrgf." )
137
138             .arg( name_          = "checkup", 
139                   description_   = "Florgel, dargel and durgel",
140                   default_value_ = 2.1 );
141
142         senf::console::OverloadedCommandNode & cbNode (
143             dir.add("cb", &cb5)
144                 .overloadDoc(
145                     "Uus Primordia fundo falsidicus corium, diurnitas humo pro leto. Sui Ueraciter\n"
146                     "hio eruca lenis qua Agalmate ut fors penitentia. Iugum obdormio anxio nuncupo\n"
147                     "iam, in vos nam Custodi." ) );
148
149         (void) cbNode;
150
151         dir.add("cb", &cb2);
152
153         parser.parse("test/cb 111 222.4; test/cb 222; test/cb foo; test/cb",
154                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 ));
155         BOOST_CHECK_EQUAL( ss.str(), "333\n" "224\n" "Value: foo\n" "1.2\n" );
156     }
157
158     {
159         std::stringstream ss;
160         senf::console::root()["test"]("cb").help(ss);
161         BOOST_CHECK_EQUAL( 
162             ss.str(), 
163             "Usage:\n"
164             "    1- cb arg11:int [checkup:double]\n"
165             "    2- cb arg21:string\n"
166             "    3- cb\n"
167             "\n"
168             "With:\n"
169             "    arg11     Bar didelfrump di desgorb. Nu widsoflar brimeldrgf.\n"
170             "    checkup   Florgel, dargel and durgel\n"
171             "        default: 2.1\n"
172             "\n"
173             "Ops fortunate, ops me ut orgia vociferatio contumax per, rudo re loco emitto\n"
174             "intolerabiliter ita iugo. Subcribo gravo. Devenio luna fonticulus Castanea\n"
175             "horum fascino Os interpretor non ipse conjuratio hora, qui filius denuntio ait\n"
176             "sono te odium Anhelo. Dum Cedo audax celox alius una Agnosco hic, ibi retineo\n"
177             "lux sto ioco. Per Re dono. Copiose reus scitus jus diligens sis scapulare\n"
178             "Servitium transi.\n"
179             "\n"
180             "Variant 1:\n"
181             "Lo nam balnearius Opprimo Pennatus, no decentia sui, dicto esse se pulchritudo,\n"
182             "pupa Sive res indifferenter. Captivo pa.\n"
183             "\n"
184             "Variant 2:\n"
185             "Uus Primordia fundo falsidicus corium, diurnitas humo pro leto. Sui Ueraciter\n"
186             "hio eruca lenis qua Agalmate ut fors penitentia. Iugum obdormio anxio nuncupo\n"
187             "iam, in vos nam Custodi.\n" );
188     }
189 }
190
191 namespace {
192
193     struct Test 
194     {
195         senf::console::ScopedDirectory<Test> dir;
196         std::string name_;
197
198         Test(std::string const & name) : dir(this), name_ (name) {
199             dir.add("name", &Test::name);
200         }
201
202         std::string name(std::string const & suffix) {
203             return name_ + suffix;
204         }
205     };
206
207 }
208
209 BOOST_AUTO_UNIT_TEST(memberParsedCommand)
210 {
211     senf::console::Executor executor;
212     senf::console::CommandParser parser;
213     senf::console::ScopedDirectory<> dir;
214     senf::console::root().add("test", dir);
215     
216     {
217         Test obj ("bar");
218         dir.add("obj", obj.dir);
219         
220         std::stringstream ss;
221         parser.parse("test/obj/name \": foo\"",
222                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 ));
223         BOOST_CHECK_EQUAL( ss.str(), "bar: foo\n" );
224     }
225 }
226     
227 ///////////////////////////////cc.e////////////////////////////////////////
228 #undef prefix_
229
230 \f
231 // Local Variables:
232 // mode: c++
233 // fill-column: 100
234 // comment-column: 40
235 // c-file-style: "senf"
236 // indent-tabs-mode: nil
237 // ispell-local-dictionary: "american"
238 // compile-command: "scons -u test"
239 // End: