cc786dc43c294dbdf770283a6554ac97490e9afd
[senf.git] / Scheduler / Console / Node.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 Node.test unit tests */
25
26 //#include "Node.test.hh"
27 //#include "Node.test.ih"
28
29 // Custom includes
30 #include <sstream>
31 #include "Node.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 BOOST_AUTO_UNIT_TEST(genericNode)
41 {
42     senf::console::GenericNode & node (
43         senf::console::root().mkdir("dir1").mkdir("dir2").doc("help info"));
44     senf::console::GenericNode::weak_ptr wp (node.thisptr());
45
46     BOOST_CHECK_EQUAL( node.name(), "dir2" );
47     BOOST_CHECK( node.parent() );
48     BOOST_CHECK_EQUAL( node.path(), "/dir1/dir2" );
49     BOOST_CHECK( node.active() );
50     std::stringstream ss;
51     node.help(ss);
52     BOOST_CHECK_EQUAL( ss.str(), "help info\n" );
53     
54     {
55         senf::console::GenericNode::ptr p (senf::console::root()["dir1"].unlink());
56         BOOST_CHECK( ! node.active() );
57         BOOST_CHECK( ! wp.expired() );
58     }
59     BOOST_CHECK( wp.expired() );
60 }
61
62 namespace {
63     void callback(std::ostream & os, senf::console::ParseCommandInfo const & command)
64     {
65         os << "callback";
66     }
67
68     template <class T>
69     struct select1st {
70         typedef T result_type;
71         template <class U> result_type operator()(U const & u) const { return u.first; }
72     };
73 }
74
75 BOOST_AUTO_UNIT_TEST(directoryNode)
76 {
77     senf::console::DirectoryNode::ptr p (senf::console::DirectoryNode::create());
78
79     BOOST_CHECK( & senf::console::root().add("dir1", p) == p.get() );
80
81     senf::console::SimpleCommandNode & fnnode (
82         senf::console::root().add( "fn", senf::console::SimpleCommandNode::create(&callback) ));
83     BOOST_CHECK( &senf::console::root()["dir1"] == p.get() );
84     BOOST_CHECK_THROW( senf::console::root()["dir2"], senf::console::UnknownNodeNameException );
85     BOOST_CHECK_THROW( senf::console::root()("dir1"), std::bad_cast );
86     BOOST_CHECK( &senf::console::root()("fn") == &fnnode );
87     BOOST_CHECK_THROW( senf::console::root()("fn2"), senf::console::UnknownNodeNameException );
88     BOOST_CHECK_THROW( senf::console::root()["fn"], std::bad_cast );
89     BOOST_CHECK( &senf::console::root().get("dir1") == p.get() );
90     
91     senf::console::root().mkdir("dir2").mkdir("dir3");
92     char const * const children[] = { "dir1", "dir2", "fn", "sys" };
93     BOOST_CHECK_EQUAL_COLLECTIONS( 
94         boost::make_transform_iterator(senf::console::root().children().begin(), 
95                                        select1st<std::string const &>()),
96         boost::make_transform_iterator(senf::console::root().children().end(),
97                                        select1st<std::string const &>()),
98         children, 
99         children+sizeof(children)/sizeof(children[0]) );
100
101     char const * const completions[] = { "dir1", "dir2" };
102     BOOST_CHECK_EQUAL_COLLECTIONS(
103         boost::make_transform_iterator(senf::console::root().completions("dir").begin(), 
104                                        select1st<std::string const &>()),
105         boost::make_transform_iterator(senf::console::root().completions("dir").end(),
106                                        select1st<std::string const &>()),
107         completions, 
108         completions+sizeof(completions)/sizeof(completions[0]) );
109
110     p->doc("test doc");
111     std::stringstream ss;
112     p->help(ss);
113     BOOST_CHECK_EQUAL( ss.str(), "test doc\n" );
114
115     BOOST_CHECK( senf::console::root().remove("dir1") == p );
116     senf::console::root().remove("dir2");
117     senf::console::root().remove("fn");
118
119     BOOST_CHECK_EQUAL( senf::console::root().children().size(), 1u );
120 }
121
122 BOOST_AUTO_UNIT_TEST(linkNode)
123 {
124     senf::console::root().mkdir("dir1");
125     senf::console::root().link("link1", senf::console::root()["dir1"]);
126
127     BOOST_CHECK( senf::console::root()["dir1"] == senf::console::root()["link1"] );
128
129     senf::console::root().remove("dir1");
130     senf::console::root().remove("link1");
131 }
132
133 namespace {
134     struct Functor {
135         void operator()(std::ostream & os, senf::console::ParseCommandInfo const &) {
136             os << "functor";
137         }
138     };
139 }
140
141 BOOST_AUTO_UNIT_TEST(senfConsoleAddNode)
142 {
143     senf::console::root().add("fn1", &callback);
144     senf::console::root().add("fn2", Functor());
145     
146     senf::console::ParseCommandInfo info;
147
148     {
149         std::stringstream ss;
150         senf::console::root()("fn1")(ss, info);
151         BOOST_CHECK_EQUAL( ss.str(), "callback" );
152     }
153
154     {
155         std::stringstream ss;
156         senf::console::root()("fn2")(ss, info);
157         BOOST_CHECK_EQUAL( ss.str(), "functor" );
158     }
159     
160     senf::console::root().remove("fn1");
161     senf::console::root().remove("fn2");
162 }
163
164 BOOST_AUTO_UNIT_TEST(simpleCommandNode)
165 {
166     senf::console::root().add("fn", senf::console::SimpleCommandNode::create(&callback))
167         .doc("help text");
168     {
169         std::stringstream ss;
170         senf::console::ParseCommandInfo info;
171         senf::console::root()("fn")(ss, info);
172         BOOST_CHECK_EQUAL( ss.str(), "callback" );
173     }
174     
175     {
176         std::stringstream ss;
177         senf::console::root().get("fn").help(ss);
178         BOOST_CHECK_EQUAL( ss.str(), "help text\n" );
179     }
180
181     senf::console::root().remove("fn");
182 }
183
184 ///////////////////////////////cc.e////////////////////////////////////////
185 #undef prefix_
186
187 \f
188 // Local Variables:
189 // mode: c++
190 // fill-column: 100
191 // comment-column: 40
192 // c-file-style: "senf"
193 // indent-tabs-mode: nil
194 // ispell-local-dictionary: "american"
195 // compile-command: "scons -u test"
196 // End: