aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/unit/testSelection.cxx
blob: 8c27b42e8f4c6307e24c206093f46afcf92d62fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/** @file testSelection.cxx
 ** Unit Tests for Scintilla internal data structures
 **/

#include <cstdint>

#include <stdexcept>
#include <string_view>
#include <vector>

#include "Debugging.h"

#include "Position.h"
#include "Selection.h"

#include "catch.hpp"

using namespace Scintilla;
using namespace Scintilla::Internal;

// Test Selection.

namespace {

constexpr SelectionPosition invalid;
constexpr SelectionPosition zero(0);
constexpr SelectionRange rangeInvalid;
constexpr SelectionRange rangeZero(0);

}

TEST_CASE("SelectionPosition") {

	SECTION("SelectionPosition") {
		SelectionPosition sel;
		REQUIRE(sel.Position() == Sci::invalidPosition);
		REQUIRE(sel.VirtualSpace() == 0);
		REQUIRE(!sel.IsValid());
		REQUIRE(sel.VirtualSpace() == 0);

		REQUIRE(sel == invalid);
		REQUIRE(sel != zero);
		sel.Reset();
		REQUIRE(sel != invalid);
		REQUIRE(sel == zero);
	}

	SECTION("Comparison") {
		constexpr SelectionPosition sel(2,3);
		REQUIRE(sel > invalid);
		REQUIRE(sel > zero);
		REQUIRE(sel >= zero);
		REQUIRE(zero < sel);
		REQUIRE(zero <= sel);

		SelectionPosition virtuous(0, 4);
		REQUIRE(virtuous > zero);
		REQUIRE(virtuous >= zero);
		REQUIRE(zero < virtuous);
		REQUIRE(zero <= virtuous);

		REQUIRE(virtuous.Position() == 0);
		REQUIRE(virtuous.VirtualSpace() == 4);

		virtuous.SetPosition(1);	// Also resets virtualSpace
		REQUIRE(virtuous.Position() == 1);
		REQUIRE(virtuous.VirtualSpace() == 0);
		virtuous.SetVirtualSpace(3);	// Does not reset position
		REQUIRE(virtuous.Position() == 1);
		REQUIRE(virtuous.VirtualSpace() == 3);
	}

	SECTION("Add") {
		SelectionPosition sel(2,3);
		sel.Add(1);
		REQUIRE(sel.Position() == 3);
		REQUIRE(sel.VirtualSpace() == 3);
		sel.AddVirtualSpace(2);
		REQUIRE(sel.Position() == 3);
		REQUIRE(sel.VirtualSpace() == 5);
	}

	SECTION("MoveForInsertDelete") {
		// There are multiple details implemented in MoveForInsertDelete that are supposed to
		// move selections in a way that appears to be natural to a user.

		SelectionPosition sel(2,3);
		sel.MoveForInsertDelete(true, 0,1, false);
		REQUIRE(sel == SelectionPosition(3,3));

		// Converts a virtual space to real space
		sel.MoveForInsertDelete(true, 3,1, false);
		REQUIRE(sel == SelectionPosition(4,2));

		// Deletion at position clears virtual space
		sel.MoveForInsertDelete(false, 4,1, false);
		REQUIRE(sel == SelectionPosition(4,0));

		sel.MoveForInsertDelete(false, 3,1, false);
		REQUIRE(sel == SelectionPosition(3,0));

		// Insert at position with and without move for equal
		sel.MoveForInsertDelete(true, 3, 1, false);
		REQUIRE(sel == SelectionPosition(3, 0));
		sel.MoveForInsertDelete(true, 3, 1, true);
		REQUIRE(sel == SelectionPosition(4, 0));

		// Deletion over the position moves to start of deletion
		sel.MoveForInsertDelete(false, 2, 5, false);
		REQUIRE(sel == SelectionPosition(2, 0));
	}

}

TEST_CASE("SelectionSegment") {

	SECTION("SelectionSegment") {
		SelectionSegment ss;
		REQUIRE(ss.start == invalid);
		REQUIRE(ss.end == invalid);
	}

}

TEST_CASE("SelectionRange") {

	SECTION("SelectionRange") {
		SelectionRange sr;
		REQUIRE(sr.anchor == invalid);
		REQUIRE(sr.caret == invalid);
	}

}

TEST_CASE("Selection") {

	SECTION("Selection") {
		Selection sel;
		
		REQUIRE(sel.selType == Selection::SelTypes::stream);
		REQUIRE(!sel.IsRectangular());
		REQUIRE(sel.Count() == 1);
		REQUIRE(sel.Main() == 0);

		REQUIRE(sel.Range(0) == rangeZero);
		REQUIRE(sel.RangeMain() == rangeZero);
		REQUIRE(sel.Rectangular() == rangeInvalid);
		REQUIRE(sel.Empty());
	}

}