Skip to content

Commit be47b94

Browse files
committed
clean up tests
1 parent 17c45ed commit be47b94

File tree

1 file changed

+112
-167
lines changed

1 file changed

+112
-167
lines changed
Lines changed: 112 additions & 167 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
interface Classification {
2+
position: number;
23
length: number;
34
class: ts.TokenClass;
45
}
@@ -8,17 +9,10 @@ interface ClassiferResult {
89
finalEndOfLineState: ts.EndOfLineState;
910
}
1011

11-
var TokenClassNames = {};
12-
TokenClassNames[ts.TokenClass.Punctuation] = "Punctuation";
13-
TokenClassNames[ts.TokenClass.Keyword] = "Keyword";
14-
TokenClassNames[ts.TokenClass.Operator] = "Operator";
15-
TokenClassNames[ts.TokenClass.Comment] = "Comment";
16-
TokenClassNames[ts.TokenClass.Whitespace] = "Whitespace";
17-
TokenClassNames[ts.TokenClass.Identifier] = "Identifier";
18-
TokenClassNames[ts.TokenClass.NumberLiteral] = "NumberLiteral";
19-
TokenClassNames[ts.TokenClass.StringLiteral] = "StringLiteral";
20-
TokenClassNames[ts.TokenClass.RegExpLiteral] = "RegExpLiteral";
21-
12+
interface ClassificationEntry {
13+
value: any;
14+
class: ts.TokenClass;
15+
}
2216

