Move Console from Scheduler into Utils
[senf.git] / Utils / Console / Traits.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2008 
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 Traits.test unit tests */
25
26 //#include "Traits.test.hh"
27 //#include "Traits.test.ih"
28
29 // Custom includes
30 #include "Traits.hh"
31 #include "ParsedCommand.hh"
32 #include "Executor.hh"
33 #include "Parse.hh"
34 #include "ScopedDirectory.hh"
35
36 #include "../../Utils/auto_unit_test.hh"
37 #include <boost/test/test_tools.hpp>
38
39 #define prefix_
40 ///////////////////////////////cc.p////////////////////////////////////////
41
42 namespace {
43     enum TestEnum { Foo, Bar };
44     SENF_CONSOLE_REGISTER_ENUM( TestEnum, (Foo)(Bar) );
45
46     TestEnum test (TestEnum value) { return value; }
47
48     struct TestClass {
49         enum MemberEnum { MemberFoo, MemberBar };
50         static MemberEnum test (MemberEnum value) { return value; }
51     };
52     SENF_CONSOLE_REGISTER_ENUM_MEMBER( TestClass, MemberEnum, (MemberFoo)(MemberBar) );
53
54     bool boolTest(bool value) { return value; }
55 }
56
57 BOOST_AUTO_UNIT_TEST(boolTraits)
58 {
59     senf::console::Executor executor;
60     senf::console::CommandParser parser;
61     senf::console::ScopedDirectory<> dir;
62     senf::console::root().add("test", dir);
63
64     dir.add("test", &boolTest);
65
66     std::stringstream ss;
67     BOOST_CHECK_NO_THROW(
68         parser.parse("test/test true; test/test false",
69                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )) );
70     BOOST_CHECK_EQUAL( ss.str(), "true\n" "false\n" );
71
72     ss.str("");
73     BOOST_CHECK_NO_THROW(
74         parser.parse("test/test enabled; test/test disabled",
75                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )) );
76     BOOST_CHECK_EQUAL( ss.str(), "true\n" "false\n" );
77
78     ss.str("");
79     BOOST_CHECK_NO_THROW(
80         parser.parse("test/test yes; test/test no",
81                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )) );
82     BOOST_CHECK_EQUAL( ss.str(), "true\n" "false\n" );
83
84     ss.str("");
85     BOOST_CHECK_NO_THROW(
86         parser.parse("test/test Y; test/test enA",
87                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )) );
88     BOOST_CHECK_EQUAL( ss.str(), "true\n" "true\n" );
89
90     dir.add("test2", &boolTest).formatter( senf::console::formatEnabledDisabled );
91     ss.str("");
92     BOOST_CHECK_NO_THROW(
93         parser.parse("test/test2 0; test/test2 -1",
94                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )) );
95     BOOST_CHECK_EQUAL( ss.str(), "disabled\n" "enabled\n" );
96 }
97
98 BOOST_AUTO_UNIT_TEST(enumSupport)
99 {
100     senf::console::Executor executor;
101     senf::console::CommandParser parser;
102     senf::console::ScopedDirectory<> dir;
103     senf::console::root().add("test", dir);
104
105     dir.add("test",&test);
106     
107     std::stringstream ss;
108     BOOST_CHECK_NO_THROW(
109         parser.parse("test/test Foo",
110                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )) );
111     BOOST_CHECK_EQUAL( ss.str(), "Foo\n" );
112
113     ss.str("");
114     BOOST_CHECK_NO_THROW(
115         parser.parse("test/test Bar",
116                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )) );
117     BOOST_CHECK_EQUAL( ss.str(), "Bar\n" );
118
119     BOOST_CHECK_THROW(
120         parser.parse("test/test (Foo Bar)",
121                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )),
122         senf::console::SyntaxErrorException );
123
124     BOOST_CHECK_THROW(
125         parser.parse("test/test Baz",
126                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )),
127         senf::console::SyntaxErrorException );
128
129     dir.add("member", &TestClass::test);
130
131     ss.str("");
132     BOOST_CHECK_NO_THROW(
133         parser.parse("test/member MemberFoo",
134                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )) );
135     BOOST_CHECK_EQUAL( ss.str(), "MemberFoo\n" );
136
137     ss.str("");
138     BOOST_CHECK_NO_THROW(
139         parser.parse("test/member MemberBar",
140                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )) );
141     BOOST_CHECK_EQUAL( ss.str(), "MemberBar\n" );
142 }
143
144
145 ///////////////////////////////cc.e////////////////////////////////////////
146 #undef prefix_
147
148 \f
149 // Local Variables:
150 // mode: c++
151 // fill-column: 100
152 // comment-column: 40
153 // c-file-style: "senf"
154 // indent-tabs-mode: nil
155 // ispell-local-dictionary: "american"
156 // compile-command: "scons -u test"
157 // End: