Skip to content

Commit

Permalink
switch to snapshot test
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonlyu123 committed Oct 3, 2023
1 parent 86230c7 commit aa81c16
Show file tree
Hide file tree
Showing 23 changed files with 249 additions and 112 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
debug.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[
{ "startLine": 0, "endLine": 1 },
{ "startLine": 4, "endLine": 7 }
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{#await somePromise catch error}
<h1>Promise Pending</h1>
{/await}

{#await somePromise

catch error}
<h1>Promise Pending</h1>
{/await}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[
{ "startLine": 0, "endLine": 1 },
{ "startLine": 6, "endLine": 7 }
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{#await somePromise}
<h1>Promise Pending</h1>
{:catch error}
<h1>Promise Errored {error}</h1>
{/await}

{#await somePromise}
<h1>Promise Pending</h1>
{
:catch error}
<h1>Promise Errored {error}</h1>
{/await}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{#await somePromise}
<h1>Loading</h1>
{/await}
Original file line number Diff line number Diff line change
@@ -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 }
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{#await somePromise}
<h1>Promise Pending</h1>
{:then value}
<h1>Promise Resolved {value}</h1>
{:catch error}
<h1>Promise Errored {error}</h1>
{/await}

{#await somePromise}
<h1>Promise Pending</h1>
{
:then value}
<h1>Promise Resolved {value}</h1>
{
:catch error}
<h1>Promise Errored {error}</h1>
{/await}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{ "startLine": 0, "endLine": 1 }]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{#await somePromise}
<h1>Promise Pending</h1>
{:catch error}
<h1>Promise Errored {error}</h1>
{/await}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[
{ "startLine": 0, "endLine": 1 },
{ "startLine": 4, "endLine": 7 }
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{#await somePromise then value}
<h1>Promise Pending</h1>
{/await}

{#await somePromise

then value}
<h1>Promise Pending</h1>
{/await}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[
{ "startLine": 0, "endLine": 1 },
{ "startLine": 4, "endLine": 5 }
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{#each items as item}
{item}
{/each}

{#each items as item}
{item}
{:else}
no items
{/each}
Original file line number Diff line number Diff line change
@@ -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 }
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{#if name1 == "world"}
<h1>Hello {name2}</h1>
{:else if name3 == "person"}
<h2>hello {name4}</h2>
{:else}
<h3>hey {name5}</h3>
{/if}

{#if kenobi}
<h2>Hello There</h2>
{/if}

{#if name1 = 'hi'}
<h2>hi</h2>
{:else}
<h3>hello</h3>
{/if}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
{ "startLine": 1, "endLine": 2 },
{ "startLine": 6, "endLine": 7 },
{ "startLine": 8, "endLine": 9 }
]
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
<div></div>
{:else}
<div></div>
{/if}
{/if
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[
{ "startLine": 1, "endLine": 2, "kind": "imports" },
{ "startLine": 4, "endLine": 5 }
]
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,4 @@
function hi() {
}
</script>

{#if 'hi'}
<div></div>
{:else}
<div></div>
{/if}
</script>
Original file line number Diff line number Diff line change
@@ -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(<any>{
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<number, string[]>();
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
});
});

0 comments on commit aa81c16

Please sign in to comment.