b9100df4b66ded5c1e0c8307cde47382db780d62
[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 "Console.hh"
31
32 #include <senf/Utils/auto_unit_test.hh>
33 #include <boost/test/test_tools.hpp>
34
35 #define prefix_
36 ///////////////////////////////cc.p////////////////////////////////////////
37
38 namespace {
39
40     char charTest(char value) { return value; }
41
42     enum TestEnum { Foo=1, Bar=2, FooBar=4 };
43     SENF_CONSOLE_REGISTER_ENUM( TestEnum, (Foo)(Bar)(FooBar) );
44
45     senf::console::FlagCollection<TestEnum> collectionTest(
46         senf::console::FlagCollection<TestEnum> flags) { return flags; }
47
48 }
49
50 BOOST_AUTO_UNIT_TEST(charAsString)
51 {
52     senf::console::Executor executor;
53     senf::console::CommandParser parser;
54     senf::console::ScopedDirectory<> dir;
55     senf::console::root().add("test", dir);
56     std::stringstream ss;
57
58     dir.add("test", boost::function<
59             senf::console::CharAsString<char> (senf::console::CharAsString<char>)>(&charTest));
60
61     ss.str("");
62     SENF_CHECK_NO_THROW(
63         parser.parse("test/test \"\\x01\"",
64                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )) );
65     BOOST_CHECK_EQUAL( ss.str(), "\x01\n" );
66 }
67
68 BOOST_AUTO_UNIT_TEST(flagCollection)
69 {
70     senf::console::Executor executor;
71     senf::console::CommandParser parser;
72     senf::console::ScopedDirectory<> dir;
73     senf::console::root().add("test", dir);
74     std::stringstream ss;
75
76     dir.add("test",&collectionTest);
77     
78     ss.str("");
79     SENF_CHECK_NO_THROW(
80         parser.parse("test/test foo",
81                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )) );
82     BOOST_CHECK_EQUAL( ss.str(), "Foo\n" );
83
84     ss.str("");
85     SENF_CHECK_NO_THROW(
86         parser.parse("test/test (foo bar)",
87                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )) );
88     BOOST_CHECK_EQUAL( ss.str(), "(Foo Bar)\n" );
89
90     ss.str("");
91     SENF_CHECK_NO_THROW(
92         parser.parse("test/test ()",
93                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )) );
94     BOOST_CHECK_EQUAL( ss.str(), "()\n" );
95 }
96
97 ///////////////////////////////cc.e////////////////////////////////////////
98 #undef prefix_
99
100 \f
101 // Local Variables:
102 // mode: c++
103 // fill-column: 100
104 // comment-column: 40
105 // c-file-style: "senf"
106 // indent-tabs-mode: nil
107 // ispell-local-dictionary: "american"
108 // compile-command: "scons -u test"
109 // End: