summaryrefslogtreecommitdiffstats
path: root/testfiles/src/svg-box-test.cpp
blob: f3fe67ba8aec85bbf0d55044cc318fc3f3bb751b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// SPDX-License-Identifier: GPL-2.0-or-later
/** @file
 * Test for SVG box
 *//*
 * Authors: see git history
 *
 * Copyright (C) 2010 Authors
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */
#include "svg/svg-box.h"
#include "svg/svg.h"

#include <cmath>
#include <glib.h>
#include <gtest/gtest.h>
#include <utility>

struct read_test_t
{
    const std::string str;
    int top;
    int right;
    int bottom;
    int left;
};
struct write_test_t
{
    const std::string in;
    const std::string out;
};

// clang-format off
read_test_t read_tests[5] = {
    {"0", 0, 0, 0, 0},
    {"1", 1, 1, 1, 1},
    {"1 2 3 4", 1, 2, 3, 4},
    {"1,2,3,4", 1, 2, 3, 4},
    {"2cm 4cm", 76, 151, 76, 151},
};
const char* fail_tests[4] = {
    "",
    "a b c d",
    "12miles",
    "14mmm",
};
write_test_t write_tests[7] = {
    {"0", "0"},
    {"1", "1"},
    {"1 1 1 1", "1"},
    {"1cm", "1cm"},
    {"4cm 2in", "4cm 2in"},
    {"7 2 4cm", "7 2 4cm"},
    {"1,2,3", "1 2 3"},
};
read_test_t set_tests[3] = {
    {"1", 1, 1, 1, 1},
    {"1 2", 1, 2, 1, 2},
    {"1 2 3 4", 1, 2, 3, 4},
};
// clang-format on

TEST(SvgBoxTest, testRead)
{
    for (size_t i = 0; i < G_N_ELEMENTS(read_tests); i++) {
        SVGBox box;
        ASSERT_TRUE(box.read(read_tests[i].str)) << read_tests[i].str;
        ASSERT_EQ(round(box.top().computed), read_tests[i].top) << read_tests[i].str;
        ASSERT_EQ(round(box.right().computed), read_tests[i].right) << read_tests[i].str;
        ASSERT_EQ(round(box.bottom().computed), read_tests[i].bottom) << read_tests[i].str;
        ASSERT_EQ(round(box.left().computed), read_tests[i].left) << read_tests[i].str;
    }
}

TEST(SvgBoxTest, testFailures)
{
    for (size_t i = 0; i < G_N_ELEMENTS(fail_tests); i++) {
        SVGLength box;
        ASSERT_TRUE( !box.read(fail_tests[i])) << fail_tests[i];
    }
}

TEST(SvgBoxTest, testWrite)
{
    for (size_t i = 0; i < G_N_ELEMENTS(write_tests); i++) {
        SVGBox box;
        ASSERT_TRUE(box.read(write_tests[i].in)) << write_tests[i].in;
        ASSERT_EQ(box.write(), write_tests[i].out) << write_tests[i].in;
    }
}

TEST(SvgBoxTest, testSet)
{
    for (auto t : set_tests) {
        SVGBox box;
        box.set(t.top, t.right, t.bottom, t.left);
        ASSERT_EQ(box.write(), t.str);
    }
}

TEST(SvgBoxTest, testToFromString)
{
    SVGBox box;
    ASSERT_TRUE(box.fromString("10mm 5", "mm"));
    ASSERT_EQ(box.toString("mm"), "10mm 5.0000001mm");
}

TEST(SvgBoxTest, testConfine)
{
    SVGBox box;
    box.set(10, 20, 10, 20);
    ASSERT_EQ(box.write(), "10 20");
    box.set(BOX_TOP, 5, true);
    ASSERT_EQ(box.write(), "5 20");
    box.set(BOX_LEFT, 10, true);
    ASSERT_EQ(box.write(), "5 10");
    box.set(BOX_LEFT, 5, true);
    ASSERT_EQ(box.write(), "5");
    box.set(BOX_BOTTOM, 7, true);
    ASSERT_EQ(box.write(), "7");
}

// vim: filetype=cpp:expandtab:shiftwidth=4:softtabstop=4:fileencoding=utf-8:textwidth=99 :