Skip to content
Browse files

Inclusive character ranges.

  • Loading branch information...
1 parent b0e2d18 commit b0f6603281ab1d0343b95ad4f0c3a789017b69b7 @georgebrock georgebrock committed Dec 9, 2012
Showing with 175 additions and 0 deletions.
  1. +24 −0 js/range.js
  2. +2 −0 test.html
  3. +149 −0 tests/unit/range.js
View
24 js/range.js
@@ -0,0 +1,24 @@
+(function () {
+ Vimulator.InclusiveCharacterRange = function (start, end) {
+ if (start.row < end.row || start.row == end.row && start.col < end.col) {
+ this.start = start;
+ this.end = end;
+ } else {
+ this.start = end;
+ this.end = start;
+ }
+ };
+
+ Vimulator.InclusiveCharacterRange.prototype.removeFrom = function (buffer) {
+ this.replaceIn(buffer, "");
+ };
+
+ Vimulator.InclusiveCharacterRange.prototype.replaceIn = function (buffer, str) {
+ buffer.lines[this.start.row] =
+ buffer.lines[this.start.row].substr(0, this.start.col) +
+ str +
+ buffer.lines[this.end.row].substr(this.end.col + 1);
+
+ buffer.lines.splice(this.start.row + 1, this.end.row - this.start.row);
+ };
+}());
View
2 test.html
@@ -19,6 +19,7 @@
<!-- Code under test -->
<script src='js/base.js'></script>
<script src='js/utils.js'></script>
+ <script src='js/range.js'></script>
<script src='js/operation.js'></script>
<script src='js/command.js'></script>
<script src='js/command_list.js'></script>
@@ -42,6 +43,7 @@
<!-- Test cases -->
<script src='tests/unit/base.js'></script>
+ <script src='tests/unit/range.js'></script>
<script src='tests/unit/operation.js'></script>
<script src='tests/unit/command.js'></script>
<script src='tests/unit/command_list.js'></script>
View
149 tests/unit/range.js
@@ -0,0 +1,149 @@
+describe("InclusiveCharacterRange", function () {
+ var buffer, range;
+
+ beforeEach(function () {
+ buffer = {
+ lines: [
+ "The first line",
+ "The second line",
+ "The third line"
+ ]
+ };
+ });
+
+ describe(".removeFrom", function () {
+ it("can remove characters in a single line", function () {
+ range = new Vimulator.InclusiveCharacterRange(
+ {row: 0, col: 4},
+ {row: 0, col: 9}
+ );
+
+ range.removeFrom(buffer);
+
+ expect(buffer.lines).toEqual([
+ "The line",
+ "The second line",
+ "The third line"
+ ]);
+ });
+
+ it("can remove chracters over multiple lines", function () {
+ range = new Vimulator.InclusiveCharacterRange(
+ {row: 0, col: 7},
+ {row: 2, col: 4}
+ );
+
+ range.removeFrom(buffer);
+
+ expect(buffer.lines).toEqual([
+ "The firhird line"
+ ]);
+ });
+
+ it("can remove characters to the end of a line", function () {
+ range = new Vimulator.InclusiveCharacterRange(
+ {row: 0, col: 3},
+ {row: 0, col: 13}
+ );
+
+ range.removeFrom(buffer);
+
+ expect(buffer.lines).toEqual([
+ "The",
+ "The second line",
+ "The third line"
+ ]);
+ });
+
+ it("can accept a start position before the end position", function () {
+ range = new Vimulator.InclusiveCharacterRange(
+ {row: 0, col: 9},
+ {row: 0, col: 4}
+ );
+
+ range.removeFrom(buffer);
+
+ expect(buffer.lines).toEqual([
+ "The line",
+ "The second line",
+ "The third line"
+ ]);
+ });
+ });
+
+ describe(".replaceIn", function () {
+ it("can replace characters in a single line", function () {
+ range = new Vimulator.InclusiveCharacterRange(
+ {row: 0, col: 4},
+ {row: 0, col: 9}
+ );
+
+ range.replaceIn(buffer, "l-l-");
+
+ expect(buffer.lines).toEqual([
+ "The l-l-line",
+ "The second line",
+ "The third line"
+ ]);
+ });
+
+ it("can replace chracters over multiple lines", function () {
+ range = new Vimulator.InclusiveCharacterRange(
+ {row: 0, col: 7},
+ {row: 2, col: 4}
+ );
+
+ range.replaceIn(buffer, "-");
+
+ expect(buffer.lines).toEqual([
+ "The fir-hird line"
+ ]);
+ });
+
+ it("can replace characters to the end of a line", function () {
+ range = new Vimulator.InclusiveCharacterRange(
+ {row: 0, col: 3},
+ {row: 0, col: 13}
+ );
+
+ range.replaceIn(buffer, "...");
+
+ expect(buffer.lines).toEqual([
+ "The...",
+ "The second line",
+ "The third line"
+ ]);
+ });
+
+ it("can accept a start position before the end position", function () {
+ range = new Vimulator.InclusiveCharacterRange(
+ {row: 0, col: 9},
+ {row: 0, col: 4}
+ );
+
+ range.replaceIn(buffer, "bee-");
+
+ expect(buffer.lines).toEqual([
+ "The bee-line",
+ "The second line",
+ "The third line"
+ ]);
+ });
+ });
+});
+
+describe("ExclusiveCharacterRange", function () {
+ describe(".removeFrom", function () {
+ });
+
+ describe(".replace", function () {
+ });
+});
+
+describe("InclusiveLineRange", function () {
+ describe(".removeFrom", function () {
+ });
+
+ describe(".replace", function () {
+ });
+});

0 comments on commit b0f6603

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