Skip to content

Commit

Permalink
add fault input tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mdeljavan committed Mar 7, 2019
1 parent a9b2b6b commit 89c66fa
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
30 changes: 29 additions & 1 deletion src/mathString.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { checkInput } from './checkPhrase';
import solve from './mathString';
import {
selectMultiplicationPhrase,
Expand All @@ -21,7 +22,12 @@ const input: string[] = [
'-1+3*((4+4*(3+4))+(3*(-1)*-2)/4)+3-5',
'1*2/4*6+4*3'
];

const faultInput = [
'1**1*1***2',
'1//3/3/////4',
'1....2+1.2+1...2',
'1+(2+2+2+3+(3)'
];
describe('test Regex for inputs', () => {
it('select numbers', () => {
expect(selectNumbers(input[0])).toEqual(['1', '+11', '+9', '-11e23']);
Expand Down Expand Up @@ -57,3 +63,25 @@ describe('solve phrases math', () => {
expect(solve(input[4])).toBe('2');
});
});
describe('detect fault input', () => {
it('throw error if use more than one * operator in one place', () => {
expect(() => {
checkInput(faultInput[0]);
}).toThrow();
});
it('throw error if use more than one / operator in one place', () => {
expect(() => {
checkInput(faultInput[1]);
}).toThrow();
});
it('throw error if use more than one . in one place', () => {
expect(() => {
checkInput(faultInput[2]);
}).toThrow();
});
it('throw error if use not equal parenthesses', () => {
expect(() => {
checkInput(faultInput[3]);
}).toThrow();
});
});
4 changes: 1 addition & 3 deletions src/solveModules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,6 @@ export const solveSummationOrSubtraction = (phrase: string) => {
''
);
const result =
mathOperator && mathOperator == mathSigns.subtraction
? +num1 - +num2
: +num1 + +num2;
mathOperator == mathSigns.subtraction ? +num1 - +num2 : +num1 + +num2;
return result >= 0 ? `+${result.toString()}` : result.toString();
};

0 comments on commit 89c66fa

Please sign in to comment.