Skip to content

Commit 1d0c4e3

Browse files
javier-godoypaodb
authored andcommitted
test: refactor Position class
1 parent c2dc6c5 commit 1d0c4e3

File tree

4 files changed

+31
-26
lines changed

4 files changed

+31
-26
lines changed

src/test/java/com/flowingcode/vaadin/addons/xterm/integration/ConsoleFeatureIT.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
*/
2020
package com.flowingcode.vaadin.addons.xterm.integration;
2121

22-
import static com.flowingcode.vaadin.addons.xterm.integration.Position.at;
2322
import static org.hamcrest.Matchers.is;
2423
import static org.hamcrest.Matchers.isEmptyString;
2524
import static org.junit.Assert.assertThat;
@@ -33,31 +32,31 @@ public class ConsoleFeatureIT extends AbstractViewTest {
3332
public void testFeature() throws InterruptedException {
3433
XTermElement term = $(XTermElement.class).first();
3534

36-
int y = term.cursorPosition().y;
35+
Position pos = term.cursorPosition();
3736

3837
term.sendKeys("HELLO");
3938
assertThat(term.currentLine(), is("HELLO"));
40-
assertThat(term.cursorPosition(), is(at(5, y)));
39+
assertThat(term.cursorPosition(), is(pos.plus(5, 0)));
4140

4241
term.sendKeys(Keys.ARROW_LEFT);
43-
assertThat(term.cursorPosition(), is(at(4, y)));
42+
assertThat(term.cursorPosition(), is(pos.plus(4, 0)));
4443

4544
term.sendKeys(Keys.ARROW_RIGHT);
46-
assertThat(term.cursorPosition(), is(at(5, y)));
45+
assertThat(term.cursorPosition(), is(pos.plus(5, 0)));
4746

4847
term.sendKeys(Keys.HOME);
49-
assertThat(term.cursorPosition(), is(at(0, y)));
48+
assertThat(term.cursorPosition(), is(pos.plus(0, 0)));
5049

5150
term.sendKeys(Keys.END);
52-
assertThat(term.cursorPosition(), is(at(5, y)));
51+
assertThat(term.cursorPosition(), is(pos.plus(5, 0)));
5352

5453
term.sendKeys(Keys.BACK_SPACE);
5554
assertThat(term.currentLine(), is("HELL"));
56-
assertThat(term.cursorPosition(), is(at(4, y)));
55+
assertThat(term.cursorPosition(), is(pos.plus(4, 0)));
5756

5857
term.sendKeys(Keys.HOME, Keys.DELETE);
5958
assertThat(term.currentLine(), is("ELL"));
60-
assertThat(term.cursorPosition(), is(at(0, y)));
59+
assertThat(term.cursorPosition(), is(pos.plus(0, 0)));
6160

6261
term.sendKeys("A");
6362
assertThat(term.currentLine(), is("AELL"));
@@ -75,11 +74,11 @@ public void testFeature() throws InterruptedException {
7574

7675
term.sendKeys("\n");
7776
assertThat(term.currentLine(), is(""));
78-
assertThat(term.cursorPosition(), is(at(0, ++y)));
77+
assertThat(term.cursorPosition(), is(pos.advance(0, 1)));
7978

8079
term.sendKeys(text);
8180
term.sendKeys(Keys.HOME);
82-
assertThat(term.cursorPosition(), is(at(0, y)));
81+
assertThat(term.cursorPosition(), is(pos));
8382
assertThat(term.currentLine(), is(text));
8483

8584
term.sendKeys("A");
@@ -91,10 +90,10 @@ public void testFeature() throws InterruptedException {
9190
assertThat(term.lineAtOffset(+1), is(text.substring(cols - 2)));
9291

9392
term.sendKeys(Keys.END);
94-
assertThat(term.cursorPosition(), is(at(2, y + 1)));
93+
assertThat(term.cursorPosition(), is(pos.plus(2, 1)));
9594

9695
term.sendKeys(Keys.HOME);
97-
assertThat(term.cursorPosition(), is(at(0, y)));
96+
assertThat(term.cursorPosition(), is(pos));
9897

9998
term.sendKeys(Keys.DELETE);
10099
assertThat(term.currentLine(), is("B" + text.substring(0, cols - 1)));

src/test/java/com/flowingcode/vaadin/addons/xterm/integration/Position.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,16 @@
2929
class Position {
3030
int x, y;
3131

32-
static Position at(int x, int y) {
33-
return new Position(x, y);
32+
/**Increments the position by <code>dx,dy</code>.*/
33+
public Position advance(int dx, int dy) {
34+
this.x+=dx;
35+
this.y+=dy;
36+
return this;
3437
}
3538

39+
/**Return a new position that is equal to this position plus <code>dx,dy</code>.*/
40+
public Position plus(int dx, int dy) {
41+
return new Position(this.x+dx, this.y+dy);
42+
}
43+
3644
}

src/test/java/com/flowingcode/vaadin/addons/xterm/integration/TerminalHistoryIT.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.flowingcode.vaadin.addons.xterm.integration;
22

3-
import static com.flowingcode.vaadin.addons.xterm.integration.Position.at;
43
import static org.hamcrest.Matchers.is;
54
import static org.hamcrest.Matchers.isEmptyString;
65
import static org.junit.Assert.assertThat;
@@ -13,10 +12,10 @@ public class TerminalHistoryIT extends AbstractViewTest {
1312
public void testArrowKeys() {
1413
XTermElement term = $(XTermElement.class).first();
1514

16-
int y = term.cursorPosition().y;
15+
Position pos = term.cursorPosition();
1716
term.sendKeys("foo1\nfoo2\n");
1817

19-
assertThat(term.cursorPosition(), is(at(0, y += 2)));
18+
assertThat(term.cursorPosition(), is(pos.advance(0, 2)));
2019
assertThat(term.lineAtOffset(0), isEmptyString());
2120

2221
term.sendKeys(Keys.ARROW_UP);
@@ -39,10 +38,10 @@ public void testArrowKeys() {
3938
public void testArrowKeysAndRestore() {
4039
XTermElement term = $(XTermElement.class).first();
4140

42-
int y = term.cursorPosition().y;
41+
Position pos = term.cursorPosition();
4342
term.sendKeys("foo1\nfoo2\n");
4443

45-
assertThat(term.cursorPosition(), is(at(0, y += 2)));
44+
assertThat(term.cursorPosition(), is(pos.advance(0, 2)));
4645
assertThat(term.lineAtOffset(0), isEmptyString());
4746

4847
term.sendKeys("bar");

src/test/java/com/flowingcode/vaadin/addons/xterm/integration/XTermIT.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
*/
2020
package com.flowingcode.vaadin.addons.xterm.integration;
2121

22-
import static com.flowingcode.vaadin.addons.xterm.integration.Position.at;
2322
import static org.hamcrest.Matchers.is;
2423
import static org.hamcrest.Matchers.not;
2524
import static org.junit.Assert.assertThat;
@@ -68,22 +67,22 @@ public void componentWorks() {
6867
public void writeText() throws InterruptedException {
6968
XTermElement term = $(XTermElement.class).first();
7069

71-
int y = term.cursorPosition().y;
70+
Position pos = term.cursorPosition();
7271

7372
term.sendKeys("HELLO");
7473
assertThat(term.currentLine(), is("HELLO"));
75-
assertThat(term.cursorPosition(), is(at(5, y)));
74+
assertThat(term.cursorPosition(), is(pos.plus(5, 0)));
7675

7776
term.sendKeys("HELLO");
7877
assertThat(term.currentLine(), is("HELLOHELLO"));
79-
assertThat(term.cursorPosition(), is(at(10, y)));
78+
assertThat(term.cursorPosition(), is(pos.plus(10, 0)));
8079

8180
term.sendKeys("\n");
8281
assertThat(term.currentLine(), is(""));
83-
assertThat(term.cursorPosition(), is(at(0, ++y)));
82+
assertThat(term.cursorPosition(), is(pos.advance(0, 1)));
8483

8584
term.sendKeys("HELLO\nWORLD");
8685
assertThat(term.currentLine(), is("WORLD"));
87-
assertThat(term.cursorPosition(), is(at(5, ++y)));
86+
assertThat(term.cursorPosition(), is(pos.advance(0, 1).plus(5, 0)));
8887
}
8988
}

0 commit comments

Comments
 (0)