Skip to content

Commit 391c943

Browse files
committed
Test the selection interpreter
1 parent 2872e8e commit 391c943

3 files changed

Lines changed: 102 additions & 4 deletions

File tree

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import * as assert from 'assert';
2+
import { SelectionInterpreter } from '../../../src/modelFactory/selectionInterpreter';
3+
4+
suite("SelectionInterpreter tests", () => {
5+
6+
test("allRows() splits text by windows-style line endings", () => {
7+
const text = "line1\r\nline2";
8+
const sut = createSut();
9+
10+
const rows = sut.allRows(text);
11+
12+
assert.equal(rows.length, 2);
13+
assert.equal(rows[0].length, 1);
14+
assert.equal(rows[1].length, 1);
15+
assert.equal(rows[0][0], "line1");
16+
assert.equal(rows[1][0], "line2");
17+
});
18+
19+
test("allRows() splits text by linux-style line endings", () => {
20+
const text = "foo\nbar";
21+
const sut = createSut();
22+
23+
const rows = sut.allRows(text);
24+
25+
assert.equal(rows.length, 2);
26+
assert.equal(rows[0].length, 1);
27+
assert.equal(rows[1].length, 1);
28+
assert.equal(rows[0][0], "foo");
29+
assert.equal(rows[1][0], "bar");
30+
});
31+
32+
test("allRows() splits each line by | cell separator", () => {
33+
const text = "h1|h2\nv1|v2";
34+
const sut = createSut();
35+
36+
const rows = sut.allRows(text);
37+
38+
assert.equal(rows.length, 2);
39+
assert.equal(rows[0].length, 2);
40+
assert.equal(rows[1].length, 2);
41+
assert.equal(rows[0][0], "h1");
42+
assert.equal(rows[0][1], "h2");
43+
assert.equal(rows[1][0], "v1");
44+
assert.equal(rows[1][1], "v2");
45+
});
46+
47+
test("separator() returns the first row", () => {
48+
const text = "h1|h2\r\n:-|-\r\nv1|v2";
49+
const sut = createSut();
50+
51+
const separator = sut.separator(text);
52+
53+
assert.equal(separator.length, 2);
54+
assert.equal(separator[0], ":-");
55+
assert.equal(separator[1], "-");
56+
});
57+
58+
function createSut(): SelectionInterpreter {
59+
return new SelectionInterpreter();
60+
}
61+
});

test/unitTests/modelFactory/tableFactory.test.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,26 @@ suite("TableFactory tests", () => {
101101
assert.equal(result, transformedTable);
102102
});
103103

104-
function createFactory(): TableFactory {
105-
return new TableFactory(_alignmentFactoryMock.object, new SelectionInterpreter(), _transformer.object);
104+
test("getModel() calls selection interpreter to get rows", () => {
105+
const expectedAlignmets: Alignment[] = [ Alignment.Left, Alignment.Left, Alignment.Left, Alignment.Left ];
106+
const transformedTable = new Table([["c1", "c2", "", "c4"], ["a", "b", "", "d"]].map(row => row.map(c => new Cell(c))), expectedAlignmets);
107+
_alignmentFactoryMock.setup(m => m.createAlignments(It.isAny())).returns(() => expectedAlignmets);
108+
_transformer.setup(_ => _.process(It.isAny())).returns(() => transformedTable);
109+
let selectionInterpreter: IMock<SelectionInterpreter> = Mock.ofType<SelectionInterpreter>();
110+
selectionInterpreter
111+
.setup(_ => _.allRows(It.isAny()))
112+
.returns(() => [["c1", "c2", "", "c4"], ["-","-","-","-"], ["a", "b", "", "d"]])
113+
.verifiable(Times.once());
114+
const sut = createFactory(selectionInterpreter.object);
115+
116+
sut.getModel("test");
117+
118+
selectionInterpreter.verifyAll();
119+
});
120+
121+
function createFactory(selectionInterpreter: SelectionInterpreter = null): TableFactory {
122+
return new TableFactory(_alignmentFactoryMock.object,
123+
selectionInterpreter == null ? new SelectionInterpreter() : selectionInterpreter,
124+
_transformer.object);
106125
}
107126
});

test/unitTests/modelFactory/tableValidator.test.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as assert from 'assert';
2+
import { IMock, Mock, It, Times } from 'typemoq';
23
import { Table } from "../../../src/models/table";
34
import { TableValidator } from "../../../src/modelFactory/tableValidator";
45
import { Alignment } from "../../../src/models/alignment";
@@ -140,7 +141,24 @@ suite("TableValidator tests", () => {
140141
assert.equal(isValid, true);
141142
});
142143

143-
function createSut(): TableValidator {
144-
return new TableValidator(new SelectionInterpreter());
144+
test("isValid() uses selection interpreter to get rows and separator", () => {
145+
let selectionInterpreter: IMock<SelectionInterpreter> = Mock.ofType<SelectionInterpreter>();
146+
selectionInterpreter
147+
.setup(_ => _.allRows(It.isAny()))
148+
.returns(() => [ ["a", "b"], ["-", "-"], ["c", "d"] ])
149+
.verifiable(Times.once());
150+
selectionInterpreter
151+
.setup(_ => _.separator(It.isAny()))
152+
.returns(() => ["-", "-"])
153+
.verifiable(Times.once());
154+
const sut = createSut(selectionInterpreter.object);
155+
156+
const isValid: boolean = sut.isValid("test");
157+
158+
selectionInterpreter.verifyAll();
159+
});
160+
161+
function createSut(selectionInterpreter: SelectionInterpreter = null): TableValidator {
162+
return new TableValidator(selectionInterpreter == null ? new SelectionInterpreter(): selectionInterpreter);
145163
}
146164
});

0 commit comments

Comments
 (0)