Skip to content

Commit 29c4bf9

Browse files
committed
Pass the alignment marker strategy to the RowViewModelFactory
1 parent bc9aa61 commit 29c4bf9

6 files changed

Lines changed: 31 additions & 44 deletions

File tree

src/extension/extension.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { BorderTransformer } from '../modelFactory/transformers/borderTransforme
1515
import { SelectionInterpreter } from '../modelFactory/selectionInterpreter';
1616
import { SeparatorPadCalculator } from '../padCalculation/separatorPadCalculator';
1717
import { PadCalculatorSelector } from '../padCalculation/padCalculatorSelector';
18+
import { AlignmentMarkerStrategy } from '../viewModelFactories/alignmentMarking';
1819

1920
// This method is called when the extension is activated.
2021
// The extension is activated the very first time the command is executed.
@@ -32,7 +33,8 @@ export function activate(context: vscode.ExtensionContext): void {
3233
new TableViewModelFactory(
3334
new RowViewModelFactory(
3435
new ContentPadCalculator(new PadCalculatorSelector(), " "),
35-
new SeparatorPadCalculator(new PadCalculatorSelector(), "-")
36+
new SeparatorPadCalculator(new PadCalculatorSelector(), "-"),
37+
new AlignmentMarkerStrategy()
3638
)
3739
),
3840
new TableStringWriter(),

src/viewModelFactories/alignmentMarking/alignmentMarkerStrategy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { IAlignmentMarker, LeftAlignmentMarker, RightAlignmentMarker, CenterAlig
44

55
export class AlignmentMarkerStrategy {
66

7-
public marker(alignment: Alignment): IAlignmentMarker {
7+
public markerFor(alignment: Alignment): IAlignmentMarker {
88
switch (alignment) {
99
case Alignment.Left: return new LeftAlignmentMarker();
1010
case Alignment.Right: return new RightAlignmentMarker();

src/viewModelFactories/rowViewModelFactory.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ export class RowViewModelFactory {
88

99
constructor(
1010
private _contentPadCalculator: PadCalculator,
11-
private _separatorPadCalculator: PadCalculator)
11+
private _separatorPadCalculator: PadCalculator,
12+
private _alignmentMarkerStrategy: AlignmentMarkerStrategy)
1213
{ }
1314

1415
public buildRow(row: number, table: Table): RowViewModel {
@@ -28,10 +29,8 @@ export class RowViewModelFactory {
2829
public buildSeparator(table: Table): RowViewModel {
2930
let resultRow = new Array(table.columnCount);
3031

31-
//TODO: extract this as constructor param
32-
let alignmentMarkerSelector = new AlignmentMarkerStrategy();
3332
for(let col = 0; col < table.columnCount; col++)
34-
resultRow[col] = alignmentMarkerSelector.marker(table.alignments[col]).mark(
33+
resultRow[col] = this._alignmentMarkerStrategy.markerFor(table.alignments[col]).mark(
3534
this._separatorPadCalculator.getLeftPadding(table, 1, col) +
3635
this._separatorPadCalculator.getRightPadding(table, 0, col)
3736
);

test/systemTests/tableRangePrettyfierFactory.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { BorderTransformer } from '../../src/modelFactory/transformers/borderTra
1919
import { SelectionInterpreter } from '../../src/modelFactory/selectionInterpreter';
2020
import { SeparatorPadCalculator } from '../../src/padCalculation/separatorPadCalculator';
2121
import { PadCalculatorSelector } from '../../src/padCalculation/padCalculatorSelector';
22+
import { AlignmentMarkerStrategy } from '../../src/viewModelFactories/alignmentMarking';
2223

2324
export class PrettyfierFromFile {
2425
private readonly _logger: ILogger;
@@ -62,7 +63,8 @@ export class PrettyfierFromFile {
6263
new TableValidator(new SelectionInterpreter()),
6364
new TableViewModelFactory(new RowViewModelFactory(
6465
new ContentPadCalculator(new PadCalculatorSelector(), " "),
65-
new SeparatorPadCalculator(new PadCalculatorSelector(), "-")
66+
new SeparatorPadCalculator(new PadCalculatorSelector(), "-"),
67+
new AlignmentMarkerStrategy()
6668
)),
6769
new TableStringWriter(),
6870
[ this._logger ]

test/unitTests/viewModelFactories/alignmentMarking/alignmentMarkerStrategy.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import { Alignment } from '../../../../src/models/alignment';
44

55
suite("AlignmentMarkerStrategy tests", () => {
66

7-
test("marker() returns a different marker strategy for the different alignemnts", () => {
7+
test("markerFor() returns a different marker strategy for the different alignemnts", () => {
88
const sut = new AlignmentMarkerStrategy();
99
const alignments = [ Alignment.NotSet, Alignment.Left, Alignment.Center, Alignment.Right ];
1010

11-
const markers: IAlignmentMarker[] = alignments.map(sut.marker);
11+
const markers: IAlignmentMarker[] = alignments.map(sut.markerFor);
1212

1313
const distinctMarkers = markers.filter((v, i, a) => a.indexOf(v) == i);
1414

test/unitTests/viewModelFactories/rowViewModelFactory.test.ts

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { RowViewModelFactory } from '../../../src/viewModelFactories/rowViewMode
66
import { Table } from '../../../src/models/table';
77
import { Alignment } from '../../../src/models/alignment';
88
import { Cell } from '../../../src/models/cell';
9+
import { AlignmentMarkerStrategy, IAlignmentMarker } from '../../../src/viewModelFactories/alignmentMarking';
910

1011
suite("RowViewModelFactory.buildRow() tests", () => {
1112
let _contentPadCalculator: IMock<PadCalculator>;
@@ -144,43 +145,23 @@ suite("RowViewModelFactory.buildSeparator() tests", () => {
144145
assert.equal(separatorRowViewModel.getValueAt(col), "----");
145146
});
146147

147-
test("Left aligned column separator starts with :", () => {
148-
const sut = createFactory(_contentPadCalculator.object, _separatorPadCalculator.object);
149-
const table = threeColumnTable(Alignment.Left);
150-
151-
_separatorPadCalculator.setup(_ => _.getLeftPadding(It.isAny(), It.isAny(), It.isAny())).returns(() => "--");
152-
_separatorPadCalculator.setup(_ => _.getRightPadding(It.isAny(), It.isAny(), It.isAny())).returns(() => "--");
153-
154-
const separatorRowViewModel = sut.buildSeparator(table);
155-
156-
for (let col = 0; col < separatorRowViewModel.columnCount; col++)
157-
assert.equal(separatorRowViewModel.getValueAt(col), ":---");
158-
});
159-
160-
test("Right aligned column separator ends with :", () => {
161-
const sut = createFactory(_contentPadCalculator.object, _separatorPadCalculator.object);
162-
const table = threeColumnTable(Alignment.Right);
163-
164-
_separatorPadCalculator.setup(_ => _.getLeftPadding(It.isAny(), It.isAny(), It.isAny())).returns(() => "--");
165-
_separatorPadCalculator.setup(_ => _.getRightPadding(It.isAny(), It.isAny(), It.isAny())).returns(() => "--");
166-
167-
const separatorRowViewModel = sut.buildSeparator(table);
168-
169-
for (let col = 0; col < separatorRowViewModel.columnCount; col++)
170-
assert.equal(separatorRowViewModel.getValueAt(col), "---:");
171-
});
148+
test("Calls alignment marker strategy and the marker returned from it", () => {
149+
const alignment = Alignment.Left;
150+
let marker = Mock.ofType<IAlignmentMarker>();
151+
marker.setup(_ => _.mark("abcd")).returns((param) => param).verifiable(Times.exactly(3));
152+
let alignmentMarkerStrategy = Mock.ofType<AlignmentMarkerStrategy>();
153+
alignmentMarkerStrategy.setup(_ => _.markerFor(alignment)).returns(() => marker.object).verifiable(Times.exactly(3));
172154

173-
test("Centrally aligned column separator starts and ends with :", () => {
174-
const sut = createFactory(_contentPadCalculator.object, _separatorPadCalculator.object);
175-
const table = threeColumnTable(Alignment.Center);
155+
const sut = createFactory(_contentPadCalculator.object, _separatorPadCalculator.object, alignmentMarkerStrategy.object);
156+
const table = threeColumnTable(alignment);
176157

177-
_separatorPadCalculator.setup(_ => _.getLeftPadding(It.isAny(), It.isAny(), It.isAny())).returns(() => "--");
178-
_separatorPadCalculator.setup(_ => _.getRightPadding(It.isAny(), It.isAny(), It.isAny())).returns(() => "--");
158+
_separatorPadCalculator.setup(_ => _.getLeftPadding(It.isAny(), It.isAny(), It.isAny())).returns(() => "ab");
159+
_separatorPadCalculator.setup(_ => _.getRightPadding(It.isAny(), It.isAny(), It.isAny())).returns(() => "cd");
179160

180-
const separatorRowViewModel = sut.buildSeparator(table);
161+
sut.buildSeparator(table);
181162

182-
for (let col = 0; col < separatorRowViewModel.columnCount; col++)
183-
assert.equal(separatorRowViewModel.getValueAt(col), ":--:");
163+
marker.verifyAll();
164+
alignmentMarkerStrategy.verifyAll();
184165
});
185166
});
186167

@@ -204,6 +185,9 @@ function tableFor(rows: string[][], alignment: Alignment) {
204185
return table;
205186
}
206187

207-
function createFactory(contentPadCalculator: PadCalculator, separatorPadCalculator: PadCalculator): RowViewModelFactory {
208-
return new RowViewModelFactory(contentPadCalculator, separatorPadCalculator);
188+
function createFactory(contentPadCalculator: PadCalculator, separatorPadCalculator: PadCalculator,
189+
alignmentStrategy: AlignmentMarkerStrategy = null): RowViewModelFactory
190+
{
191+
alignmentStrategy = alignmentStrategy == null ? new AlignmentMarkerStrategy() : alignmentStrategy;
192+
return new RowViewModelFactory(contentPadCalculator, separatorPadCalculator, alignmentStrategy);
209193
}

0 commit comments

Comments
 (0)