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