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 Config.test unit tests */
26 //#include "Config.test.hh"
27 //#include "Config.test.ih"
32 #include "ScopedDirectory.hh"
33 #include "ParsedCommand.hh"
34 #include <boost/filesystem/operations.hpp>
36 #include "../Utils/auto_unit_test.hh"
37 #include <boost/test/test_tools.hpp>
40 ///////////////////////////////cc.p////////////////////////////////////////
47 void fun1(int v) { var1 = v; }
48 void fun2() { var2 = true; }
53 TempFile(std::string const & name) : name_ (name), file_ (name_.c_str()) {}
54 ~TempFile() { file_.close(); boost::filesystem::remove(name_); }
56 template <class T> TempFile & operator<<(T const & v) { file_ << v; return *this; }
57 enum Closer { close }; void operator<<(Closer) { file_.close(); }
58 std::string const & name() { return name_; }
68 BOOST_AUTO_UNIT_TEST(configFile)
70 TempFile cfgf ("test.cfg");
71 cfgf << "dir1/fun1 10;\n"
74 senf::console::ScopedDirectory<> dir1;
75 senf::console::root().add("dir1", dir1);
76 dir1.add("fun1",&fun1);
79 senf::console::ConfigFile cfg (cfgf.name());
82 SENF_CHECK_NO_THROW( cfg.parse() )
83 BOOST_CHECK_EQUAL( var1, 10 );
86 SENF_CHECK_NO_THROW( cfg.parse() )
87 BOOST_CHECK_EQUAL( var1, 0 );
91 SENF_CHECK_NO_THROW( cfg.parse() )
92 BOOST_CHECK_EQUAL( var1, 10 );
96 BOOST_AUTO_UNIT_TEST(configFileRestrict)
98 TempFile cfgf ("test.cfg");
99 cfgf << "dir1/fun1 10;\n"
103 senf::console::ScopedDirectory<> dir1;
104 senf::console::root().add("dir1", dir1);
105 dir1.add("fun1",&fun1);
110 senf::console::ConfigFile cfg (cfgf.name());
111 SENF_CHECK_NO_THROW( cfg.parse(dir1) );
112 BOOST_CHECK_EQUAL( var1, 10 );
113 BOOST_CHECK_EQUAL( var2, false );
114 BOOST_CHECK( cfg.parsed(dir1) );
115 BOOST_CHECK( ! cfg.complete() );
117 senf::console::ScopedDirectory<> dir2;
118 senf::console::root().add("dir2", dir2);
119 dir2.add("fun2",&fun2);
123 SENF_CHECK_NO_THROW( cfg.parse() );
124 BOOST_CHECK_EQUAL( var1, 0 );
125 BOOST_CHECK_EQUAL( var2, true );
126 BOOST_CHECK( cfg.complete() );
130 BOOST_AUTO_UNIT_TEST(configFileSkipGroup)
132 TempFile cfgf ("test.cfg");
133 cfgf << "dir1/fun1 10;\n"
134 << "dir2 { dir3 { fun2; } fun1 5; }"
137 senf::console::ScopedDirectory<> dir1;
138 senf::console::root().add("dir1", dir1);
139 dir1.add("fun1",&fun1);
141 senf::console::ScopedDirectory<> dir2;
142 senf::console::root().add("dir2", dir2);
144 dir2.mkdir("dir3").add("fun2", &fun2);
145 dir2.add("fun1", &fun1);
150 senf::console::ConfigFile cfg (cfgf.name());
151 SENF_CHECK_NO_THROW( cfg.parse(dir1) );
152 BOOST_CHECK_EQUAL( var1, 10 );
153 BOOST_CHECK_EQUAL( var2, false );
154 BOOST_CHECK( cfg.parsed(dir1) );
158 SENF_CHECK_NO_THROW( cfg.parse(dir2["dir3"]) );
159 BOOST_CHECK_EQUAL( var1, 0 );
160 BOOST_CHECK_EQUAL( var2, true );
161 BOOST_CHECK( ! cfg.parsed(dir2) );
165 SENF_CHECK_NO_THROW( cfg.parse() );
166 BOOST_CHECK_EQUAL( var1, 5 );
167 BOOST_CHECK_EQUAL( var2, false );
171 BOOST_AUTO_UNIT_TEST(configRestrictAndLink)
173 TempFile cfgf ("test.cfg");
174 cfgf << "dir1/fun1 10;\n"
175 << "link1 { dir3 { fun2; } fun1 5; }"
178 senf::console::ScopedDirectory<> dir1;
179 senf::console::root().add("dir1", dir1);
180 dir1.add("fun1",&fun1);
182 senf::console::ScopedDirectory<> dir2;
183 dir1.add("dir2", dir2);
185 dir2.mkdir("dir3").add("fun2", &fun2);
186 dir2.add("fun1", &fun1);
188 senf::console::ScopedDirectory<> dir4;
189 senf::console::root().add("dir4", dir4);
190 dir4.link("link1", dir2);
193 senf::console::ConfigFile cfg (cfgf.name(), dir4);
197 SENF_CHECK_NO_THROW( cfg.parse(dir2["dir3"]) );
198 BOOST_CHECK_EQUAL( var1, 0 );
199 BOOST_CHECK_EQUAL( var2, true );
201 BOOST_CHECK_THROW( cfg.parse(), senf::console::SyntaxErrorException );
205 ///////////////////////////////cc.e////////////////////////////////////////
212 // comment-column: 40
213 // c-file-style: "senf"
214 // indent-tabs-mode: nil
215 // ispell-local-dictionary: "american"
216 // compile-command: "scons -u test"