summaryrefslogtreecommitdiff
path: root/tests/parser.spec.ts
blob: 41fafa3169a39ea74fdf9baaa85fcb586da49422 (plain) (blame)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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')],
          }),
        ],
      }),
    ])

    expect(parse(`update(state-container, --count, var(--count))`)).toEqual([
      Expr.Call({
        name: 'update',
        args: [
          Expr.Identifier('state-container'),
          Expr.VarIdentifier('--count'),
          Expr.Call({
            name: 'var',
            args: [Expr.VarIdentifier('--count')],
          }),
        ],
      }),
    ])
  })

  it('should parse number and css units', () => {
    expect(parse(`100`)).toEqual([Expr.LiteralNumber({ value: 100, unit: '' })])
    expect(parse(`100s`)).toEqual([
      Expr.LiteralNumber({ value: 100, unit: 's' }),
    ])
    expect(parse(`100ms`)).toEqual([
      Expr.LiteralNumber({ value: 100, unit: 'ms' }),
    ])
    expect(parse(`3.82`)).toEqual([
      Expr.LiteralNumber({ value: 3.82, unit: '' }),
    ])
    expect(parse(`3.82s`)).toEqual([
      Expr.LiteralNumber({ value: 3.82, unit: 's' }),
    ])
    expect(parse(`3.82ms`)).toEqual([
      Expr.LiteralNumber({ value: 3.82, unit: 'ms' }),
    ])
    expect(parse(`-100`)).toEqual([
      Expr.LiteralNumber({ value: -100, unit: '' }),
    ])
    expect(parse(`-100s`)).toEqual([
      Expr.LiteralNumber({ value: -100, unit: 's' }),
    ])
    expect(parse(`-100ms`)).toEqual([
      Expr.LiteralNumber({ value: -100, unit: 'ms' }),
    ])
    expect(parse(`-3.82`)).toEqual([
      Expr.LiteralNumber({ value: -3.82, unit: '' }),
    ])
    expect(parse(`-3.82s`)).toEqual([
      Expr.LiteralNumber({ value: -3.82, unit: 's' }),
    ])
    expect(parse(`-3.82ms`)).toEqual([
      Expr.LiteralNumber({ value: -3.82, unit: 'ms' }),
    ])
  })
})