2b239d600e2010b0f4759f1e489ef7deae16284d
[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 <sstream>
31 #include "ParsedCommand.hh"
32 #include "Executor.hh"
33 #include "Parse.hh"
34 #include "ScopedDirectory.hh"
35
36 #include <boost/test/auto_unit_test.hpp>
37 #include <boost/test/test_tools.hpp>
38
39 #define prefix_
40 ///////////////////////////////cc.p////////////////////////////////////////
41
42 namespace {
43
44     int cb1(int a, double b) { return int(a+b); }
45     double cb2() { return 1.2; }
46     void cb3(int) {}
47     std::string cb4(std::ostream & os) { os << "text\n"; return "value"; }
48     void cb5(std::ostream & os, int v) { os << "Value: " << v << "\n"; }
49 }
50
51 BOOST_AUTO_UNIT_TEST(parsedCommand)
52 {
53     senf::console::Executor executor;
54     senf::console::CommandParser parser;
55     senf::console::ScopedDirectory<> dir;
56     senf::console::root().add("test", dir);
57
58     {
59         std::stringstream ss;
60         dir.add("cb1", &cb1);
61         parser.parse("test/cb1 2 3.2", 
62                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 ));
63         BOOST_CHECK_EQUAL( ss.str(), "5\n" );
64     }
65
66     {
67         std::stringstream ss;
68         dir.add("cb2", &cb2);
69         parser.parse("test/cb2",
70                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 ));
71         BOOST_CHECK_EQUAL( ss.str(), "1.2\n" );
72     }
73
74     {
75         std::stringstream ss;
76         dir.add("cb3", &cb3);
77         parser.parse("test/cb3 234",
78                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 ));
79         BOOST_CHECK_EQUAL( ss.str(), "" );
80     }
81
82     {
83         std::stringstream ss;
84         dir.add("cb4", &cb4);
85         parser.parse("test/cb4",
86                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 ));
87         BOOST_CHECK_EQUAL( ss.str(), "text\n" "value\n" );
88     }
89
90     {
91         std::stringstream ss;
92         dir.add("cb5", &cb5);
93         parser.parse("test/cb5 1234",
94                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 ));
95         BOOST_CHECK_EQUAL( ss.str(), "Value: 1234\n" );
96     }
97
98     {
99         std::stringstream ss;
100
101         BOOST_CHECK_THROW( 
102             parser.parse("test/cb1 2 3.2 foo", 
103                          boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )),
104             senf::console::SyntaxErrorException );
105
106         BOOST_CHECK_THROW(
107             parser.parse("test/cb1 2", 
108                          boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )),
109             senf::console::SyntaxErrorException );
110
111         BOOST_CHECK_THROW(
112             parser.parse("test/cb1 2 foo", 
113                          boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )),
114             senf::console::SyntaxErrorException );
115     }
116
117     {
118         std::stringstream ss;
119
120         dir.add("cb", &cb1);
121         dir.add("cb", &cb5);
122         dir.add("cb", &cb2);
123         parser.parse("test/cb 111 222.4; test/cb 222; test/cb",
124                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 ));
125         BOOST_CHECK_EQUAL( ss.str(), "333\n" "Value: 222\n" "1.2\n" );
126     }
127 }
128
129 namespace {
130
131     struct Test 
132     {
133         senf::console::ScopedDirectory<Test> dir;
134         std::string name_;
135
136         Test(std::string const & name) : dir(this), name_ (name) {
137             dir.add("name", &Test::name);
138         }
139
140         std::string name(std::string const & suffix) {
141             return name_ + suffix;
142         }
143
144     };
145 }
146
147 BOOST_AUTO_UNIT_TEST(memberParsedCommand)
148 {
149     senf::console::Executor executor;
150     senf::console::CommandParser parser;
151     senf::console::ScopedDirectory<> dir;
152     senf::console::root().add("test", dir);
153     
154     {
155         Test obj ("bar");
156         dir.add("obj", obj.dir);
157         
158         std::stringstream ss;
159         parser.parse("test/obj/name \": foo\"",
160                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 ));
161         BOOST_CHECK_EQUAL( ss.str(), "bar: foo\n" );
162     }
163 }
164     
165 ///////////////////////////////cc.e////////////////////////////////////////
166 #undef prefix_
167
168 \f
169 // Local Variables:
170 // mode: c++
171 // fill-column: 100
172 // comment-column: 40
173 // c-file-style: "senf"
174 // indent-tabs-mode: nil
175 // ispell-local-dictionary: "american"
176 // compile-command: "scons -u test"
177 // End: