Skip to content

Commit

Permalink
Search subtrees up to the root. Closes #26 (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
brollb authored Apr 4, 2022
1 parent 008cec0 commit f73b2f5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/common/JSONImporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -819,11 +819,16 @@ define([

let skipNodes = [];
if (searchOpts.startHint) {
const match = await this.findNodeWhere(core, searchOpts.startHint, fn);
if (match) {
return match;
let startNode = searchOpts.startHint;
let match = null;
while (startNode) {
match = await this.findNodeWhere(core, startNode, fn, skipNodes);
if (match) {
return match;
}
skipNodes.push(startNode);
startNode = core.getParent(startNode);
}
skipNodes.push(searchOpts.startHint);
}

return await this.findNodeWhere(core, node, fn, skipNodes);
Expand Down
38 changes: 38 additions & 0 deletions test/common/JSONImporter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ describe('JSONImporter', function () {
const _ = testFixture.requirejs('underscore');
const Core = testFixture.requirejs('common/core/coreQ');
const Importer = testFixture.requirejs('webgme-json-importer/JSONImporter');
const NodeSelections = Importer.NodeSelections;
const assert = require('assert');
const gmeConfig = testFixture.getGmeConfig();
const path = testFixture.path;
Expand Down Expand Up @@ -1065,6 +1066,43 @@ describe('JSONImporter', function () {
);
});

it('should search children for @guid first', async function() {
const fco = await core.loadByPath(root, '/1');
const parent = core.createNode({base: fco, parent: root});
const node = core.createNode({base: fco, parent});
const guid = core.getGuid(node);

const parentJson = {
children: [
{
id: `@guid:${guid}`
}
]
};
const selectors = new NodeSelections();
await importer.resolveSelectors(parent, parentJson, selectors);
assert.equal(selectors.cache.length, 1);
});

it('should search children of ancestors for @guid next', async function() {
const fco = await core.loadByPath(root, '/1');
const gparent = core.createNode({base: fco, parent: root});
const parent = core.createNode({base: fco, parent: gparent});
const node = core.createNode({base: fco, parent: gparent});
const guid = core.getGuid(node);

const parentJson = {
children: [
{
id: `@guid:${guid}`
}
]
};
const selectors = new NodeSelections();
await importer.resolveSelectors(parent, parentJson, selectors);
assert.equal(selectors.cache.length, 2);
});

it('should set guid when creating @guid nodes', async function() {
const fco = await core.loadByPath(root, '/1');
const node = core.createNode({base: fco, parent: root});
Expand Down

0 comments on commit f73b2f5

Please sign in to comment.