Skip to content

Commit

Permalink
[labs/analyzer] Factor setupAnalyzerForTest preamble out of tests (#3661
Browse files Browse the repository at this point in the history
)

* Factor setupAnalyzerForTest preamble out of tests.

* Separate module setup into separate helper.
  • Loading branch information
kevinpschaaf committed Feb 23, 2023
1 parent d4b2f43 commit b0c3f82
Show file tree
Hide file tree
Showing 12 changed files with 174 additions and 312 deletions.
2 changes: 2 additions & 0 deletions .changeset/soft-foxes-protect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
27 changes: 9 additions & 18 deletions packages/labs/analyzer/src/test/analyzer_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,19 @@ import {suite} from 'uvu';
// eslint-disable-next-line import/extensions
import * as assert from 'uvu/assert';
import * as path from 'path';
import {fileURLToPath} from 'url';
import {getOutputFilename, getSourceFilename, languages} from './utils.js';

import {createPackageAnalyzer, Analyzer, AbsolutePath} from '../index.js';
import {
AnalyzerTestContext,
getOutputFilename,
getSourceFilename,
languages,
setupAnalyzerForTest,
} from './utils.js';

for (const lang of languages) {
const test = suite<{analyzer: Analyzer; packagePath: AbsolutePath}>(
`Basic Analyzer tests (${lang})`
);
const test = suite<AnalyzerTestContext>(`Basic Analyzer tests (${lang})`);

test.before((ctx) => {
try {
const packagePath = (ctx.packagePath = fileURLToPath(
new URL(`../test-files/${lang}/basic-elements`, import.meta.url).href
) as AbsolutePath);
ctx.analyzer = createPackageAnalyzer(packagePath);
} catch (error) {
// Uvu has a bug where it silently ignores failures in before and after,
// see https://github.com/lukeed/uvu/issues/191.
console.error('uvu before error', error);
process.exit(1);
}
setupAnalyzerForTest(ctx, lang, 'basic-elements');
});

test('Reads project files', ({analyzer, packagePath}) => {
Expand Down
41 changes: 6 additions & 35 deletions packages/labs/analyzer/src/test/javascript/functions_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,17 @@
import {suite} from 'uvu';
// eslint-disable-next-line import/extensions
import * as assert from 'uvu/assert';
import {fileURLToPath} from 'url';
import {getSourceFilename, languages} from '../utils.js';

import {
createPackageAnalyzer,
Analyzer,
AbsolutePath,
Module,
} from '../../index.js';
AnalyzerModuleTestContext,
languages,
setupAnalyzerForTestWithModule,
} from '../utils.js';

for (const lang of languages) {
const test = suite<{
analyzer: Analyzer;
packagePath: AbsolutePath;
module: Module;
}>(`Module tests (${lang})`);
const test = suite<AnalyzerModuleTestContext>(`Function tests (${lang})`);

test.before((ctx) => {
try {
const packagePath = fileURLToPath(
new URL(`../../test-files/${lang}/functions`, import.meta.url).href
) as AbsolutePath;
const analyzer = createPackageAnalyzer(packagePath);

const result = analyzer.getPackage();
const file = getSourceFilename('functions', lang);
const module = result.modules.find((m) => m.sourcePath === file);
if (module === undefined) {
throw new Error(`Analyzer did not analyze file '${file}'`);
}

ctx.packagePath = packagePath;
ctx.analyzer = analyzer;
ctx.module = module;
} catch (error) {
// Uvu has a bug where it silently ignores failures in before and after,
// see https://github.com/lukeed/uvu/issues/191.
console.error('uvu before error', error);
process.exit(1);
}
setupAnalyzerForTestWithModule(ctx, lang, 'functions', 'functions');
});

test('Function 1', ({module}) => {
Expand Down
45 changes: 10 additions & 35 deletions packages/labs/analyzer/src/test/javascript/modules_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,21 @@ import {suite} from 'uvu';
// eslint-disable-next-line import/extensions
import * as assert from 'uvu/assert';
import path from 'path';
import {fileURLToPath} from 'url';
import {getSourceFilename, InMemoryAnalyzer, languages} from '../utils.js';

import {
createPackageAnalyzer,
Analyzer,
AbsolutePath,
Module,
} from '../../index.js';
AnalyzerModuleTestContext,
getSourceFilename,
InMemoryAnalyzer,
languages,
setupAnalyzerForTestWithModule,
} from '../utils.js';

import {AbsolutePath} from '../../index.js';

for (const lang of languages) {
const test = suite<{
analyzer: Analyzer;
packagePath: AbsolutePath;
module: Module;
}>(`Module tests (${lang})`);
const test = suite<AnalyzerModuleTestContext>(`Module tests (${lang})`);

test.before((ctx) => {
try {
const packagePath = fileURLToPath(
new URL(`../../test-files/${lang}/modules`, import.meta.url).href
) as AbsolutePath;
const analyzer = createPackageAnalyzer(packagePath);

const result = analyzer.getPackage();
const file = getSourceFilename('module-a', lang);
const module = result.modules.find((m) => m.sourcePath === file);
if (module === undefined) {
throw new Error(`Analyzer did not analyze file '${file}'`);
}

ctx.packagePath = packagePath;
ctx.analyzer = analyzer;
ctx.module = module;
} catch (error) {
// Uvu has a bug where it silently ignores failures in before and after,
// see https://github.com/lukeed/uvu/issues/191.
console.error('uvu before error', error);
process.exit(1);
}
setupAnalyzerForTestWithModule(ctx, lang, 'modules', 'module-a');
});

test('Dependencies correctly analyzed', ({module}) => {
Expand Down
50 changes: 14 additions & 36 deletions packages/labs/analyzer/src/test/lit-element/events_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,25 @@
import {suite} from 'uvu';
// eslint-disable-next-line import/extensions
import * as assert from 'uvu/assert';
import {fileURLToPath} from 'url';
import {getSourceFilename, languages} from '../utils.js';

import {LitElementDeclaration} from '../../lib/model.js';
import {
createPackageAnalyzer,
Analyzer,
AbsolutePath,
LitElementDeclaration,
} from '../../index.js';
AnalyzerModuleTestContext,
languages,
setupAnalyzerForTestWithModule,
} from '../utils.js';

interface TestContext extends AnalyzerModuleTestContext {
element: LitElementDeclaration;
}

for (const lang of languages) {
const test = suite<{
analyzer: Analyzer;
packagePath: AbsolutePath;
element: LitElementDeclaration;
}>(`LitElement event tests (${lang})`);
const test = suite<TestContext>(`LitElement event tests (${lang})`);

test.before((ctx) => {
try {
const packagePath = fileURLToPath(
new URL(`../../test-files/${lang}/events`, import.meta.url).href
) as AbsolutePath;
const analyzer = createPackageAnalyzer(packagePath);

const result = analyzer.getPackage();
const elementAModule = result.modules.find(
(m) => m.sourcePath === getSourceFilename('element-a', lang)
);
const element = elementAModule!.declarations.filter((d) =>
d.isLitElementDeclaration()
)[0] as LitElementDeclaration;

ctx.packagePath = packagePath;
ctx.analyzer = analyzer;
ctx.element = element;
} catch (error) {
// Uvu has a bug where it silently ignores failures in before and after,
// see https://github.com/lukeed/uvu/issues/191.
console.error('uvu before error', error);
process.exit(1);
}
setupAnalyzerForTestWithModule(ctx, lang, 'events', 'element-a');
ctx.element = ctx.module.declarations.find((d) =>
d.isLitElementDeclaration()
) as LitElementDeclaration;
});

test('Correct number of events found', ({element}) => {
Expand Down
34 changes: 10 additions & 24 deletions packages/labs/analyzer/src/test/lit-element/jsdoc_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,21 @@
import {suite} from 'uvu';
// eslint-disable-next-line import/extensions
import * as assert from 'uvu/assert';
import {fileURLToPath} from 'url';
import {getSourceFilename, InMemoryAnalyzer, languages} from '../utils.js';
import {
AnalyzerTestContext,
getSourceFilename,
InMemoryAnalyzer,
languages,
setupAnalyzerForTest,
} from '../utils.js';

import {createPackageAnalyzer, Module, AbsolutePath} from '../../index.js';
import {AbsolutePath} from '../../index.js';

for (const lang of languages) {
const test = suite<{
getModule: (name: string) => Module;
}>(`LitElement event tests (${lang})`);
const test = suite<AnalyzerTestContext>(`JSDoc tests (${lang})`);

test.before((ctx) => {
try {
const packagePath = fileURLToPath(
new URL(`../../test-files/${lang}/jsdoc`, import.meta.url).href
) as AbsolutePath;
const analyzer = createPackageAnalyzer(packagePath);
ctx.getModule = (name: string) =>
analyzer.getModule(
getSourceFilename(
analyzer.path.join(packagePath, name),
lang
) as AbsolutePath
);
} catch (error) {
// Uvu has a bug where it silently ignores failures in before and after,
// see https://github.com/lukeed/uvu/issues/191.
console.error('uvu before error', error);
process.exit(1);
}
setupAnalyzerForTest(ctx, lang, 'jsdoc');
});

// slots
Expand Down
61 changes: 18 additions & 43 deletions packages/labs/analyzer/src/test/lit-element/lit-element_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,63 +7,43 @@
import {suite} from 'uvu';
// eslint-disable-next-line import/extensions
import * as assert from 'uvu/assert';
import {fileURLToPath} from 'url';
import {getSourceFilename, languages} from '../utils.js';

import {createPackageAnalyzer, Analyzer, AbsolutePath} from '../../index.js';
import {
AnalyzerTestContext,
languages,
setupAnalyzerForTest,
} from '../utils.js';

// Get actual constructor to test internal ability to assert the type
// of a dereferenced Declaration
import {ClassDeclaration, LitElementDeclaration} from '../../lib/model.js';

for (const lang of languages) {
const test = suite<{analyzer: Analyzer; packagePath: AbsolutePath}>(
`LitElement tests (${lang})`
);
const test = suite<AnalyzerTestContext>(`LitElement tests (${lang})`);

test.before((ctx) => {
try {
const packagePath = (ctx.packagePath = fileURLToPath(
new URL(`../../test-files/${lang}/basic-elements`, import.meta.url).href
) as AbsolutePath);
ctx.analyzer = createPackageAnalyzer(packagePath);
} catch (error) {
// Uvu has a bug where it silently ignores failures in before and after,
// see https://github.com/lukeed/uvu/issues/191.
console.error('uvu before error', error);
process.exit(1);
}
setupAnalyzerForTest(ctx, lang, 'basic-elements');
});

test('isLitElementDeclaration returns false for non-LitElement', ({
analyzer,
getModule,
}) => {
const result = analyzer.getPackage();
const elementAModule = result.modules.find(
(m) => m.sourcePath === getSourceFilename('not-lit', lang)
);
const elementAModule = getModule('not-lit');
const decl = elementAModule?.getDeclaration('NotLit');
assert.ok(decl);
assert.equal(decl.isLitElementDeclaration(), false);
});

test('Analyzer finds LitElement declarations', ({analyzer}) => {
const result = analyzer.getPackage();
const elementAModule = result.modules.find(
(m) => m.sourcePath === getSourceFilename('element-a', lang)
);
test('Analyzer finds LitElement declarations', ({getModule}) => {
const elementAModule = getModule('element-a');
assert.equal(elementAModule?.declarations.length, 1);
const decl = elementAModule!.declarations[0];
assert.equal(decl.name, 'ElementA');
assert.ok(decl.isLitElementDeclaration());
assert.equal(decl.tagname, 'element-a');
});

test('Analyzer finds LitElement properties ', ({analyzer}) => {
const result = analyzer.getPackage();
const elementAModule = result.modules.find(
(m) => m.sourcePath === getSourceFilename('element-a', lang)
);
test('Analyzer finds LitElement properties ', ({getModule}) => {
const elementAModule = getModule('element-a');
const decl = elementAModule?.getDeclaration('ElementA');
assert.ok(decl?.isLitElementDeclaration());

Expand Down Expand Up @@ -94,12 +74,10 @@ for (const lang of languages) {
});

test('Analyzer finds LitElement properties from static getter', ({
analyzer,
getModule,
}) => {
const result = analyzer.getPackage();
const elementBModule = result.modules.find(
(m) => m.sourcePath === getSourceFilename('element-b', lang)
);
const elementBModule = getModule('element-b');

const decl = elementBModule?.getDeclaration('ElementB');
assert.ok(decl?.isLitElementDeclaration());

Expand All @@ -123,11 +101,8 @@ for (const lang of languages) {
assert.equal(bProp.typeOption, 'Number');
});

test('Analyezr finds subclass of LitElement', ({analyzer}) => {
const result = analyzer.getPackage();
const module = result.modules.find(
(m) => m.sourcePath === getSourceFilename('element-c', lang)
);
test('Analyezr finds subclass of LitElement', ({getModule}) => {
const module = getModule('element-c');
const elementC = module?.getDeclaration('ElementC');
assert.ok(elementC?.isLitElementDeclaration());

Expand Down

0 comments on commit b0c3f82

Please sign in to comment.