Skip to content

Commit

Permalink
Refactor operations functions (return null when cannot apply)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreKavalerski committed Jul 3, 2020
1 parent 2811d14 commit f5ad483
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/functions/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ function moveUpOperation(state: State){
const newPosition = new StateItemPosition(newLinePosition, nullPosition.col);
return changePositions(nullPosition, newPosition, state);
}
return null;
}

function moveRightOperation(state: State){
Expand All @@ -36,6 +37,7 @@ function moveRightOperation(state: State){
const newPosition = new StateItemPosition(nullPosition.line, newColPosition);
return changePositions(nullPosition, newPosition, state);
}
return null;
}

function moveDownOperation(state: State){
Expand All @@ -45,6 +47,7 @@ function moveDownOperation(state: State){
const newPosition = new StateItemPosition(newLinePosition, nullPosition.col);
return changePositions(nullPosition, newPosition, state);
}
return null;
}

function moveLeftOperation(state: State){
Expand All @@ -55,6 +58,7 @@ function moveLeftOperation(state: State){
const newPosition = new StateItemPosition(nullPosition.line, newColPosition);
return changePositions(nullPosition, newPosition, state);
}
return null;
}


Expand Down
26 changes: 26 additions & 0 deletions tests/operations.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,31 @@ describe('Testing returns of applyOperation function', () => {
const goalState: State = [[5,null,8], [4,1,6], [7, 2, 3]];
expect(applyOperation(actualState, operations.left)).to.eql(goalState);
});
});

describe('Testing returns of applyOperation function when operation cannot be applied', () => {

it('should return null when call applyOperation function using movUp operation', () => {
const actualState: State = [[null,5,8], [4,1,6], [7, 2, 3]];

expect(applyOperation(actualState, operations.up)).to.eql(null);
});

it('should return null when call applyOperation function using movRight operation', () => {
const actualState: State = [[5,8,6], [4,1,null], [7, 2, 3]];

expect(applyOperation(actualState, operations.right)).to.eql(null);
});

it('should return null when call applyOperation function using movDown operation', () => {
const actualState: State = [[5,8,6], [4,1,3], [7, null, 2]];

expect(applyOperation(actualState, operations.down)).to.eql(null);
});

it('should return null when call applyOperation function using movLeft operation', () => {
const actualState: State = [[5,8,6], [null,4,1], [7, 3, 2]];

expect(applyOperation(actualState, operations.left)).to.eql(null);
});
});

0 comments on commit f5ad483

Please sign in to comment.