Skip to content

Commit

Permalink
fixed issue where no root node needs to be defined in JSON definition
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikolas Howard committed Mar 5, 2024
1 parent 814c995 commit 9076a75
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 4 deletions.
3 changes: 3 additions & 0 deletions dist/bundle.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions dist/bundle.js.map

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions dist/index.js.map

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions src/BehaviourTreeDefinitionValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,11 @@ function validateNode(definition: any, depth: number): void {
);
}

// If this node is at the very base of the definition then it MUST be a root node.
if (depth === 0 && definition.type !== "root") {
throw new Error(`expected root node at base of definition but got node of type '${definition.type}'`);
}

// How we validate this node definition will depend on its type.
switch (definition.type) {
case "action":
Expand Down
25 changes: 25 additions & 0 deletions test/BehaviourTreeDefinitionValidator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,31 @@ describe("The validateDefinition function takes a tree definition as an argument
});
});

describe("the root node in the definition is not actually a root node", () => {
it("(MDSL)", () => {
verifyResult("action [noop]", false, "expected root node at base of definition");
});

it("(JSON)", () => {
const definition = {
type: "action",
call: "noop"
};

// The definition can be either an array (of root node definitions) or an object (the single primary root node definition), verify both.
verifyResult(
definition,
false,
"expected root node at base of definition but got node of type 'action'"
);
verifyResult(
[definition],
false,
"expected root node at base of definition but got node of type 'action'"
);
});
});

describe("there are duplicate root node identifiers", () => {
it("(MDSL)", () => {
verifyResult(
Expand Down

0 comments on commit 9076a75

Please sign in to comment.