summaryrefslogtreecommitdiff
path: root/editeng
diff options
context:
space:
mode:
authorTomaž Vajngerl <tomaz.vajngerl@collabora.co.uk>2023-12-27 14:31:53 +0900
committerTomaž Vajngerl <quikee@gmail.com>2023-12-31 10:32:00 +0100
commit5b4dfce8933b1a2c18b9fc00d2b8a9a8ce37b510 (patch)
tree8f0dbf866fcef58a755ed61e7489d4f57c9a8987 /editeng
parent025a49a40a3c0c1be5bf4383e87a1cc60014b7f4 (diff)
editeng: cleanup operators and constructors for EditSelection + test
Move the constructors into class body, cleanup operators so they use more standard class based operators and use default for != as it will just be a neagtion of ==. Change-Id: I1b99db6c9a82468ab76091eb93a5f3641024c65b Reviewed-on: https://gerrit.libreoffice.org/c/core/+/161365 Tested-by: Jenkins Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
Diffstat (limited to 'editeng')
-rw-r--r--editeng/CppunitTest_editeng_core.mk1
-rw-r--r--editeng/inc/EditSelection.hxx25
-rw-r--r--editeng/qa/unit/EditSelectionTest.cxx143
-rw-r--r--editeng/source/editeng/editdoc.cxx23
4 files changed, 162 insertions, 30 deletions
diff --git a/editeng/CppunitTest_editeng_core.mk b/editeng/CppunitTest_editeng_core.mk
index 47be49fbcd96..3fc6d9bde688 100644
--- a/editeng/CppunitTest_editeng_core.mk
+++ b/editeng/CppunitTest_editeng_core.mk
@@ -17,6 +17,7 @@ $(eval $(call gb_CppunitTest_add_exception_objects,editeng_core, \
editeng/qa/unit/EPaMTest \
editeng/qa/unit/EditLineTest \
editeng/qa/unit/EditPaMTest \
+ editeng/qa/unit/EditSelectionTest \
))
$(eval $(call gb_CppunitTest_use_library_objects,editeng_core,editeng))
diff --git a/editeng/inc/EditSelection.hxx b/editeng/inc/EditSelection.hxx
index c94fce2c0881..9d11a9685c30 100644
--- a/editeng/inc/EditSelection.hxx
+++ b/editeng/inc/EditSelection.hxx
@@ -28,10 +28,19 @@ private:
EditPaM aEndPaM;
public:
- EditSelection();
+ EditSelection() = default;
- EditSelection(const EditPaM& rStartAndAnd);
- EditSelection(const EditPaM& rStart, const EditPaM& rEnd);
+ EditSelection(const EditPaM& rStartAndEnd)
+ : aStartPaM(rStartAndEnd)
+ , aEndPaM(rStartAndEnd)
+ {
+ }
+
+ EditSelection(const EditPaM& rStart, const EditPaM& rEnd)
+ : aStartPaM(rStart)
+ , aEndPaM(rEnd)
+ {
+ }
EditPaM& Min() { return aStartPaM; }
EditPaM& Max() { return aEndPaM; }
@@ -45,12 +54,14 @@ public:
void Adjust(const EditDoc& rNodes);
- EditSelection& operator=(const EditPaM& r);
- bool operator==(const EditSelection& r) const
+ EditSelection& operator=(const EditPaM& rPaM)
{
- return (aStartPaM == r.aStartPaM) && (aEndPaM == r.aEndPaM);
+ aStartPaM = rPaM;
+ aEndPaM = rPaM;
+ return *this;
}
- bool operator!=(const EditSelection& r) const { return !(r == *this); }
+
+ bool operator==(const EditSelection& rOther) const = default;
};
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/editeng/qa/unit/EditSelectionTest.cxx b/editeng/qa/unit/EditSelectionTest.cxx
new file mode 100644
index 000000000000..b3308fbc2fa9
--- /dev/null
+++ b/editeng/qa/unit/EditSelectionTest.cxx
@@ -0,0 +1,143 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#include <test/bootstrapfixture.hxx>
+#include <EditSelection.hxx>
+#include <editdoc.hxx>
+
+namespace
+{
+class EditSelectionTest : public test::BootstrapFixture
+{
+protected:
+ rtl::Reference<EditEngineItemPool> mpItemPool;
+
+public:
+ void setUp() override
+ {
+ test::BootstrapFixture::setUp();
+ mpItemPool = new EditEngineItemPool();
+ }
+
+ void tearDown() override
+ {
+ mpItemPool.clear();
+ test::BootstrapFixture::tearDown();
+ }
+};
+
+CPPUNIT_TEST_FIXTURE(EditSelectionTest, testConstruction)
+{
+ // Check empty selections
+ EditSelection aEmpty;
+ CPPUNIT_ASSERT(aEmpty.Min().GetNode() == nullptr);
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(0), aEmpty.Min().GetIndex());
+
+ CPPUNIT_ASSERT(aEmpty.Max().GetNode() == nullptr);
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(0), aEmpty.Max().GetIndex());
+
+ // Create content nodes
+ ContentNode aContentNode1(*mpItemPool);
+ ContentNode* pContentNode1 = &aContentNode1;
+ ContentNode aContentNode2(*mpItemPool);
+ ContentNode* pContentNode2 = &aContentNode2;
+
+ // Check selection with (node1 10, node1 20)
+ {
+ EditSelection aNew(EditPaM(&aContentNode1, 10), EditPaM(&aContentNode1, 20));
+
+ CPPUNIT_ASSERT_EQUAL(true, aNew.Min().GetNode() == pContentNode1);
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(10), aNew.Min().GetIndex());
+
+ CPPUNIT_ASSERT_EQUAL(true, aNew.Max().GetNode() == pContentNode1);
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(20), aNew.Max().GetIndex());
+ }
+
+ // Check selection with (node1 10, node2 10)
+ {
+ EditSelection aNew(EditPaM(&aContentNode1, 10), EditPaM(&aContentNode2, 10));
+
+ CPPUNIT_ASSERT_EQUAL(true, aNew.Min().GetNode() == pContentNode1);
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(10), aNew.Min().GetIndex());
+
+ CPPUNIT_ASSERT_EQUAL(true, aNew.Max().GetNode() == pContentNode2);
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(10), aNew.Max().GetIndex());
+ }
+}
+
+CPPUNIT_TEST_FIXTURE(EditSelectionTest, testEquals)
+{
+ // Check empty equality
+ EditSelection aEditSelectionEmpty1;
+ EditSelection aEditSelectionEmpty2;
+
+ CPPUNIT_ASSERT_EQUAL(true, aEditSelectionEmpty1 == aEditSelectionEmpty2);
+ CPPUNIT_ASSERT_EQUAL(false, aEditSelectionEmpty1 != aEditSelectionEmpty2);
+
+ // Check equal, in-equal
+ ContentNode aContentNode(*mpItemPool);
+
+ EditSelection aEditSelection1(EditPaM(&aContentNode, 10), EditPaM(&aContentNode, 20));
+
+ CPPUNIT_ASSERT_EQUAL(false, aEditSelectionEmpty1 == aEditSelection1);
+ CPPUNIT_ASSERT_EQUAL(true, aEditSelectionEmpty1 != aEditSelection1);
+
+ EditSelection aEditSelection2(EditPaM(&aContentNode, 15), EditPaM(&aContentNode, 20));
+ CPPUNIT_ASSERT_EQUAL(false, aEditSelection1 == aEditSelection2);
+ CPPUNIT_ASSERT_EQUAL(true, aEditSelection1 != aEditSelection2);
+
+ EditSelection aEditSelection3(EditPaM(&aContentNode, 10), EditPaM(&aContentNode, 20));
+ CPPUNIT_ASSERT_EQUAL(true, aEditSelection1 == aEditSelection3);
+ CPPUNIT_ASSERT_EQUAL(false, aEditSelection1 != aEditSelection3);
+}
+
+CPPUNIT_TEST_FIXTURE(EditSelectionTest, testAssign)
+{
+ ContentNode aContentNode(*mpItemPool);
+ ContentNode* pContentNode = &aContentNode;
+
+ EditSelection aEditSelection1(EditPaM(&aContentNode, 20), EditPaM(&aContentNode, 20));
+ EditSelection aEditSelection2;
+
+ // Check selection1 == selection2 (empty)
+ CPPUNIT_ASSERT_EQUAL(false, aEditSelection1 == aEditSelection2);
+ CPPUNIT_ASSERT_EQUAL(true, aEditSelection1 != aEditSelection2);
+
+ // Assign with selection2 with (15, 20)
+ aEditSelection2 = EditSelection(EditPaM(pContentNode, 15), EditPaM(pContentNode, 20));
+
+ // Check if it matches aEditSelection1 (20, 20) -> no
+ CPPUNIT_ASSERT_EQUAL(false, aEditSelection1 == aEditSelection2);
+ CPPUNIT_ASSERT_EQUAL(true, aEditSelection1 != aEditSelection2);
+
+ // Check if selection2 vales are set correctly
+ CPPUNIT_ASSERT_EQUAL(true, aEditSelection2.Min().GetNode() == pContentNode);
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(15), aEditSelection2.Min().GetIndex());
+
+ CPPUNIT_ASSERT_EQUAL(true, aEditSelection2.Max().GetNode() == pContentNode);
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(20), aEditSelection2.Max().GetIndex());
+
+ // Assign EditPaM value (20)
+ aEditSelection2 = EditPaM(pContentNode, 20);
+
+ // Check if it matches aEditSelection1 (20, 20) -> yes
+ CPPUNIT_ASSERT_EQUAL(true, aEditSelection1 == aEditSelection2);
+ CPPUNIT_ASSERT_EQUAL(false, aEditSelection1 != aEditSelection2);
+
+ // Check if selection2 vales are set correctly - expect selection min and max == the EditPaM (20)
+ CPPUNIT_ASSERT_EQUAL(true, aEditSelection2.Min().GetNode() == pContentNode);
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(20), aEditSelection2.Min().GetIndex());
+
+ CPPUNIT_ASSERT_EQUAL(true, aEditSelection2.Max().GetNode() == pContentNode);
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(20), aEditSelection2.Max().GetIndex());
+}
+
+} // end anonymous namespace
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/editeng/source/editeng/editdoc.cxx b/editeng/source/editeng/editdoc.cxx
index d61683619a84..1b10e1cfeb59 100644
--- a/editeng/source/editeng/editdoc.cxx
+++ b/editeng/source/editeng/editdoc.cxx
@@ -902,29 +902,6 @@ bool EditSelection::DbgIsBuggy( EditDoc const & rDoc ) const
return aStartPaM.DbgIsBuggy( rDoc ) || aEndPaM.DbgIsBuggy( rDoc );
}
-EditSelection::EditSelection()
-{
-}
-
-EditSelection::EditSelection( const EditPaM& rStartAndAnd ) :
- aStartPaM(rStartAndAnd),
- aEndPaM(rStartAndAnd)
-{
-}
-
-EditSelection::EditSelection( const EditPaM& rStart, const EditPaM& rEnd ) :
- aStartPaM(rStart),
- aEndPaM(rEnd)
-{
-}
-
-EditSelection& EditSelection::operator = ( const EditPaM& rPaM )
-{
- aStartPaM = rPaM;
- aEndPaM = rPaM;
- return *this;
-}
-
void EditSelection::Adjust( const EditDoc& rNodes )
{
DBG_ASSERT( aStartPaM.GetIndex() <= aStartPaM.GetNode()->Len(), "Index out of range in Adjust(1)" );