From 7bafbeae90ea8b605612060ca29e13001ff825a0 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Thu, 18 Aug 2022 11:28:08 -0700 Subject: [PATCH] Add fourslash function for validating syntactic classification (#50362) --- src/harness/fourslashImpl.ts | 7 +++++++ src/harness/fourslashInterfaceImpl.ts | 4 ++++ tests/cases/fourslash/fourslash.ts | 6 ++++-- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/harness/fourslashImpl.ts b/src/harness/fourslashImpl.ts index 04297d1d049..d0ed9fd6c03 100644 --- a/src/harness/fourslashImpl.ts +++ b/src/harness/fourslashImpl.ts @@ -2749,6 +2749,13 @@ namespace FourSlash { // fs.writeFileSync(testfilePath, newfile); } + public verifyEncodedSyntacticClassificationsLength(expected: number) { + const actual = this.languageService.getEncodedSyntacticClassifications(this.activeFile.fileName, ts.createTextSpan(0, this.activeFile.content.length)); + if (actual.spans.length !== expected) { + this.raiseError(`encodedSyntacticClassificationsLength failed - expected total spans to be ${expected} got ${actual.spans.length}`); + } + } + public verifyEncodedSemanticClassificationsLength(format: ts.SemanticClassificationFormat, expected: number) { const actual = this.languageService.getEncodedSemanticClassifications(this.activeFile.fileName, ts.createTextSpan(0, this.activeFile.content.length), format); if (actual.spans.length !== expected) { diff --git a/src/harness/fourslashInterfaceImpl.ts b/src/harness/fourslashInterfaceImpl.ts index ddb8c02c69c..93508a079ab 100644 --- a/src/harness/fourslashInterfaceImpl.ts +++ b/src/harness/fourslashInterfaceImpl.ts @@ -541,6 +541,10 @@ namespace FourSlashInterface { this.state.verifySyntacticClassifications(classifications); } + public encodedSyntacticClassificationsLength(length: number) { + this.state.verifyEncodedSyntacticClassificationsLength(length); + } + public encodedSemanticClassificationsLength(format: ts.SemanticClassificationFormat, length: number) { this.state.verifyEncodedSemanticClassificationsLength(format, length); } diff --git a/tests/cases/fourslash/fourslash.ts b/tests/cases/fourslash/fourslash.ts index a6c1804affb..bc3f1aab46e 100644 --- a/tests/cases/fourslash/fourslash.ts +++ b/tests/cases/fourslash/fourslash.ts @@ -378,8 +378,10 @@ declare namespace FourSlashInterface { rangesAreDocumentHighlights(ranges?: Range[], options?: VerifyDocumentHighlightsOptions): void; rangesWithSameTextAreDocumentHighlights(): void; documentHighlightsOf(startRange: Range, ranges: Range[], options?: VerifyDocumentHighlightsOptions): void; - /** Prefer semanticClassificationsAre for more descriptive tests */ - encodedSemanticClassificationsLength(format: "original" | "2020", length: number) + /** Prefer {@link syntacticClassificationsAre} for more descriptive tests */ + encodedSyntacticClassificationsLength(expected: number): void; + /** Prefer {@link semanticClassificationsAre} for more descriptive tests */ + encodedSemanticClassificationsLength(format: "original" | "2020", length: number): void; /** * This method *requires* a contiguous, complete, and ordered stream of classifications for a file. */