Console: Refactor config file parser into several classes
[senf.git] / Console / ProgramOptions.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 ProgramOptions.test unit tests */
25
26 //#include "ProgramOptions.test.hh"
27 //#include "ProgramOptions.test.ih"
28
29 // Custom includes
30 #include "ProgramOptions.hh"
31 #include "ScopedDirectory.hh"
32 #include "ParsedCommand.hh"
33
34 #include "../Utils/auto_unit_test.hh"
35 #include <boost/test/test_tools.hpp>
36
37 #define prefix_
38 ///////////////////////////////cc.p////////////////////////////////////////
39
40 namespace {
41     std::string val1;
42     bool val2 (false);
43
44     void fun1(std::string p)
45     { val1 = p; }
46
47     void fun2()
48     { val2 = true; }
49 }
50
51 BOOST_AUTO_UNIT_TEST(programOptions)
52 {
53     senf::console::ScopedDirectory<> root;
54     senf::console::root().add("root", root);
55
56     root.mkdir("dir1").add("fun1", &fun1);
57     root.add("fun2", &fun2);
58     root.mkdir("name-with-dashes").add("fun-2", &fun2);
59
60     {
61         char * argv[] = { "--dir1-fun1=foo","--fun2" };
62         senf::console::ProgramOptions opts (sizeof(argv)/sizeof(argv[0]), argv, root);
63
64         SENF_CHECK_NO_THROW( opts.parse() );
65         BOOST_CHECK_EQUAL( val1, "foo" );
66         BOOST_CHECK_EQUAL( val2, true );
67     }
68
69     {
70         char * argv[] = { "--d-f=foo","--fun" };
71         senf::console::ProgramOptions opts (sizeof(argv)/sizeof(argv[0]), argv, root);
72
73         val1 = "";
74         val2 = false;
75
76         SENF_CHECK_NO_THROW( opts.parse() );
77         BOOST_CHECK_EQUAL( val1, "foo" );
78         BOOST_CHECK_EQUAL( val2, true );
79     }
80
81     {
82         char * argv[] = { "--name-w-fun" };
83         senf::console::ProgramOptions opts (sizeof(argv)/sizeof(argv[0]), argv, root);
84
85         val1 = "";
86         val2 = false;
87
88         SENF_CHECK_NO_THROW( opts.parse() );
89         BOOST_CHECK_EQUAL( val1, "" );
90         BOOST_CHECK_EQUAL( val2, true );
91     }
92 }
93
94 ///////////////////////////////cc.e////////////////////////////////////////
95 #undef prefix_
96
97 \f
98 // Local Variables:
99 // mode: c++
100 // fill-column: 100
101 // comment-column: 40
102 // c-file-style: "senf"
103 // indent-tabs-mode: nil
104 // ispell-local-dictionary: "american"
105 // compile-command: "scons -u test"
106 // End: