508753049ef2c38150330f0ca8f5a3225f2bdabd
[senf.git] / senf / Utils / Console / ScopedDirectory.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 ScopedDirectory unit tests */
25
26 //#include "ScopedDirectory.test.hh"
27 //#include "ScopedDirectory.test.ih"
28
29 // Custom includes
30 #include <sstream>
31 #include "Console.hh"
32 #include <boost/iterator/transform_iterator.hpp>
33
34 #include <senf/Utils/auto_unit_test.hh>
35 #include <boost/test/test_tools.hpp>
36
37 #define prefix_
38 ///////////////////////////////cc.p////////////////////////////////////////
39
40 namespace {
41     struct TestObject {
42         typedef TestObject Self;
43
44         senf::console::ScopedDirectory<Self> dir;
45         TestObject() : dir(this) {
46             dir.add("member", senf::console::factory::Command(&Self::member, this));
47         }
48
49         void member(std::ostream & os, senf::console::ParseCommandInfo const &) {
50             os << "member";
51         }
52     };
53 }
54
55 SENF_AUTO_UNIT_TEST(scopedDirectory)
56 {
57     {
58         TestObject ob;
59         senf::console::root().add("ob",ob.dir);
60         std::stringstream ss;
61         senf::console::ParseCommandInfo info;
62         senf::console::root()["ob"]("member")(ss, info);
63         BOOST_CHECK_EQUAL( ss.str(), "member" );
64     }
65     BOOST_CHECK_THROW( senf::console::root()["ob"], senf::console::UnknownNodeNameException );
66 }
67
68 namespace {
69     void callback(std::ostream & os, senf::console::ParseCommandInfo const &) {
70         os << "cb";
71     }
72 }
73
74 SENF_AUTO_UNIT_TEST(scopedDirectoryVoid)
75 {
76     namespace fty = senf::console::factory;
77
78     {
79         senf::console::ScopedDirectory<> dir;
80         senf::console::root().add("dir", dir);
81         dir.add("cb", fty::Command(&callback));
82         std::stringstream ss;
83         senf::console::ParseCommandInfo info;
84         senf::console::root()["dir"]("cb")(ss, info);
85         BOOST_CHECK_EQUAL( ss.str(), "cb" );
86     }
87     BOOST_CHECK_THROW( senf::console::root()["dir"],
88                        senf::console::UnknownNodeNameException );
89 }
90
91 namespace {
92     template <class T>
93     struct select1st {
94         typedef T result_type;
95         template <class U> result_type operator()(U const & u) const { return u.first; }
96     };
97 }
98
99 SENF_AUTO_UNIT_TEST(scopedDirectoryBase)
100 {
101     namespace fty = senf::console::factory;
102
103     {
104         senf::console::ScopedDirectory<> dir;
105         senf::console::root().add("dir", dir);
106         dir.add("foo",fty::Directory());
107         dir.add("cb", fty::Command(&callback));
108         BOOST_CHECK( &dir["foo"] == &dir.get("foo") );
109         BOOST_CHECK( &dir("cb") == &dir.get("cb") );
110         BOOST_CHECK_EQUAL(dir.name(), "dir");
111
112         char const * const children[] = { "cb", "foo" };
113         BOOST_CHECK_EQUAL_COLLECTIONS(
114             boost::make_transform_iterator(dir.children().begin(),
115                                            select1st<std::string const &>()),
116             boost::make_transform_iterator(dir.children().end(),
117                                            select1st<std::string const &>()),
118             children,
119             children+sizeof(children)/sizeof(children[0]) );
120
121         dir.doc("dir");
122         std::stringstream ss;
123         dir.node().help(ss);
124         BOOST_CHECK_EQUAL( ss.str(), "dir\n" );
125     }
126 }
127
128 ///////////////////////////////cc.e////////////////////////////////////////
129 #undef prefix_
130
131 \f
132 // Local Variables:
133 // mode: c++
134 // fill-column: 100
135 // comment-column: 40
136 // c-file-style: "senf"
137 // indent-tabs-mode: nil
138 // ispell-local-dictionary: "american"
139 // compile-command: "scons -u test"
140 // End: