switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Utils / Console / ProgramOptions.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 ProgramOptions unit tests */
30
31 //#include "ProgramOptions.test.hh"
32 //#include "ProgramOptions.test.ih"
33
34 // Custom includes
35 #include "Console.hh"
36
37 #include <senf/Utils/auto_unit_test.hh>
38 #include <boost/test/test_tools.hpp>
39
40 #define prefix_
41 //-/////////////////////////////////////////////////////////////////////////////////////////////////
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
54 SENF_AUTO_UNIT_TEST(programOptions)
55 {
56     namespace fty = senf::console::factory;
57
58     senf::console::ScopedDirectory<> root;
59     senf::console::root().add("root", root);
60
61     root.add("dir1", fty::Directory()).add("fun1", fty::Command(&fun1));
62     root.add("fun2", fty::Command(&fun2));
63     root.add("name-with-dashes", fty::Directory()).add("fun-2", fty::Command(&fun2));
64
65     {
66         char const * argv[] = { "", "--dir1-fun1=foo","--fun2" };
67         senf::console::ProgramOptions opts (sizeof(argv)/sizeof(argv[0]), argv, root);
68
69         SENF_CHECK_NO_THROW( opts.parse() );
70         BOOST_CHECK_EQUAL( val1, "foo" );
71         BOOST_CHECK_EQUAL( val2, true );
72     }
73
74     {
75         char const * argv[] = { "", "--d-f=foo","--fun" };
76         senf::console::ProgramOptions opts (sizeof(argv)/sizeof(argv[0]), argv, root);
77
78         val1 = "";
79         val2 = false;
80
81         SENF_CHECK_NO_THROW( opts.parse() );
82         BOOST_CHECK_EQUAL( val1, "foo" );
83         BOOST_CHECK_EQUAL( val2, true );
84     }
85
86     {
87         char const * argv[] = { "", "--name-w-fun" };
88         senf::console::ProgramOptions opts (sizeof(argv)/sizeof(argv[0]), argv, root);
89
90         val1 = "";
91         val2 = false;
92
93         SENF_CHECK_NO_THROW( opts.parse() );
94         BOOST_CHECK_EQUAL( val1, "" );
95         BOOST_CHECK_EQUAL( val2, true );
96     }
97
98     {
99         char const * argv[] = { "", "-ab" };
100         senf::console::ProgramOptions opts(sizeof(argv)/sizeof(argv[0]), argv, root);
101         opts
102             .alias('a', "--dir1-fun1=baz")
103             .alias('b', "--fun2");
104
105         val1 = "";
106         val2 = false;
107
108         SENF_CHECK_NO_THROW( opts.parse() );
109         BOOST_CHECK_EQUAL( val1, "baz" );
110         BOOST_CHECK_EQUAL( val2, true );
111     }
112
113     {
114         char const * argv[] = { "", "-badoo" };
115         senf::console::ProgramOptions opts(sizeof(argv)/sizeof(argv[0]), argv, root);
116         opts
117             .alias('a', "--dir1-fun1", true)
118             .alias('b', "--fun2");
119
120         val1 = "";
121         val2 = false;
122
123         SENF_CHECK_NO_THROW( opts.parse() );
124         BOOST_CHECK_EQUAL( val1, "doo" );
125         BOOST_CHECK_EQUAL( val2, true );
126     }
127
128     {
129         char const * argv[] = { "", "-a","dii","-b" };
130         senf::console::ProgramOptions opts(sizeof(argv)/sizeof(argv[0]), argv, root);
131         opts
132             .alias('a', "--dir1-fun1", true)
133             .alias('b', "--fun2");
134
135         val1 = "";
136         val2 = false;
137
138         SENF_CHECK_NO_THROW( opts.parse() );
139         BOOST_CHECK_EQUAL( val1, "dii" );
140         BOOST_CHECK_EQUAL( val2, true );
141     }
142 }
143
144 //-/////////////////////////////////////////////////////////////////////////////////////////////////
145 #undef prefix_
146
147 \f
148 // Local Variables:
149 // mode: c++
150 // fill-column: 100
151 // comment-column: 40
152 // c-file-style: "senf"
153 // indent-tabs-mode: nil
154 // ispell-local-dictionary: "american"
155 // compile-command: "scons -u test"
156 // End: