Skip to content

Commit

Permalink
Don't leak undefined ctx properties from turnOrder.actionPlayers (#382)
Browse files Browse the repository at this point in the history
- Removes _actionPlayersOthers because it is never read
- Defaults _actionPlayersOnce to false rather than undefined

Fixes #381
  • Loading branch information
p00ya authored and nicolodavis committed Mar 29, 2019
1 parent e79c2a1 commit ec7dde5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/core/turn-order.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ function setActionPlayers(G, ctx, arg) {
return {
...ctx,
actionPlayers,
_actionPlayersOnce: arg.once,
_actionPlayersOthers: arg.others,
_actionPlayersOnce: arg.once || false,
};
}

Expand Down
39 changes: 39 additions & 0 deletions src/core/turn-order.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,29 @@ import { makeMove, gameEvent } from './action-creators';
import { InitializeGame, CreateGameReducer } from './reducer';

describe('turnOrder', () => {
// Defines a matcher for testing that ctx has no undefined properties.
// Identifies which property is undefined.
expect.extend({
toHaveUndefinedProperties(ctx) {
const undefinedEntry = Object.entries(ctx).find(entry => {
const [, value] = entry;
return value === undefined;
});
if (undefinedEntry === undefined) {
return {
message: () => `expected some properties of ctx to be undefined`,
pass: false,
};
} else {
const [k] = undefinedEntry;
return {
message: () => `expected ctx.${k} to be defined`,
pass: true,
};
}
},
});

test('DEFAULT', () => {
const flow = FlowWithPhases({
startingPhase: 'A',
Expand All @@ -23,6 +46,8 @@ describe('turnOrder', () => {
state = flow.init(state);
expect(state.ctx.currentPlayer).toBe('0');
expect(state.ctx.actionPlayers).toEqual(['0']);
expect(state.ctx).not.toHaveUndefinedProperties();

state = flow.processGameEvent(state, gameEvent('endTurn'));
expect(state.ctx.currentPlayer).toBe('1');
expect(state.ctx.actionPlayers).toEqual(['1']);
Expand All @@ -43,6 +68,8 @@ describe('turnOrder', () => {
state = flow.init(state);
expect(state.ctx.currentPlayer).toBe('0');
expect(state.ctx.actionPlayers).toEqual(['0']);
expect(state.ctx).not.toHaveUndefinedProperties();

state = flow.processGameEvent(state, gameEvent('endTurn'));
expect(state.ctx.currentPlayer).toBe('1');
expect(state.ctx.actionPlayers).toEqual(['1']);
Expand All @@ -61,6 +88,8 @@ describe('turnOrder', () => {
state = flow.init(state);
expect(state.ctx.currentPlayer).toBe('0');
expect(state.ctx.actionPlayers).toEqual(['0', '1']);
expect(state.ctx).not.toHaveUndefinedProperties();

state = flow.processGameEvent(state, gameEvent('endTurn'));
expect(state.ctx.currentPlayer).toBe('0');
expect(state.ctx.actionPlayers).toEqual(['0', '1']);
Expand All @@ -79,6 +108,7 @@ describe('turnOrder', () => {
expect(state.ctx.phase).toBe('A');
expect(state.ctx.currentPlayer).toBe('0');
expect(state.ctx.actionPlayers).toEqual(['0', '1']);
expect(state.ctx).not.toHaveUndefinedProperties();

state = flow.processGameEvent(state, gameEvent('endTurn'));

Expand Down Expand Up @@ -108,6 +138,8 @@ describe('turnOrder', () => {
state = flow.init(state);
expect(state.ctx.currentPlayer).toBe('0');
expect(state.ctx.actionPlayers).toEqual(['1', '2']);
expect(state.ctx).not.toHaveUndefinedProperties();

state = flow.processGameEvent(state, gameEvent('endTurn'));
expect(state.ctx.currentPlayer).toBe('0');
expect(state.ctx.actionPlayers).toEqual(['1', '2']);
Expand All @@ -126,6 +158,7 @@ describe('turnOrder', () => {
expect(state.ctx.phase).toBe('A');
expect(state.ctx.currentPlayer).toBe('0');
expect(state.ctx.actionPlayers).toEqual(['1', '2']);
expect(state.ctx).not.toHaveUndefinedProperties();

state = flow.processGameEvent(state, gameEvent('endTurn'));

Expand Down Expand Up @@ -155,6 +188,8 @@ describe('turnOrder', () => {
state = flow.init(state);

expect(state.ctx.currentPlayer).toBe('1');
expect(state.ctx).not.toHaveUndefinedProperties();

state = flow.processGameEvent(state, gameEvent('endTurn'));
expect(state.ctx.currentPlayer).toBe('0');
});
Expand All @@ -168,6 +203,8 @@ describe('turnOrder', () => {
state = flow.init(state);

expect(state.ctx.currentPlayer).toBe('2');
expect(state.ctx).not.toHaveUndefinedProperties();

state = flow.processGameEvent(state, gameEvent('endTurn'));
expect(state.ctx.currentPlayer).toBe('1');
state = flow.processGameEvent(state, gameEvent('endTurn'));
Expand All @@ -184,6 +221,8 @@ describe('turnOrder', () => {
state = flow.init(state);
expect(state.ctx.currentPlayer).toBe('9');
expect(state.ctx.actionPlayers).toEqual(['9']);
expect(state.ctx).not.toHaveUndefinedProperties();

state = flow.processGameEvent(state, gameEvent('endTurn'));
expect(state.ctx.currentPlayer).toBe('3');
expect(state.ctx.actionPlayers).toEqual(['3']);
Expand Down

0 comments on commit ec7dde5

Please sign in to comment.