UNPKG

behaviortree

Version:

A JavaScript implementation of Behavior Trees. They are useful for implementing AIs. For Browsers and NodeJS.

67 lines (66 loc) 2.39 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); /* eslint-env jest */ const constants_1 = require("../constants"); const LoopDecorator_1 = __importDefault(require("./LoopDecorator")); const Task_1 = __importDefault(require("../Task")); describe('LoopDecorator', () => { const task = new Task_1.default({ run(blackboard) { ++blackboard.count; return constants_1.SUCCESS; } }); const endingTask = new Task_1.default({ run(blackboard) { ++blackboard.count; return blackboard.count === 2 ? constants_1.FAILURE : constants_1.SUCCESS; } }); const inifinityTask = new Task_1.default({ run(blackboard) { ++blackboard.count; return blackboard.count === 100 ? constants_1.FAILURE : constants_1.SUCCESS; } }); let blackboard; let decoratedTask; let decoratedEndingTask; let decoratedInfinityTask; beforeEach(() => { blackboard = { count: 0 }; decoratedTask = new LoopDecorator_1.default({ node: task }); }); it('has a nodeType', () => { expect(decoratedTask.nodeType).toEqual('LoopDecorator'); }); describe('given a looping count', () => { beforeEach(() => { const config = { loop: 3 }; decoratedTask = new LoopDecorator_1.default({ config, node: task }); decoratedEndingTask = new LoopDecorator_1.default({ config, node: endingTask }); }); it('repeats the task that amount of times', () => { decoratedTask.run(blackboard); expect(blackboard.count).toEqual(3); }); it('stops repeating if FAILURE is returned', () => { decoratedEndingTask.run(blackboard); expect(blackboard.count).toEqual(2); }); }); describe('without a looping count', () => { beforeEach(() => { decoratedInfinityTask = new LoopDecorator_1.default({ node: inifinityTask }); }); it('is looped indefinetely or until FAILURE occures', () => { decoratedInfinityTask.run(blackboard); expect(blackboard.count).toEqual(100); }); }); });