blueshell
Version:
A Behavior Tree implementation in modern Javascript
184 lines • 10.1 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 lib_1 = require("../../lib");
const Behavior = __importStar(require("../../lib"));
class TestState {
constructor() {
this.successCount = 0;
this.failureCount = 0;
}
}
class TestRunningAction extends Behavior.Action {
constructor() {
super(...arguments);
this.numCalls = 0;
}
onEvent(state) {
this.numCalls++;
const nodeStorage = this.getNodeStorage(state);
if (this.numCalls > 1) {
chai_1.assert.strictEqual(nodeStorage.lastResult, lib_1.rc.RUNNING);
}
if (this.numCalls > 2) {
return lib_1.rc.SUCCESS;
}
else {
return lib_1.rc.RUNNING;
}
}
}
describe('IfElseWithNodeCondition', function () {
const successAction = new (class extends Behavior.Action {
onEvent(state) {
state.successCount++;
return lib_1.rc.SUCCESS;
}
})();
const failureAction = new (class extends Behavior.Action {
onEvent(state) {
state.failureCount++;
return lib_1.rc.FAILURE;
}
})();
context('conditional returns success (true)', function () {
it('should return success (consequent) when conditional is true with no alternative', function () {
const ifElse = new Behavior.IfElseWithNodeCondition('testIfElse', successAction, successAction);
const state = new TestState();
const res = ifElse.handleEvent(state, 'testEvent');
chai_1.assert.notOk(state.errorReason);
chai_1.assert.equal(res, lib_1.rc.SUCCESS, 'Behavior Tree not success');
chai_1.assert.strictEqual(state.successCount, 2, 'Expected Action was not called');
chai_1.assert.strictEqual(ifElse.getChildren().length, 2, 'ifElse does not have 2 children');
});
it('should return success (consequent) when conditional is true with an alternative rc', function () {
const ifElse = new Behavior.IfElseWithNodeCondition('testIfElse', successAction, successAction, lib_1.rc.FAILURE);
const state = new TestState();
const res = ifElse.handleEvent(state, 'testEvent');
chai_1.assert.notOk(state.errorReason);
chai_1.assert.equal(res, lib_1.rc.SUCCESS, 'Behavior Tree not success');
chai_1.assert.strictEqual(state.successCount, 2, 'Expected Action was not called');
chai_1.assert.strictEqual(ifElse.getChildren().length, 3, 'ifElse does not have 3 children');
});
it('should return success (consequent) when conditional is true with an alternative node', function () {
const ifElse = new Behavior.IfElseWithNodeCondition('testIfElse', successAction, successAction, failureAction);
const state = new TestState();
const res = ifElse.handleEvent(state, 'testEvent');
chai_1.assert.notOk(state.errorReason);
chai_1.assert.equal(res, lib_1.rc.SUCCESS, 'Behavior Tree not success');
chai_1.assert.strictEqual(state.successCount, 2, 'Expected Action was not called');
chai_1.assert.strictEqual(state.failureCount, 0, 'Unexpected Action was called');
chai_1.assert.strictEqual(ifElse.getChildren().length, 3, 'ifElse does not have 3 children');
});
});
context('conditional returns failure (false)', function () {
it('should return failure when conditional is false and there is no alternative', function () {
const ifElse = new Behavior.IfElseWithNodeCondition('testIfElse', failureAction, successAction);
const state = new TestState();
const res = ifElse.handleEvent(state, 'testEvent');
chai_1.assert.notOk(state.errorReason);
chai_1.assert.equal(res, lib_1.rc.FAILURE, 'Behavior Tree not failure');
chai_1.assert.strictEqual(state.successCount, 0, 'Unexpected Action was called');
chai_1.assert.strictEqual(state.failureCount, 1, 'Expected Action was not called');
});
it('should return success (alternative) when conditional is false with an alternative rc', function () {
const ifElse = new Behavior.IfElseWithNodeCondition('testIfElse', failureAction, failureAction, lib_1.rc.SUCCESS);
const state = new TestState();
const res = ifElse.handleEvent(state, 'testEvent');
chai_1.assert.notOk(state.errorReason);
chai_1.assert.equal(res, lib_1.rc.SUCCESS, 'Behavior Tree not success');
chai_1.assert.strictEqual(state.failureCount, 1, 'Expected Action was not called once');
});
it('should return success (alternative) when conditional is false with an alternative node', function () {
const ifElse = new Behavior.IfElseWithNodeCondition('testIfElse', failureAction, failureAction, successAction);
const state = new TestState();
const res = ifElse.handleEvent(state, 'testEvent');
chai_1.assert.notOk(state.errorReason);
chai_1.assert.equal(res, lib_1.rc.SUCCESS, 'Behavior Tree not success');
chai_1.assert.strictEqual(state.successCount, 1, 'Expected Action was not called once');
chai_1.assert.strictEqual(state.failureCount, 1, 'Expected Action was not called once');
});
});
context('conditional returns success (true) after running twice', function () {
it('should return success (consequent) when conditional is true after running with no alternative', function () {
const condition = new TestRunningAction('testRunningAction');
const ifElse = new Behavior.IfElseWithNodeCondition('testIfElse', condition, successAction);
const state = new TestState();
let res = ifElse.handleEvent(state, 'testEvent');
chai_1.assert.strictEqual(res, lib_1.rc.RUNNING);
res = ifElse.handleEvent(state, 'testEvent');
chai_1.assert.strictEqual(res, lib_1.rc.RUNNING);
res = ifElse.handleEvent(state, 'testEvent');
chai_1.assert.notOk(state.errorReason);
chai_1.assert.equal(res, lib_1.rc.SUCCESS, 'Behavior Tree not success');
chai_1.assert.strictEqual(state.successCount, 1, 'Expected Action was not called once');
chai_1.assert.strictEqual(condition.numCalls, 3, 'Condition not called 3 times');
});
it('should return success (consequent) when conditional is true after running with alternative rc', function () {
const condition = new TestRunningAction('testRunningAction');
const ifElse = new Behavior.IfElseWithNodeCondition('testIfElse', condition, successAction, lib_1.rc.FAILURE);
const state = new TestState();
let res = ifElse.handleEvent(state, 'testEvent');
chai_1.assert.strictEqual(res, lib_1.rc.RUNNING);
res = ifElse.handleEvent(state, 'testEvent');
chai_1.assert.strictEqual(res, lib_1.rc.RUNNING);
res = ifElse.handleEvent(state, 'testEvent');
chai_1.assert.notOk(state.errorReason);
chai_1.assert.equal(res, lib_1.rc.SUCCESS, 'Behavior Tree not success');
chai_1.assert.strictEqual(state.successCount, 1, 'Expected Action was not called once');
chai_1.assert.strictEqual(condition.numCalls, 3, 'Condition not called 3 times');
});
it('should return success (consequent) when conditional is true after running with alternative node', function () {
const condition = new TestRunningAction('testRunningAction');
const ifElse = new Behavior.IfElseWithNodeCondition('testIfElse', condition, successAction, failureAction);
const state = new TestState();
let res = ifElse.handleEvent(state, 'testEvent');
chai_1.assert.strictEqual(res, lib_1.rc.RUNNING);
res = ifElse.handleEvent(state, 'testEvent');
chai_1.assert.strictEqual(res, lib_1.rc.RUNNING);
res = ifElse.handleEvent(state, 'testEvent');
chai_1.assert.notOk(state.errorReason);
chai_1.assert.equal(res, lib_1.rc.SUCCESS, 'Behavior Tree not success');
chai_1.assert.strictEqual(state.successCount, 1, 'Expected Action was not called once');
chai_1.assert.strictEqual(state.failureCount, 0, 'Unexpected Action was called');
chai_1.assert.strictEqual(condition.numCalls, 3, 'Condition not called 3 times');
});
});
});
//# sourceMappingURL=IfElseWithNodeCondition.test.js.map