From aa81c16a3ef564a0b4aec0e5c349729d050642c1 Mon Sep 17 00:00:00 2001 From: "Lyu, Wei Da" Date: Tue, 3 Oct 2023 08:47:11 +0800 Subject: [PATCH] switch to snapshot test --- .../features/FoldingRangeProvider.test.ts | 104 --------------- .../features/folding-range/.gitignore | 1 + .../fixtures/await-catch/expectedv2.json | 4 + .../fixtures/await-catch/input.svelte | 9 ++ .../await-peding-catch/expectedv2.json | 4 + .../fixtures/await-peding-catch/input.svelte | 12 ++ .../await-pending-only/expectedv2.json | 1 + .../fixtures/await-pending-only/input.svelte | 3 + .../await-pending-then-catch/expectedv2.json | 8 ++ .../await-pending-then-catch/input.svelte | 17 +++ .../await-pending-then/expectedv2.json | 1 + .../fixtures/await-pending-then/input.svelte | 5 + .../fixtures/await-then/expectedv2.json | 4 + .../fixtures/await-then/input.svelte | 9 ++ .../fixtures/each-block/expectedv2.json | 4 + .../fixtures/each-block/input.svelte | 9 ++ .../fixtures/if-block/expectedv2.json | 8 ++ .../fixtures/if-block/input.svelte | 17 +++ .../fixtures/parser-error/expectedv2.json | 5 + .../fixtures/parser-error/input.svelte} | 2 +- .../fixtures/script/expectedv2.json | 4 + .../fixtures/script/input.svelte} | 8 +- .../features/folding-range/index.test.ts | 122 ++++++++++++++++++ 23 files changed, 249 insertions(+), 112 deletions(-) delete mode 100644 packages/language-server/test/plugins/typescript/features/FoldingRangeProvider.test.ts create mode 100644 packages/language-server/test/plugins/typescript/features/folding-range/.gitignore create mode 100644 packages/language-server/test/plugins/typescript/features/folding-range/fixtures/await-catch/expectedv2.json create mode 100644 packages/language-server/test/plugins/typescript/features/folding-range/fixtures/await-catch/input.svelte create mode 100644 packages/language-server/test/plugins/typescript/features/folding-range/fixtures/await-peding-catch/expectedv2.json create mode 100644 packages/language-server/test/plugins/typescript/features/folding-range/fixtures/await-peding-catch/input.svelte create mode 100644 packages/language-server/test/plugins/typescript/features/folding-range/fixtures/await-pending-only/expectedv2.json create mode 100644 packages/language-server/test/plugins/typescript/features/folding-range/fixtures/await-pending-only/input.svelte create mode 100644 packages/language-server/test/plugins/typescript/features/folding-range/fixtures/await-pending-then-catch/expectedv2.json create mode 100644 packages/language-server/test/plugins/typescript/features/folding-range/fixtures/await-pending-then-catch/input.svelte create mode 100644 packages/language-server/test/plugins/typescript/features/folding-range/fixtures/await-pending-then/expectedv2.json create mode 100644 packages/language-server/test/plugins/typescript/features/folding-range/fixtures/await-pending-then/input.svelte create mode 100644 packages/language-server/test/plugins/typescript/features/folding-range/fixtures/await-then/expectedv2.json create mode 100644 packages/language-server/test/plugins/typescript/features/folding-range/fixtures/await-then/input.svelte create mode 100644 packages/language-server/test/plugins/typescript/features/folding-range/fixtures/each-block/expectedv2.json create mode 100644 packages/language-server/test/plugins/typescript/features/folding-range/fixtures/each-block/input.svelte create mode 100644 packages/language-server/test/plugins/typescript/features/folding-range/fixtures/if-block/expectedv2.json create mode 100644 packages/language-server/test/plugins/typescript/features/folding-range/fixtures/if-block/input.svelte create mode 100644 packages/language-server/test/plugins/typescript/features/folding-range/fixtures/parser-error/expectedv2.json rename packages/language-server/test/plugins/typescript/{testfiles/folding-range/folding-parser-error.svelte => features/folding-range/fixtures/parser-error/input.svelte} (94%) create mode 100644 packages/language-server/test/plugins/typescript/features/folding-range/fixtures/script/expectedv2.json rename packages/language-server/test/plugins/typescript/{testfiles/folding-range/folding.svelte => features/folding-range/fixtures/script/input.svelte} (59%) create mode 100644 packages/language-server/test/plugins/typescript/features/folding-range/index.test.ts diff --git a/packages/language-server/test/plugins/typescript/features/FoldingRangeProvider.test.ts b/packages/language-server/test/plugins/typescript/features/FoldingRangeProvider.test.ts deleted file mode 100644 index f8284e90c..000000000 --- a/packages/language-server/test/plugins/typescript/features/FoldingRangeProvider.test.ts +++ /dev/null @@ -1,104 +0,0 @@ -import * as assert from 'assert'; -import * as path from 'path'; -import ts from 'typescript'; -import { FoldingRange, FoldingRangeKind } from 'vscode-languageserver'; -import { Document, DocumentManager } from '../../../../src/lib/documents'; -import { LSConfigManager } from '../../../../src/ls-config'; -import { FoldingRangeProviderImpl } from '../../../../src/plugins/typescript/features/FoldingRangeProvider'; -import { LSAndTSDocResolver } from '../../../../src/plugins/typescript/LSAndTSDocResolver'; -import { __resetCache } from '../../../../src/plugins/typescript/service'; -import { pathToUrl } from '../../../../src/utils'; -import { serviceWarmup } from '../test-utils'; - -const testDir = path.join(__dirname, '..'); -const foldingTestDir = path.join(testDir, 'testfiles', 'folding-range'); - -describe('FoldingRangeProvider', function () { - serviceWarmup(this, foldingTestDir, pathToUrl(testDir)); - - function setup(filename: string) { - const docManager = new DocumentManager( - (textDocument) => new Document(textDocument.uri, textDocument.text) - ); - const lsConfigManager = new LSConfigManager(); - - lsConfigManager.updateClientCapabilities({ - textDocument: { foldingRange: { lineFoldingOnly: true } } - }); - const lsAndTsDocResolver = new LSAndTSDocResolver(docManager, [testDir], lsConfigManager); - const provider = new FoldingRangeProviderImpl(lsAndTsDocResolver, lsConfigManager); - const filePath = path.join(foldingTestDir, filename); - const document = docManager.openClientDocument({ - uri: pathToUrl(filePath), - text: ts.sys.readFile(filePath) || '' - }); - return { provider, document }; - } - - it('provides folding', async () => { - const { provider, document } = setup('folding.svelte'); - - const foldingRanges = await provider.getFoldingRanges(document); - - assert.deepStrictEqual(foldingRanges, [ - { - startLine: 1, - endLine: 2, - startCharacter: undefined, - kind: FoldingRangeKind.Imports, - endCharacter: undefined - }, - { - startLine: 4, - endLine: 5, - startCharacter: undefined, - kind: undefined, - endCharacter: undefined - }, - { - startLine: 9, - endLine: 10, - startCharacter: undefined, - kind: undefined, - endCharacter: undefined - }, - { - startLine: 11, - endLine: 12, - startCharacter: undefined, - kind: undefined, - endCharacter: undefined - } - ]); - }); - - it('provides folding during parser error', async () => { - const { provider, document } = setup('folding-parser-error.svelte'); - - const foldingRanges = await provider.getFoldingRanges(document); - - assert.deepStrictEqual(foldingRanges, [ - { - startLine: 1, - endLine: 2, - startCharacter: undefined, - kind: undefined, - endCharacter: undefined - }, - { - startLine: 6, - endLine: 7, - startCharacter: undefined, - kind: undefined, - endCharacter: undefined - }, - { - startLine: 8, - endLine: 9, - startCharacter: undefined, - kind: undefined, - endCharacter: undefined - } - ]); - }); -}); diff --git a/packages/language-server/test/plugins/typescript/features/folding-range/.gitignore b/packages/language-server/test/plugins/typescript/features/folding-range/.gitignore new file mode 100644 index 000000000..c3bc17626 --- /dev/null +++ b/packages/language-server/test/plugins/typescript/features/folding-range/.gitignore @@ -0,0 +1 @@ +debug.svelte \ No newline at end of file diff --git a/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/await-catch/expectedv2.json b/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/await-catch/expectedv2.json new file mode 100644 index 000000000..98bf02d30 --- /dev/null +++ b/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/await-catch/expectedv2.json @@ -0,0 +1,4 @@ +[ + { "startLine": 0, "endLine": 1 }, + { "startLine": 4, "endLine": 7 } +] diff --git a/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/await-catch/input.svelte b/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/await-catch/input.svelte new file mode 100644 index 000000000..e9e6d5619 --- /dev/null +++ b/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/await-catch/input.svelte @@ -0,0 +1,9 @@ +{#await somePromise catch error} +

Promise Pending

+{/await} + +{#await somePromise + +catch error} +

Promise Pending

+{/await} \ No newline at end of file diff --git a/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/await-peding-catch/expectedv2.json b/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/await-peding-catch/expectedv2.json new file mode 100644 index 000000000..ff07652a2 --- /dev/null +++ b/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/await-peding-catch/expectedv2.json @@ -0,0 +1,4 @@ +[ + { "startLine": 0, "endLine": 1 }, + { "startLine": 6, "endLine": 7 } +] diff --git a/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/await-peding-catch/input.svelte b/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/await-peding-catch/input.svelte new file mode 100644 index 000000000..2345b706b --- /dev/null +++ b/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/await-peding-catch/input.svelte @@ -0,0 +1,12 @@ +{#await somePromise} +

Promise Pending

+{:catch error} +

Promise Errored {error}

+{/await} + +{#await somePromise} +

Promise Pending

+{ + :catch error} +

Promise Errored {error}

+{/await} \ No newline at end of file diff --git a/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/await-pending-only/expectedv2.json b/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/await-pending-only/expectedv2.json new file mode 100644 index 000000000..fe51488c7 --- /dev/null +++ b/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/await-pending-only/expectedv2.json @@ -0,0 +1 @@ +[] diff --git a/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/await-pending-only/input.svelte b/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/await-pending-only/input.svelte new file mode 100644 index 000000000..cc4ccd6f6 --- /dev/null +++ b/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/await-pending-only/input.svelte @@ -0,0 +1,3 @@ +{#await somePromise} +

Loading

+{/await} \ No newline at end of file diff --git a/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/await-pending-then-catch/expectedv2.json b/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/await-pending-then-catch/expectedv2.json new file mode 100644 index 000000000..a621896be --- /dev/null +++ b/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/await-pending-then-catch/expectedv2.json @@ -0,0 +1,8 @@ +[ + { "startLine": 2, "endLine": 3 }, + { "startLine": 0, "endLine": 1 }, + { "startLine": 4, "endLine": 5 }, + { "startLine": 10, "endLine": 12 }, + { "startLine": 8, "endLine": 9 }, + { "startLine": 13, "endLine": 15 } +] diff --git a/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/await-pending-then-catch/input.svelte b/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/await-pending-then-catch/input.svelte new file mode 100644 index 000000000..af0e22c8e --- /dev/null +++ b/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/await-pending-then-catch/input.svelte @@ -0,0 +1,17 @@ +{#await somePromise} +

Promise Pending

+{:then value} +

Promise Resolved {value}

+{:catch error} +

Promise Errored {error}

+{/await} + +{#await somePromise} +

Promise Pending

+{ + :then value} +

Promise Resolved {value}

+{ + :catch error} +

Promise Errored {error}

+{/await} diff --git a/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/await-pending-then/expectedv2.json b/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/await-pending-then/expectedv2.json new file mode 100644 index 000000000..ee866a024 --- /dev/null +++ b/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/await-pending-then/expectedv2.json @@ -0,0 +1 @@ +[{ "startLine": 0, "endLine": 1 }] diff --git a/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/await-pending-then/input.svelte b/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/await-pending-then/input.svelte new file mode 100644 index 000000000..edd46fdb1 --- /dev/null +++ b/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/await-pending-then/input.svelte @@ -0,0 +1,5 @@ +{#await somePromise} +

Promise Pending

+{:catch error} +

Promise Errored {error}

+{/await} \ No newline at end of file diff --git a/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/await-then/expectedv2.json b/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/await-then/expectedv2.json new file mode 100644 index 000000000..98bf02d30 --- /dev/null +++ b/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/await-then/expectedv2.json @@ -0,0 +1,4 @@ +[ + { "startLine": 0, "endLine": 1 }, + { "startLine": 4, "endLine": 7 } +] diff --git a/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/await-then/input.svelte b/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/await-then/input.svelte new file mode 100644 index 000000000..9708ebccc --- /dev/null +++ b/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/await-then/input.svelte @@ -0,0 +1,9 @@ +{#await somePromise then value} +

Promise Pending

+{/await} + +{#await somePromise + + then value} +

Promise Pending

+{/await} \ No newline at end of file diff --git a/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/each-block/expectedv2.json b/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/each-block/expectedv2.json new file mode 100644 index 000000000..d94ee7104 --- /dev/null +++ b/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/each-block/expectedv2.json @@ -0,0 +1,4 @@ +[ + { "startLine": 0, "endLine": 1 }, + { "startLine": 4, "endLine": 5 } +] diff --git a/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/each-block/input.svelte b/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/each-block/input.svelte new file mode 100644 index 000000000..5324264c2 --- /dev/null +++ b/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/each-block/input.svelte @@ -0,0 +1,9 @@ +{#each items as item} + {item} +{/each} + +{#each items as item} + {item} +{:else} + no items +{/each} \ No newline at end of file diff --git a/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/if-block/expectedv2.json b/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/if-block/expectedv2.json new file mode 100644 index 000000000..1a70a9ed7 --- /dev/null +++ b/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/if-block/expectedv2.json @@ -0,0 +1,8 @@ +[ + { "startLine": 0, "endLine": 1 }, + { "startLine": 2, "endLine": 3 }, + { "startLine": 4, "endLine": 5 }, + { "startLine": 8, "endLine": 9 }, + { "startLine": 12, "endLine": 13 }, + { "startLine": 14, "endLine": 15 } +] diff --git a/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/if-block/input.svelte b/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/if-block/input.svelte new file mode 100644 index 000000000..0444a9773 --- /dev/null +++ b/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/if-block/input.svelte @@ -0,0 +1,17 @@ +{#if name1 == "world"} +

Hello {name2}

+{:else if name3 == "person"} +

hello {name4}

+{:else} +

hey {name5}

+{/if} + +{#if kenobi} +

Hello There

+{/if} + +{#if name1 = 'hi'} +

hi

+{:else} +

hello

+{/if} \ No newline at end of file diff --git a/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/parser-error/expectedv2.json b/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/parser-error/expectedv2.json new file mode 100644 index 000000000..4b0d4a657 --- /dev/null +++ b/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/parser-error/expectedv2.json @@ -0,0 +1,5 @@ +[ + { "startLine": 1, "endLine": 2 }, + { "startLine": 6, "endLine": 7 }, + { "startLine": 8, "endLine": 9 } +] diff --git a/packages/language-server/test/plugins/typescript/testfiles/folding-range/folding-parser-error.svelte b/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/parser-error/input.svelte similarity index 94% rename from packages/language-server/test/plugins/typescript/testfiles/folding-range/folding-parser-error.svelte rename to packages/language-server/test/plugins/typescript/features/folding-range/fixtures/parser-error/input.svelte index 93393525d..e2c5abde5 100644 --- a/packages/language-server/test/plugins/typescript/testfiles/folding-range/folding-parser-error.svelte +++ b/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/parser-error/input.svelte @@ -8,4 +8,4 @@
{:else}
-{/if} +{/if diff --git a/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/script/expectedv2.json b/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/script/expectedv2.json new file mode 100644 index 000000000..395632733 --- /dev/null +++ b/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/script/expectedv2.json @@ -0,0 +1,4 @@ +[ + { "startLine": 1, "endLine": 2, "kind": "imports" }, + { "startLine": 4, "endLine": 5 } +] diff --git a/packages/language-server/test/plugins/typescript/testfiles/folding-range/folding.svelte b/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/script/input.svelte similarity index 59% rename from packages/language-server/test/plugins/typescript/testfiles/folding-range/folding.svelte rename to packages/language-server/test/plugins/typescript/features/folding-range/fixtures/script/input.svelte index 153d8ee30..3046b7cd6 100644 --- a/packages/language-server/test/plugins/typescript/testfiles/folding-range/folding.svelte +++ b/packages/language-server/test/plugins/typescript/features/folding-range/fixtures/script/input.svelte @@ -5,10 +5,4 @@ function hi() { } - - -{#if 'hi'} -
- {:else} -
-{/if} + \ No newline at end of file diff --git a/packages/language-server/test/plugins/typescript/features/folding-range/index.test.ts b/packages/language-server/test/plugins/typescript/features/folding-range/index.test.ts new file mode 100644 index 000000000..aefd82e11 --- /dev/null +++ b/packages/language-server/test/plugins/typescript/features/folding-range/index.test.ts @@ -0,0 +1,122 @@ +import * as assert from 'assert'; +import { readFileSync, writeFileSync } from 'fs'; +import { join } from 'path'; +import ts from 'typescript'; +import { Document, DocumentManager } from '../../../../../src/lib/documents'; +import { LSConfigManager } from '../../../../../src/ls-config'; +import { LSAndTSDocResolver } from '../../../../../src/plugins'; +import { FoldingRangeProviderImpl } from '../../../../../src/plugins/typescript/features/FoldingRangeProvider'; +import { pathToUrl } from '../../../../../src/utils'; +import { + createJsonSnapshotFormatter, + createSnapshotTester, + updateSnapshotIfFailedOrEmpty +} from '../../test-utils'; + +function setup(workspaceDir: string, filePath: string) { + const docManager = new DocumentManager( + (textDocument) => new Document(textDocument.uri, textDocument.text) + ); + const configManager = new LSConfigManager(); + configManager.updateClientCapabilities({ + textDocument: { foldingRange: { lineFoldingOnly: true } } + }); + const lsAndTsDocResolver = new LSAndTSDocResolver( + docManager, + [pathToUrl(workspaceDir)], + configManager + ); + const plugin = new FoldingRangeProviderImpl(lsAndTsDocResolver, configManager); + const document = docManager.openClientDocument({ + uri: pathToUrl(filePath), + text: ts.sys.readFile(filePath) || '' + }); + return { plugin, document, docManager, lsAndTsDocResolver }; +} + +async function executeTest( + inputFile: string, + { + workspaceDir, + dir + }: { + workspaceDir: string; + dir: string; + } +) { + const expected = 'expectedv2.json'; + const { plugin, document } = setup(workspaceDir, inputFile); + const folding = await plugin.getFoldingRanges(document); + + const expectedFile = join(dir, expected); + if (process.argv.includes('--debug')) { + writeFileSync(join(dir, 'debug.svelte'), appendFoldingAsComment()); + } + + const snapshotFormatter = await createJsonSnapshotFormatter(dir); + + await updateSnapshotIfFailedOrEmpty({ + assertion() { + assert.deepStrictEqual( + JSON.parse(JSON.stringify(folding)), + JSON.parse(readFileSync(expectedFile, 'utf-8')) + ); + }, + expectedFile, + getFileContent() { + return snapshotFormatter(folding); + }, + rootDir: __dirname + }); + + function appendFoldingAsComment() { + if (!folding) { + return document.getText(); + } + + const offsetMap = new Map(); + const lineLength = document + .getText() + .split('\n') + .map((line) => (line[line.length - 1] === '\r' ? line.length - 1 : line.length)); + + for (const fold of folding) { + const startOffset = document.offsetAt({ + line: fold.startLine, + character: lineLength[fold.startLine] + }); + const endOffset = document.offsetAt({ + line: fold.endLine, + character: lineLength[fold.endLine] + }); + + offsetMap.set(startOffset, (offsetMap.get(startOffset) ?? []).concat(`/*s*/`)); + offsetMap.set(endOffset, (offsetMap.get(endOffset) ?? []).concat(`/*e*/`)); + } + + const offsets = Array.from(offsetMap.keys()).sort((a, b) => a - b); + const parts: string[] = []; + + for (let index = 0; index < offsets.length; index++) { + const offset = offsets[index]; + parts.push( + document.getText().slice(offsets[index - 1], offset), + ...(offsetMap.get(offset) ?? []) + ); + } + + parts.push(document.getText().slice(offsets[offsets.length - 1])); + + return parts.join(''); + } +} + +const executeTests = createSnapshotTester(executeTest); + +describe.only('FoldingRangeProvider', function () { + executeTests({ + dir: join(__dirname, 'fixtures'), + workspaceDir: join(__dirname, 'fixtures'), + context: this + }); +});