switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Utils / Console / Config.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2008
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief Config unit tests */
30
31 //#include "Config.test.hh"
32 //#include "Config.test.ih"
33
34 // Custom includes
35 #include "Console.hh"
36 #include <boost/filesystem/operations.hpp>
37
38 #include <senf/Utils/auto_unit_test.hh>
39 #include <boost/test/test_tools.hpp>
40
41 #define prefix_
42 //-/////////////////////////////////////////////////////////////////////////////////////////////////
43
44 namespace {
45     std::string val1;
46     bool val2 (false);
47
48     void fun1(std::string p)
49     { val1 = p; }
50
51     void fun2()
52     { val2 = true; }
53
54     class TempFile
55     {
56     public:
57         TempFile(std::string const & name) : name_ (name), file_ (name_.c_str()) {}
58         ~TempFile() { file_.close(); boost::filesystem::remove(name_); }
59
60         template <class T> TempFile & operator<<(T const & v) { file_ << v; return *this; }
61         enum Closer { close }; void operator<<(Closer) { file_.close(); }
62         std::string const & name() { return name_; }
63
64     private:
65         std::string name_;
66         std::ofstream file_;
67     };
68
69 }
70
71 SENF_AUTO_UNIT_TEST(configBundle_empty)
72 {
73     TempFile cfg ("test.cfg");
74     cfg << TempFile::close;
75
76     senf::console::ScopedDirectory<> root;
77     root.add("fun2", senf::console::factory::Command(&fun2));
78
79     senf::console::ConfigBundle bundle(root);
80     bundle.add( senf::console::FileConfig(cfg.name()));
81
82     SENF_CHECK_NO_THROW( bundle.parse() );
83 }
84
85 SENF_AUTO_UNIT_TEST(configBundle)
86 {
87     namespace fty = senf::console::factory;
88
89     senf::console::ScopedDirectory<> root;
90     senf::console::root().add("root", root);
91
92     senf::console::ScopedDirectory<> chroot;
93     senf::console::root().add("chroot", chroot);
94
95     root.add("dir1", fty::Directory()).add("fun1", fty::Command(&fun1));
96     root.add("fun2", fty::Command(&fun2));
97     chroot.add("dir1", fty::Directory()).add("fun1", fty::Command(&fun1));
98     chroot.add("fun2", fty::Command(&fun2));
99
100     TempFile cfg ("test.cfg");
101     cfg << "dir1/fun1 foo; fun2;" << TempFile::close;
102
103     char const * argv[] = { "", "--dir1-fun1=bar" };
104
105     senf::console::ConfigBundle bundle(root);
106     bundle.add( senf::console::FileConfig(cfg.name()) );
107     bundle.add( senf::console::OptionsConfig(sizeof(argv)/sizeof(argv[0]), argv) );
108
109     SENF_CHECK_NO_THROW( bundle.parse() );
110     BOOST_CHECK_EQUAL( val1, "bar" );
111     BOOST_CHECK_EQUAL( val2, true );
112
113     bundle.chroot( chroot);
114     SENF_CHECK_NO_THROW( bundle.parse() );
115     BOOST_CHECK_EQUAL( val1, "bar" );
116     BOOST_CHECK_EQUAL( val2, true );
117 }
118
119 //-/////////////////////////////////////////////////////////////////////////////////////////////////
120 #undef prefix_
121
122 \f
123 // Local Variables:
124 // mode: c++
125 // fill-column: 100
126 // comment-column: 40
127 // c-file-style: "senf"
128 // indent-tabs-mode: nil
129 // ispell-local-dictionary: "american"
130 // compile-command: "scons -u test"
131 // End: