Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikolas Howard committed Feb 9, 2024
1 parent 66d4672 commit 92dfa55
Show file tree
Hide file tree
Showing 22 changed files with 1,187 additions and 342 deletions.
63 changes: 46 additions & 17 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.

63 changes: 46 additions & 17 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.

66 changes: 54 additions & 12 deletions src/BehaviourTreeDefinitionValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,8 @@ function validateNode(definition: any, depth: number): void {
validateRootNode(definition, depth);
break;

case "success":
validateSuccessNode(definition, depth);
case "succeed":
validateSucceedNode(definition, depth);
break;

case "fail":
Expand Down Expand Up @@ -274,6 +274,10 @@ function validateNode(definition: any, depth: number): void {
validateParallelNode(definition, depth);
break;

case "lotto":
validateLottoNode(definition, depth);
break;

default:
throw new Error(`unexpected node type of '${definition.type}' at depth '${depth}'`);
}
Expand Down Expand Up @@ -352,19 +356,19 @@ function validateRootNode(definition: any, depth: number): void {
}

/**
* Validate an object that we expect to be a success node definition.
* @param definition An object that we expect to be a success node definition.
* Validate an object that we expect to be a succeed node definition.
* @param definition An object that we expect to be a succeed node definition.
* @param depth The depth of the node in the definition tree.
*/
function validateSuccessNode(definition: any, depth: number): void {
function validateSucceedNode(definition: any, depth: number): void {
// Check that the node type is correct.
if (definition.type !== "success") {
throw new Error(`expected node type of 'success' for success node at depth '${depth}'`);
if (definition.type !== "succeed") {
throw new Error(`expected node type of 'succeed' for succeed node at depth '${depth}'`);
}

// A success node is a decorator node, so must have a child node defined.
// A succeed node is a decorator node, so must have a child node defined.
if (typeof definition.child === "undefined") {
throw new Error(`expected property 'child' to be defined for success node at depth '${depth}'`);
throw new Error(`expected property 'child' to be defined for succeed node at depth '${depth}'`);
}

// Validate the node attributes.
Expand Down Expand Up @@ -440,7 +444,7 @@ function validateRepeatNode(definition: any, depth: number): void {
if (typeof definition.iterations !== "undefined") {
if (Array.isArray(definition.iterations)) {
// Check whether any elements of the array are not integer values.
const containsNonInteger = !!definition.iterations.find((value: unknown) => !isInteger(value));
const containsNonInteger = !!definition.iterations.filter((value: unknown) => !isInteger(value)).length;

// If the 'iterations' property is an array then it MUST contain two integer values.
if (definition.iterations.length !== 2 || containsNonInteger) {
Expand Down Expand Up @@ -503,7 +507,7 @@ function validateRetryNode(definition: any, depth: number): void {
if (typeof definition.attempts !== "undefined") {
if (Array.isArray(definition.attempts)) {
// Check whether any elements of the array are not integer values.
const containsNonInteger = !!definition.attempts.find((value: unknown) => !isInteger(value));
const containsNonInteger = !!definition.attempts.filter((value: unknown) => !isInteger(value)).length;

// If the 'attempts' property is an array then it MUST contain two integer values.
if (definition.attempts.length !== 2 || containsNonInteger) {
Expand Down Expand Up @@ -646,7 +650,7 @@ function validateWaitNode(definition: any, depth: number): void {
if (typeof definition.duration !== "undefined") {
if (Array.isArray(definition.duration)) {
// Check whether any elements of the array are not integer values.
const containsNonInteger = !!definition.duration.find((value: unknown) => !isInteger(value));
const containsNonInteger = !!definition.duration.filter((value: unknown) => !isInteger(value)).length;

// If the 'duration' property is an array then it MUST contain two integer values.
if (definition.duration.length !== 2 || containsNonInteger) {
Expand Down Expand Up @@ -755,6 +759,44 @@ function validateParallelNode(definition: any, depth: number): void {
definition.children.forEach((child: any) => validateNode(child, depth + 1));
}

/**
* Validate an object that we expect to be a lotto node definition.
* @param definition An object that we expect to be a lotto node definition.
* @param depth The depth of the node in the definition tree.
*/
function validateLottoNode(definition: any, depth: number): void {
// Check that the node type is correct.
if (definition.type !== "lotto") {
throw new Error(`expected node type of 'lotto' for lotto node at depth '${depth}'`);
}

// A lotto node is a composite node, so must have a children nodes array defined.
if (!Array.isArray(definition.children) || definition.children.length === 0) {
throw new Error(`expected non-empty 'children' array to be defined for lotto node at depth '${depth}'`);
}

// Check whether a 'weights' property has been defined, if it has we expect it to be an array of weights.
if (typeof definition.weights !== "undefined") {
// Check that the weights property is an array of positive integers with an element for each child node element.
if (
!Array.isArray(definition.weights) ||
definition.weights.length !== definition.children.length ||
definition.weights.filter((value: unknown) => !isInteger(value)).length ||
definition.weights.filter((value: number) => value < 0).length
) {
throw new Error(
`expected an array of positive integer weight values with a length matching the number of child nodes for 'weights' property if defined for lotto node at depth '${depth}'`
);
}
}

// Validate the node attributes.
validateNodeAttributes(definition, depth);

// Validate the child nodes of this composite node.
definition.children.forEach((child: any) => validateNode(child, depth + 1));
}

/**
* A helper function to create a failure validation result with the given error message.
* @param errorMessage The validation failure error message.
Expand Down
Loading

0 comments on commit 92dfa55

Please sign in to comment.