From f288ce5a1be44571a74c9a3b5773fcd4be0c0201 Mon Sep 17 00:00:00 2001 From: Piotr Swigon Date: Mon, 28 May 2018 15:13:40 +1000 Subject: [PATCH] Introduces a StubLoader for tests (#1387) --- runtime/test/particle-api-test.js | 29 ++++----------- runtime/test/particle-shape-loading-test.js | 15 +++----- runtime/test/planner-tests.js | 13 ++----- runtime/test/recipe-resolver-test.js | 14 +------ runtime/test/schema-tests.js | 18 +++------ runtime/test/slot-composer-tests.js | 8 ++-- runtime/testing/stub-loader.js | 41 +++++++++++++++++++++ runtime/testing/test-helper.js | 7 ++-- 8 files changed, 72 insertions(+), 73 deletions(-) create mode 100644 runtime/testing/stub-loader.js diff --git a/runtime/test/particle-api-test.js b/runtime/test/particle-api-test.js index 0eacd1b1763..45cc62520ac 100644 --- a/runtime/test/particle-api-test.js +++ b/runtime/test/particle-api-test.js @@ -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() { diff --git a/runtime/test/particle-shape-loading-test.js b/runtime/test/particle-shape-loading-test.js index afae7d37edd..99721fc112c 100644 --- a/runtime/test/particle-shape-loading-test.js +++ b/runtime/test/particle-shape-loading-test.js @@ -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'; @@ -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}) => { @@ -64,11 +63,7 @@ describe('particle-shape-loading', function() { } } } - }); - `; - return super.loadResource(path); - } - }(); + });`}); let pecFactory = function(id) { let channel = new MessageChannel(); @@ -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'); diff --git a/runtime/test/planner-tests.js b/runtime/test/planner-tests.js index 6e2cf0cfffa..4ee426fb20d 100644 --- a/runtime/test/planner-tests.js +++ b/runtime/test/planner-tests.js @@ -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'; @@ -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 { @@ -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); diff --git a/runtime/test/recipe-resolver-test.js b/runtime/test/recipe-resolver-test.js index f543158c4d9..8261d0f8af1 100644 --- a/runtime/test/recipe-resolver-test.js +++ b/runtime/test/recipe-resolver-test.js @@ -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'; @@ -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]; }; diff --git a/runtime/test/schema-tests.js b/runtime/test/schema-tests.js index ed0e463ec59..ba695dc6aeb 100644 --- a/runtime/test/schema-tests.js +++ b/runtime/test/schema-tests.js @@ -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 @@ -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); diff --git a/runtime/test/slot-composer-tests.js b/runtime/test/slot-composer-tests.js index 0ee8d0961f4..0607392d212 100644 --- a/runtime/test/slot-composer-tests.js +++ b/runtime/test/slot-composer-tests.js @@ -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 { @@ -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); diff --git a/runtime/testing/stub-loader.js b/runtime/testing/stub-loader.js new file mode 100644 index 00000000000..6914a134dec --- /dev/null +++ b/runtime/testing/stub-loader.js @@ -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); + } +} diff --git a/runtime/testing/test-helper.js b/runtime/testing/test-helper.js index 7ea3cb66476..e27bbe63cb8 100644 --- a/runtime/testing/test-helper.js +++ b/runtime/testing/test-helper.js @@ -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'; @@ -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);