1 /******************************************************************************
3 * Copyright (C) 2012-2013 Icinga Development Team (http://www.icinga.org/) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
20 #include "base/convert.h"
21 #include "base/object.h"
22 #include <boost/test/unit_test.hpp>
23 #include <boost/smart_ptr/make_shared.hpp>
26 using namespace icinga;
28 BOOST_AUTO_TEST_SUITE(base_convert)
30 BOOST_AUTO_TEST_CASE(tolong)
32 BOOST_CHECK_THROW(Convert::ToLong(" 7"), boost::exception);
33 BOOST_CHECK(Convert::ToLong("-7") == -7);
34 BOOST_CHECK_THROW(Convert::ToLong("7a"), boost::exception);
37 BOOST_AUTO_TEST_CASE(todouble)
39 BOOST_CHECK_THROW(Convert::ToDouble(" 7.3"), boost::exception);
40 BOOST_CHECK(Convert::ToDouble("-7.3") == -7.3);
41 BOOST_CHECK_THROW(Convert::ToDouble("7.3a"), boost::exception);
44 BOOST_AUTO_TEST_CASE(tostring)
46 BOOST_CHECK(Convert::ToString(7) == "7");
47 BOOST_CHECK(Convert::ToString(7.3) == "7.3");
48 BOOST_CHECK(Convert::ToString("hello") == "hello");
50 Object::Ptr object = boost::make_shared<Object>();
51 BOOST_CHECK(Convert::ToString(object) == "Object of type 'icinga::Object'");
54 BOOST_AUTO_TEST_CASE(tobool)
56 BOOST_CHECK_THROW(Convert::ToBool("a"), boost::exception);
57 BOOST_CHECK(Convert::ToBool("0") == false);
58 BOOST_CHECK(Convert::ToBool("1") == true);
59 BOOST_CHECK(Convert::ToBool("2") == true);
62 BOOST_AUTO_TEST_SUITE_END()