-
Notifications
You must be signed in to change notification settings - Fork 0
/
integration.test.ts
38 lines (31 loc) · 1.06 KB
/
integration.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import Lexer from '../src/Lexer';
import TokensEvaluator from '../src/TokensEvaluator';
import {
FourArithmeticOperatorsTokenMatcher,
OperandsTokenMatcher,
UnknownTokenMatcher,
} from '../src/matchers';
const createSimpleRpnCalculator = () => {
const tokensEvaluator = new TokensEvaluator;
const lexer = new Lexer([
OperandsTokenMatcher,
FourArithmeticOperatorsTokenMatcher,
UnknownTokenMatcher
]);
return (input: string): number | undefined => {
const tokens = lexer.splitChunkToTokens(input);
tokensEvaluator.eval(tokens);
return tokensEvaluator.getLastStackElement();
};
};
describe('Integration of Lexer and TokensEvaluator', () => {
test('it evaluates rpn expression', () => {
const rpn = createSimpleRpnCalculator();
expect(rpn('5 5 5 8 + + -')).toEqual(-13);
});
test('it stores state between invokations', () => {
const rpn = createSimpleRpnCalculator();
expect(rpn('5 5 5 8 + + -')).toEqual(-13);
expect(rpn('13 +')).toEqual(0);
});
});