Skip to content

Commit

Permalink
CommandManager Test
Browse files Browse the repository at this point in the history
  • Loading branch information
jenglamlow committed May 5, 2019
1 parent b762e79 commit bd0dbeb
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 36 deletions.
21 changes: 14 additions & 7 deletions src/CommandManager.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { IGameState } from './Wiser';

export interface ICommand {
command: string;
diff: any;
undo: () => void;
redo: () => void;
undo: (state: any, diff: any) => void;
redo: (state: any, diff: any) => void;
}

export interface ICommandManager {
Expand All @@ -18,6 +16,11 @@ export interface ICommandManager {
export class CommandManager implements ICommandManager {
private commands: ICommand[] = [];
private index: number = -1;
private state: any;

public constructor(state: any) {
this.state = state;
}

public add(command: ICommand) {
this.commands = this.commands.slice(0, this.index + 1);
Expand All @@ -28,13 +31,17 @@ export class CommandManager implements ICommandManager {
public redo() {
if (this.index < this.commands.length - 1) {
this.index += 1;
this.commands[this.index].redo();
const currentCommand = this.commands[this.index];
const diff = currentCommand.diff;
currentCommand.redo(this.state, diff);
}
}

public undo() {
if (this.index >= 0) {
this.commands[this.index].undo();
const currentCommand = this.commands[this.index];
const diff = currentCommand.diff;
currentCommand.undo(this.state, diff);
this.index -= 1;
}
}
Expand All @@ -44,7 +51,7 @@ export class CommandManager implements ICommandManager {
}

public stack(index: number) {
if (index < this.commands.length - 1) {
if (index < this.commands.length) {
return this.commands[index];
}

Expand Down
16 changes: 2 additions & 14 deletions src/Wiser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,13 @@ export class Wiser {
public manager: ICommandManager;

constructor(red: string = 'Red Team', white: string = 'White Team', numOfBalls: number = 7) {
this.manager = new CommandManager();

// Initialize Game State
this.state = gameState;
this.state.info.r.name = red;
this.state.info.w.name = white;

this.initBallState(numOfBalls);
this.manager = new CommandManager(this.state.match);
}

public process(input: string) {
Expand Down Expand Up @@ -202,7 +201,7 @@ export class Wiser {
}

private nullify(type: nullifyType, target) {
const validSequence = this.state.match.sequences.filter(s => s.nullified === false);
const validSequence = this.state.match.sequences.filter(s => !s.nullified);

if (type === 'rescue') {
// Find non-missHit target
Expand Down Expand Up @@ -385,14 +384,3 @@ export class Wiser {
// wiser.process('r3w4');

// console.log(wiser.state.match.r.balls[0]);

// const change = deepDiff.diff(s0,s1);
// let t;
// // deepDiff.applyDiff(t,s0, change);
// if (change) {
// for (const i of change) {
// deepDiff.applyChange(s0, {}, i);
// deepDiff.revertChange(s1, {}, i);

// }
// }
52 changes: 37 additions & 15 deletions src/__tests__/CommandManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,54 @@ import deepDiff from 'deep-diff';

import { CommandManager } from '../CommandManager';

test('Add', () => {
const cm = new CommandManager();

const s0 = {
test('Basic Add, Undo, Redo', () => {
const s = {
state: 0,
list: [],
list: [] as string[],
};

const s1 = {
state: 1,
list: ['r1'],
};
const cm = new CommandManager(s);

const prev = JSON.parse(JSON.stringify(s));

s.state = 1;
s.list.push('r1');

const curr = JSON.parse(JSON.stringify(s));

const command = {
command: 'r1r2',
diff: deepDiff.diff(s0, s1),
undo: () => {
// console.log(diff);
diff: deepDiff.diff(prev, s),
undo: (state, diff) => {
for (const i of diff) {
deepDiff.revertChange(state, {}, i);
}
},
redo: () => {
// console.log('redeo')
redo: (state, diff) => {
for (const i of diff) {
deepDiff.applyChange(state, {}, i);
}
},
};

cm.add(command);

// console.log(cm.stackList());
expect(cm.stackList().length).toBe(1);
expect(cm.stack(0)).toEqual(command);
// Index number exceed test
expect(cm.stack(2)).toEqual(null);

cm.undo();
expect(s).toEqual(prev);

// If undo stack is empty, should remain the same state
cm.undo();
expect(s).toEqual(prev);

cm.redo();
expect(s).toEqual(curr);

// If redo stack is empty, should remain the same state
cm.redo();
expect(s).toEqual(curr);
});

0 comments on commit bd0dbeb

Please sign in to comment.