switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Utils / Console / Variables.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 Variables unit tests */
30
31 //#include "Variables.test.hh"
32 //#include "Variables.test.ih"
33
34 // Custom includes
35 #include <iostream>
36 #include <sstream>
37 #include "Console.hh"
38
39 #include <senf/Utils/auto_unit_test.hh>
40 #include <boost/test/test_tools.hpp>
41
42 #define prefix_
43 //-/////////////////////////////////////////////////////////////////////////////////////////////////
44
45 namespace {
46     void testParser(senf::console::ParseCommandInfo::TokensRange const &, int & value)
47     { value = 0; }
48
49     void testFormatter(int value, std::ostream & os)
50     { os << '[' << value << ']'; }
51
52     static bool changed_ (false);
53     void testCallback(int oldValue)
54     { changed_ = true; }
55 }
56
57 SENF_AUTO_UNIT_TEST(variables)
58 {
59     namespace fty = senf::console::factory;
60
61     senf::console::Executor executor;
62     senf::console::CommandParser parser;
63     senf::console::ScopedDirectory<> dir;
64     senf::console::root().add("test", dir);
65
66     int var (5);
67
68     std::stringstream ss;
69     dir.add("var", fty::Variable(var)
70         .doc("Current blorg limit")
71         .formatter(&testFormatter)
72         .parser(&testParser)
73         .typeName("number")
74         .onChange(&testCallback)
75         );
76     parser.parse("test/var; test/var 10; test/var",
77                  boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 ));
78     BOOST_CHECK_EQUAL( ss.str(), "[5]\n[0]\n" );
79     BOOST_CHECK( changed_ );
80
81     ss.str("");
82     dir("var").help(ss);
83     BOOST_CHECK_EQUAL(ss.str(),
84                       "Usage:\n"
85                       "    1- var\n"
86                       "    2- var new_value:number\n"
87                       "\n"
88                       "Current blorg limit\n");
89
90     senf::console::CommandNode & refvar (dir.add("refvar", fty::Variable(boost::ref(var))
91         .doc("Current blorg limit")
92         .formatter(&testFormatter)
93         .parser(&testParser)
94         .typeName("number")
95     ));
96
97     senf::IGNORE( refvar );
98
99     dir.add("crefvar", fty::Variable(boost::cref(var))
100         .doc("Current blorg limit")
101         .formatter(&testFormatter)
102         );
103 }
104
105 namespace {
106
107     class Test2
108     {
109     public:
110         senf::console::ScopedDirectory<Test2> dir;
111
112         Test2() : dir(this), var_(0)
113             { dir.add("var", senf::console::factory::Variable(boost::ref(var_))); }
114
115     private:
116         int var_;
117     };
118
119 }
120
121 SENF_AUTO_UNIT_TEST(memberVariables)
122 {
123     Test2 test2ob;
124     BOOST_CHECK( true );
125 }
126
127 #ifdef COMPILE_CHECK
128
129 COMPILE_FAIL(constVariable)
130 {
131     senf::console::ScopedDirectory<> dir;
132     int var;
133     dir.add("var", boost::cref(var))
134         .parser(&testParser);
135 }
136
137 #endif
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: