From c7a137861494edd65d0c8de76ab09f422ab15481 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Sun, 21 Jan 2024 16:12:43 +0530 Subject: feat(eval): adds evaluation for simple binop expressions --- tests/calc.spec.ts | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 tests/calc.spec.ts (limited to 'tests/calc.spec.ts') diff --git a/tests/calc.spec.ts b/tests/calc.spec.ts new file mode 100644 index 0000000..a4e11cd --- /dev/null +++ b/tests/calc.spec.ts @@ -0,0 +1,46 @@ +import { EvalActions, EvalValue, evalExpr } from '../src/eval' +import { parseExpr } from '../src/parser' +import { matchString } from '../src/utils/adt' + +describe('calc', () => { + const variables = (name: string) => + matchString(name, { + '--test-8rem': () => '8rem', + _: () => {}, + }) + const actions: EvalActions = { + addClass: jest.fn(), + removeClass: jest.fn(), + delay: jest.fn(), + jsEval: jest.fn(), + loadCssx: jest.fn(), + getVariable: jest.fn(variables), + updateVariable: jest.fn(), + setAttribute: jest.fn(), + getAttribute: jest.fn(), + withEvent: jest.fn(), + getFormData: jest.fn(), + sendRequest: jest.fn(), + addChildren: jest.fn(), + removeElement: jest.fn(), + callMethod: jest.fn(), + evaluateInScope: jest.fn(), + } + + describe.each([ + ['calc(8rem)', EvalValue.Number(128)], + ['calc(5 + 8)', EvalValue.Number(13)], + ['calc(5 * 8 + 1)', EvalValue.Number(41)], + ['calc(5 * (8 + 1))', EvalValue.Number(45)], + ['calc(5px * (8rem + 1))', EvalValue.Number(645)], + ['calc(5px * 8rem/2 + 1)', EvalValue.Number(321)], + ['calc(var(--test-8rem))', EvalValue.Number(128)], + ['calc(var(--test-1))', EvalValue.Number(0)], // Var not found + ['calc(5px * var(--test-8rem)/2 + 1)', EvalValue.Number(321)], + ])('when given "%s"', (expr, expected) => { + it('should evaluate the result of math', async () => { + const evalValue = await evalExpr(parseExpr(expr), actions) + expect(evalValue).toEqual(expected) + }) + }) +}) -- cgit v1.3.1