From be47b94c8f6a025b0bba9c453c42930296dba45e Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Fri, 8 Aug 2014 16:45:10 -0700 Subject: [PATCH] clean up tests --- .../cases/unittests/services/colorization.ts | 281 +++++++----------- 1 file changed, 113 insertions(+), 168 deletions(-) diff --git a/tests/cases/unittests/services/colorization.ts b/tests/cases/unittests/services/colorization.ts index e395f20490c..414942bbded 100644 --- a/tests/cases/unittests/services/colorization.ts +++ b/tests/cases/unittests/services/colorization.ts @@ -1,4 +1,5 @@ interface Classification { + position: number; length: number; class: ts.TokenClass; } @@ -8,17 +9,10 @@ interface ClassiferResult { finalEndOfLineState: ts.EndOfLineState; } -var TokenClassNames = {}; -TokenClassNames[ts.TokenClass.Punctuation] = "Punctuation"; -TokenClassNames[ts.TokenClass.Keyword] = "Keyword"; -TokenClassNames[ts.TokenClass.Operator] = "Operator"; -TokenClassNames[ts.TokenClass.Comment] = "Comment"; -TokenClassNames[ts.TokenClass.Whitespace] = "Whitespace"; -TokenClassNames[ts.TokenClass.Identifier] = "Identifier"; -TokenClassNames[ts.TokenClass.NumberLiteral] = "NumberLiteral"; -TokenClassNames[ts.TokenClass.StringLiteral] = "StringLiteral"; -TokenClassNames[ts.TokenClass.RegExpLiteral] = "RegExpLiteral"; - +interface ClassificationEntry { + value: any; + class: ts.TokenClass; +} describe('Colorization', function () { var mytypescriptLS = new Harness.LanguageService.TypeScriptLS(); @@ -28,20 +22,21 @@ describe('Colorization', function () { var classResult = myclassifier.getClassificationsForLine(code, initialEndOfLineState).split('\n'); var tuples: Classification[] = []; var i = 0; - var computedLength = 0; + var position = 0; for (; i < classResult.length - 1; i += 2) { var t = tuples[i / 2] = { + position: position, length: parseInt(classResult[i]), class: parseInt(classResult[i + 1]) }; assert.isTrue(t.length > 0, "Result length should be greater than 0, got :" + t.length); - computedLength += t.length; + position += t.length; } var finalEndOfLineState = classResult[classResult.length - 1]; - assert.equal(computedLength, code.length, "Expected accumilative length of all entries to match the length of the source. expected: " + code.length + ", but got: " + computedLength); + assert.equal(position, code.length, "Expected accumilative length of all entries to match the length of the source. expected: " + code.length + ", but got: " + position); return { tuples: tuples, @@ -52,182 +47,132 @@ describe('Colorization', function () { function verifyClassification(classification: Classification, expectedLength: number, expectedClass: number) { assert.isNotNull(classification); assert.equal(classification.length, expectedLength, "Classification length does not match expected. Expected: " + expectedLength + ", Actual: " + classification.length); - assert.equal(classification.class, expectedClass, "Classification class does not match expected. Expected: " + TokenClassNames[expectedClass] + ", Actual: " + TokenClassNames[classification.class]); + assert.equal(classification.class, expectedClass, "Classification class does not match expected. Expected: " + ts.TokenClass[expectedClass] + ", Actual: " + ts.TokenClass[classification.class]); } - describe("test cases for colorization", function () { - var results = getClassifications('var x:string = "foo"; //Hello'); + function getEntryAtPosistion(result: ClassiferResult, position: number) { + for (var i = 0, n = result.tuples.length; i < n; i++) { + if (result.tuples[i].position === position) return result.tuples[i]; + } + return undefined; + } - it("checks for a keyword", function () { - verifyClassification(results.tuples[0], 3, ts.TokenClass.Keyword); + function punctuation(text: string) { return { value: text, class: ts.TokenClass.Punctuation }; } + function keyword(text: string) { return { value: text, class: ts.TokenClass.Keyword }; } + function operator(text: string) { return { value: text, class: ts.TokenClass.Operator }; } + function comment(text: string) { return { value: text, class: ts.TokenClass.Comment }; } + function whitespace(text: string) { return { value: text, class: ts.TokenClass.Whitespace }; } + function identifier(text: string) { return { value: text, class: ts.TokenClass.Identifier }; } + function numberLiteral(text: string) { return { value: text, class: ts.TokenClass.NumberLiteral }; } + function stringLiteral(text: string) { return { value: text, class: ts.TokenClass.StringLiteral }; } + function regExpLiteral(text: string) { return { value: text, class: ts.TokenClass.RegExpLiteral }; } + function finalEndOfLineState(value: number) { return { value: value, class: undefined }; } + + function test(text: string, initialEndOfLineState: ts.EndOfLineState, ...expectedEntries: ClassificationEntry[]): void { + var result = getClassifications(text, initialEndOfLineState); + + for (var i = 0, n = expectedEntries.length; i < n; i++) { + var expectedEntry = expectedEntries[i]; + + if (expectedEntry.class === undefined) { + assert.equal(result.finalEndOfLineState, expectedEntry.value, "final endOfLineState does not match expected."); + } + else { + var actualEntryPosition = text.indexOf(expectedEntry.value); + assert(actualEntryPosition >= 0, "token: '" + expectedEntry.value + "' does not exit in text: '" + text + "'."); + + var actualEntry = getEntryAtPosistion(result, actualEntryPosition); + + assert(actualEntry, "Could not find classification entry for '" + expectedEntry.value + "' at position: " + actualEntryPosition); + assert.equal(actualEntry.length, expectedEntry.value.length, "Classification class does not match expected."); + assert.equal(actualEntry.class, expectedEntry.class, "Classification class does not match expected."); + } + } + } + + describe("test getClassifications", function () { + it("Returns correct token classes", function () { + test("var x: string = \"foo\"; //Hello", + ts.EndOfLineState.Start, + keyword("var"), + whitespace(" "), + identifier("x"), + punctuation(":"), + keyword("string"), + operator("="), + stringLiteral("\"foo\""), + comment("//Hello"), + punctuation(";")); }); - it("checks for a whitespace", function () { - verifyClassification(results.tuples[1], 1, ts.TokenClass.Whitespace); + it("classifies correctelly a comment after a divide operator", function () { + test("1 / 2 // comment", + ts.EndOfLineState.Start, + numberLiteral("1"), + whitespace(" "), + operator("/"), + numberLiteral("2"), + comment("// comment")); }); - it("checks for a identifier", function () { - verifyClassification(results.tuples[2], 1, ts.TokenClass.Identifier); + it("classifies correctelly a literal after a divide operator", function () { + test("1 / 2, 3 / 4", + ts.EndOfLineState.Start, + numberLiteral("1"), + whitespace(" "), + operator("/"), + numberLiteral("2"), + numberLiteral("3"), + numberLiteral("4"), + operator(",")); }); - it("checks for an punctuation", function () { - verifyClassification(results.tuples[3], 1, ts.TokenClass.Punctuation); + it("classifies correctelly an unterminated multi-line string", function () { + test("'line1\\", + ts.EndOfLineState.Start, + stringLiteral("'line1\\"), + finalEndOfLineState(ts.EndOfLineState.InSingleQuoteStringLiteral)); }); - it("checks for a operator", function () { - verifyClassification(results.tuples[6], 1, ts.TokenClass.Operator); + it("classifies correctelly the second line of an unterminated multi-line string", function () { + test("\\", + ts.EndOfLineState.InDoubleQuoteStringLiteral, + stringLiteral("\\"), + finalEndOfLineState(ts.EndOfLineState.InDoubleQuoteStringLiteral)); }); - it("checks for a string literal", function () { - verifyClassification(results.tuples[8], 5, ts.TokenClass.StringLiteral); + it("classifies correctelly the last line of a multi-line string", function () { + test("'", + ts.EndOfLineState.InSingleQuoteStringLiteral, + stringLiteral("'"), + finalEndOfLineState(ts.EndOfLineState.Start)); }); - it("checks for a comment", function () { - verifyClassification(results.tuples[11], 7, ts.TokenClass.Comment); - }); - }); - - describe("test comment colorization after a divide operator", function () { - var results = getClassifications('1 / 1 // comment'); - - it("checks for a number literal", function () { - verifyClassification(results.tuples[0], 1, ts.TokenClass.NumberLiteral); + it("classifies correctelly an unterminated multiline comment", function () { + test("/*", + ts.EndOfLineState.Start, + comment("/*"), + finalEndOfLineState(ts.EndOfLineState.InMultiLineCommentTrivia)); }); - it("checks for a whitespace", function () { - verifyClassification(results.tuples[1], 1, ts.TokenClass.Whitespace); + it("classifies correctelly an unterminated multiline comment with trailing space", function () { + test("/* ", + ts.EndOfLineState.Start, + comment("/* "), + finalEndOfLineState(ts.EndOfLineState.InMultiLineCommentTrivia)); }); - it("checks for a operator", function () { - verifyClassification(results.tuples[2], 1, ts.TokenClass.Operator); - }); - - it("checks for a whitespace", function () { - verifyClassification(results.tuples[3], 1, ts.TokenClass.Whitespace); - }); - - it("checks for a number literal", function () { - verifyClassification(results.tuples[4], 1, ts.TokenClass.NumberLiteral); - }); - - it("checks for a whitespace", function () { - verifyClassification(results.tuples[5], 1, ts.TokenClass.Whitespace); - }); - - it("checks for a comment", function () { - verifyClassification(results.tuples[6], 10, ts.TokenClass.Comment); - }); - }); - - describe("test literal colorization after a divide operator", function () { - var results = getClassifications('1 / 2, 1 / 2'); - - it("checks for a number literal", function () { - verifyClassification(results.tuples[0], 1, ts.TokenClass.NumberLiteral); - }); - - it("checks for a whitespace", function () { - verifyClassification(results.tuples[1], 1, ts.TokenClass.Whitespace); - }); - - it("checks for a operator", function () { - verifyClassification(results.tuples[2], 1, ts.TokenClass.Operator); - }); - - it("checks for a whitespace", function () { - verifyClassification(results.tuples[3], 1, ts.TokenClass.Whitespace); - }); - - it("checks for a number literal", function () { - verifyClassification(results.tuples[4], 1, ts.TokenClass.NumberLiteral); - }); - - it("checks for a operator", function () { - verifyClassification(results.tuples[5], 1, ts.TokenClass.Operator); - }); - - it("checks for a whitespace", function () { - verifyClassification(results.tuples[6], 1, ts.TokenClass.Whitespace); - }); - - it("checks for a number literal", function () { - verifyClassification(results.tuples[7], 1, ts.TokenClass.NumberLiteral); - }); - - it("checks for a whitespace", function () { - verifyClassification(results.tuples[8], 1, ts.TokenClass.Whitespace); - }); - - it("checks for a operator", function () { - verifyClassification(results.tuples[9], 1, ts.TokenClass.Operator); - }); - - it("checks for a whitespace", function () { - verifyClassification(results.tuples[10], 1, ts.TokenClass.Whitespace); - }); - - it("checks for a number literal", function () { - verifyClassification(results.tuples[11], 1, ts.TokenClass.NumberLiteral); - }); - - }); - - describe("test cases for colorizing multi-line string", function () { - it("classifies first line correctelly", function () { - var results = getClassifications("'line1\\", ts.EndOfLineState.Start); - - assert.equal(results.tuples.length, 1); - verifyClassification(results.tuples[0], 7, ts.TokenClass.StringLiteral); - assert.equal(results.finalEndOfLineState, ts.EndOfLineState.InSingleQuoteStringLiteral); - }); - - it("classifies second line correctelly", function () { - var results = getClassifications("\\", ts.EndOfLineState.InDoubleQuoteStringLiteral); - - assert.equal(results.tuples.length, 1); - verifyClassification(results.tuples[0], 1, ts.TokenClass.StringLiteral); - assert.equal(results.finalEndOfLineState, ts.EndOfLineState.InDoubleQuoteStringLiteral); - }); - - it("classifies third line correctelly", function () { - var results = getClassifications("'", ts.EndOfLineState.InSingleQuoteStringLiteral); - - assert.equal(results.tuples.length, 1); - verifyClassification(results.tuples[0], 1, ts.TokenClass.StringLiteral); - assert.equal(results.finalEndOfLineState, ts.EndOfLineState.Start); - }); - }); - - describe("test cases for colorizing unterminted multi-line comment", function () { - it("unterminated multi-line comment correctelly", function () { - var results = getClassifications("/*", ts.EndOfLineState.Start); - - assert.equal(results.tuples.length, 1); - verifyClassification(results.tuples[0], 2, ts.TokenClass.Comment); - assert.equal(results.finalEndOfLineState, ts.EndOfLineState.InMultiLineCommentTrivia); - }); - - it("unterminated multi-line comment with trailing space correctelly", function () { - var results = getClassifications("/* ", ts.EndOfLineState.Start); - - assert.equal(results.tuples.length, 1); - verifyClassification(results.tuples[0], 3, ts.TokenClass.Comment); - assert.equal(results.finalEndOfLineState, ts.EndOfLineState.InMultiLineCommentTrivia); - }); - }); - - describe("test cases for colorizing keywords", function () { - it("classifies keyword after a dot", function () { - var results = getClassifications("a.var", ts.EndOfLineState.Start); - verifyClassification(results.tuples[2], 3, ts.TokenClass.Identifier); + it("classifies correctelly a keyword after a dot", function () { + test("a.var", + ts.EndOfLineState.Start, + identifier("var")); }); it("classifies keyword after a dot on previous line", function () { - var results = getClassifications("var", ts.EndOfLineState.EndingWithDotToken); - - assert.equal(results.tuples.length, 1); - verifyClassification(results.tuples[0], 3, ts.TokenClass.Identifier); - assert.equal(results.finalEndOfLineState, ts.EndOfLineState.Start); + test("var", + ts.EndOfLineState.EndingWithDotToken, + identifier("var"), + finalEndOfLineState(ts.EndOfLineState.Start)); }); }); }); \ No newline at end of file