switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Utils / Console / STLSupport.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2009
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief STLSupport unit tests */
30
31 //#include "STLSupport.test.hh"
32 //#include "STLSupport.test.ih"
33
34 // Custom includes
35 #include <boost/assign/list_of.hpp>
36 #include "Console.hh"
37
38 #include <senf/Utils/auto_unit_test.hh>
39 #include <boost/test/test_tools.hpp>
40
41 #define prefix_
42 //-/////////////////////////////////////////////////////////////////////////////////////////////////
43
44 namespace {
45
46     template <class Container>
47     struct Summer
48     {
49         static int test(Container const & data)
50         {
51             int sum (0);
52             for (typename Container::const_iterator i (data.begin()), i_end (data.end());
53                  i != i_end; ++i)
54                 sum += *i;
55             return sum;
56         }
57     };
58
59     std::pair<std::string, int> mapTest(std::map<std::string,int> const & data)
60     {
61         std::string keys;
62         int sum (0);
63         for (std::map<std::string,int>::const_iterator i (data.begin()), i_end (data.end());
64              i != i_end; ++i) {
65             keys += i->first;
66             sum += i->second;
67         }
68         return std::make_pair(keys,sum);
69     }
70
71 }
72
73 SENF_AUTO_UNIT_TEST(vectorSupport)
74 {
75     namespace fty = senf::console::factory;
76
77     senf::console::Executor executor;
78     senf::console::CommandParser parser;
79     senf::console::ScopedDirectory<> dir;
80     senf::console::root().add("test", dir);
81
82     std::vector<int> defv (boost::assign::list_of(7)(2).to_container(defv));
83     dir.add("test", fty::Command(&Summer<std::vector<int> >::test)
84         .arg("data", "test data", senf::console::kw::default_value = defv)
85         );
86     std::stringstream ss;
87
88     SENF_CHECK_NO_THROW(
89         parser.parse("test/test; test/test (); test/test 5; test/test (13); test/test (4 5 8)",
90                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )) );
91     BOOST_CHECK_EQUAL( ss.str(), "9\n" "0\n" "5\n" "13\n" "17\n" );
92
93     ss.str("");
94     SENF_CHECK_NO_THROW(
95         parser.parse("help test/test",
96                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )) );
97     BOOST_CHECK_EQUAL(
98         ss.str(),
99         "Usage:\n"
100         "    test [data:vector<int>]\n"
101         "\n"
102         "With:\n"
103         "    data      test data\n"
104         "        default: (7 2)\n" );
105 }
106
107 SENF_AUTO_UNIT_TEST(listSupport)
108 {
109     namespace fty = senf::console::factory;
110
111     senf::console::Executor executor;
112     senf::console::CommandParser parser;
113     senf::console::ScopedDirectory<> dir;
114     senf::console::root().add("test", dir);
115
116     std::list<int> defv (boost::assign::list_of(7)(2).to_container(defv));
117     dir.add("test", fty::Command(&Summer<std::list<int> >::test)
118         .arg("data", "test data", senf::console::kw::default_value = defv)
119         );
120     std::stringstream ss;
121
122     SENF_CHECK_NO_THROW(
123         parser.parse("test/test; test/test (); test/test 5; test/test (13); test/test (4 5 8)",
124                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )) );
125     BOOST_CHECK_EQUAL( ss.str(), "9\n" "0\n" "5\n" "13\n" "17\n" );
126
127     ss.str("");
128     SENF_CHECK_NO_THROW(
129         parser.parse("help test/test",
130                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )) );
131     BOOST_CHECK_EQUAL(
132         ss.str(),
133         "Usage:\n"
134         "    test [data:list<int>]\n"
135         "\n"
136         "With:\n"
137         "    data      test data\n"
138         "        default: (7 2)\n" );
139 }
140
141 SENF_AUTO_UNIT_TEST(setSupport)
142 {
143     namespace fty = senf::console::factory;
144
145     senf::console::Executor executor;
146     senf::console::CommandParser parser;
147     senf::console::ScopedDirectory<> dir;
148     senf::console::root().add("test", dir);
149
150     std::set<int> defv (boost::assign::list_of(7)(2).to_container(defv));
151     dir.add("test", fty::Command(&Summer<std::set<int> >::test)
152         .arg("data", "test data", senf::console::kw::default_value = defv)
153         );
154     std::stringstream ss;
155
156     SENF_CHECK_NO_THROW(
157         parser.parse("test/test; test/test (); test/test 5; test/test (13); test/test (4 5 8)",
158                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )) );
159     BOOST_CHECK_EQUAL( ss.str(), "9\n" "0\n" "5\n" "13\n" "17\n" );
160
161     ss.str("");
162     SENF_CHECK_NO_THROW(
163         parser.parse("help test/test",
164                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )) );
165     BOOST_CHECK_EQUAL(
166         ss.str(),
167         "Usage:\n"
168         "    test [data:set<int>]\n"
169         "\n"
170         "With:\n"
171         "    data      test data\n"
172         "        default: (2 7)\n" );
173 }
174
175 SENF_AUTO_UNIT_TEST(mapSupport)
176 {
177     namespace fty = senf::console::factory;
178
179     senf::console::Executor executor;
180     senf::console::CommandParser parser;
181     senf::console::ScopedDirectory<> dir;
182     senf::console::root().add("test", dir);
183
184     std::map<std::string, int> defv (
185         boost::assign::map_list_of("foo bar",7)("bar",2).to_container(defv));
186     dir.add("test", fty::Command(&mapTest)
187         .arg("data", "test data", senf::console::kw::default_value = defv)
188         );
189     std::stringstream ss;
190
191     SENF_CHECK_NO_THROW(
192         parser.parse("test/test; test/test (); "
193                      "test/test (vier=4 fuenf = 5 acht=8 )",
194                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )) );
195     BOOST_CHECK_EQUAL( ss.str(), "(\"barfoo bar\" 9)\n" "(\"\" 0)\n" "(achtfuenfvier 17)\n" ); //
196
197     ss.str("");
198     SENF_CHECK_NO_THROW(
199         parser.parse("help test/test",
200                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )) );
201     BOOST_CHECK_EQUAL(
202         ss.str(),
203         "Usage:\n"
204         "    test [data:map<string,int>]\n"
205         "\n"
206         "With:\n"
207         "    data      test data\n"
208         "        default: (bar=2 \"foo bar\"=7)\n" );
209 }
210
211 //-/////////////////////////////////////////////////////////////////////////////////////////////////
212 #undef prefix_
213
214 \f
215 // Local Variables:
216 // mode: c++
217 // fill-column: 100
218 // comment-column: 40
219 // c-file-style: "senf"
220 // indent-tabs-mode: nil
221 // ispell-local-dictionary: "american"
222 // compile-command: "scons -u test"
223 // End: