Skip to content

Commit e5dcc58

Browse files
committed
Add unit tests for the table indentation detector
1 parent 4054944 commit e5dcc58

1 file changed

Lines changed: 330 additions & 0 deletions

File tree

Lines changed: 330 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,330 @@
1+
import * as assert from 'assert';
2+
import { FairTableIndentationDetector } from "../../../src/modelFactory/tableIndentationDetector";
3+
4+
suite("FairTableIndentationDetector tests", () => {
5+
6+
// hasIndentation() method tests
7+
test("hasIndentation() with no indented lines returns false", () => {
8+
const leftPadsPerLine: string[] = ["", "", ""];
9+
const sut = createDetector();
10+
11+
const result = sut.testHasIndentation(leftPadsPerLine);
12+
13+
assert.strictEqual(result, false);
14+
});
15+
16+
test("hasIndentation() with all lines indented returns true", () => {
17+
const leftPadsPerLine: string[] = [" ", " ", "\t"];
18+
const sut = createDetector();
19+
20+
const result = sut.testHasIndentation(leftPadsPerLine);
21+
22+
assert.strictEqual(result, true);
23+
});
24+
25+
test("hasIndentation() with majority indented lines returns true", () => {
26+
const leftPadsPerLine: string[] = [" ", "", " ", "\t"];
27+
const sut = createDetector();
28+
29+
const result = sut.testHasIndentation(leftPadsPerLine);
30+
31+
assert.strictEqual(result, true);
32+
});
33+
34+
test("hasIndentation() with minority indented lines returns false", () => {
35+
const leftPadsPerLine: string[] = [" ", "", "", ""];
36+
const sut = createDetector();
37+
38+
const result = sut.testHasIndentation(leftPadsPerLine);
39+
40+
assert.strictEqual(result, false);
41+
});
42+
43+
test("hasIndentation() with exactly half indented lines returns true", () => {
44+
const leftPadsPerLine: string[] = [" ", " ", "", ""];
45+
const sut = createDetector();
46+
47+
const result = sut.testHasIndentation(leftPadsPerLine);
48+
49+
assert.strictEqual(result, true);
50+
});
51+
52+
test("hasIndentation() with single line no indentation returns false", () => {
53+
const leftPadsPerLine: string[] = [""];
54+
const sut = createDetector();
55+
56+
const result = sut.testHasIndentation(leftPadsPerLine);
57+
58+
assert.strictEqual(result, false);
59+
});
60+
61+
test("hasIndentation() with single line with indentation returns true", () => {
62+
const leftPadsPerLine: string[] = [" "];
63+
const sut = createDetector();
64+
65+
const result = sut.testHasIndentation(leftPadsPerLine);
66+
67+
assert.strictEqual(result, true);
68+
});
69+
70+
test("hasIndentation() with two lines one indented returns true", () => {
71+
const leftPadsPerLine: string[] = [" ", ""];
72+
const sut = createDetector();
73+
74+
const result = sut.testHasIndentation(leftPadsPerLine);
75+
76+
assert.strictEqual(result, true);
77+
});
78+
79+
test("hasIndentation() with empty array returns true", () => {
80+
const leftPadsPerLine: string[] = [];
81+
const sut = createDetector();
82+
83+
const result = sut.testHasIndentation(leftPadsPerLine);
84+
85+
assert.strictEqual(result, true);
86+
});
87+
88+
// getLeftPad() method tests
89+
90+
test("getLeftPad() with no indentation returns empty string", () => {
91+
const lines: string[] = [
92+
"| col1 | col2 |",
93+
"|------|------|",
94+
"| a | b |"
95+
];
96+
const sut = createDetector();
97+
98+
const result = sut.getLeftPad(lines);
99+
100+
assert.strictEqual(result, "");
101+
});
102+
103+
test("getLeftPad() with consistent space indentation returns indentation", () => {
104+
const lines: string[] = [
105+
" | col1 | col2 |",
106+
" |------|------|",
107+
" | a | b |"
108+
];
109+
const sut = createDetector();
110+
111+
const result = sut.getLeftPad(lines);
112+
113+
assert.strictEqual(result, " ");
114+
});
115+
116+
test("getLeftPad() with consistent tab indentation returns tab indentation", () => {
117+
const lines: string[] = [
118+
"\t\t| col1 | col2 |",
119+
"\t\t|------|------|",
120+
"\t\t| a | b |"
121+
];
122+
const sut = createDetector();
123+
124+
const result = sut.getLeftPad(lines);
125+
126+
assert.strictEqual(result, "\t\t");
127+
});
128+
129+
test("getLeftPad() with mixed tab and space indentation prefers tab prefix", () => {
130+
const lines: string[] = [
131+
"\t\t | col1 | col2 |",
132+
"\t\t |------|------|",
133+
"\t\t | a | b |"
134+
];
135+
const sut = createDetector();
136+
137+
const result = sut.getLeftPad(lines);
138+
139+
assert.strictEqual(result, "\t\t");
140+
});
141+
142+
test("getLeftPad() with partial tab indentation uses longest common tab prefix", () => {
143+
const lines: string[] = [
144+
"\t\t\t| col1 | col2 |",
145+
"\t\t|------|------|",
146+
"\t\t\t\t| a | b |"
147+
];
148+
const sut = createDetector();
149+
150+
const result = sut.getLeftPad(lines);
151+
152+
assert.strictEqual(result, "\t\t");
153+
});
154+
155+
test("getLeftPad() with majority indented lines uses most common indentation", () => {
156+
const lines: string[] = [
157+
" | col1 | col2 |",
158+
"|------|------|",
159+
" | a | b |",
160+
" | c | d |"
161+
];
162+
const sut = createDetector();
163+
164+
const result = sut.getLeftPad(lines);
165+
166+
assert.strictEqual(result, " ");
167+
});
168+
169+
test("getLeftPad() with minority indented lines returns empty string", () => {
170+
const lines: string[] = [
171+
" | col1 | col2 |",
172+
"|------|------|",
173+
"| a | b |",
174+
"| c | d |"
175+
];
176+
const sut = createDetector();
177+
178+
const result = sut.getLeftPad(lines);
179+
180+
assert.strictEqual(result, "");
181+
});
182+
183+
test("getLeftPad() with exactly half indented lines uses indentation", () => {
184+
const lines: string[] = [
185+
" | col1 | col2 |",
186+
" |------|------|",
187+
"| a | b |",
188+
"| c | d |"
189+
];
190+
const sut = createDetector();
191+
192+
const result = sut.getLeftPad(lines);
193+
194+
assert.strictEqual(result, " ");
195+
});
196+
197+
test("getLeftPad() with different space indentations uses most frequent", () => {
198+
const lines: string[] = [
199+
" | col1 | col2 |",
200+
" |------|------|",
201+
" | a | b |",
202+
" | c | d |"
203+
];
204+
const sut = createDetector();
205+
206+
const result = sut.getLeftPad(lines);
207+
208+
assert.strictEqual(result, " ");
209+
});
210+
211+
test("getLeftPad() with equal frequency indentations uses first occurrence", () => {
212+
const lines: string[] = [
213+
" | col1 | col2 |",
214+
" |------|------|",
215+
" | a | b |",
216+
" | c | d |"
217+
];
218+
const sut = createDetector();
219+
220+
const result = sut.getLeftPad(lines);
221+
222+
assert.strictEqual(result, " ");
223+
});
224+
225+
test("getLeftPad() with single unique indentation uses first line indentation", () => {
226+
const lines: string[] = [
227+
" | col1 | col2 |",
228+
" |------|------|",
229+
" | a | b |"
230+
];
231+
const sut = createDetector();
232+
233+
const result = sut.getLeftPad(lines);
234+
235+
assert.strictEqual(result, " ");
236+
});
237+
238+
test("getLeftPad() with empty lines ignores them", () => {
239+
const lines: string[] = [
240+
" | col1 | col2 |",
241+
"",
242+
" |------|------|",
243+
" | a | b |"
244+
];
245+
const sut = createDetector();
246+
247+
const result = sut.getLeftPad(lines);
248+
249+
assert.strictEqual(result, " ");
250+
});
251+
252+
test("getLeftPad() with whitespace-only lines ignores them", () => {
253+
const lines: string[] = [
254+
" | col1 | col2 |",
255+
" ",
256+
" |------|------|",
257+
" | a | b |"
258+
];
259+
const sut = createDetector();
260+
261+
const result = sut.getLeftPad(lines);
262+
263+
assert.strictEqual(result, " ");
264+
});
265+
266+
test("getLeftPad() with mixed tabs and spaces fallback to space logic when no common tab prefix", () => {
267+
const lines: string[] = [
268+
" | col1 | col2 |",
269+
"\t|------|------|",
270+
" | a | b |",
271+
" | c | d |"
272+
];
273+
const sut = createDetector();
274+
275+
const result = sut.getLeftPad(lines);
276+
277+
assert.strictEqual(result, " ");
278+
});
279+
280+
test("getLeftPad() with single line returns empty string when no indentation", () => {
281+
const lines: string[] = [
282+
"| col1 | col2 |"
283+
];
284+
const sut = createDetector();
285+
286+
const result = sut.getLeftPad(lines);
287+
288+
assert.strictEqual(result, "");
289+
});
290+
291+
test("getLeftPad() with complex tab scenario maintains common prefix", () => {
292+
const lines: string[] = [
293+
"\t\t\t | col1 | col2 |",
294+
"\t\t |------|------|",
295+
"\t\t\t\t\t| a | b |"
296+
];
297+
const sut = createDetector();
298+
299+
const result = sut.getLeftPad(lines);
300+
301+
assert.strictEqual(result, "\t\t");
302+
});
303+
304+
test("getLeftPad() with no common tab prefix falls back to space logic", () => {
305+
const lines: string[] = [
306+
"\t | col1 | col2 |",
307+
" \t|------|------|",
308+
"\t | a | b |"
309+
];
310+
const sut = createDetector();
311+
312+
const result = sut.getLeftPad(lines);
313+
314+
assert.strictEqual(result, "\t ");
315+
});
316+
317+
function createDetector(): TestableFairTableIndentationDetector {
318+
return new TestableFairTableIndentationDetector();
319+
}
320+
321+
class TestableFairTableIndentationDetector extends FairTableIndentationDetector {
322+
public testHasIndentation(leftPadsPerLine: string[]): boolean {
323+
return this.hasIndentation(leftPadsPerLine);
324+
}
325+
326+
public testGetIndentationChars(leftPadsPerLine: string[]): string {
327+
return this.getIndentationChars(leftPadsPerLine);
328+
}
329+
}
330+
});

0 commit comments

Comments
 (0)