Missing changes from last commit
[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         var1 = 0;
80         senf::console::ConfigFile cfg (cfgf.name());
81         SENF_CHECK_NO_THROW( cfg.parse() )
82         BOOST_CHECK_EQUAL( var1, 10 );
83     }
84 }
85
86 BOOST_AUTO_UNIT_TEST(configFileRestrict)
87 {
88     TempFile cfgf ("test.cfg");
89     cfgf << "dir1/fun1 10;\n"
90          << "dir2/fun2;\n"
91          << TempFile::close;
92     
93     senf::console::ScopedDirectory<> dir1;
94     senf::console::root().add("dir1", dir1);
95     dir1.add("fun1",&fun1);
96     
97     {
98         var1 = 0;
99         var2 = false;
100         senf::console::ConfigFile cfg (cfgf.name());
101         SENF_CHECK_NO_THROW( cfg.parse(dir1.node()) );
102         BOOST_CHECK_EQUAL( var1, 10 );
103         BOOST_CHECK_EQUAL( var2, false );
104
105         senf::console::ScopedDirectory<> dir2;
106         senf::console::root().add("dir2", dir2);
107         dir2.add("fun2",&fun2);
108
109         var1 = 0;
110         var2 = false;
111         SENF_CHECK_NO_THROW( cfg.parse() );
112         BOOST_CHECK_EQUAL( var1, 0 );
113         BOOST_CHECK_EQUAL( var2, true );
114     }
115 }
116
117 BOOST_AUTO_UNIT_TEST(configFileSkipGroup)
118 {
119     TempFile cfgf ("test.cfg");
120     cfgf << "dir1/fun1 10;\n"
121          << "dir2 { dir3 { fun2; } fun1 5; }"
122          << TempFile::close;
123     
124     senf::console::ScopedDirectory<> dir1;
125     senf::console::root().add("dir1", dir1);
126     dir1.add("fun1",&fun1);
127     
128     senf::console::ScopedDirectory<> dir2;
129     senf::console::root().add("dir2", dir2);
130     
131     dir2.mkdir("dir3").add("fun2", &fun2);
132     dir2.add("fun1", &fun1);
133
134     {
135         var1 = 0;
136         var2 = false;
137         senf::console::ConfigFile cfg (cfgf.name());
138         SENF_CHECK_NO_THROW( cfg.parse(dir1.node()) );
139         BOOST_CHECK_EQUAL( var1, 10 );
140         BOOST_CHECK_EQUAL( var2, false );
141
142         var1 = 0;
143         var2 = false;
144         SENF_CHECK_NO_THROW( cfg.parse(dir2["dir3"]) );
145         BOOST_CHECK_EQUAL( var1, 0 );
146         BOOST_CHECK_EQUAL( var2, true );
147
148         var1 = 0;
149         var2 = false;
150         SENF_CHECK_NO_THROW( cfg.parse() );
151         BOOST_CHECK_EQUAL( var1, 5 );
152         BOOST_CHECK_EQUAL( var2, false );
153     }
154 }
155
156 ///////////////////////////////cc.e////////////////////////////////////////
157 #undef prefix_
158
159 \f
160 // Local Variables:
161 // mode: c++
162 // fill-column: 100
163 // comment-column: 40
164 // c-file-style: "senf"
165 // indent-tabs-mode: nil
166 // ispell-local-dictionary: "american"
167 // compile-command: "scons -u test"
168 // End: