f675893b0167786f8cd0327f6e59b9fbe322a4ac
[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 "ScopedDirectory.hh"
32 #include <boost/iterator/transform_iterator.hpp>
33
34 #include "../../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", &Self::member);
47         }
48
49         void member(std::ostream & os, senf::console::ParseCommandInfo const &) {
50             os << "member";
51         }
52     };
53 }
54
55 BOOST_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 BOOST_AUTO_UNIT_TEST(scopedDirectoryVoid)
75 {
76     {
77         senf::console::ScopedDirectory<> dir;
78         senf::console::root().add("dir", dir);
79         dir.add("cb", &callback);
80         std::stringstream ss;
81         senf::console::ParseCommandInfo info;
82         senf::console::root()["dir"]("cb")(ss, info);
83         BOOST_CHECK_EQUAL( ss.str(), "cb" );
84     }
85     BOOST_CHECK_THROW( senf::console::root()["dir"],
86                        senf::console::UnknownNodeNameException );
87 }
88
89 namespace {
90     template <class T>
91     struct select1st {
92         typedef T result_type;
93         template <class U> result_type operator()(U const & u) const { return u.first; }
94     };
95 }
96
97 BOOST_AUTO_UNIT_TEST(scopedDirectoryBase)
98 {
99     {
100         senf::console::ScopedDirectory<> dir;
101         senf::console::root().add("dir", dir);
102         dir.mkdir("foo");
103         dir.add("cb", &callback);
104         BOOST_CHECK( &dir["foo"] == &dir.get("foo") );
105         BOOST_CHECK( &dir("cb") == &dir.get("cb") );
106         
107         char const * const children[] = { "cb", "foo" };
108         BOOST_CHECK_EQUAL_COLLECTIONS( 
109             boost::make_transform_iterator(dir.children().begin(),
110                                            select1st<std::string const &>()),
111             boost::make_transform_iterator(dir.children().end(),
112                                            select1st<std::string const &>()),
113             children, 
114             children+sizeof(children)/sizeof(children[0]) );
115         
116         dir.doc("dir");
117         std::stringstream ss;
118         dir.node().help(ss);
119         BOOST_CHECK_EQUAL( ss.str(), "dir\n" );
120     }
121 }
122
123 ///////////////////////////////cc.e////////////////////////////////////////
124 #undef prefix_
125
126 \f
127 // Local Variables:
128 // mode: c++
129 // fill-column: 100
130 // comment-column: 40
131 // c-file-style: "senf"
132 // indent-tabs-mode: nil
133 // ispell-local-dictionary: "american"
134 // compile-command: "scons -u test"
135 // End: