Skip to content

Commit 3965647

Browse files
committed
Fix unit tests and add unit tests for path helper
1 parent 11da16d commit 3965647

14 files changed

+140
-23
lines changed

.gitignore

+4-1
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,7 @@ perf.data*
6767

6868
# clion
6969
*.idea
70-
*.idea/
70+
*.idea/
71+
72+
# misc
73+
.directory

src/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ set(KIMAGEANNOTATOR_SRCS
6161
${CMAKE_CURRENT_SOURCE_DIR}/annotations/items/helper/TextCursor.cpp
6262
${CMAKE_CURRENT_SOURCE_DIR}/annotations/items/helper/CapsLockStatusChecker.cpp
6363
${CMAKE_CURRENT_SOURCE_DIR}/annotations/items/helper/NumberRectHelper.cpp
64-
${CMAKE_CURRENT_SOURCE_DIR}/annotations/items/helper/ShapeHelper.cpp
64+
${CMAKE_CURRENT_SOURCE_DIR}/annotations/items/helper/AnnotationShapeCreator.cpp
6565
${CMAKE_CURRENT_SOURCE_DIR}/annotations/undo/AddCommand.cpp
6666
${CMAKE_CURRENT_SOURCE_DIR}/annotations/undo/DeleteCommand.cpp
6767
${CMAKE_CURRENT_SOURCE_DIR}/annotations/undo/MoveCommand.cpp

src/annotations/items/AnnotationArrow.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ void AnnotationArrow::updateShape()
4040
QLineF shaft(*mLine);
4141
shaft.setLength(shaft.length() - 5);
4242

43-
auto arrow = ShapeHelper::createArrowHead(properties()->width() / 2);
44-
arrow = ShapeHelper::translate(arrow, mLine->p2(), -mLine->angle());
43+
auto arrow = AnnotationShapeCreator::createArrowHead(properties()->width() / 2);
44+
arrow = AnnotationShapeCreator::translate(arrow, mLine->p2(), -mLine->angle());
4545

4646
QPainterPath path(shaft.p1());
4747
path.lineTo(shaft.p2());

src/annotations/items/AnnotationArrow.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#define KIMAGEANNOTATOR_ANNOTATIONARROW_H
2222

2323
#include "src/annotations/items/AbstractAnnotationLine.h"
24-
#include "src/annotations/items/helper/ShapeHelper.h"
24+
#include "src/annotations/items/helper/AnnotationShapeCreator.h"
2525

2626
namespace kImageAnnotator {
2727

src/annotations/items/AnnotationDoubleArrow.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ void AnnotationDoubleArrow::updateShape()
4343
shaft.setPoints(shaft.p2(), shaft.p1());
4444
shaft.setLength(shaft.length() - 5);
4545

46-
auto arrow = ShapeHelper::createArrowHead(properties()->width() / 2);
47-
auto startArrowHead = ShapeHelper::translate(arrow, mLine->p2(), -mLine->angle());
48-
auto endArrowHead = ShapeHelper::translate(arrow, mLine->p1(), -mLine->angle() + 180);
46+
auto arrow = AnnotationShapeCreator::createArrowHead(properties()->width() / 2);
47+
auto startArrowHead = AnnotationShapeCreator::translate(arrow, mLine->p2(), -mLine->angle());
48+
auto endArrowHead = AnnotationShapeCreator::translate(arrow, mLine->p1(), -mLine->angle() + 180);
4949

5050
QPainterPath path(shaft.p1());
5151
path.lineTo(shaft.p2());

src/annotations/items/AnnotationNumberPointer.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ void AnnotationNumberPointer::updateShape()
7676
mRect->moveCenter(mLine->p1());
7777
BaseAnnotationNumber::updateRect(mRect, textProperties()->font());
7878

79-
auto pointer = ShapeHelper::createPointer(mRect->width() * 0.7, mLine->length());
80-
auto finishedPointer = ShapeHelper::translate(pointer, mLine->p2(), -mLine->angle());
79+
auto pointer = AnnotationShapeCreator::createPointer(mRect->width() * 0.7, mLine->length());
80+
auto finishedPointer = AnnotationShapeCreator::translate(pointer, mLine->p2(), -mLine->angle());
8181

8282
QPainterPath path(mLine->p1());
8383
path.setFillRule(Qt::WindingFill);

src/annotations/items/AnnotationNumberPointer.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
#include "src/annotations/items/AbstractAnnotationLine.h"
2424
#include "src/annotations/items/BaseAnnotationNumber.h"
25-
#include "src/annotations/items/helper/ShapeHelper.h"
25+
#include "src/annotations/items/helper/AnnotationShapeCreator.h"
2626
#include "src/annotations/properties/AnnotationTextProperties.h"
2727

2828
namespace kImageAnnotator {

src/annotations/items/helper/ShapeHelper.cpp src/annotations/items/helper/AnnotationShapeCreator.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
* Boston, MA 02110-1301, USA.
1818
*/
1919

20-
#include "ShapeHelper.h"
20+
#include "AnnotationShapeCreator.h"
2121

2222
namespace kImageAnnotator {
2323

24-
QPolygonF ShapeHelper::createPointer(qreal width, qreal length)
24+
QPolygonF AnnotationShapeCreator::createPointer(qreal width, qreal length)
2525
{
2626
QPointF p0(0, 0);
2727
QPointF p1(-length, width / 2);
@@ -31,7 +31,7 @@ QPolygonF ShapeHelper::createPointer(qreal width, qreal length)
3131
return pointer << p0 << p1 << p2 << p0;
3232
}
3333

34-
QPolygonF ShapeHelper::createArrowHead(int scaleFactor)
34+
QPolygonF AnnotationShapeCreator::createArrowHead(int scaleFactor)
3535
{
3636
auto arrowHeadLength = 15 + scaleFactor;
3737
auto arrowHeadWidth = 5 + scaleFactor;
@@ -46,7 +46,7 @@ QPolygonF ShapeHelper::createArrowHead(int scaleFactor)
4646
return arrow << p0 << p1 << p2 << p3 << p0;
4747
}
4848

49-
QPolygonF ShapeHelper::translate(const QPolygonF &shape, const QPointF &pos, qreal angle)
49+
QPolygonF AnnotationShapeCreator::translate(const QPolygonF &shape, const QPointF &pos, qreal angle)
5050
{
5151
return QTransform().translate(pos.x(), pos.y()).rotate(angle).map(shape);
5252
}

src/annotations/items/helper/ShapeHelper.h src/annotations/items/helper/AnnotationShapeCreator.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@
1717
* Boston, MA 02110-1301, USA.
1818
*/
1919

20-
#ifndef KIMAGEANNOTATOR_SHAPEHELPER_H
21-
#define KIMAGEANNOTATOR_SHAPEHELPER_H
20+
#ifndef KIMAGEANNOTATOR_ANNOTATIONSHAPECREATOR_H
21+
#define KIMAGEANNOTATOR_ANNOTATIONSHAPECREATOR_H
2222

2323
#include <QPolygonF>
2424
#include <QTransform>
2525

2626
namespace kImageAnnotator {
2727

28-
class ShapeHelper
28+
class AnnotationShapeCreator
2929
{
3030
public:
3131
static QPolygonF createPointer(qreal width, qreal length);
@@ -35,4 +35,4 @@ class ShapeHelper
3535

3636
} // namespace kImageAnnotator
3737

38-
#endif //KIMAGEANNOTATOR_SHAPEHELPER_H
38+
#endif //KIMAGEANNOTATOR_ANNOTATIONSHAPECREATOR_H

src/annotations/items/helper/NumberRectHelper.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
* Boston, MA 02110-1301, USA.
1818
*/
1919

20-
#ifndef NUMBERRECTHELPER_H
21-
#define NUMBERRECTHELPER_H
20+
#ifndef KIMAGEANNOTATOR_NUMBERRECTHELPER_H
21+
#define KIMAGEANNOTATOR_NUMBERRECTHELPER_H
2222

2323
#include <QString>
2424
#include <QSizeF>
@@ -41,4 +41,4 @@ class NumberRectHelper
4141

4242
} // namespace kImageAnnotator
4343

44-
#endif //NUMBERRECTHELPER_H
44+
#endif //KIMAGEANNOTATOR_NUMBERRECTHELPER_H

tests/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ set(UNITTEST_SRC
2727
common/helper/MathHelperTest.cpp
2828
common/helper/ItemHelperTest.cpp
2929
common/helper/ShapeHelperTest.cpp
30+
common/helper/PathHelperTest.cpp
3031
gui/cropper/CropSelectionRestrictorTest.cpp
3132
gui/cropper/CropSelectionMoveHelperTest.cpp
3233
gui/cropper/CropSelectionHandlerTest.cpp

tests/annotations/core/AnnotationItemFactoryTest.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ void AnnotationItemFactoryTest::TestCreate_Should_ReturnNullPtrForUnknownType()
6666
{
6767
auto config = new Config;
6868
auto settingsProvider = new MockSettingsProvider();
69-
settingsProvider->setToolType((ToolTypes) 13);
69+
settingsProvider->setToolType((ToolTypes) 14);
7070
AnnotationPropertiesFactory propertiesFactory(config, settingsProvider);
7171
AnnotationItemFactory itemFactory(&propertiesFactory, settingsProvider);
7272

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright (C) 2020 Damir Porobic <[email protected]>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
17+
* Boston, MA 02110-1301, USA.
18+
*/
19+
20+
#include "PathHelperTest.h"
21+
22+
void PathHelperTest::TestExtractFilename_Should_ReturnOnlyFilename_When_FilenameHasFormat()
23+
{
24+
auto expected = QStringLiteral("myFile");
25+
auto filename = PathHelper::extractFilename(QStringLiteral("/home/kimageannotator/") + expected + QStringLiteral(".png"));
26+
27+
QCOMPARE(filename, expected);
28+
}
29+
30+
void PathHelperTest::TestExtractFilename_Should_ReturnOnlyFilename_When_FilenameHasNoFormat()
31+
{
32+
auto expected = QStringLiteral("myFile");
33+
auto filename = PathHelper::extractFilename(QStringLiteral("/home/kimageannotator/") + expected);
34+
35+
QCOMPARE(filename, expected);
36+
}
37+
38+
void PathHelperTest::TestExtractFilename_Should_ReturnOnlyFilename_When_FilenameWithoutPathWasProvided()
39+
{
40+
auto expected = QStringLiteral("myFile");
41+
auto filename = PathHelper::extractFilename(expected);
42+
43+
QCOMPARE(filename, expected);
44+
}
45+
46+
void PathHelperTest::TestExtractFilenameWithFormat_Should_ReturnOnlyFilename_When_FilenameHasFormat()
47+
{
48+
auto expected = QStringLiteral("myFile.png");
49+
auto filename = PathHelper::extractFilenameWithFormat(QStringLiteral("/home/kimageannotator/") + expected);
50+
51+
QCOMPARE(filename, expected);
52+
}
53+
54+
void PathHelperTest::TestExtractFilenameWithFormat_Should_ReturnOnlyFilename_When_FilenameWithoutPathWasProvided()
55+
{
56+
auto expected = QStringLiteral("myFile.png");
57+
auto filename = PathHelper::extractFilenameWithFormat(expected);
58+
59+
QCOMPARE(filename, expected);
60+
}
61+
62+
void PathHelperTest::TestPrettyFilename_Should_ReplaceUnderscoresWithSpacesAndCapitalizeFirstLetters()
63+
{
64+
auto input = QStringLiteral("my_test_File.png");
65+
auto expected = QStringLiteral("My Test File.png");
66+
auto filename = PathHelper::prettyFilename(input);
67+
68+
QCOMPARE(filename, expected);
69+
}
70+
71+
QTEST_MAIN(PathHelperTest);

tests/common/helper/PathHelperTest.h

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright (C) 2020 Damir Porobic <[email protected]>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
17+
* Boston, MA 02110-1301, USA.
18+
*/
19+
20+
#ifndef KIMAGEANNOTATOR_PATHHELPERTEST_H
21+
#define KIMAGEANNOTATOR_PATHHELPERTEST_H
22+
23+
#include <QtTest>
24+
25+
#include "src/common/helper/PathHelper.h"
26+
27+
using kImageAnnotator::PathHelper;
28+
29+
class PathHelperTest : public QObject
30+
{
31+
Q_OBJECT
32+
private slots:
33+
void TestExtractFilename_Should_ReturnOnlyFilename_When_FilenameHasFormat();
34+
void TestExtractFilename_Should_ReturnOnlyFilename_When_FilenameHasNoFormat();
35+
void TestExtractFilename_Should_ReturnOnlyFilename_When_FilenameWithoutPathWasProvided();
36+
void TestExtractFilenameWithFormat_Should_ReturnOnlyFilename_When_FilenameHasFormat();
37+
void TestExtractFilenameWithFormat_Should_ReturnOnlyFilename_When_FilenameWithoutPathWasProvided();
38+
void TestPrettyFilename_Should_ReplaceUnderscoresWithSpacesAndCapitalizeFirstLetters();
39+
};
40+
41+
42+
#endif //KIMAGEANNOTATOR_PATHHELPERTEST_H

0 commit comments

Comments
 (0)