Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[new feat] implemnent gr#import #548

Merged
merged 2 commits into from
Jan 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/Core/GrimoireInterfaceImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,38 @@ export default class GrimoireInterfaceImpl extends EEObject {
return GomlLoader.callInitializedAlready;
}

public import(path: string): any {
const pathes = path.split("/");
Utility.assert(pathes.length > 2 && pathes[1] === "ref", `invalid import path: ${path}`);
const pluginName = pathes[0];
const importPath = pathes.slice(2);
if (pluginName === "grimoirejs") {
let target = this as any;
for (let i = 0; i < importPath.length; i++) {
if (target[importPath[i]]) {
target = target[importPath[i]];
} else {
throw new Error(`import path ${path} is not found in ${pluginName}`);
}
}
return target;
}
for (const key in this.lib) {
let target = this.lib[key];
if (target.__NAME__ === pluginName) {
for (let i = 0; i < importPath.length; i++) {
if (target[importPath[i]]) {
target = target[importPath[i]];
} else {
throw new Error(`import path ${path} is not found in ${pluginName}`);
}
}
return target;
}
}
throw new Error(`plugin ${pluginName} is not registered.`);
}

/**
* start observation goml mutation.
*/
Expand Down
47 changes: 47 additions & 0 deletions test/Core/GrimoireInterfaceTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,3 +364,50 @@ test("register and resolvePlugins works preperly", async() => {
await GrimoireInterface.resolvePlugins();
assert.callOrder(spy1, spy2);
});

test("import works preperly", async t => {
GrimoireInterface.lib.hoge = {
__NAME__: "grimoirejs-hoge",
__VERSION__: "1.2.3",
};
const component = {
ComponentA: 1,
ComponentB: 2,
ComponentC: 3,
};
const converter = {
ConverterA: 4,
ConverterB: 5,
ConverterC: 6,
};
GrimoireInterface.lib.hoge.Component = component;
GrimoireInterface.lib.hoge.Converter = converter;
(GrimoireInterface as any).Core = { GomlNode: 7 };

t.truthy(GrimoireInterface.import("grimoirejs-hoge/ref/Component/ComponentA") === 1);
t.truthy(GrimoireInterface.import("grimoirejs/ref/Core/GomlNode") === 7);
let err = t.throws(() => {
GrimoireInterface.import("invalidpath");
});
t.truthy(err.message.includes("invalid"));

err = t.throws(() => {
GrimoireInterface.import("notfound/ref/Component/HogeComponent");
});
t.truthy(err.message.includes("is not registered."));

err = t.throws(() => {
GrimoireInterface.import("grimoirejs-hoge/ref/notfound/ComponentA");
});
t.truthy(err.message.includes("not found"));

err = t.throws(() => {
GrimoireInterface.import("grimoirejs-hoge/ref/Component/Component");
});
t.truthy(err.message.includes("not found"));

err = t.throws(() => {
GrimoireInterface.import("grimoirejs-hoge/ref/Component/ComponentA/Component");
});
t.truthy(err.message.includes("not found"));
});