Scheduler/Console: Fix adding variables to ScopedDirectory instances
[senf.git] / Scheduler / Console / ConfigFile.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 ConfigFile.test unit tests */
25
26 //#include "ConfigFile.test.hh"
27 //#include "ConfigFile.test.ih"
28
29 // Custom includes
30 #include "ConfigFile.hh"
31 #include <fstream>
32 #include "ScopedDirectory.hh"
33 #include "ParsedCommand.hh"
34 #include <boost/filesystem/operations.hpp>
35
36 #include "../../Utils/auto_unit_test.hh"
37 #include <boost/test/test_tools.hpp>
38
39 #define prefix_
40 ///////////////////////////////cc.p////////////////////////////////////////
41
42 namespace {
43
44     int var1 (0);
45     bool var2 (false);
46     
47     void fun1(int v) { var1 = v; }
48     void fun2() { var2 = true; }
49
50     class TempFile
51     {
52     public:
53         TempFile(std::string const & name) : name_ (name), file_ (name_.c_str()) {}
54         ~TempFile() { file_.close(); boost::filesystem::remove(name_); }
55         
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_; }
59
60     private:
61         std::string name_;
62         std::ofstream file_;
63     };
64     
65 }
66
67 BOOST_AUTO_UNIT_TEST(configFile)
68 {
69     TempFile cfgf ("test.cfg");
70     cfgf << "dir1/fun1 10;\n" 
71          << TempFile::close;
72     
73     senf::console::ScopedDirectory<> dir1;
74     senf::console::root().add("dir1", dir1);
75     dir1.add("fun1",&fun1);
76
77     {
78         senf::console::ConfigFile cfg (cfgf.name());
79
80         var1 = 0;
81         SENF_CHECK_NO_THROW( cfg.parse() )
82         BOOST_CHECK_EQUAL( var1, 10 );
83
84         var1 = 0;
85         SENF_CHECK_NO_THROW( cfg.parse() )
86         BOOST_CHECK_EQUAL( var1, 0 );
87
88         var1 = 0;
89         cfg.reset();
90         SENF_CHECK_NO_THROW( cfg.parse() )
91         BOOST_CHECK_EQUAL( var1, 10 );
92     }
93 }
94
95 BOOST_AUTO_UNIT_TEST(configFileRestrict)
96 {
97     TempFile cfgf ("test.cfg");
98     cfgf << "dir1/fun1 10;\n"
99          << "dir2/fun2;\n"
100          << TempFile::close;
101     
102     senf::console::ScopedDirectory<> dir1;
103     senf::console::root().add("dir1", dir1);
104     dir1.add("fun1",&fun1);
105     
106     {
107         var1 = 0;
108         var2 = false;
109         senf::console::ConfigFile cfg (cfgf.name());
110         SENF_CHECK_NO_THROW( cfg.parse(dir1) );
111         BOOST_CHECK_EQUAL( var1, 10 );
112         BOOST_CHECK_EQUAL( var2, false );
113         BOOST_CHECK( cfg.parsed(dir1) );
114         BOOST_CHECK( ! cfg.complete() );
115
116         senf::console::ScopedDirectory<> dir2;
117         senf::console::root().add("dir2", dir2);
118         dir2.add("fun2",&fun2);
119
120         var1 = 0;
121         var2 = false;
122         SENF_CHECK_NO_THROW( cfg.parse() );
123         BOOST_CHECK_EQUAL( var1, 0 );
124         BOOST_CHECK_EQUAL( var2, true );
125         BOOST_CHECK( cfg.complete() );
126     }
127 }
128
129 BOOST_AUTO_UNIT_TEST(configFileSkipGroup)
130 {
131     TempFile cfgf ("test.cfg");
132     cfgf << "dir1/fun1 10;\n"
133          << "dir2 { dir3 { fun2; } fun1 5; }"
134          << TempFile::close;
135     
136     senf::console::ScopedDirectory<> dir1;
137     senf::console::root().add("dir1", dir1);
138     dir1.add("fun1",&fun1);
139     
140     senf::console::ScopedDirectory<> dir2;
141     senf::console::root().add("dir2", dir2);
142     
143     dir2.mkdir("dir3").add("fun2", &fun2);
144     dir2.add("fun1", &fun1);
145
146     {
147         var1 = 0;
148         var2 = false;
149         senf::console::ConfigFile cfg (cfgf.name());
150         SENF_CHECK_NO_THROW( cfg.parse(dir1) );
151         BOOST_CHECK_EQUAL( var1, 10 );
152         BOOST_CHECK_EQUAL( var2, false );
153         BOOST_CHECK( cfg.parsed(dir1) );
154
155         var1 = 0;
156         var2 = false;
157         SENF_CHECK_NO_THROW( cfg.parse(dir2["dir3"]) );
158         BOOST_CHECK_EQUAL( var1, 0 );
159         BOOST_CHECK_EQUAL( var2, true );
160         BOOST_CHECK( ! cfg.parsed(dir2) );
161
162         var1 = 0;
163         var2 = false;
164         SENF_CHECK_NO_THROW( cfg.parse() );
165         BOOST_CHECK_EQUAL( var1, 5 );
166         BOOST_CHECK_EQUAL( var2, false );
167     }
168 }
169
170 BOOST_AUTO_UNIT_TEST(configRestrictAndLink)
171 {
172     TempFile cfgf ("test.cfg");
173     cfgf << "dir1/fun1 10;\n"
174          << "link1 { dir3 { fun2; } fun1 5; }"
175          << TempFile::close;
176     
177     senf::console::ScopedDirectory<> dir1;
178     senf::console::root().add("dir1", dir1);
179     dir1.add("fun1",&fun1);
180     
181     senf::console::ScopedDirectory<> dir2;
182     dir1.add("dir2", dir2);
183     
184     dir2.mkdir("dir3").add("fun2", &fun2);
185     dir2.add("fun1", &fun1);
186
187     senf::console::ScopedDirectory<> dir4;
188     senf::console::root().add("dir4", dir4);
189     dir4.link("link1", dir2);
190
191     {
192         senf::console::ConfigFile cfg (cfgf.name(), dir4);
193
194         var1 = 0;
195         var2 = false;
196         SENF_CHECK_NO_THROW( cfg.parse(dir2["dir3"]) );
197         BOOST_CHECK_EQUAL( var1, 0 );
198         BOOST_CHECK_EQUAL( var2, true );
199
200         BOOST_CHECK_THROW( cfg.parse(), senf::console::SyntaxErrorException );
201     }
202 }
203
204 ///////////////////////////////cc.e////////////////////////////////////////
205 #undef prefix_
206
207 \f
208 // Local Variables:
209 // mode: c++
210 // fill-column: 100
211 // comment-column: 40
212 // c-file-style: "senf"
213 // indent-tabs-mode: nil
214 // ispell-local-dictionary: "american"
215 // compile-command: "scons -u test"
216 // End: