Add 'rt' library to build (needed at least by gentoo)
[senf.git] / Console / Config.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 Config.test unit tests */
25
26 //#include "Config.test.hh"
27 //#include "Config.test.ih"
28
29 // Custom includes
30 #include "Config.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
68 BOOST_AUTO_UNIT_TEST(configFile)
69 {
70     TempFile cfgf ("test.cfg");
71     cfgf << "dir1/fun1 10;\n" 
72          << TempFile::close;
73     
74     senf::console::ScopedDirectory<> dir1;
75     senf::console::root().add("dir1", dir1);
76     dir1.add("fun1",&fun1);
77
78     {
79         senf::console::ConfigFile cfg (cfgf.name());
80
81         var1 = 0;
82         SENF_CHECK_NO_THROW( cfg.parse() )
83         BOOST_CHECK_EQUAL( var1, 10 );
84
85         var1 = 0;
86         SENF_CHECK_NO_THROW( cfg.parse() )
87         BOOST_CHECK_EQUAL( var1, 0 );
88
89         var1 = 0;
90         cfg.reset();
91         SENF_CHECK_NO_THROW( cfg.parse() )
92         BOOST_CHECK_EQUAL( var1, 10 );
93     }
94 }
95
96 BOOST_AUTO_UNIT_TEST(configFileRestrict)
97 {
98     TempFile cfgf ("test.cfg");
99     cfgf << "dir1/fun1 10;\n"
100          << "dir2/fun2;\n"
101          << TempFile::close;
102     
103     senf::console::ScopedDirectory<> dir1;
104     senf::console::root().add("dir1", dir1);
105     dir1.add("fun1",&fun1);
106     
107     {
108         var1 = 0;
109         var2 = false;
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() );
116
117         senf::console::ScopedDirectory<> dir2;
118         senf::console::root().add("dir2", dir2);
119         dir2.add("fun2",&fun2);
120
121         var1 = 0;
122         var2 = false;
123         SENF_CHECK_NO_THROW( cfg.parse() );
124         BOOST_CHECK_EQUAL( var1, 0 );
125         BOOST_CHECK_EQUAL( var2, true );
126         BOOST_CHECK( cfg.complete() );
127     }
128 }
129
130 BOOST_AUTO_UNIT_TEST(configFileSkipGroup)
131 {
132     TempFile cfgf ("test.cfg");
133     cfgf << "dir1/fun1 10;\n"
134          << "dir2 { dir3 { fun2; } fun1 5; }"
135          << TempFile::close;
136     
137     senf::console::ScopedDirectory<> dir1;
138     senf::console::root().add("dir1", dir1);
139     dir1.add("fun1",&fun1);
140     
141     senf::console::ScopedDirectory<> dir2;
142     senf::console::root().add("dir2", dir2);
143     
144     dir2.mkdir("dir3").add("fun2", &fun2);
145     dir2.add("fun1", &fun1);
146
147     {
148         var1 = 0;
149         var2 = false;
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) );
155
156         var1 = 0;
157         var2 = false;
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) );
162
163         var1 = 0;
164         var2 = false;
165         SENF_CHECK_NO_THROW( cfg.parse() );
166         BOOST_CHECK_EQUAL( var1, 5 );
167         BOOST_CHECK_EQUAL( var2, false );
168     }
169 }
170
171 ///////////////////////////////cc.e////////////////////////////////////////
172 #undef prefix_
173
174 \f
175 // Local Variables:
176 // mode: c++
177 // fill-column: 100
178 // comment-column: 40
179 // c-file-style: "senf"
180 // indent-tabs-mode: nil
181 // ispell-local-dictionary: "american"
182 // compile-command: "scons -u test"
183 // End: