| 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ |
| 2 | /* librevenge |
| 3 | * Version: MPL 2.0 / LGPLv2.1+ |
| 4 | * |
| 5 | * This Source Code Form is subject to the terms of the Mozilla Public |
| 6 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 | * |
| 9 | * Alternatively, the contents of this file may be used under the terms |
| 10 | * of the GNU Lesser General Public License Version 2.1 or later |
| 11 | * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are |
| 12 | * applicable instead of those above. |
| 13 | */ |
| 14 | |
| 15 | #include <cppunit/TestFixture.h> |
| 16 | #include <cppunit/extensions/HelperMacros.h> |
| 17 | |
| 18 | #include <librevenge/librevenge.h> |
| 19 | |
| 20 | namespace librevenge |
| 21 | { |
| 22 | |
| 23 | template<typename CharT, typename Traits> |
| 24 | std::basic_ostream<CharT, Traits> &operator<<(std::basic_ostream<CharT, Traits> &out, const RVNGString &str) |
| 25 | { |
| 26 | return out << str.cstr(); |
| 27 | } |
| 28 | |
| 29 | } |
| 30 | |
| 31 | namespace test |
| 32 | { |
| 33 | |
| 34 | using namespace librevenge; |
| 35 | |
| 36 | class RVNGPropertyListTest : public CPPUNIT_NS::TestFixture |
| 37 | { |
| 38 | public: |
| 39 | virtual void setUp(); |
| 40 | virtual void tearDown(); |
| 41 | |
| 42 | private: |
| 43 | CPPUNIT_TEST_SUITE(RVNGPropertyListTest); |
| 44 | CPPUNIT_TEST(testInsertWithConversion); |
| 45 | CPPUNIT_TEST_SUITE_END(); |
| 46 | |
| 47 | private: |
| 48 | void testInsertWithConversion(); |
| 49 | }; |
| 50 | |
| 51 | void RVNGPropertyListTest::setUp() |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | void RVNGPropertyListTest::tearDown() |
| 56 | { |
| 57 | } |
| 58 | |
| 59 | void RVNGPropertyListTest::testInsertWithConversion() |
| 60 | { |
| 61 | RVNGPropertyList props; |
| 62 | |
| 63 | // valid int |
| 64 | props.insert("int", RVNGString("42")); |
| 65 | CPPUNIT_ASSERT_EQUAL(42, props["int"]->getInt()); |
| 66 | CPPUNIT_ASSERT_EQUAL(RVNG_GENERIC, props["int"]->getUnit()); |
| 67 | props.insert("int", RVNGString(" 42 ")); |
| 68 | CPPUNIT_ASSERT_EQUAL(42, props["int"]->getInt()); |
| 69 | // invalid int |
| 70 | props.insert("int", RVNGString("42a")); |
| 71 | CPPUNIT_ASSERT_EQUAL(0, props["int"]->getInt()); |
| 72 | CPPUNIT_ASSERT_EQUAL(RVNGString("42a"), props["int"]->getStr()); |
| 73 | |
| 74 | const double eps = 0.00001; |
| 75 | // valid double |
| 76 | props.insert("double", RVNGString("3.14")); |
| 77 | CPPUNIT_ASSERT_DOUBLES_EQUAL(3.14, props["double"]->getDouble(), eps); |
| 78 | CPPUNIT_ASSERT_EQUAL(RVNG_GENERIC, props["double"]->getUnit()); |
| 79 | props.insert("double", RVNGString(" 3.14 ")); |
| 80 | CPPUNIT_ASSERT_DOUBLES_EQUAL(3.14, props["double"]->getDouble(), eps); |
| 81 | CPPUNIT_ASSERT_EQUAL(RVNG_GENERIC, props["double"]->getUnit()); |
| 82 | // valid double with unit |
| 83 | props.insert("double", RVNGString("3.14pt")); |
| 84 | CPPUNIT_ASSERT_DOUBLES_EQUAL(3.14, props["double"]->getDouble(), eps); |
| 85 | CPPUNIT_ASSERT_EQUAL(RVNG_POINT, props["double"]->getUnit()); |
| 86 | // TODO: is this really expected to work? |
| 87 | props.insert("double", RVNGString("3.14 pt")); |
| 88 | CPPUNIT_ASSERT_DOUBLES_EQUAL(3.14, props["double"]->getDouble(), eps); |
| 89 | CPPUNIT_ASSERT_EQUAL(RVNG_POINT, props["double"]->getUnit()); |
| 90 | props.insert("double", RVNGString("3.14in")); |
| 91 | CPPUNIT_ASSERT_DOUBLES_EQUAL(3.14, props["double"]->getDouble(), eps); |
| 92 | CPPUNIT_ASSERT_EQUAL(RVNG_INCH, props["double"]->getUnit()); |
| 93 | props.insert("double", RVNGString("314%")); |
| 94 | CPPUNIT_ASSERT_DOUBLES_EQUAL(3.14, props["double"]->getDouble(), eps); |
| 95 | CPPUNIT_ASSERT_EQUAL(RVNG_PERCENT, props["double"]->getUnit()); |
| 96 | props.insert("double", RVNGString("3.14*")); |
| 97 | CPPUNIT_ASSERT_DOUBLES_EQUAL(3.14, props["double"]->getDouble(), eps); |
| 98 | CPPUNIT_ASSERT_EQUAL(RVNG_TWIP, props["double"]->getUnit()); |
| 99 | props.insert("double", RVNGString("2.54cm")); |
| 100 | CPPUNIT_ASSERT_DOUBLES_EQUAL(1.0, props["double"]->getDouble(), eps); |
| 101 | CPPUNIT_ASSERT_EQUAL(RVNG_INCH, props["double"]->getUnit()); |
| 102 | props.insert("double", RVNGString("25.4mm")); |
| 103 | CPPUNIT_ASSERT_DOUBLES_EQUAL(1.0, props["double"]->getDouble(), eps); |
| 104 | CPPUNIT_ASSERT_EQUAL(RVNG_INCH, props["double"]->getUnit()); |
| 105 | // invalid double |
| 106 | props.insert("double", RVNGString("25.4a")); |
| 107 | CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, props["double"]->getDouble(), eps); |
| 108 | CPPUNIT_ASSERT_EQUAL(RVNG_UNIT_ERROR, props["double"]->getUnit()); |
| 109 | |
| 110 | // valid bool |
| 111 | props.insert("bool", RVNGString("true")); |
| 112 | CPPUNIT_ASSERT_EQUAL(1, props["bool"]->getInt()); |
| 113 | CPPUNIT_ASSERT_EQUAL(RVNGString("true"), props["bool"]->getStr()); |
| 114 | props.insert("bool", RVNGString("FALSE")); |
| 115 | CPPUNIT_ASSERT_EQUAL(0, props["bool"]->getInt()); |
| 116 | CPPUNIT_ASSERT_EQUAL(RVNGString("false"), props["bool"]->getStr()); |
| 117 | props.insert("bool", RVNGString("TRUE")); |
| 118 | CPPUNIT_ASSERT_EQUAL(1, props["bool"]->getInt()); |
| 119 | props.insert("bool", RVNGString(" true ")); |
| 120 | CPPUNIT_ASSERT_EQUAL(1, props["bool"]->getInt()); |
| 121 | // invalid bool |
| 122 | props.insert("bool", RVNGString("truer")); |
| 123 | CPPUNIT_ASSERT_EQUAL(0, props["bool"]->getInt()); |
| 124 | } |
| 125 | |
| 126 | CPPUNIT_TEST_SUITE_REGISTRATION(RVNGPropertyListTest); |
| 127 | |
| 128 | } |
| 129 | |
| 130 | /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */ |