Utils/Console: Add CharAsString utility
[senf.git] / senf / Utils / Console / Utility.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2009 
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 Utility.test unit tests */
25
26 //#include "Utility.test.hh"
27 //#include "Utility.test.ih"
28
29 // Custom includes
30 #include "Utility.hh"
31 #include "Traits.hh"
32 #include "ParsedCommand.hh"
33 #include "Executor.hh"
34 #include "Parse.hh"
35 #include "ScopedDirectory.hh"
36
37 #include <senf/Utils/auto_unit_test.hh>
38 #include <boost/test/test_tools.hpp>
39
40 #define prefix_
41 ///////////////////////////////cc.p////////////////////////////////////////
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 BOOST_AUTO_UNIT_TEST(charAsString)
56 {
57     senf::console::Executor executor;
58     senf::console::CommandParser parser;
59     senf::console::ScopedDirectory<> dir;
60     senf::console::root().add("test", dir);
61     std::stringstream ss;
62
63     dir.add("test", boost::function<
64             senf::console::CharAsString<char> (senf::console::CharAsString<char>)>(&charTest));
65
66     ss.str("");
67     SENF_CHECK_NO_THROW(
68         parser.parse("test/test \"\\x01\"",
69                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )) );
70     BOOST_CHECK_EQUAL( ss.str(), "\x01\n" );
71 }
72
73 BOOST_AUTO_UNIT_TEST(flagCollection)
74 {
75     senf::console::Executor executor;
76     senf::console::CommandParser parser;
77     senf::console::ScopedDirectory<> dir;
78     senf::console::root().add("test", dir);
79     std::stringstream ss;
80
81     dir.add("test",&collectionTest);
82     
83     ss.str("");
84     SENF_CHECK_NO_THROW(
85         parser.parse("test/test foo",
86                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )) );
87     BOOST_CHECK_EQUAL( ss.str(), "Foo\n" );
88
89     ss.str("");
90     SENF_CHECK_NO_THROW(
91         parser.parse("test/test (foo bar)",
92                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )) );
93     BOOST_CHECK_EQUAL( ss.str(), "(Foo Bar)\n" );
94
95     ss.str("");
96     SENF_CHECK_NO_THROW(
97         parser.parse("test/test ()",
98                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )) );
99     BOOST_CHECK_EQUAL( ss.str(), "()\n" );
100 }
101
102 ///////////////////////////////cc.e////////////////////////////////////////
103 #undef prefix_
104
105 \f
106 // Local Variables:
107 // mode: c++
108 // fill-column: 100
109 // comment-column: 40
110 // c-file-style: "senf"
111 // indent-tabs-mode: nil
112 // ispell-local-dictionary: "american"
113 // compile-command: "scons -u test"
114 // End: