Utils: Add some type traits in type_traits.hh
[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 ///////////////////////////////cc.e////////////////////////////////////////
119 #undef prefix_
120
121 \f
122 // Local Variables:
123 // mode: c++
124 // fill-column: 100
125 // comment-column: 40
126 // c-file-style: "senf"
127 // indent-tabs-mode: nil
128 // ispell-local-dictionary: "american"
129 // compile-command: "scons -u test"
130 // End: