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