1 /*
2  * LibrePCB - Professional EDA for everyone!
3  * Copyright (C) 2013 LibrePCB Developers, see AUTHORS.md for contributors.
4  * https://librepcb.org/
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 /*******************************************************************************
21  *  Includes
22  ******************************************************************************/
23 
24 #include <gtest/gtest.h>
25 #include <librepcb/common/application.h>
26 #include <librepcb/common/geometry/polygon.h>
27 
28 /*******************************************************************************
29  *  Namespace
30  ******************************************************************************/
31 namespace librepcb {
32 namespace tests {
33 
34 /*******************************************************************************
35  *  Test Class
36  ******************************************************************************/
37 
38 class PolygonTest : public ::testing::Test {};
39 
40 /*******************************************************************************
41  *  Test Methods
42  ******************************************************************************/
43 
TEST_F(PolygonTest,testConstructFromSExpressionV01)44 TEST_F(PolygonTest, testConstructFromSExpressionV01) {
45   // Attention: Do NOT modify this string! It represents the freezed(!) file
46   // format V0.1 and even current versions of LibrePCB must be able to load it!
47   SExpression sexpr = SExpression::parse(
48       "(polygon 2889d60c-1d18-44c3-bf9e-07733b67e480 (layer bot_stop_mask)\n"
49       " (width 0.1) (fill true) (grab_area false)\n"
50       " (vertex (position 0.0 0.0) (angle 0.0))\n"
51       " (vertex (position 120.0 0.0) (angle 0.0))\n"
52       " (vertex (position 120.0 4.0) (angle 0.0))\n"
53       " (vertex (position 0.0 4.0) (angle 0.0))\n"
54       " (vertex (position 0.0 0.0) (angle 0.0))\n"
55       ")\n",
56       FilePath());
57   Polygon obj(sexpr, Version::fromString("0.1"));
58   EXPECT_EQ(Uuid::fromString("2889d60c-1d18-44c3-bf9e-07733b67e480"),
59             obj.getUuid());
60   EXPECT_EQ(GraphicsLayerName("bot_stop_mask"), obj.getLayerName());
61   EXPECT_EQ(UnsignedLength(100000), obj.getLineWidth());
62   EXPECT_EQ(true, obj.isFilled());
63   EXPECT_EQ(false, obj.isGrabArea());
64   EXPECT_EQ(5, obj.getPath().getVertices().count());
65 }
66 
TEST_F(PolygonTest,testConstructFromSExpressionCurrentVersion)67 TEST_F(PolygonTest, testConstructFromSExpressionCurrentVersion) {
68   SExpression sexpr = SExpression::parse(
69       "(polygon 2889d60c-1d18-44c3-bf9e-07733b67e480 (layer bot_stop_mask)\n"
70       " (width 0.1) (fill true) (grab_area false)\n"
71       " (vertex (position 0.0 0.0) (angle 0.0))\n"
72       " (vertex (position 120.0 0.0) (angle 0.0))\n"
73       " (vertex (position 120.0 4.0) (angle 0.0))\n"
74       " (vertex (position 0.0 4.0) (angle 0.0))\n"
75       " (vertex (position 0.0 0.0) (angle 0.0))\n"
76       ")\n",
77       FilePath());
78   Polygon obj(sexpr, qApp->getFileFormatVersion());
79   EXPECT_EQ(Uuid::fromString("2889d60c-1d18-44c3-bf9e-07733b67e480"),
80             obj.getUuid());
81   EXPECT_EQ(GraphicsLayerName("bot_stop_mask"), obj.getLayerName());
82   EXPECT_EQ(UnsignedLength(100000), obj.getLineWidth());
83   EXPECT_EQ(true, obj.isFilled());
84   EXPECT_EQ(false, obj.isGrabArea());
85   EXPECT_EQ(5, obj.getPath().getVertices().count());
86 }
87 
TEST_F(PolygonTest,testSerializeAndDeserialize)88 TEST_F(PolygonTest, testSerializeAndDeserialize) {
89   Polygon obj1(
90       Uuid::createRandom(), GraphicsLayerName("foo"), UnsignedLength(456), true,
91       false,
92       Path({Vertex(Point(1, 2), Angle(3)), Vertex(Point(4, 5), Angle(6))}));
93   SExpression sexpr1 = obj1.serializeToDomElement("polygon");
94 
95   Polygon obj2(sexpr1, qApp->getFileFormatVersion());
96   SExpression sexpr2 = obj2.serializeToDomElement("polygon");
97 
98   EXPECT_EQ(sexpr1.toByteArray(), sexpr2.toByteArray());
99 }
100 
101 /*******************************************************************************
102  *  End of File
103  ******************************************************************************/
104 
105 }  // namespace tests
106 }  // namespace librepcb
107