Enhance save as template and new from template dialogs
[inkscape.git] / testfiles / src / svg-box-test.cpp
blobb0abf9954bce163e8a206dc10fd2e5bd1ad21ac6
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /** @file
3 * Test for SVG box
4 *//*
5 * Authors: see git history
7 * Copyright (C) 2010 Authors
8 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
9 */
10 #include "svg/svg-box.h"
11 #include "svg/svg.h"
13 #include <cmath>
14 #include <glib.h>
15 #include <gtest/gtest.h>
16 #include <utility>
18 struct read_test_t
20 const std::string str;
21 int top;
22 int right;
23 int bottom;
24 int left;
26 struct write_test_t
28 const std::string in;
29 const std::string out;
32 // clang-format off
33 read_test_t read_tests[5] = {
34 {"0", 0, 0, 0, 0},
35 {"1", 1, 1, 1, 1},
36 {"1 2 3 4", 1, 2, 3, 4},
37 {"1,2,3,4", 1, 2, 3, 4},
38 {"2cm 4cm", 76, 151, 76, 151},
40 const char* fail_tests[4] = {
41 "",
42 "a b c d",
43 "12miles",
44 "14mmm",
46 write_test_t write_tests[7] = {
47 {"0", "0"},
48 {"1", "1"},
49 {"1 1 1 1", "1"},
50 {"1cm", "37.795277"},
51 {"4cm 2in", "151.18111 192"},
52 {"7 2 4cm", "7 2 151.18111"},
53 {"1,2,3", "1 2 3"},
55 read_test_t set_tests[3] = {
56 {"1", 1, 1, 1, 1},
57 {"1 2", 1, 2, 1, 2},
58 {"1 2 3 4", 1, 2, 3, 4},
60 // clang-format on
62 TEST(SvgBoxTest, testRead)
64 for (size_t i = 0; i < G_N_ELEMENTS(read_tests); i++) {
65 SVGBox box;
66 ASSERT_TRUE(box.read(read_tests[i].str, Geom::Scale(1))) << read_tests[i].str;
67 ASSERT_EQ(round(box.top().computed), read_tests[i].top) << read_tests[i].str;
68 ASSERT_EQ(round(box.right().computed), read_tests[i].right) << read_tests[i].str;
69 ASSERT_EQ(round(box.bottom().computed), read_tests[i].bottom) << read_tests[i].str;
70 ASSERT_EQ(round(box.left().computed), read_tests[i].left) << read_tests[i].str;
74 TEST(SvgBoxTest, testFailures)
76 for (size_t i = 0; i < G_N_ELEMENTS(fail_tests); i++) {
77 SVGLength box;
78 ASSERT_TRUE( !box.read(fail_tests[i])) << fail_tests[i];
82 TEST(SvgBoxTest, testWrite)
84 for (size_t i = 0; i < G_N_ELEMENTS(write_tests); i++) {
85 SVGBox box;
86 ASSERT_TRUE(box.read(write_tests[i].in, Geom::Scale(1))) << write_tests[i].in;
87 ASSERT_EQ(box.write(), write_tests[i].out) << write_tests[i].in;
91 TEST(SvgBoxTest, testSet)
93 for (auto t : set_tests) {
94 SVGBox box;
95 box.set(t.top, t.right, t.bottom, t.left);
96 ASSERT_EQ(box.write(), t.str);
100 TEST(SvgBoxTest, testToFromString)
102 SVGBox box;
103 ASSERT_TRUE(box.fromString("10mm 5", "mm", Geom::Scale(5)));
104 ASSERT_EQ(box.toString("mm", Geom::Scale(5)), "10mm 5.0000001mm");
105 // This result is mm to px (internal conversion) plus scale test.
106 ASSERT_EQ(box.write(), "7.5590553 3.7795277");
109 TEST(SvgBoxTest, testConfine)
111 SVGBox box;
112 box.set(10, 20, 10, 20);
113 ASSERT_EQ(box.write(), "10 20");
114 box.set(BOX_TOP, 5, true);
115 ASSERT_EQ(box.write(), "5 20");
116 box.set(BOX_LEFT, 10, true);
117 ASSERT_EQ(box.write(), "5 10");
118 box.set(BOX_LEFT, 5, true);
119 ASSERT_EQ(box.write(), "5");
120 box.set(BOX_BOTTOM, 7, true);
121 ASSERT_EQ(box.write(), "7");
124 // vim: filetype=cpp:expandtab:shiftwidth=4:softtabstop=4:fileencoding=utf-8:textwidth=99 :