File indexing completed on 2024-04-28 05:52:39

0001 /*
0002     This file is part of the Okteta Gui library, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2006 Friedrich W. H. Kossebau <kossebau@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007 */
0008 
0009 #include "selectiontest.hpp"
0010 
0011 // test object
0012 #include <selection.hpp>
0013 // Qt
0014 #include <QTest>
0015 
0016 namespace Okteta {
0017 
0018 // local variables
0019 static constexpr Address Start = 15;
0020 static constexpr Address End = 27;
0021 
0022 void SelectionTest::testPlainConstructor()
0023 {
0024     const Selection selection;
0025     QVERIFY(!selection.isValid());
0026     QVERIFY(!selection.started());
0027     QVERIFY(!selection.justStarted());
0028 }
0029 
0030 void SelectionTest::testAnchorConstructor()
0031 {
0032     const Selection selection(Start);
0033     QCOMPARE(selection.anchor(), Start);
0034     QVERIFY(!selection.isValid());
0035     QVERIFY(selection.started());
0036     QVERIFY(selection.justStarted());
0037 }
0038 
0039 void SelectionTest::testCopyConstructor()
0040 {
0041     const Selection otherInvalidSelection;
0042 
0043     const Selection inValidSelection(otherInvalidSelection);
0044     QVERIFY(!inValidSelection.isValid());
0045     QVERIFY(!inValidSelection.started());
0046     QVERIFY(!inValidSelection.justStarted());
0047 
0048     const Selection otherJustStartedSelection(Start);
0049 
0050     const Selection justStartedSelection(otherJustStartedSelection);
0051     QCOMPARE(justStartedSelection.anchor(), Start);
0052     QVERIFY(!justStartedSelection.isValid());
0053     QVERIFY(justStartedSelection.started());
0054     QVERIFY(justStartedSelection.justStarted());
0055 
0056     Selection otherSelection(Start);
0057     otherSelection.setEnd(End);
0058 
0059     const Selection selection(otherSelection);
0060     QCOMPARE(selection.start(), Start);
0061     QCOMPARE(selection.end(), End - 1);
0062     QCOMPARE(selection.anchor(), Start);
0063     QVERIFY(selection.isValid());
0064     QVERIFY(selection.started());
0065     QVERIFY(!selection.justStarted());
0066     QVERIFY(selection.isForward());
0067 }
0068 
0069 void SelectionTest::testCompare()
0070 {
0071     // invalid
0072     Selection selection;
0073     Selection otherSelection;
0074     QVERIFY(selection == otherSelection);
0075     QVERIFY(!(selection != otherSelection));
0076 
0077     // justStarted
0078     // same
0079     selection.setStart(Start);
0080     otherSelection.setStart(Start);
0081     QVERIFY(selection == otherSelection);
0082     QVERIFY(!(selection != otherSelection));
0083 
0084     // different start
0085     otherSelection.setStart(Start+1);
0086     QVERIFY(!(selection == otherSelection));
0087     QVERIFY(selection != otherSelection);
0088 
0089     // range
0090     // same
0091     selection.setStart(Start);
0092     selection.setEnd(End);
0093     otherSelection.setStart(Start);
0094     otherSelection.setEnd(End);
0095     QVERIFY(selection == otherSelection);
0096     QVERIFY(!(selection != otherSelection));
0097     // different start
0098     otherSelection.setStart(Start + 1);
0099     QVERIFY(!(selection == otherSelection));
0100     QVERIFY(selection != otherSelection);
0101     // different end
0102     otherSelection.setStart(Start);
0103     otherSelection.setEnd(End+1);
0104     QVERIFY(!(selection == otherSelection));
0105     QVERIFY(selection != otherSelection);
0106 }
0107 
0108 void SelectionTest::testSetStart()
0109 {
0110     Selection selection;
0111     selection.setStart(Start);
0112     QCOMPARE(selection.anchor(), Start);
0113     QVERIFY(!selection.isValid());
0114     QVERIFY(selection.started());
0115     QVERIFY(selection.justStarted());
0116 }
0117 
0118 void SelectionTest::testSetStartEnd()
0119 {
0120     Selection selection;
0121     selection.setStart(Start);
0122     selection.setEnd(End);
0123     QCOMPARE(selection.start(), Start);
0124     QCOMPARE(selection.end(), End - 1);
0125     QCOMPARE(selection.anchor(), Start);
0126     QVERIFY(selection.isValid());
0127     QVERIFY(selection.started());
0128     QVERIFY(!selection.justStarted());
0129     QVERIFY(selection.isForward());
0130 
0131     selection.setStart(End);
0132     selection.setEnd(Start);
0133     QCOMPARE(selection.start(), Start);
0134     QCOMPARE(selection.end(), End - 1);
0135     QCOMPARE(selection.anchor(), End);
0136     QVERIFY(selection.isValid());
0137     QVERIFY(selection.started());
0138     QVERIFY(!selection.justStarted());
0139     QVERIFY(!selection.isForward());
0140 }
0141 
0142 void SelectionTest::testCancel()
0143 {
0144     Selection selection;
0145     selection.setStart(Start);
0146     selection.setEnd(End);
0147     selection.cancel();
0148     QVERIFY(!selection.isValid());
0149     QVERIFY(!selection.started());
0150     QVERIFY(!selection.justStarted());
0151 }
0152 
0153 void SelectionTest::testSetForward()
0154 {
0155     Selection selection;
0156     selection.setStart(Start);
0157     selection.setEnd(End);
0158     selection.setForward(false);
0159     QCOMPARE(selection.start(), Start);
0160     QCOMPARE(selection.end(), End - 1);
0161     QCOMPARE(selection.anchor(), End);
0162     QVERIFY(selection.isValid());
0163     QVERIFY(selection.started());
0164     QVERIFY(!selection.justStarted());
0165     QVERIFY(!selection.isForward());
0166 
0167     selection.setForward(true);
0168     QCOMPARE(selection.start(), Start);
0169     QCOMPARE(selection.end(), End - 1);
0170     QCOMPARE(selection.anchor(), Start);
0171     QVERIFY(selection.isValid());
0172     QVERIFY(selection.started());
0173     QVERIFY(!selection.justStarted());
0174     QVERIFY(selection.isForward());
0175 }
0176 
0177 void SelectionTest::testReverse()
0178 {
0179     Selection selection;
0180     selection.setStart(Start);
0181     selection.setEnd(End);
0182     selection.reverse();
0183     QCOMPARE(selection.start(), Start);
0184     QCOMPARE(selection.end(), End - 1);
0185     QCOMPARE(selection.anchor(), End);
0186     QVERIFY(selection.isValid());
0187     QVERIFY(selection.started());
0188     QVERIFY(!selection.justStarted());
0189     QVERIFY(!selection.isForward());
0190 
0191     selection.reverse();
0192     QCOMPARE(selection.start(), Start);
0193     QCOMPARE(selection.end(), End - 1);
0194     QCOMPARE(selection.anchor(), Start);
0195     QVERIFY(selection.isValid());
0196     QVERIFY(selection.started());
0197     QVERIFY(!selection.justStarted());
0198     QVERIFY(selection.isForward());
0199 }
0200 
0201 }
0202 
0203 QTEST_MAIN(Okteta::SelectionTest)
0204 
0205 #include "moc_selectiontest.cpp"