Skip to content
Browse files

Ranges should know what they include.

  • Loading branch information...
1 parent d5d5aae commit 48593a581075dd87499ebd5d120227aa1a4bf593 @georgebrock georgebrock committed Dec 17, 2012
Showing with 69 additions and 0 deletions.
  1. +15 −0 js/range.js
  2. +54 −0 tests/unit/range.js
View
15 js/range.js
@@ -56,6 +56,21 @@
return this.originalEnd.col === buffer.lines[this.originalEnd.row].length - 1;
};
+ Vimulator.CharacterRange.prototype.contains = function (position) {
+ var r = position.row,
+ c = position.col,
+ afterStart, beforeEnd;
+
+ afterStart = this.start.row < r ||
+ this.start.row === r && this.start.col <= c;
+
+ beforeEnd = this.end.row > r ||
+ this.end.row === r &&
+ (this.end.col > c || this.inclusive && this.end.col === c);
+
+ return afterStart && beforeEnd;
+ };
+
Vimulator.LineRange = function (start, end) {
assignOrdered(this, start, end);
View
54 tests/unit/range.js
@@ -138,6 +138,33 @@ describe("CharacterRange set to inclusive", function () {
]);
});
});
+
+ describe(".contains", function () {
+ beforeEach(function () {
+ range = new Vimulator.CharacterRange(
+ {row: 0, col: 7},
+ {row: 2, col: 4},
+ {inclusive: true}
+ );
+ });
+
+ it("returns true for the start character", function () {
+ expect(range.contains({row: 0, col: 7})).toBe(true);
+ });
+
+ it("returns true for the end character", function () {
+ expect(range.contains({row: 2, col: 4})).toBe(true);
+ });
+
+ it("returns true in the middle of the range", function () {
+ expect(range.contains({row: 1, col: 5})).toBe(true);
+ });
+
+ it("returns false for a character outside the range", function () {
+ expect(range.contains({row: 0, col: 4})).toBe(false);
+ expect(range.contains({row: 2, col: 6})).toBe(false);
+ });
+ });
});
describe("CharacterRange set to exclusive", function () {
@@ -280,6 +307,33 @@ describe("CharacterRange set to exclusive", function () {
]);
});
});
+
+ describe(".contains", function () {
+ beforeEach(function () {
+ range = new Vimulator.CharacterRange(
+ {row: 0, col: 7},
+ {row: 2, col: 4},
+ {inclusive: false}
+ );
+ });
+
+ it("returns true for the start character", function () {
+ expect(range.contains({row: 0, col: 7})).toBe(true);
+ });
+
+ it("returns false for the end character", function () {
+ expect(range.contains({row: 2, col: 4})).toBe(false);
+ });
+
+ it("returns true in the middle of the range", function () {
+ expect(range.contains({row: 1, col: 5})).toBe(true);
+ });
+
+ it("returns false for a character outside the range", function () {
+ expect(range.contains({row: 0, col: 4})).toBe(false);
+ expect(range.contains({row: 2, col: 6})).toBe(false);
+ });
+ });
});
describe("LineRange", function () {

0 comments on commit 48593a5

Please sign in to comment.
Something went wrong with that request. Please try again.