blueshell
Version:
A Behavior Tree implementation in modern Javascript
174 lines • 9.15 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Created by josh on 1/10/16.
*/
const chai_1 = require("chai");
const sinon = __importStar(require("sinon"));
const lib_1 = require("../../lib");
const Behavior = __importStar(require("../../lib"));
const RunningAction = Behavior.RunningAction;
class TestState {
}
class TestAction extends RunningAction {
constructor(name, numIter = 0, actiavteReturns = lib_1.rc.RUNNING, testValue) {
super(name);
this.numIter = numIter;
this.actiavteReturns = actiavteReturns;
this.testValue = testValue;
this.count = 0;
}
activate() {
this.count = 0;
return this.actiavteReturns;
}
isCompletionEvent(event) {
this.count++;
return this.count === this.numIter || event === this.testValue;
}
}
describe('RunningAction', function () {
let action;
let state;
let activateSpy;
let isCompletionEventSpy;
let onCompleteSpy;
let onIncompleteSpy;
function setupSpies() {
activateSpy = sinon.spy(action, 'activate');
isCompletionEventSpy = sinon.spy(action, 'isCompletionEvent');
onCompleteSpy = sinon.spy(action, 'onComplete');
onIncompleteSpy = sinon.spy(action, 'onIncomplete');
}
beforeEach(function () {
state = new TestState();
});
afterEach(function () {
sinon.restore();
});
context('Activate returns non-RUNNING on first handleEvent call', function () {
it('returns SUCCESS', function () {
action = new TestAction('TestRunningAction', undefined, lib_1.rc.SUCCESS);
setupSpies();
const res = action.handleEvent(state, 'foo');
chai_1.assert.strictEqual(res, lib_1.rc.SUCCESS, 'result on first handleEvent was not success');
chai_1.assert.strictEqual(activateSpy.callCount, 1, 'activate not called once');
chai_1.assert.strictEqual(isCompletionEventSpy.callCount, 0, 'isCompleteionEvent called');
chai_1.assert.strictEqual(onCompleteSpy.callCount, 0, 'onComplete called');
chai_1.assert.strictEqual(onIncompleteSpy.callCount, 0, 'onIncomplete called');
});
it('returns FAILURE', function () {
action = new TestAction('TestRunningAction', undefined, lib_1.rc.FAILURE);
setupSpies();
const res = action.handleEvent(state, 'foo');
chai_1.assert.strictEqual(res, lib_1.rc.FAILURE, 'result on first handleEvent was not failure');
chai_1.assert.strictEqual(activateSpy.callCount, 1, 'activate not called once');
chai_1.assert.strictEqual(isCompletionEventSpy.callCount, 0, 'isCompleteionEvent called');
chai_1.assert.strictEqual(onCompleteSpy.callCount, 0, 'onComplete called');
chai_1.assert.strictEqual(onIncompleteSpy.callCount, 0, 'onIncomplete called');
});
it('returns ERROR', function () {
action = new TestAction('TestRunningAction', undefined, lib_1.rc.ERROR);
setupSpies();
const res = action.handleEvent(state, 'foo');
chai_1.assert.strictEqual(res, lib_1.rc.ERROR, 'result on first handleEvent was not error');
chai_1.assert.strictEqual(activateSpy.callCount, 1, 'activate not called once');
chai_1.assert.strictEqual(isCompletionEventSpy.callCount, 0, 'isCompleteionEvent called');
chai_1.assert.strictEqual(onCompleteSpy.callCount, 0, 'onComplete called');
chai_1.assert.strictEqual(onIncompleteSpy.callCount, 0, 'onIncomplete called');
});
});
context('RUNNING tests', function () {
it('returns after second handleEvent', function () {
action = new TestAction('TestRunningAction', 1);
setupSpies();
let res = action.handleEvent(state, 'foo');
chai_1.assert.strictEqual(res, lib_1.rc.RUNNING, 'first handleEvent not RUNNING');
res = action.handleEvent(state, 'foo');
chai_1.assert.strictEqual(res, lib_1.rc.SUCCESS, 'second handleEvent not SUCCESS');
chai_1.assert.strictEqual(activateSpy.callCount, 1, 'activate not called once');
chai_1.assert.strictEqual(isCompletionEventSpy.callCount, 1, 'isCompleteionEvent not called once');
chai_1.assert.strictEqual(onCompleteSpy.callCount, 1, 'onComplete not called once');
chai_1.assert.strictEqual(onIncompleteSpy.callCount, 0, 'onIncomplete called');
});
it('returns after second handleEvent', function () {
action = new TestAction('TestRunningAction', 2);
setupSpies();
let res = action.handleEvent(state, 'foo');
chai_1.assert.strictEqual(res, lib_1.rc.RUNNING, 'first handleEvent not RUNNING');
res = action.handleEvent(state, 'foo');
chai_1.assert.strictEqual(res, lib_1.rc.RUNNING, 'second handleEvent not RUNNING');
res = action.handleEvent(state, 'foo');
chai_1.assert.strictEqual(res, lib_1.rc.SUCCESS, 'third handleEvent not SUCCESS');
chai_1.assert.strictEqual(activateSpy.callCount, 1, 'activate not called once');
chai_1.assert.strictEqual(isCompletionEventSpy.callCount, 2, 'isCompleteionEvent not called twice');
chai_1.assert.strictEqual(onCompleteSpy.callCount, 1, 'onComplete not called once');
chai_1.assert.strictEqual(onIncompleteSpy.callCount, 1, 'onIncomplete not called once');
});
});
context('additional tests', function () {
it('can use event to determine completion', function () {
action = new TestAction('TestRunningAction', 100, lib_1.rc.RUNNING, 'bar');
setupSpies();
let res = action.handleEvent(state, 'foo');
chai_1.assert.strictEqual(res, lib_1.rc.RUNNING, 'first handleEvent not RUNNING');
res = action.handleEvent(state, 'foo');
chai_1.assert.strictEqual(res, lib_1.rc.RUNNING, 'second handleEvent not RUNNING');
res = action.handleEvent(state, 'bar');
chai_1.assert.strictEqual(res, lib_1.rc.SUCCESS, 'third handleEvent not SUCCESS');
chai_1.assert.strictEqual(activateSpy.callCount, 1, 'activate not called once');
chai_1.assert.strictEqual(isCompletionEventSpy.callCount, 2, 'isCompleteionEvent not called twice');
chai_1.assert.strictEqual(onCompleteSpy.callCount, 1, 'onComplete not called once');
chai_1.assert.strictEqual(onIncompleteSpy.callCount, 1, 'onIncomplete not called once');
});
it('resets when resetNodeStorage() is called', function () {
action = new TestAction('TestRunningAction', 100, lib_1.rc.RUNNING, 'bar');
setupSpies();
let res = action.handleEvent(state, 'foo');
chai_1.assert.strictEqual(res, lib_1.rc.RUNNING, 'first handleEvent not RUNNING');
action.resetNodeStorage(state);
res = action.handleEvent(state, 'foo');
chai_1.assert.strictEqual(res, lib_1.rc.RUNNING, 'second handleEvent not RUNNING');
res = action.handleEvent(state, 'bar');
chai_1.assert.strictEqual(res, lib_1.rc.SUCCESS, 'third handleEvent not SUCCESS');
chai_1.assert.strictEqual(activateSpy.callCount, 2, 'activate not called twice');
chai_1.assert.strictEqual(isCompletionEventSpy.callCount, 1, 'isCompleteionEvent not called twice');
chai_1.assert.strictEqual(onCompleteSpy.callCount, 1, 'onComplete not called once');
chai_1.assert.strictEqual(onIncompleteSpy.callCount, 0, 'onIncomplete called');
});
});
});
//# sourceMappingURL=RunningAction.test.js.map