From e01cb693bc5737792e2b37abfd98d2d8f81bac4d Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Thu, 10 Aug 2023 22:45:00 +0530 Subject: feat: adds simple evaluator --- tests/eval.spec.ts | 29 ++++++++++++++++++ tests/parse-expr.spec.ts | 76 ------------------------------------------------ tests/parser.spec.ts | 76 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+), 76 deletions(-) create mode 100644 tests/eval.spec.ts delete mode 100644 tests/parse-expr.spec.ts create mode 100644 tests/parser.spec.ts (limited to 'tests') diff --git a/tests/eval.spec.ts b/tests/eval.spec.ts new file mode 100644 index 0000000..63062e3 --- /dev/null +++ b/tests/eval.spec.ts @@ -0,0 +1,29 @@ +import { Dependencies, evalExpr } from '../src/eval' +import { Expr } from '../src/parser' + +describe('eval', () => { + const deps: Dependencies = { + addClass: jest.fn(), + removeClass: jest.fn(), + } + + it('should add classes', async () => { + await evalExpr(Expr.Call({ + name: 'add-class', + args: [ Expr.Identifier('element-id'), Expr.LiteralString('class-name') ], + }), deps) + + expect(deps.addClass).toHaveBeenCalledTimes(1) + expect(deps.addClass).toHaveBeenCalledWith('element-id', 'class-name') + }) + + it('should add classes', async () => { + await evalExpr(Expr.Call({ + name: 'remove-class', + args: [ Expr.Identifier('element-id'), Expr.LiteralString('class-name') ], + }), deps) + + expect(deps.removeClass).toHaveBeenCalledTimes(1) + expect(deps.removeClass).toHaveBeenCalledWith('element-id', 'class-name') + }) +}) diff --git a/tests/parse-expr.spec.ts b/tests/parse-expr.spec.ts deleted file mode 100644 index f873464..0000000 --- a/tests/parse-expr.spec.ts +++ /dev/null @@ -1,76 +0,0 @@ -import { Expr, parse } from '../src/parse-expr' - -describe('parser', () => { - it('should parse function call', () => { - expect(parse('hello()')).toEqual([Expr.Call({ name: 'hello', args: [] })]) - expect(parse('hello ( wow , foo ) ')).toEqual([ - Expr.Call({ - name: 'hello', - args: [Expr.Identifier('wow'), Expr.Identifier('foo')], - }), - ]) - expect(parse('hello(wow,foo)')).toEqual([ - Expr.Call({ - name: 'hello', - args: [Expr.Identifier('wow'), Expr.Identifier('foo')], - }), - ]) - expect(parse('hello(wow,foo, coolio)')).toEqual([ - Expr.Call({ - name: 'hello', - args: [ - Expr.Identifier('wow'), - Expr.Identifier('foo'), - Expr.Identifier('coolio'), - ], - }), - ]) - expect(parse('hello(wow)')).toEqual([ - Expr.Call({ name: 'hello', args: [Expr.Identifier('wow')] }), - ]) - }) - - it('should parse sequential function calls', () => { - expect(parse('hello(world) foo-doo(bar, baz)')).toEqual([ - Expr.Call({ - name: 'hello', - args: [Expr.Identifier('world')], - }), - Expr.Call({ - name: 'foo-doo', - args: [Expr.Identifier('bar'), Expr.Identifier('baz')], - }), - ]) - }) - - it('should parse string literal', () => { - expect(parse(`"hello world toodles \' nice single quote there"`)).toEqual([ - Expr.LiteralString(`hello world toodles \' nice single quote there`), - ]) - - expect(parse(` 'hello world toodles \" nice double quote there' `)).toEqual( - [Expr.LiteralString(`hello world toodles \" nice double quote there`)] - ) - }) - - it('should parse var identifiers', () => { - expect(parse(`var(--hello, 'default')`)).toEqual([ - Expr.Call({ - name: 'var', - args: [Expr.VarIdentifier('--hello'), Expr.LiteralString(`default`)], - }), - ]) - - expect(parse(`calc(var(--hello))`)).toEqual([ - Expr.Call({ - name: 'calc', - args: [ - Expr.Call({ - name: 'var', - args: [Expr.VarIdentifier('--hello')], - }), - ], - }), - ]) - }) -}) diff --git a/tests/parser.spec.ts b/tests/parser.spec.ts new file mode 100644 index 0000000..48c398c --- /dev/null +++ b/tests/parser.spec.ts @@ -0,0 +1,76 @@ +import { Expr, parse } from '../src/parser' + +describe('parser', () => { + it('should parse function call', () => { + expect(parse('hello()')).toEqual([Expr.Call({ name: 'hello', args: [] })]) + expect(parse('hello ( wow , foo ) ')).toEqual([ + Expr.Call({ + name: 'hello', + args: [Expr.Identifier('wow'), Expr.Identifier('foo')], + }), + ]) + expect(parse('hello(wow,foo)')).toEqual([ + Expr.Call({ + name: 'hello', + args: [Expr.Identifier('wow'), Expr.Identifier('foo')], + }), + ]) + expect(parse('hello(wow,foo, coolio)')).toEqual([ + Expr.Call({ + name: 'hello', + args: [ + Expr.Identifier('wow'), + Expr.Identifier('foo'), + Expr.Identifier('coolio'), + ], + }), + ]) + expect(parse('hello(wow)')).toEqual([ + Expr.Call({ name: 'hello', args: [Expr.Identifier('wow')] }), + ]) + }) + + it('should parse sequential function calls', () => { + expect(parse('hello(world) foo-doo(bar, baz)')).toEqual([ + Expr.Call({ + name: 'hello', + args: [Expr.Identifier('world')], + }), + Expr.Call({ + name: 'foo-doo', + args: [Expr.Identifier('bar'), Expr.Identifier('baz')], + }), + ]) + }) + + it('should parse string literal', () => { + expect(parse(`"hello world toodles \' nice single quote there"`)).toEqual([ + Expr.LiteralString(`hello world toodles \' nice single quote there`), + ]) + + expect(parse(` 'hello world toodles \" nice double quote there' `)).toEqual( + [Expr.LiteralString(`hello world toodles \" nice double quote there`)] + ) + }) + + it('should parse var identifiers', () => { + expect(parse(`var(--hello, 'default')`)).toEqual([ + Expr.Call({ + name: 'var', + args: [Expr.VarIdentifier('--hello'), Expr.LiteralString(`default`)], + }), + ]) + + expect(parse(`calc(var(--hello))`)).toEqual([ + Expr.Call({ + name: 'calc', + args: [ + Expr.Call({ + name: 'var', + args: [Expr.VarIdentifier('--hello')], + }), + ], + }), + ]) + }) +}) -- cgit v1.3.1