Переглянути джерело

Merge pull request #11 from csmith/lineposition

Add normalise method to line position, unit test.
pull/21/head
Greg Holmes 9 роки тому
джерело
коміт
0f87fb81e0

+ 3
- 0
src/META-INF/MANIFEST.MF Переглянути файл

@@ -0,0 +1,3 @@
1
+Manifest-Version: 1.0
2
+Main-Class: com.dmdirc.Main
3
+

+ 21
- 1
src/com/dmdirc/ui/messages/LinePosition.java Переглянути файл

@@ -136,10 +136,30 @@ public class LinePosition {
136 136
         this.startPos = startPos;
137 137
     }
138 138
 
139
+    /**
140
+     * Gets a new, normalised version of this position. Start and end lines and positions are
141
+     * normalised such that the same range of lines and positions are included, but the start
142
+     * line and position comes before the end line and position.
143
+     *
144
+     * @return A normalised copy of this position.
145
+     */
146
+    public LinePosition getNormalised() {
147
+        if (startLine > endLine) {
148
+            // Multi-line "backwards" selection; swap both lines and positions.
149
+            return new LinePosition(endLine, endPos, startLine, startPos);
150
+        } else if (startLine == endLine && startPos > endPos) {
151
+            // Single-line "backwards" selection; just swap the positions.
152
+            return new LinePosition(startLine, endPos, endLine, startPos);
153
+        } else {
154
+            // Single- or multi-line "forward" selection; swap nothing.
155
+            return new LinePosition(startLine, startPos, endLine, endPos);
156
+        }
157
+    }
158
+
139 159
     @Override
140 160
     public String toString() {
141 161
         return "Position[" + startLine + ", " + startPos + ", " + endLine
142
-                + ", " + endPos + "]";
162
+                + ", " + endPos + ']';
143 163
     }
144 164
 
145 165
 }

+ 89
- 0
test/com/dmdirc/ui/messages/LinePositionTest.java Переглянути файл

@@ -0,0 +1,89 @@
1
+/*
2
+ * Copyright (c) 2006-2014 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.ui.messages;
24
+
25
+import org.junit.Test;
26
+
27
+import static org.junit.Assert.assertEquals;
28
+
29
+public class LinePositionTest {
30
+
31
+    @Test
32
+    public void testNormaliseWithForwardMultiLineSelection() {
33
+        final LinePosition input = new LinePosition(2, 5, 4, 1);
34
+        assertEquals(input.toString(), input.getNormalised().toString());
35
+    }
36
+
37
+    @Test
38
+    public void testNormaliseWithForwardSingleLineSelection() {
39
+        final LinePosition input = new LinePosition(2, 5, 2, 8);
40
+        assertEquals(input.toString(), input.getNormalised().toString());
41
+    }
42
+
43
+    @Test
44
+    public void testNormaliseWithBackwardMultiLineSelection() {
45
+        final LinePosition input = new LinePosition(5, 8, 2, 5);
46
+        final LinePosition expected = new LinePosition(2, 5, 5, 8);
47
+        assertEquals(expected.toString(), input.getNormalised().toString());
48
+    }
49
+
50
+    @Test
51
+    public void testNormaliseWithBackwardSingleLineSelection() {
52
+        final LinePosition input = new LinePosition(2, 8, 2, 5);
53
+        final LinePosition expected = new LinePosition(2, 5, 2, 8);
54
+        assertEquals(expected.toString(), input.getNormalised().toString());
55
+    }
56
+
57
+    @Test
58
+    public void testCloneConstructor() {
59
+        final LinePosition input = new LinePosition(2, 4, 6, 8);
60
+        assertEquals(input.toString(), new LinePosition(input).toString());
61
+    }
62
+
63
+    @Test
64
+    public void testGetters() {
65
+        final LinePosition input = new LinePosition(2, 4, 6, 8);
66
+        assertEquals(2, input.getStartLine());
67
+        assertEquals(4, input.getStartPos());
68
+        assertEquals(6, input.getEndLine());
69
+        assertEquals(8, input.getEndPos());
70
+    }
71
+
72
+    @Test
73
+    public void testSetters() {
74
+        final LinePosition input = new LinePosition(2, 4, 6, 8);
75
+
76
+        input.setStartLine(17);
77
+        assertEquals(17, input.getStartLine());
78
+        input.setStartPos(27);
79
+        assertEquals(27, input.getStartPos());
80
+        input.setEndLine(13);
81
+        assertEquals(13, input.getEndLine());
82
+        input.setEndPos(786);
83
+        assertEquals(786, input.getEndPos());
84
+
85
+        final LinePosition expected = new LinePosition(17, 27, 13, 786);
86
+        assertEquals(expected.toString(), input.toString());
87
+    }
88
+
89
+}

Завантаження…
Відмінити
Зберегти