switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Utils / Console / Utility.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2009
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 Utility.test unit tests */
30
31 //#include "Utility.test.hh"
32 //#include "Utility.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
45     char charTest(char value) { return value; }
46
47     enum TestEnum { Foo=1, Bar=2, FooBar=4 };
48     SENF_CONSOLE_REGISTER_ENUM( TestEnum, (Foo)(Bar)(FooBar) );
49
50     senf::console::FlagCollection<TestEnum> collectionTest(
51         senf::console::FlagCollection<TestEnum> flags) { return flags; }
52
53 }
54
55 SENF_AUTO_UNIT_TEST(charAsString)
56 {
57     namespace fty = senf::console::factory;
58
59     senf::console::Executor executor;
60     senf::console::CommandParser parser;
61     senf::console::ScopedDirectory<> dir;
62     senf::console::root().add("test", dir);
63     std::stringstream ss;
64
65     dir.add("test",
66             fty::Command<senf::console::CharAsString<char> (senf::console::CharAsString<char>)>(
67                 &charTest));
68
69     ss.str("");
70     SENF_CHECK_NO_THROW(
71         parser.parse("test/test \"\\x01\"",
72                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )) );
73     BOOST_CHECK_EQUAL( ss.str(), "\x01\n" );
74 }
75
76 SENF_AUTO_UNIT_TEST(flagCollection)
77 {
78     namespace fty = senf::console::factory;
79
80     senf::console::Executor executor;
81     senf::console::CommandParser parser;
82     senf::console::ScopedDirectory<> dir;
83     senf::console::root().add("test", dir);
84     std::stringstream ss;
85
86     dir.add("test",fty::Command(&collectionTest));
87
88     ss.str("");
89     SENF_CHECK_NO_THROW(
90         parser.parse("test/test foo",
91                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )) );
92     BOOST_CHECK_EQUAL( ss.str(), "Foo\n" );
93
94     ss.str("");
95     SENF_CHECK_NO_THROW(
96         parser.parse("test/test (foo bar)",
97                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )) );
98     BOOST_CHECK_EQUAL( ss.str(), "(Foo Bar)\n" );
99
100     ss.str("");
101     SENF_CHECK_NO_THROW(
102         parser.parse("test/test ()",
103                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )) );
104     BOOST_CHECK_EQUAL( ss.str(), "()\n" );
105 }
106
107 //-/////////////////////////////////////////////////////////////////////////////////////////////////
108 #undef prefix_
109
110 \f
111 // Local Variables:
112 // mode: c++
113 // fill-column: 100
114 // comment-column: 40
115 // c-file-style: "senf"
116 // indent-tabs-mode: nil
117 // ispell-local-dictionary: "american"
118 // compile-command: "scons -u test"
119 // End: