Debian Codegraph @

src/test/EPUBPathTest.cpp

raw
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /*
3 * This file is part of the libepubgen project.
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
10 #include <string>
11
12 #include <cppunit/TestFixture.h>
13 #include <cppunit/extensions/HelperMacros.h>
14
15 #include "EPUBPath.h"
16
17 #define ASSERT_THROW(expression) \
18 { \
19 bool threw = false; \
20 try \
21 { \
22 (void) expression; \
23 } \
24 catch (...) \
25 { \
26 threw = true; \
27 } \
28 CPPUNIT_ASSERT_MESSAGE(#expression " did not throw any exception", threw); \
29 }
30
31 namespace test
32 {
33
34 using libepubgen::EPUBPath;
35
36 using std::string;
37
38 class EPUBPathTest : public CPPUNIT_NS::TestFixture
39 {
40 public:
41 virtual void setUp();
42 virtual void tearDown();
43
44 private:
45 CPPUNIT_TEST_SUITE(EPUBPathTest);
46 CPPUNIT_TEST(testConstruction);
47 CPPUNIT_TEST(testAppend);
48 CPPUNIT_TEST(testRelative);
49 CPPUNIT_TEST(testOperators);
50 CPPUNIT_TEST_SUITE_END();
51
52 private:
53 void testConstruction();
54 void testAppend();
55 void testRelative();
56 void testOperators();
57 };
58
59 void EPUBPathTest::setUp()
60 {
61 }
62
63 void EPUBPathTest::tearDown()
64 {
65 }
66
67 void EPUBPathTest::testConstruction()
68 {
69 CPPUNIT_ASSERT("a" == EPUBPath("a").str());
70 CPPUNIT_ASSERT("a/b/c" == EPUBPath("a/b/c").str());
71 // normalization
72 CPPUNIT_ASSERT("a/b/c" == EPUBPath("/a//b///c").str());
73
74 ASSERT_THROW(EPUBPath("."));
75 ASSERT_THROW(EPUBPath(".."));
76 ASSERT_THROW(EPUBPath("a/./b"));
77 ASSERT_THROW(EPUBPath("a/../b"));
78 }
79
80 void EPUBPathTest::testAppend()
81 {
82 EPUBPath path("a");
83 path.appendComponent("b");
84 CPPUNIT_ASSERT("a/b" == path.str());
85 ASSERT_THROW(path.appendComponent("."));
86 CPPUNIT_ASSERT("a/b" == path.str());
87 ASSERT_THROW(path.appendComponent(".."));
88 CPPUNIT_ASSERT("a/b" == path.str());
89 ASSERT_THROW(path.appendComponent("/"));
90 CPPUNIT_ASSERT("a/b" == path.str());
91 ASSERT_THROW(path.appendComponent("c/d"));
92 CPPUNIT_ASSERT("a/b" == path.str());
93
94 path.append(EPUBPath("c/d"));
95 CPPUNIT_ASSERT("a/b/c/d" == path.str());
96 }
97
98 void EPUBPathTest::testRelative()
99 {
100 const EPUBPath path("a/b/c");
101
102 CPPUNIT_ASSERT_EQUAL(string("b/c"), path.relativeTo(EPUBPath("a/d")).str());
103 CPPUNIT_ASSERT_EQUAL(string("c"), path.relativeTo(EPUBPath("a/b/d")).str());
104 CPPUNIT_ASSERT_EQUAL(string("../c"), path.relativeTo(EPUBPath("a/b/d/e")).str());
105 CPPUNIT_ASSERT_EQUAL(string("../a/b/c"), path.relativeTo(EPUBPath("d/e")).str());
106 CPPUNIT_ASSERT_EQUAL(string("../../a/b/c"), path.relativeTo(EPUBPath("d/e/f")).str());
107
108 // pathological cases
109 CPPUNIT_ASSERT_EQUAL(string("b/c"), path.relativeTo(EPUBPath("a/b")).str());
110 CPPUNIT_ASSERT_EQUAL(string("c"), path.relativeTo(EPUBPath("a/b/c")).str());
111 CPPUNIT_ASSERT_EQUAL(string("../c"), path.relativeTo(EPUBPath("a/b/c/d")).str());
112 }
113
114 void EPUBPathTest::testOperators()
115 {
116 CPPUNIT_ASSERT(EPUBPath("a/b") == EPUBPath("a/b"));
117 CPPUNIT_ASSERT(EPUBPath("a/b") != EPUBPath("a/b/c"));
118 CPPUNIT_ASSERT(EPUBPath("a/b/c") != EPUBPath("a/b"));
119 CPPUNIT_ASSERT(EPUBPath("a/b") != EPUBPath("a/c"));
120 CPPUNIT_ASSERT(EPUBPath("a/c") != EPUBPath("a/b"));
121
122 CPPUNIT_ASSERT(EPUBPath("a/b/c") == EPUBPath("a") / "b" / "c");
123 }
124
125 CPPUNIT_TEST_SUITE_REGISTRATION(EPUBPathTest);
126
127 }
128
129 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */