Utils/Console: Fix DirectoryNode::add(...) API
[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(senf::membind(&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.mkdir("foo");
107         dir.add("cb", fty::Command(&callback));
108         BOOST_CHECK( &dir["foo"] == &dir.get("foo") );
109         BOOST_CHECK( &dir("cb") == &dir.get("cb") );
110         
111         char const * const children[] = { "cb", "foo" };
112         BOOST_CHECK_EQUAL_COLLECTIONS( 
113             boost::make_transform_iterator(dir.children().begin(),
114                                            select1st<std::string const &>()),
115             boost::make_transform_iterator(dir.children().end(),
116                                            select1st<std::string const &>()),
117             children, 
118             children+sizeof(children)/sizeof(children[0]) );
119         
120         dir.doc("dir");
121         std::stringstream ss;
122         dir.node().help(ss);
123         BOOST_CHECK_EQUAL( ss.str(), "dir\n" );
124     }
125 }
126
127 ///////////////////////////////cc.e////////////////////////////////////////
128 #undef prefix_
129
130 \f
131 // Local Variables:
132 // mode: c++
133 // fill-column: 100
134 // comment-column: 40
135 // c-file-style: "senf"
136 // indent-tabs-mode: nil
137 // ispell-local-dictionary: "american"
138 // compile-command: "scons -u test"
139 // End: