4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
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.
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.
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.
24 \brief Node.test unit tests */
26 //#include "Node.test.hh"
27 //#include "Node.test.ih"
32 #include <boost/iterator/transform_iterator.hpp>
34 #include "../Utils/auto_unit_test.hh"
35 #include <boost/test/test_tools.hpp>
38 ///////////////////////////////cc.p////////////////////////////////////////
40 BOOST_AUTO_UNIT_TEST(gnericNode)
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());
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() );
52 BOOST_CHECK_EQUAL( ss.str(), "help info\n" );
55 senf::console::GenericNode::ptr p (senf::console::root()["dir1"].unlink());
56 BOOST_CHECK( ! node.active() );
57 BOOST_CHECK( ! wp.expired() );
59 BOOST_CHECK( wp.expired() );
63 void callback(std::ostream & os, senf::console::ParseCommandInfo const & command)
70 typedef T result_type;
71 template <class U> result_type operator()(U const & u) const { return u.first; }
75 BOOST_AUTO_UNIT_TEST(directoryNode)
77 senf::console::DirectoryNode::ptr p (senf::console::DirectoryNode::create());
79 BOOST_CHECK( & senf::console::root().add("dir1", p) == p.get() );
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() );
91 senf::console::root().mkdir("dir2").mkdir("dir3");
92 char const * const children[] = { "dir1", "dir2", "fn" };
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 &>()),
99 children+sizeof(children)/sizeof(children[0]) );
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 &>()),
108 completions+sizeof(completions)/sizeof(completions[0]) );
110 char const * const path[] = { "..", "dir2", "dir3" };
111 BOOST_CHECK( &senf::console::root()["dir1"].traverse( boost::make_iterator_range(
113 path+sizeof(path)/sizeof(path[0])) )
114 == &senf::console::root()["dir2"]["dir3"] );
116 char const * const incompletePath[] = { "d" };
117 BOOST_CHECK( &senf::console::root()["dir2"].traverse( boost::make_iterator_range(
119 incompletePath+sizeof(incompletePath)/sizeof(incompletePath[0])),
121 == &senf::console::root()["dir2"]["dir3"] );
124 std::stringstream ss;
126 BOOST_CHECK_EQUAL( ss.str(), "test doc\n" );
128 BOOST_CHECK( senf::console::root().remove("dir1") == p );
129 senf::console::root().remove("dir2");
130 senf::console::root().remove("fn");
132 BOOST_CHECK_EQUAL( senf::console::root().children().size(), 0u );
137 void operator()(std::ostream & os, senf::console::ParseCommandInfo const &) {
143 BOOST_AUTO_UNIT_TEST(senfConsoleAddNode)
145 senf::console::root().add("fn1", &callback);
146 senf::console::root().add("fn2", Functor());
148 senf::console::ParseCommandInfo info;
151 std::stringstream ss;
152 senf::console::root()("fn1")(ss, info);
153 BOOST_CHECK_EQUAL( ss.str(), "callback" );
157 std::stringstream ss;
158 senf::console::root()("fn2")(ss, info);
159 BOOST_CHECK_EQUAL( ss.str(), "functor" );
162 senf::console::root().remove("fn1");
163 senf::console::root().remove("fn2");
166 BOOST_AUTO_UNIT_TEST(simpleCommandNode)
168 senf::console::root().add("fn", senf::console::SimpleCommandNode::create(&callback))
171 std::stringstream ss;
172 senf::console::ParseCommandInfo info;
173 senf::console::root()("fn")(ss, info);
174 BOOST_CHECK_EQUAL( ss.str(), "callback" );
178 std::stringstream ss;
179 senf::console::root().get("fn").help(ss);
180 BOOST_CHECK_EQUAL( ss.str(), "help text\n" );
183 senf::console::root().remove("fn");
186 ///////////////////////////////cc.e////////////////////////////////////////
193 // comment-column: 40
194 // c-file-style: "senf"
195 // indent-tabs-mode: nil
196 // ispell-local-dictionary: "american"
197 // compile-command: "scons -u test"