Skip to content

Commit

Permalink
split TestSpecFile and ReadmeFile
Browse files Browse the repository at this point in the history
  • Loading branch information
kainino0x committed Jul 8, 2019
1 parent 08a2a2b commit df985da
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 22 deletions.
21 changes: 13 additions & 8 deletions src/framework/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,24 @@ import { TestSuiteListing } from './listing.js';
import { TestSpecID, TestCaseID } from './id.js';

// One of the following:
// - A shell object describing a directory (from its README.txt).
// - An actual .spec.ts file, as imported.
// - A *filtered* list of cases from a single .spec.ts file.
export interface TestSpecFile {
readonly description: string;
// undefined for README.txt, defined for a test module.
readonly g?: RunCaseIterable;
readonly g: RunCaseIterable;
}

// A shell object describing a directory (from its README.txt).
export interface ReadmeFile {
readonly description: string;
}

export type TestSpecOrReadme = TestSpecFile | ReadmeFile;

// A pending loaded spec (.spec.ts) file, plus identifying information.
export interface TestQueryResult {
readonly id: TestSpecID;
readonly spec: Promise<TestSpecFile>;
readonly spec: Promise<TestSpecOrReadme>;
}

type TestQueryResults = IterableIterator<TestQueryResult>;
Expand All @@ -44,7 +49,7 @@ function filterTestGroup(group: RunCaseIterable, filter: TestGroupFilter): RunCa

export interface TestFileLoader {
listing(suite: string): Promise<TestSuiteListing>;
import(path: string): Promise<TestSpecFile>;
import(path: string): Promise<TestSpecOrReadme>;
}

class DefaultTestFileLoader implements TestFileLoader {
Expand Down Expand Up @@ -156,9 +161,9 @@ export class TestLoader {
for (const { path, description } of specs) {
if (path.startsWith(groupPrefix)) {
const isReadme = path === '' || path.endsWith('/');
const spec: Promise<TestSpecFile> = isReadme
? Promise.resolve({ description })
: this.fileLoader.import(`${suite}/${path}.spec.js`);
const spec = isReadme
? Promise.resolve({ description } as ReadmeFile)
: (this.fileLoader.import(`${suite}/${path}.spec.js`) as Promise<TestSpecFile>);
entries.push({ id: { suite, path }, spec });
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/runtime/cmdline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import * as fs from 'fs';
import * as process from 'process';

import { TestSpecFile, TestLoader } from '../framework/loader.js';
import { TestLoader, TestSpecOrReadme } from '../framework/loader.js';
import { Logger, LiveTestCaseResult } from '../framework/logger.js';
import { TestSpecID } from '../framework/id.js';
import { makeQueryString } from '../framework/url_query.js';
Expand Down Expand Up @@ -45,7 +45,7 @@ for (const a of process.argv.slice(2)) {

const log = new Logger();
const queryResults = await Promise.all(
Array.from(listing, ({ id, spec }) => spec.then((s: TestSpecFile) => ({ id, spec: s })))
Array.from(listing, ({ id, spec }) => spec.then((s: TestSpecOrReadme) => ({ id, spec: s })))
);

const failed: Array<[TestSpecID, LiveTestCaseResult]> = [];
Expand All @@ -54,7 +54,7 @@ for (const a of process.argv.slice(2)) {
// TODO: don't run all tests all at once
const running = [];
for (const qr of queryResults) {
if (!qr.spec.g) {
if (!('g' in qr.spec)) {
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/standalone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ function mkCase(testcasesVis: HTMLElement, query: string, t: RunCase) {
for (const qr of queryResults) {
const testcasesVis = makeTest(qr.id, qr.spec.description);

if (!qr.spec.g) {
if (!('g' in qr.spec)) {
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/wpt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ declare function async_test(f: (this: WptTestObject) => Promise<void>, name: str
);

for (const qr of queryResults) {
if (!qr.spec.g) {
if (!('g' in qr.spec)) {
continue;
}

Expand Down
18 changes: 9 additions & 9 deletions src/suites/unittests/loading.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
TestGroup,
RunCase,
} from '../../framework/index.js';
import { TestSpecFile, TestLoader, TestFileLoader } from '../../framework/loader.js';
import { TestLoader, TestFileLoader, TestSpecOrReadme } from '../../framework/loader.js';
import { TestSuiteListingEntry, TestSuiteListing } from '../../framework/listing.js';
import { Logger } from '../../framework/logger.js';

Expand All @@ -24,7 +24,7 @@ const listingData: { [k: string]: TestSuiteListingEntry[] } = {
suite2: [{ path: '', description: 'desc 2a' }, { path: 'foof', description: 'desc 2b' }],
};

const specsData: { [k: string]: TestSpecFile } = {
const specsData: { [k: string]: TestSpecOrReadme } = {
'suite1/README.txt': { description: 'desc 1a' },
'suite1/foo.spec.js': {
description: 'desc 1b',
Expand Down Expand Up @@ -77,7 +77,7 @@ class FakeTestFileLoader implements TestFileLoader {
return listingData[suite];
}

async import(path: string): Promise<TestSpecFile> {
async import(path: string): Promise<TestSpecOrReadme> {
if (!specsData.hasOwnProperty(path)) {
throw new Error('[test] mock file ' + path + ' does not exist');
}
Expand All @@ -91,7 +91,7 @@ class LoadingTest extends DefaultFixture {
async load(filters: string[]) {
const listing = await this.loader.loadTests(filters);
const entries = Promise.all(
Array.from(listing, ({ id, spec }) => spec.then((s: TestSpecFile) => ({ id, spec: s })))
Array.from(listing, ({ id, spec }) => spec.then((s: TestSpecOrReadme) => ({ id, spec: s })))
);
return entries;
}
Expand All @@ -102,11 +102,11 @@ class LoadingTest extends DefaultFixture {
if (a.length !== 1) {
throw new Error('more than one group');
}
const g = a[0].spec.g;
if (g === undefined) {
const spec = a[0].spec;
if (!('g' in spec)) {
throw new Error('group undefined');
}
return Array.from(g.iterate(rec));
return Array.from(spec.g.iterate(rec));
}
}

Expand Down Expand Up @@ -139,7 +139,7 @@ g.test('whole group', async t => {
const foo = (await t.load(['suite1:foo:']))[0];
t.expect(foo.id.suite === 'suite1');
t.expect(foo.id.path === 'foo');
if (foo.spec.g === undefined) {
if (!('g' in foo.spec)) {
throw new Error('foo group');
}
const [rec] = new Logger().record({ suite: '', path: '' });
Expand Down Expand Up @@ -182,7 +182,7 @@ g.test('end2end', async t => {
t.expect(l[0].id.suite === 'suite2');
t.expect(l[0].id.path === 'foof');
t.expect(l[0].spec.description === 'desc 2b');
if (l[0].spec.g === undefined) {
if (!('g' in l[0].spec)) {
throw new Error();
}
t.expect(l[0].spec.g.iterate instanceof Function);
Expand Down

0 comments on commit df985da

Please sign in to comment.