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 ConfigFile unit tests */
26 //#include "ConfigFile.test.hh"
27 //#include "ConfigFile.test.ih"
32 #include <boost/filesystem/operations.hpp>
34 #include <senf/Utils/auto_unit_test.hh>
35 #include <boost/test/test_tools.hpp>
38 ///////////////////////////////cc.p////////////////////////////////////////
45 void fun1(int v) { var1 = v; }
46 void fun2() { var2 = true; }
51 TempFile(std::string const & name) : name_ (name), file_ (name_.c_str()) {}
52 ~TempFile() { file_.close(); boost::filesystem::remove(name_); }
54 template <class T> TempFile & operator<<(T const & v) { file_ << v; return *this; }
55 enum Closer { close }; void operator<<(Closer) { file_.close(); }
56 std::string const & name() { return name_; }
65 SENF_AUTO_UNIT_TEST(configFile)
67 namespace fty = senf::console::factory;
69 TempFile cfgf ("test.cfg");
70 cfgf << "dir1/fun1 10;\n"
73 senf::console::ScopedDirectory<> dir1;
74 senf::console::root().add("dir1", dir1);
75 dir1.add("fun1",fty::Command(&fun1));
78 senf::console::ConfigFile cfg (cfgf.name());
81 SENF_CHECK_NO_THROW( cfg.parse() );
82 BOOST_CHECK_EQUAL( var1, 10 );
85 SENF_CHECK_NO_THROW( cfg.parse() );
86 BOOST_CHECK_EQUAL( var1, 0 );
90 SENF_CHECK_NO_THROW( cfg.parse() );
91 BOOST_CHECK_EQUAL( var1, 10 );
95 senf::console::ConfigFile cfg ("i don't exist");
97 SENF_CHECK_NO_THROW( cfg.parse() );
101 SENF_AUTO_UNIT_TEST(configFileRestrict)
103 namespace fty = senf::console::factory;
105 TempFile cfgf ("test.cfg");
106 cfgf << "dir1/fun1 10;\n"
110 senf::console::ScopedDirectory<> dir1;
111 senf::console::root().add("dir1", dir1);
112 dir1.add("fun1",fty::Command(&fun1));
117 senf::console::ConfigFile cfg (cfgf.name());
118 SENF_CHECK_NO_THROW( cfg.parse(dir1) );
119 BOOST_CHECK_EQUAL( var1, 10 );
120 BOOST_CHECK_EQUAL( var2, false );
121 BOOST_CHECK( cfg.parsed(dir1) );
122 BOOST_CHECK( ! cfg.complete() );
124 senf::console::ScopedDirectory<> dir2;
125 senf::console::root().add("dir2", dir2);
126 dir2.add("fun2",fty::Command(&fun2));
130 SENF_CHECK_NO_THROW( cfg.parse() );
131 BOOST_CHECK_EQUAL( var1, 0 );
132 BOOST_CHECK_EQUAL( var2, true );
133 BOOST_CHECK( cfg.complete() );
137 SENF_AUTO_UNIT_TEST(configFileSkipGroup)
139 namespace fty = senf::console::factory;
141 TempFile cfgf ("test.cfg");
142 cfgf << "dir1/fun1 10;\n"
143 << "dir2 { dir3 { fun2; } fun1 5; }"
146 senf::console::ScopedDirectory<> dir1;
147 senf::console::root().add("dir1", dir1);
148 dir1.add("fun1",fty::Command(&fun1));
150 senf::console::ScopedDirectory<> dir2;
151 senf::console::root().add("dir2", dir2);
153 dir2.add("dir3",fty::Directory()).add("fun2", fty::Command(&fun2));
154 dir2.add("fun1", fty::Command(&fun1));
159 senf::console::ConfigFile cfg (cfgf.name());
160 SENF_CHECK_NO_THROW( cfg.parse(dir1) );
161 BOOST_CHECK_EQUAL( var1, 10 );
162 BOOST_CHECK_EQUAL( var2, false );
163 BOOST_CHECK( cfg.parsed(dir1) );
167 SENF_CHECK_NO_THROW( cfg.parse(dir2["dir3"]) );
168 BOOST_CHECK_EQUAL( var1, 0 );
169 BOOST_CHECK_EQUAL( var2, true );
170 BOOST_CHECK( ! cfg.parsed(dir2) );
174 SENF_CHECK_NO_THROW( cfg.parse() );
175 BOOST_CHECK_EQUAL( var1, 5 );
176 BOOST_CHECK_EQUAL( var2, false );
180 SENF_AUTO_UNIT_TEST(configRestrictAndLink)
182 namespace fty = senf::console::factory;
184 TempFile cfgf ("test.cfg");
185 cfgf << "dir1/fun1 10;\n"
186 << "link1 { dir3 { fun2; } fun1 5; }"
189 senf::console::ScopedDirectory<> dir1;
190 senf::console::root().add("dir1", dir1);
191 dir1.add("fun1",fty::Command(&fun1));
193 senf::console::ScopedDirectory<> dir2;
194 dir1.add("dir2", dir2);
196 dir2.add("dir3",fty::Directory()).add("fun2", fty::Command(&fun2));
197 dir2.add("fun1", fty::Command(&fun1));
199 senf::console::ScopedDirectory<> dir4;
200 senf::console::root().add("dir4", dir4);
201 dir4.add("link1", fty::Link(dir2));
204 senf::console::ConfigFile cfg (cfgf.name(), dir4);
208 SENF_CHECK_NO_THROW( cfg.parse(dir2["dir3"]) );
209 BOOST_CHECK_EQUAL( var1, 0 );
210 BOOST_CHECK_EQUAL( var2, true );
212 BOOST_CHECK_THROW( cfg.parse(), senf::console::SyntaxErrorException );
216 ///////////////////////////////cc.e////////////////////////////////////////
223 // comment-column: 40
224 // c-file-style: "senf"
225 // indent-tabs-mode: nil
226 // ispell-local-dictionary: "american"
227 // compile-command: "scons -u test"