194c7a88d521b63238c682e43ee0aaecaa9658d3
[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 //-/////////////////////////////////////////////////////////////////////////////////////////////////
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 SENF_AUTO_UNIT_TEST(charAsString)
51 {
52     namespace fty = senf::console::factory;
53
54     senf::console::Executor executor;
55     senf::console::CommandParser parser;
56     senf::console::ScopedDirectory<> dir;
57     senf::console::root().add("test", dir);
58     std::stringstream ss;
59
60     dir.add("test",
61             fty::Command<senf::console::CharAsString<char> (senf::console::CharAsString<char>)>(
62                 &charTest));
63
64     ss.str("");
65     SENF_CHECK_NO_THROW(
66         parser.parse("test/test \"\\x01\"",
67                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )) );
68     BOOST_CHECK_EQUAL( ss.str(), "\x01\n" );
69 }
70
71 SENF_AUTO_UNIT_TEST(flagCollection)
72 {
73     namespace fty = senf::console::factory;
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",fty::Command(&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 //-/////////////////////////////////////////////////////////////////////////////////////////////////
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: