Skip to content

Commit

Permalink
Introduces a StubLoader for tests (PolymerLabs#1387)
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrswigon authored May 28, 2018
1 parent bec3d58 commit f288ce5
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 73 deletions.
29 changes: 7 additions & 22 deletions runtime/test/particle-api-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,16 @@ import {Manifest} from '../manifest.js';
import {assert} from './chai-web.js';
import * as util from '../testing/test-util.js';
import {Arc} from '../arc.js';
import {MessageChannel} from '../message-channel.js';
import {InnerPEC} from '../inner-PEC.js';
import {Loader} from '../loader.js';
import {StubLoader} from '../testing/stub-loader.js';
import {TestHelper} from '../testing/test-helper.js';

async function loadFilesIntoNewArc(fileMap) {
let registry = {};
let loader = new class extends Loader {
loadResource(path) {
return fileMap[path];
}
path(fileName) {
return fileName;
}
join(_, file) {
return file;
}
const testHelper = new TestHelper({loader: new StubLoader(fileMap)});
await testHelper.loadManifest('manifest');
return {
arc: testHelper.arc,
manifest: testHelper.arc._context
};
let manifest = await Manifest.load('manifest', loader, {registry});
let pecFactory = function(id) {
let channel = new MessageChannel();
new InnerPEC(channel.port1, `${id}:inner`, loader);
return channel.port2;
};
let arc = new Arc({id: 'test', pecFactory, loader});
return {manifest, arc};
}

describe('particle-api', function() {
Expand Down
15 changes: 5 additions & 10 deletions runtime/test/particle-shape-loading-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {Arc} from '../arc.js';
import {MessageChannel} from '../message-channel.js';
import {InnerPEC} from '../inner-PEC.js';
import {Loader} from '../loader.js';
import {StubLoader} from '../testing/stub-loader.js';
import {Recipe} from '../recipe/recipe.js';
import {Type} from '../type.js';
import {Shape} from '../shape.js';
Expand All @@ -23,10 +24,8 @@ import {ParticleSpec} from '../particle-spec.js';
describe('particle-shape-loading', function() {

it('loads shapes into particles', async () => {
let loader = new class extends Loader {
loadResource(path) {
if (path == 'outer-particle.js')
return `
let loader = new StubLoader({
'outer-particle.js': `
"use strict";
defineParticle(({Particle}) => {
Expand Down Expand Up @@ -64,11 +63,7 @@ describe('particle-shape-loading', function() {
}
}
}
});
`;
return super.loadResource(path);
}
}();
});`});

let pecFactory = function(id) {
let channel = new MessageChannel();
Expand Down Expand Up @@ -153,7 +148,7 @@ describe('particle-shape-loading', function() {
input <- v1
`, {loader, fileName: './test.manifest'});

let arc = new Arc({id: 'test', pecFactory, context: manifest});
let arc = new Arc({id: 'test', pecFactory, context: manifest});

let fooType = manifest.findTypeByName('Foo');
let barType = manifest.findTypeByName('Bar');
Expand Down
13 changes: 4 additions & 9 deletions runtime/test/planner-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import {Arc} from '../arc.js';
import {Loader} from '../loader.js';
import {StubLoader} from '../testing/stub-loader.js';
import {Planner} from '../planner.js';
import {assert} from './chai-web.js';
import {Manifest} from '../manifest.js';
Expand Down Expand Up @@ -41,9 +42,9 @@ const assertRecipeResolved = recipe => {

const loadTestArcAndRunSpeculation = async (manifest, manifestLoadedCallback) => {
const registry = {};
const loader = new class extends Loader {
loadResource(path) {
return {manifest}[path];
const loader = new class extends StubLoader {
constructor() {
super({manifest});
}
async requireParticle(fileName) {
let clazz = class {
Expand All @@ -57,12 +58,6 @@ const loadTestArcAndRunSpeculation = async (manifest, manifestLoadedCallback) =>
};
return clazz;
}
path(fileName) {
return fileName;
}
join(_, file) {
return file;
}
};
const loadedManifest = await Manifest.load('manifest', loader, {registry});
manifestLoadedCallback(loadedManifest);
Expand Down
14 changes: 2 additions & 12 deletions runtime/test/recipe-resolver-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/

import {Arc} from '../arc.js';
import {Loader} from '../loader.js';
import {StubLoader} from '../testing/stub-loader.js';
import {Manifest} from '../manifest.js';
import {RecipeResolver} from '../recipe/recipe-resolver.js';

Expand All @@ -18,17 +18,7 @@ import {assert} from './chai-web.js';
describe('RecipeResolver', function() {
const buildRecipe = async (content) => {
let registry = {};
let loader = new class extends Loader {
loadResource(path) {
return content[path];
}
path(fileName) {
return fileName;
}
join(_, file) {
return file;
}
};
let loader = new StubLoader(content);
let manifest = await Manifest.load('manifest', loader, {registry});
return manifest.recipes[0];
};
Expand Down
18 changes: 5 additions & 13 deletions runtime/test/schema-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@
*/

import {assert} from './chai-web.js';
import {Loader} from '../loader.js';
import {StubLoader} from '../testing/stub-loader.js';
import {Manifest} from '../manifest.js';
import {Schema} from '../schema.js';

describe('schema', function() {
let loader = new class extends Loader {
loadResource(fileName) {
if (fileName == 'Product.schema') {
return `
let loader = new StubLoader({
'Product.schema': `
import './shell/artifacts/Things/Thing.schema'
schema Product extends Thing
Text category
Expand All @@ -37,14 +35,8 @@ describe('schema', function() {
schema AlienLife
Boolean isBasedOnDna
`;
}
return new Loader().loadResource(fileName);
}
join(_, file) {
return file;
}
};
`
});

it('schemas load recursively', async function() {
let manifest = await Manifest.load('Product.schema', loader);
Expand Down
8 changes: 4 additions & 4 deletions runtime/test/slot-composer-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {Manifest} from '../manifest.js';
import {Planner} from '../planner.js';
import {MessageChannel} from '../message-channel.js';
import {InnerPEC} from '../inner-PEC.js';
import {Loader} from '../loader.js';
import {StubLoader} from '../testing/stub-loader.js';
import * as util from '../testing/test-util.js';

class MockSlot extends Slot {
Expand All @@ -44,9 +44,9 @@ async function initSlotComposer(recipeStr) {
slotComposer._affordance._slotClass = MockSlot;

let manifest = (await Manifest.parse(recipeStr));
let loader = new class extends Loader {
loadResource(fileName) { return `defineParticle(({Particle}) => { return class P extends Particle {} });`; }
};
let loader = new StubLoader({
'*': `defineParticle(({Particle}) => { return class P extends Particle {} });`
});
const pecFactory = function(id) {
const channel = new MessageChannel();
new InnerPEC(channel.port1, `${id}:inner`, loader);
Expand Down
41 changes: 41 additions & 0 deletions runtime/testing/stub-loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* @license
* Copyright (c) 2018 Google Inc. All rights reserved.
* This code may only be used under the BSD style license found at
* http://polymer.github.io/LICENSE.txt
* Code distributed by Google as part of this project is also
* subject to an additional IP rights grant found at
* http://polymer.github.io/PATENTS.txt
*/
import {Loader} from '../loader.js';

/** @class StubLoader
* A Loader initialized with a per-path canned responses.
* Value for '*' key can be specified for a response if the path did not match.
* If '*' is not specified and path is not matched, Loader logic is invoked.
*/
export class StubLoader extends Loader {
constructor(fileMap) {
super();
this._fileMap = fileMap;
if (fileMap.hasOwnProperty('*')) {
this._cannedResponse = fileMap['*'];
}
}
loadResource(path) {
return this._fileMap.hasOwnProperty(path)
? this._fileMap[path]
: (this._cannedResponse || super.loadResource(path));
}
path(fileName) {
return (this._fileMap.hasOwnProperty(fileName) || this._cannedResponse)
? fileName
: super.path(fileName);
}
join(prefix, path) {
// If referring from stubbed content, don't prepend stubbed filename.
return (this._fileMap.hasOwnProperty(prefix) || this._cannedResponse)
? path
: super.join(prefix, path);
}
}
7 changes: 4 additions & 3 deletions runtime/testing/test-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {assert} from '../test/chai-web.js';
import {Arc} from '../arc.js';
import {Manifest} from '../manifest.js';
import {Loader} from '../loader.js';
import {StubLoader} from '../testing/stub-loader.js';
import {Planner} from '../planner.js';
import {Random} from '../random.js';
import {MockSlotComposer} from '../testing/mock-slot-composer.js';
Expand Down Expand Up @@ -72,9 +73,9 @@ export class TestHelper {
*/
static async parseManifestAndPlan(manifestString, options) {
options = options || {};
options.loader = options.loader || new class extends Loader {
loadResource(fileName) { return `defineParticle(({Particle}) => { return class P extends Particle {} });`; }
};
options.loader = options.loader || new StubLoader({
'*': `defineParticle(({Particle}) => { return class P extends Particle {} });`
});
let helper = new TestHelper(options);
await helper.parseManifest(manifestString);
await helper.makePlans(options);
Expand Down

0 comments on commit f288ce5

Please sign in to comment.