Fix documentation build under maverick (doxygen 1.7.1)
[senf.git] / senf / Utils / Console / STLSupport.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2009
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 STLSupport unit tests */
25
26 //#include "STLSupport.test.hh"
27 //#include "STLSupport.test.ih"
28
29 // Custom includes
30 #include <boost/assign/list_of.hpp>
31 #include "Console.hh"
32
33 #include <senf/Utils/auto_unit_test.hh>
34 #include <boost/test/test_tools.hpp>
35
36 #define prefix_
37 //-/////////////////////////////////////////////////////////////////////////////////////////////////
38
39 namespace {
40
41     template <class Container>
42     struct Summer
43     {
44         static int test(Container const & data)
45         {
46             int sum (0);
47             for (typename Container::const_iterator i (data.begin()), i_end (data.end());
48                  i != i_end; ++i)
49                 sum += *i;
50             return sum;
51         }
52     };
53
54     std::pair<std::string, int> mapTest(std::map<std::string,int> const & data)
55     {
56         std::string keys;
57         int sum (0);
58         for (std::map<std::string,int>::const_iterator i (data.begin()), i_end (data.end());
59              i != i_end; ++i) {
60             keys += i->first;
61             sum += i->second;
62         }
63         return std::make_pair(keys,sum);
64     }
65
66 }
67
68 SENF_AUTO_UNIT_TEST(vectorSupport)
69 {
70     namespace fty = senf::console::factory;
71
72     senf::console::Executor executor;
73     senf::console::CommandParser parser;
74     senf::console::ScopedDirectory<> dir;
75     senf::console::root().add("test", dir);
76
77     std::vector<int> defv (boost::assign::list_of(7)(2).to_container(defv));
78     dir.add("test", fty::Command(&Summer<std::vector<int> >::test)
79         .arg("data", "test data", senf::console::kw::default_value = defv)
80         );
81     std::stringstream ss;
82
83     SENF_CHECK_NO_THROW(
84         parser.parse("test/test; test/test (); test/test 5; test/test (13); test/test (4 5 8)",
85                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )) );
86     BOOST_CHECK_EQUAL( ss.str(), "9\n" "0\n" "5\n" "13\n" "17\n" );
87
88     ss.str("");
89     SENF_CHECK_NO_THROW(
90         parser.parse("help test/test",
91                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )) );
92     BOOST_CHECK_EQUAL(
93         ss.str(),
94         "Usage:\n"
95         "    test [data:vector<int>]\n"
96         "\n"
97         "With:\n"
98         "    data      test data\n"
99         "        default: (7 2)\n" );
100 }
101
102 SENF_AUTO_UNIT_TEST(listSupport)
103 {
104     namespace fty = senf::console::factory;
105
106     senf::console::Executor executor;
107     senf::console::CommandParser parser;
108     senf::console::ScopedDirectory<> dir;
109     senf::console::root().add("test", dir);
110
111     std::list<int> defv (boost::assign::list_of(7)(2).to_container(defv));
112     dir.add("test", fty::Command(&Summer<std::list<int> >::test)
113         .arg("data", "test data", senf::console::kw::default_value = defv)
114         );
115     std::stringstream ss;
116
117     SENF_CHECK_NO_THROW(
118         parser.parse("test/test; test/test (); test/test 5; test/test (13); test/test (4 5 8)",
119                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )) );
120     BOOST_CHECK_EQUAL( ss.str(), "9\n" "0\n" "5\n" "13\n" "17\n" );
121
122     ss.str("");
123     SENF_CHECK_NO_THROW(
124         parser.parse("help test/test",
125                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )) );
126     BOOST_CHECK_EQUAL(
127         ss.str(),
128         "Usage:\n"
129         "    test [data:list<int>]\n"
130         "\n"
131         "With:\n"
132         "    data      test data\n"
133         "        default: (7 2)\n" );
134 }
135
136 SENF_AUTO_UNIT_TEST(setSupport)
137 {
138     namespace fty = senf::console::factory;
139
140     senf::console::Executor executor;
141     senf::console::CommandParser parser;
142     senf::console::ScopedDirectory<> dir;
143     senf::console::root().add("test", dir);
144
145     std::set<int> defv (boost::assign::list_of(7)(2).to_container(defv));
146     dir.add("test", fty::Command(&Summer<std::set<int> >::test)
147         .arg("data", "test data", senf::console::kw::default_value = defv)
148         );
149     std::stringstream ss;
150
151     SENF_CHECK_NO_THROW(
152         parser.parse("test/test; test/test (); test/test 5; test/test (13); test/test (4 5 8)",
153                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )) );
154     BOOST_CHECK_EQUAL( ss.str(), "9\n" "0\n" "5\n" "13\n" "17\n" );
155
156     ss.str("");
157     SENF_CHECK_NO_THROW(
158         parser.parse("help test/test",
159                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )) );
160     BOOST_CHECK_EQUAL(
161         ss.str(),
162         "Usage:\n"
163         "    test [data:set<int>]\n"
164         "\n"
165         "With:\n"
166         "    data      test data\n"
167         "        default: (2 7)\n" );
168 }
169
170 SENF_AUTO_UNIT_TEST(mapSupport)
171 {
172     namespace fty = senf::console::factory;
173
174     senf::console::Executor executor;
175     senf::console::CommandParser parser;
176     senf::console::ScopedDirectory<> dir;
177     senf::console::root().add("test", dir);
178
179     std::map<std::string, int> defv (
180         boost::assign::map_list_of("foo bar",7)("bar",2).to_container(defv));
181     dir.add("test", fty::Command(&mapTest)
182         .arg("data", "test data", senf::console::kw::default_value = defv)
183         );
184     std::stringstream ss;
185
186     SENF_CHECK_NO_THROW(
187         parser.parse("test/test; test/test (); "
188                      "test/test (vier=4 fuenf = 5 acht=8 )",
189                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )) );
190     BOOST_CHECK_EQUAL( ss.str(), "(\"barfoo bar\" 9)\n" "(\"\" 0)\n" "(achtfuenfvier 17)\n" ); //
191
192     ss.str("");
193     SENF_CHECK_NO_THROW(
194         parser.parse("help test/test",
195                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )) );
196     BOOST_CHECK_EQUAL(
197         ss.str(),
198         "Usage:\n"
199         "    test [data:map<string,int>]\n"
200         "\n"
201         "With:\n"
202         "    data      test data\n"
203         "        default: (bar=2 \"foo bar\"=7)\n" );
204 }
205
206 //-/////////////////////////////////////////////////////////////////////////////////////////////////
207 #undef prefix_
208
209 \f
210 // Local Variables:
211 // mode: c++
212 // fill-column: 100
213 // comment-column: 40
214 // c-file-style: "senf"
215 // indent-tabs-mode: nil
216 // ispell-local-dictionary: "american"
217 // compile-command: "scons -u test"
218 // End: