Skip to content

Commit

Permalink
refactor(errors): Make table error result its own class
Browse files Browse the repository at this point in the history
Closes #43
  • Loading branch information
derikb committed Dec 26, 2021
1 parent c4b75da commit bc8d7c4
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 14 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
npm-debug.log
npm-debug.log
.nyc_output
2 changes: 1 addition & 1 deletion src/RandomTableResult.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default class RandomTableResult {
* Is this an error result.
*/
get isError () {
return this.table === 'error';
return false;
}
toString () {
return this.result;
Expand Down
13 changes: 13 additions & 0 deletions src/TableErrorResult.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import RandomTableResult from './RandomTableResult.js';

/**
* Class for results from RandomTable
*/
export default class TableErrorResult extends RandomTableResult {
/**
* Is this an error result.
*/
get isError () {
return true;
}
}
12 changes: 7 additions & 5 deletions src/TableRoller.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import RandomTableResult from './RandomTableResult.js';
import RandomTableResultSet from './RandomTableResultSet.js';
import { getDiceResult } from './dice_roller.js';
import TableError from './TableError.js';
import TableErrorResult from './TableErrorResult.js';

/**
* Define the regex to find tokens
Expand Down Expand Up @@ -34,11 +35,12 @@ class TableRoller {
/**
* Return an error result
* @param {String} error Error message
* @returns {RandomTableResult}
* @param {String} table Sub/table name if relevant.
* @returns {TableErrorResult}
*/
_getErrorResult (error = '') {
return new RandomTableResult({
table: 'error',
_getErrorResult (error = '', table = '') {
return new TableErrorResult({
table: table,
result: error
});
}
Expand Down Expand Up @@ -70,7 +72,7 @@ class TableRoller {
let o = []; // Results
const entry = rtable.getRandomEntry(table);
if (entry === null || !(entry instanceof RandomTableEntry)) {
return [this._getErrorResult('Invalid subtable name.')];
return [this._getErrorResult('Invalid subtable name.', table)];
}
// if print is false we suppress the output from this table
// (good for top-level tables that have subtables prop set)
Expand Down
15 changes: 8 additions & 7 deletions test/TableRollerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import RandomTableResult from '../src/RandomTableResult.js';
import RandomTableResultSet from '../src/RandomTableResultSet.js';
import TableError from '../src/TableError.js';
import TableRoller from '../src/TableRoller.js';
import TableErrorResult from '../src/TableErrorResult.js';

const roller = new TableRoller({
token_types: {
Expand Down Expand Up @@ -81,8 +82,10 @@ describe('TableRoller', function () {

describe('_selectFromTable', function () {
it('should return error on invalid subtable', function () {
expect(roller._selectFromTable(colorTable, 'shades')[0]).to.deep.include({
table: 'error',
const errorResult = roller._selectFromTable(colorTable, 'shades')[0];
expect(errorResult).to.be.instanceOf(TableErrorResult);
expect(errorResult).to.deep.include({
table: 'shades',
result: 'Invalid subtable name.'
});
});
Expand Down Expand Up @@ -160,8 +163,8 @@ describe('TableRoller', function () {
it('should return Error ResultSet for thrown range error', function () {
// Catch RangeError
const errorResult = roller.convertToken('{{range_error:foo:bar}}');
expect(errorResult.results[0]).to.be.instanceOf(TableErrorResult);
expect(errorResult.results[0]).to.deep.include({
table: 'error',
result: 'out of memory'
});
});
Expand All @@ -170,8 +173,8 @@ describe('TableRoller', function () {
describe('getResultSetForTable', function () {
it('should return error set on invalid table', function () {
const errorResult = roller.getResultSetForTable('table_string');
expect(errorResult.results[0]).to.be.instanceOf(TableErrorResult);
expect(errorResult.results[0]).to.deep.include({
table: 'error',
result: 'Invalid table data.'
});
});
Expand All @@ -190,9 +193,7 @@ describe('TableRoller', function () {
describe('getTableResultSetByKey', function () {
it('should return error result on bad table', function () {
const errorResult = roller.getTableResultSetByKey('table_string');
expect(errorResult.results[0]).to.deep.include({
table: 'error'
});
expect(errorResult.results[0]).to.be.instanceOf(TableErrorResult);
});

it('should return result set', function () {
Expand Down

0 comments on commit bc8d7c4

Please sign in to comment.