2317
describe('Colorization', function () {
2418
var mytypescriptLS = new Harness.LanguageService.TypeScriptLS();
@@ -28,20 +22,21 @@ describe('Colorization', function () {
2822
var classResult = myclassifier.getClassificationsForLine(code, initialEndOfLineState).split('\n');
2923
var tuples: Classification[] = [];
3024
var i = 0;
31-
var computedLength = 0;
25+
var position = 0;
3226

3327
for (; i < classResult.length - 1; i += 2) {
3428
var t = tuples[i / 2] = {
29+
position: position,
3530
length: parseInt(classResult[i]),
3631
class: parseInt(classResult[i + 1])
3732
};
3833

3934
assert.isTrue(t.length > 0, "Result length should be greater than 0, got :" + t.length);
40-
computedLength += t.length;
35+
position += t.length;
4136
}
4237
var finalEndOfLineState = classResult[classResult.length - 1];
4338

44-
assert.equal(computedLength, code.length, "Expected accumilative length of all entries to match the length of the source. expected: " + code.length + ", but got: " + computedLength);
39+
assert.equal(position, code.length, "Expected accumilative length of all entries to match the length of the source. expected: " + code.length + ", but got: " + position);
4540

4641
return {
4742
tuples: tuples,
@@ -52,182 +47,132 @@ describe('Colorization', function () {
5247
function verifyClassification(classification: Classification, expectedLength: number, expectedClass: number) {
5348
assert.isNotNull(classification);
5449
assert.equal(classification.length, expectedLength, "Classification length does not match expected. Expected: " + expectedLength + ", Actual: " + classification.length);
55-
assert.equal(classification.class, expectedClass, "Classification class does not match expected. Expected: " + TokenClassNames[expectedClass] + ", Actual: " + TokenClassNames[classification.class]);
50+
assert.equal(classification.class, expectedClass, "Classification class does not match expected. Expected: " + ts.TokenClass[expectedClass] + ", Actual: " + ts.TokenClass[classification.class]);
5651
}
5752

58-
describe("test cases for colorization", function () {
59-
var results = getClassifications('var x:string = "foo"; //Hello');
60-
61-
it("checks for a keyword", function () {
62-
verifyClassification(results.tuples[0], 3, ts.TokenClass.Keyword);
63-
});
64-
65-
it("checks for a whitespace", function () {
66-
verifyClassification(results.tuples[1], 1, ts.TokenClass.Whitespace);
67-
});
68-
69-
it("checks for a identifier", function () {
70-
verifyClassification(results.tuples[2], 1, ts.TokenClass.Identifier);
71-
});
72-
73-
it("checks for an punctuation", function () {
74-
verifyClassification(results.tuples[3], 1, ts.TokenClass.Punctuation);
75-
});
76-
77-
it("checks for a operator", function () {
78-
verifyClassification(results.tuples[6], 1, ts.TokenClass.Operator);
79-
});
80-
81-
it("checks for a string literal", function () {
82-
verifyClassification(results.tuples[8], 5, ts.TokenClass.StringLiteral);
83-
});
84-
85-
it("checks for a comment", function () {
86-
verifyClassification(results.tuples[11], 7, ts.TokenClass.Comment);
87-
});
88-
});
89-
90-
describe("test comment colorization after a divide operator", function () {
91-
var results = getClassifications('1 / 1 // comment');
92-
93-
it("checks for a number literal", function () {
94-
verifyClassification(results.tuples[0], 1, ts.TokenClass.NumberLiteral);
95-
});
96-
97-
it("checks for a whitespace", function () {
98-
verifyClassification(results.tuples[1], 1, ts.TokenClass.Whitespace);
99-
});
100-
101-
it("checks for a operator", function () {
102-
verifyClassification(results.tuples[2], 1, ts.TokenClass.Operator);
103-
});
104-
105-
it("checks for a whitespace", function () {
106-
verifyClassification(results.tuples[3], 1, ts.TokenClass.Whitespace);
107-
});
108-
109-
it("checks for a number literal", function () {
110-
verifyClassification(results.tuples[4], 1, ts.TokenClass.NumberLiteral);
111-
});
112-
113-
it("checks for a whitespace", function () {
114-
verifyClassification(results.tuples[5], 1, ts.TokenClass.Whitespace);
115-
});
116-
117-
it("checks for a comment", function () {
118-
verifyClassification(results.tuples[6], 10, ts.TokenClass.Comment);
119-
});
120-
});
121-
122-
describe("test literal colorization after a divide operator", function () {
123-
var results = getClassifications('1 / 2, 1 / 2');
124-
125-
it("checks for a number literal", function () {
126-
verifyClassification(results.tuples[0], 1, ts.TokenClass.NumberLiteral);
127-
});
128-
129-
it("checks for a whitespace", function () {
130-
verifyClassification(results.tuples[1], 1, ts.TokenClass.Whitespace);
131-
});
132-
133-
it("checks for a operator", function () {
134-
verifyClassification(results.tuples[2], 1, ts.TokenClass.Operator);
135-
});
136-
137-
it("checks for a whitespace", function () {
138-
verifyClassification(results.tuples[3], 1, ts.TokenClass.Whitespace);
139-
});
140-
141-
it("checks for a number literal", function () {
142-
verifyClassification(results.tuples[4], 1, ts.TokenClass.NumberLiteral);
143-
});
144-
145-
it("checks for a operator", function () {
146-
verifyClassification(results.tuples[5], 1, ts.TokenClass.Operator);
147-
});
148-
149-
it("checks for a whitespace", function () {
150-
verifyClassification(results.tuples[6], 1, ts.TokenClass.Whitespace);
151-
});
152-
153-
it("checks for a number literal", function () {
154-
verifyClassification(results.tuples[7], 1, ts.TokenClass.NumberLiteral);
155-
});
53+
function getEntryAtPosistion(result: ClassiferResult, position: number) {
54+
for (var i = 0, n = result.tuples.length; i < n; i++) {
55+
if (result.tuples[i].position === position) return result.tuples[i];
56+
}
57+
return undefined;
58+
}
15659

157-
it("checks for a whitespace", function () {
158-
verifyClassification(results.tuples[8], 1, ts.TokenClass.Whitespace);
159-
});
60+
function punctuation(text: string) { return { value: text, class: ts.TokenClass.Punctuation }; }
61+
function keyword(text: string) { return { value: text, class: ts.TokenClass.Keyword }; }
62+
function operator(text: string) { return { value: text, class: ts.TokenClass.Operator }; }
63+
function comment(text: string) { return { value: text, class: ts.TokenClass.Comment }; }
64+
function whitespace(text: string) { return { value: text, class: ts.TokenClass.Whitespace }; }
65+
function identifier(text: string) { return { value: text, class: ts.TokenClass.Identifier }; }
66+
function numberLiteral(text: string) { return { value: text, class: ts.TokenClass.NumberLiteral }; }
67+
function stringLiteral(text: string) { return { value: text, class: ts.TokenClass.StringLiteral }; }
68+
function regExpLiteral(text: string) { return { value: text, class: ts.TokenClass.RegExpLiteral }; }
69+
function finalEndOfLineState(value: number) { return { value: value, class: undefined }; }
70+
71+
function test(text: string, initialEndOfLineState: ts.EndOfLineState, ...expectedEntries: ClassificationEntry[]): void {
72+
var result = getClassifications(text, initialEndOfLineState);
73+
74+
for (var i = 0, n = expectedEntries.length; i < n; i++) {
75+
var expectedEntry = expectedEntries[i];
76+
77+
if (expectedEntry.class === undefined) {
78+
assert.equal(result.finalEndOfLineState, expectedEntry.value, "final endOfLineState does not match expected.");
79+
}
80+
else {
81+
var actualEntryPosition = text.indexOf(expectedEntry.value);
82+
assert(actualEntryPosition >= 0, "token: '" + expectedEntry.value + "' does not exit in text: '" + text + "'.");
83+
84+
var actualEntry = getEntryAtPosistion(result, actualEntryPosition);
85+
86+
assert(actualEntry, "Could not find classification entry for '" + expectedEntry.value + "' at position: " + actualEntryPosition);
87+
assert.equal(actualEntry.length, expectedEntry.value.length, "Classification class does not match expected.");
88+
assert.equal(actualEntry.class, expectedEntry.class, "Classification class does not match expected.");
89+
}
90+
}
91+
}
16092

161-
it("checks for a operator", function () {
162-
verifyClassification(results.tuples[9], 1, ts.TokenClass.Operator);
93+
describe("test getClassifications", function () {
94+
it("Returns correct token classes", function () {
95+
test("var x: string = \"foo\"; //Hello",
96+
ts.EndOfLineState.Start,
97+
keyword("var"),
98+
whitespace(" "),
99+
identifier("x"),
100+
punctuation(":"),
101+
keyword("string"),
102+
operator("="),
103+
stringLiteral("\"foo\""),
104+
comment("//Hello"),
105+
punctuation(";"));
163106
});
164107

165-
it("checks for a whitespace", function () {
166-
verifyClassification(results.tuples[10], 1, ts.TokenClass.Whitespace);
108+
it("classifies correctelly a comment after a divide operator", function () {
109+
test("1 / 2 // comment",
110+
ts.EndOfLineState.Start,
111+
numberLiteral("1"),
112+
whitespace(" "),
113+
operator("/"),
114+
numberLiteral("2"),
115+
comment("// comment"));
167116
});
168117

169-
it("checks for a number literal", function () {
170-
verifyClassification(results.tuples[11], 1, ts.TokenClass.NumberLiteral);
118+
it("classifies correctelly a literal after a divide operator", function () {
119+
test("1 / 2, 3 / 4",
120+
ts.EndOfLineState.Start,
121+
numberLiteral("1"),
122+
whitespace(" "),
123+
operator("/"),
124+
numberLiteral("2"),
125+
numberLiteral("3"),
126+
numberLiteral("4"),
127+
operator(","));
171128
});
172129

173-
});
174-
175-
describe("test cases for colorizing multi-line string", function () {
176-
it("classifies first line correctelly", function () {
177-
var results = getClassifications("'line1\\", ts.EndOfLineState.Start);
178-
179-
assert.equal(results.tuples.length, 1);
180-
verifyClassification(results.tuples[0], 7, ts.TokenClass.StringLiteral);
181-
assert.equal(results.finalEndOfLineState, ts.EndOfLineState.InSingleQuoteStringLiteral);
130+
it("classifies correctelly an unterminated multi-line string", function () {
131+
test("'line1\\",
132+
ts.EndOfLineState.Start,
133+
stringLiteral("'line1\\"),
134+
finalEndOfLineState(ts.EndOfLineState.InSingleQuoteStringLiteral));
182135
});
183136

184-
it("classifies second line correctelly", function () {
185-
var results = getClassifications("\\", ts.EndOfLineState.InDoubleQuoteStringLiteral);
186-
187-
assert.equal(results.tuples.length, 1);
188-
verifyClassification(results.tuples[0], 1, ts.TokenClass.StringLiteral);
189-
assert.equal(results.finalEndOfLineState, ts.EndOfLineState.InDoubleQuoteStringLiteral);
137+
it("classifies correctelly the second line of an unterminated multi-line string", function () {
138+
test("\\",
139+
ts.EndOfLineState.InDoubleQuoteStringLiteral,
140+
stringLiteral("\\"),
141+
finalEndOfLineState(ts.EndOfLineState.InDoubleQuoteStringLiteral));
190142
});
191143

192-
it("classifies third line correctelly", function () {
193-
var results = getClassifications("'", ts.EndOfLineState.InSingleQuoteStringLiteral);
194-
195-
assert.equal(results.tuples.length, 1);
196-
verifyClassification(results.tuples[0], 1, ts.TokenClass.StringLiteral);
197-
assert.equal(results.finalEndOfLineState, ts.EndOfLineState.Start);
144+
it("classifies correctelly the last line of a multi-line string", function () {
145+
test("'",
146+
ts.EndOfLineState.InSingleQuoteStringLiteral,
147+
stringLiteral("'"),
148+
finalEndOfLineState(ts.EndOfLineState.Start));
198149
});
199-
});
200150

201-
describe("test cases for colorizing unterminted multi-line comment", function () {
202-
it("unterminated multi-line comment correctelly", function () {
203-
var results = getClassifications("/*", ts.EndOfLineState.Start);
204-
205-
assert.equal(results.tuples.length, 1);
206-
verifyClassification(results.tuples[0], 2, ts.TokenClass.Comment);
207-
assert.equal(results.finalEndOfLineState, ts.EndOfLineState.InMultiLineCommentTrivia);
151+
it("classifies correctelly an unterminated multiline comment", function () {
152+
test("/*",
153+
ts.EndOfLineState.Start,
154+
comment("/*"),
155+
finalEndOfLineState(ts.EndOfLineState.InMultiLineCommentTrivia));
208156
});
209157

210-
it("unterminated multi-line comment with trailing space correctelly", function () {
211-
var results = getClassifications("/* ", ts.EndOfLineState.Start);
212-
213-
assert.equal(results.tuples.length, 1);
214-
verifyClassification(results.tuples[0], 3, ts.TokenClass.Comment);
215-
assert.equal(results.finalEndOfLineState, ts.EndOfLineState.InMultiLineCommentTrivia);
158+
it("classifies correctelly an unterminated multiline comment with trailing space", function () {
159+
test("/* ",
160+
ts.EndOfLineState.Start,
161+
comment("/* "),
162+
finalEndOfLineState(ts.EndOfLineState.InMultiLineCommentTrivia));
216163
});
217-
});
218164

219-
describe("test cases for colorizing keywords", function () {
220-
it("classifies keyword after a dot", function () {
221-
var results = getClassifications("a.var", ts.EndOfLineState.Start);
222-
verifyClassification(results.tuples[2], 3, ts.TokenClass.Identifier);
165+
it("classifies correctelly a keyword after a dot", function () {
166+
test("a.var",
167+
ts.EndOfLineState.Start,
168+
identifier("var"));
223169
});
224170

225171
it("classifies keyword after a dot on previous line", function () {
226-
var results = getClassifications("var", ts.EndOfLineState.EndingWithDotToken);
227-
228-
assert.equal(results.tuples.length, 1);
229-
verifyClassification(results.tuples[0], 3, ts.TokenClass.Identifier);
230-
assert.equal(results.finalEndOfLineState, ts.EndOfLineState.Start);
172+
test("var",
173+
ts.EndOfLineState.EndingWithDotToken,
174+
identifier("var"),
175+
finalEndOfLineState(ts.EndOfLineState.Start));
231176
});
232177
});
233178
});

0 commit comments

Comments
 (0)