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