40065b3f3bfce2e22b008205db2ae1999664d286
[senf.git] / senf / Utils / 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 unit tests */
25
26 //#include "ProgramOptions.test.hh"
27 //#include "ProgramOptions.test.ih"
28
29 // Custom includes
30 #include "Console.hh"
31
32 #include <senf/Utils/auto_unit_test.hh>
33 #include <boost/test/test_tools.hpp>
34
35 #define prefix_
36 //-/////////////////////////////////////////////////////////////////////////////////////////////////
37
38 namespace {
39     std::string val1;
40     bool val2 (false);
41
42     void fun1(std::string p)
43     { val1 = p; }
44
45     void fun2()
46     { val2 = true; }
47 }
48
49 SENF_AUTO_UNIT_TEST(programOptions)
50 {
51     namespace fty = senf::console::factory;
52
53     senf::console::ScopedDirectory<> root;
54     senf::console::root().add("root", root);
55
56     root.add("dir1", fty::Directory()).add("fun1", fty::Command(&fun1));
57     root.add("fun2", fty::Command(&fun2));
58     root.add("name-with-dashes", fty::Directory()).add("fun-2", fty::Command(&fun2));
59
60     {
61         char const * 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 const * 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 const * 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         char const * argv[] = { "", "-ab" };
95         senf::console::ProgramOptions opts(sizeof(argv)/sizeof(argv[0]), argv, root);
96         opts
97             .alias('a', "--dir1-fun1=baz")
98             .alias('b', "--fun2");
99
100         val1 = "";
101         val2 = false;
102
103         SENF_CHECK_NO_THROW( opts.parse() );
104         BOOST_CHECK_EQUAL( val1, "baz" );
105         BOOST_CHECK_EQUAL( val2, true );
106     }
107
108     {
109         char const * argv[] = { "", "-badoo" };
110         senf::console::ProgramOptions opts(sizeof(argv)/sizeof(argv[0]), argv, root);
111         opts
112             .alias('a', "--dir1-fun1", true)
113             .alias('b', "--fun2");
114
115         val1 = "";
116         val2 = false;
117
118         SENF_CHECK_NO_THROW( opts.parse() );
119         BOOST_CHECK_EQUAL( val1, "doo" );
120         BOOST_CHECK_EQUAL( val2, true );
121     }
122
123     {
124         char const * argv[] = { "", "-a","dii","-b" };
125         senf::console::ProgramOptions opts(sizeof(argv)/sizeof(argv[0]), argv, root);
126         opts
127             .alias('a', "--dir1-fun1", true)
128             .alias('b', "--fun2");
129
130         val1 = "";
131         val2 = false;
132
133         SENF_CHECK_NO_THROW( opts.parse() );
134         BOOST_CHECK_EQUAL( val1, "dii" );
135         BOOST_CHECK_EQUAL( val2, true );
136     }
137 }
138
139 //-/////////////////////////////////////////////////////////////////////////////////////////////////
140 #undef prefix_
141
142 \f
143 // Local Variables:
144 // mode: c++
145 // fill-column: 100
146 // comment-column: 40
147 // c-file-style: "senf"
148 // indent-tabs-mode: nil
149 // ispell-local-dictionary: "american"
150 // compile-command: "scons -u test"
151 // End: