Fix SCons 1.2.0 build failure
[senf.git] / Utils / 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 unit tests */
25
26 //#include "Config.test.hh"
27 //#include "Config.test.ih"
28
29 // Custom includes
30 #include "Config.hh"
31 #include "ScopedDirectory.hh"
32 #include "ParsedCommand.hh"
33 #include "ConfigFile.hh"
34 #include "ProgramOptions.hh"
35 #include <boost/filesystem/operations.hpp>
36
37 #include "../../Utils/auto_unit_test.hh"
38 #include <boost/test/test_tools.hpp>
39
40 #define prefix_
41 ///////////////////////////////cc.p////////////////////////////////////////
42
43 namespace {
44     std::string val1;
45     bool val2 (false);
46
47     void fun1(std::string p)
48     { val1 = p; }
49
50     void fun2()
51     { val2 = true; }
52
53     class TempFile
54     {
55     public:
56         TempFile(std::string const & name) : name_ (name), file_ (name_.c_str()) {}
57         ~TempFile() { file_.close(); boost::filesystem::remove(name_); }
58         
59         template <class T> TempFile & operator<<(T const & v) { file_ << v; return *this; }
60         enum Closer { close }; void operator<<(Closer) { file_.close(); }
61         std::string const & name() { return name_; }
62
63     private:
64         std::string name_;
65         std::ofstream file_;
66     };
67 }
68
69 BOOST_AUTO_UNIT_TEST(configBundle)
70 {
71     senf::console::ScopedDirectory<> root;
72     senf::console::root().add("root", root);
73
74     senf::console::ScopedDirectory<> chroot;
75     senf::console::root().add("chroot", chroot);
76
77     root.mkdir("dir1").add("fun1", &fun1);
78     root.add("fun2", &fun2);
79     chroot.mkdir("dir1").add("fun1", &fun1);
80     chroot.add("fun2", &fun2);
81
82     TempFile cfg ("test.cfg");
83     cfg << "dir1/fun1 foo; fun2;" << TempFile::close;
84
85     char const * argv[] = { "", "--dir1-fun1=bar" };
86
87     senf::console::ConfigBundle bundle(root);
88     bundle.add( senf::console::FileConfig(cfg.name()) );
89     bundle.add( senf::console::OptionsConfig(sizeof(argv)/sizeof(argv[0]), argv) );
90
91     SENF_CHECK_NO_THROW( bundle.parse() );
92     BOOST_CHECK_EQUAL( val1, "bar" );
93     BOOST_CHECK_EQUAL( val2, true );
94     
95     bundle.chroot( chroot);
96     SENF_CHECK_NO_THROW( bundle.parse() );
97     BOOST_CHECK_EQUAL( val1, "bar" );
98     BOOST_CHECK_EQUAL( val2, true );
99 }
100
101 ///////////////////////////////cc.e////////////////////////////////////////
102 #undef prefix_
103
104 \f
105 // Local Variables:
106 // mode: c++
107 // fill-column: 100
108 // comment-column: 40
109 // c-file-style: "senf"
110 // indent-tabs-mode: nil
111 // ispell-local-dictionary: "american"
112 // compile-command: "scons -u test"
113 // End: