diff options
| author | Akshay Nair <phenax5@gmail.com> | 2024-01-21 16:12:43 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2024-01-21 16:12:43 +0530 |
| commit | c7a137861494edd65d0c8de76ab09f422ab15481 (patch) | |
| tree | 0aaed5755228ed38cde3ac63dfd8701e7d7442a1 /tests | |
| parent | f7a4596469d0652f868cad2d97a9edf92f4bc4c7 (diff) | |
| download | css-everything-c7a137861494edd65d0c8de76ab09f422ab15481.tar.gz css-everything-c7a137861494edd65d0c8de76ab09f422ab15481.zip | |
feat(eval): adds evaluation for simple binop expressions
Diffstat (limited to '')
| -rw-r--r-- | tests/calc.spec.ts | 46 | ||||
| -rw-r--r-- | tests/parser.spec.ts | 18 |
2 files changed, 64 insertions, 0 deletions
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) + }) + }) +}) diff --git a/tests/parser.spec.ts b/tests/parser.spec.ts index 494bb51..16480e6 100644 --- a/tests/parser.spec.ts +++ b/tests/parser.spec.ts @@ -267,5 +267,23 @@ describe('parser', () => { }), ]) }) + + it('parses calc expression with vars', () => { + expect(parse(`calc(5px * var(--value))`)).toEqual([ + Expr.Call({ + name: 'calc', + args: [ + Expr.BinOp({ + op: '*', + left: Expr.LiteralNumber({ value: 5, unit: 'px' }), + right: Expr.Call({ + name: 'var', + args: [Expr.VarIdentifier('--value')], + }), + }), + ], + }), + ]) + }) }) }) |
