summaryrefslogtreecommitdiff
path: root/tests/parser.spec.ts
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2024-01-21 14:36:47 +0530
committerAkshay Nair <phenax5@gmail.com>2024-01-21 14:36:47 +0530
commitf7a4596469d0652f868cad2d97a9edf92f4bc4c7 (patch)
tree6ad377459a86e5ad1f6d45e7bfaaec27403027ee /tests/parser.spec.ts
parentb31fe6447ec2e05bb9ccf3cffd85510d936c31d6 (diff)
downloadcss-everything-f7a4596469d0652f868cad2d97a9edf92f4bc4c7.tar.gz
css-everything-f7a4596469d0652f868cad2d97a9edf92f4bc4c7.zip
feat(parser): adds calc expression parser
Diffstat (limited to '')
-rw-r--r--tests/parser.spec.ts64
1 files changed, 56 insertions, 8 deletions
diff --git a/tests/parser.spec.ts b/tests/parser.spec.ts
index 825200e..494bb51 100644
--- a/tests/parser.spec.ts
+++ b/tests/parser.spec.ts
@@ -1,7 +1,7 @@
import { Expr, SelectorComp, parse, parseDeclarations } from '../src/parser'
describe('parser', () => {
- it('should parse function call', () => {
+ it('parses function call', () => {
expect(parse('hello()')).toEqual([Expr.Call({ name: 'hello', args: [] })])
expect(parse('hello ( wow , foo ) ')).toEqual([
Expr.Call({
@@ -30,7 +30,7 @@ describe('parser', () => {
])
})
- it('should parse sequential function calls', () => {
+ it('parses sequential function calls', () => {
expect(parse('hello(world) foo-doo(bar, baz)')).toEqual([
Expr.Call({
name: 'hello',
@@ -43,7 +43,7 @@ describe('parser', () => {
])
})
- it('should parse string literal', () => {
+ it('parses string literal', () => {
expect(parse(`"hello world toodles ' nice single quote there"`)).toEqual([
Expr.LiteralString(`hello world toodles ' nice single quote there`),
])
@@ -53,7 +53,7 @@ describe('parser', () => {
])
})
- it('should parse var identifiers', () => {
+ it('parses var identifiers', () => {
expect(parse(`var(--hello, 'default')`)).toEqual([
Expr.Call({
name: 'var',
@@ -88,7 +88,7 @@ describe('parser', () => {
])
})
- it('should parse number and css units', () => {
+ it('parses number and css units', () => {
expect(parse(`100`)).toEqual([Expr.LiteralNumber({ value: 100, unit: '' })])
expect(parse(`100s`)).toEqual([
Expr.LiteralNumber({ value: 100, unit: 's' }),
@@ -125,7 +125,7 @@ describe('parser', () => {
])
})
- it('should parse pair and map expressions', () => {
+ it('parses pair and map expressions', () => {
expect(parse(`--hello: "foobar is here"`)).toEqual([
Expr.Pair({
key: '--hello',
@@ -156,7 +156,7 @@ describe('parser', () => {
})
describe('parseDeclarations', () => {
- it('should parse complex selectors', () => {
+ it('parses complex selectors', () => {
expect(
parseDeclarations(`button#something.my-class[hello=world]`),
).toEqual([
@@ -194,7 +194,7 @@ describe('parser', () => {
])
})
- it('should parse declarations', () => {
+ it('parses declarations', () => {
expect(
parseDeclarations(
`instance(button#something, map(--text: "wow", --color: red))`,
@@ -220,4 +220,52 @@ describe('parser', () => {
])
})
})
+
+ describe('calc', () => {
+ it('parses calc expression', () => {
+ expect(parse(`calc(50% * 10px + 1px )`)).toEqual([
+ Expr.Call({
+ name: 'calc',
+ args: [
+ Expr.BinOp({
+ op: '+',
+ left: Expr.BinOp({
+ op: '*',
+ left: Expr.LiteralNumber({ value: 50, unit: '%' }),
+ right: Expr.LiteralNumber({ value: 10, unit: 'px' }),
+ }),
+ right: Expr.LiteralNumber({ value: 1, unit: 'px' }),
+ }),
+ ],
+ }),
+ ])
+ })
+
+ it('parses calc expression with parens', () => {
+ expect(parse(`calc((5))`)).toEqual([
+ Expr.Call({
+ name: 'calc',
+ args: [
+ Expr.Parens({ expr: Expr.LiteralNumber({ value: 5, unit: '' }) }),
+ ],
+ }),
+ ])
+ expect(parse(`calc(50% * (10px + 1px) )`)).toEqual([
+ Expr.Call({
+ name: 'calc',
+ args: [
+ Expr.BinOp({
+ op: '*',
+ left: Expr.LiteralNumber({ value: 50, unit: '%' }),
+ right: Expr.BinOp({
+ op: '+',
+ left: Expr.LiteralNumber({ value: 10, unit: 'px' }),
+ right: Expr.LiteralNumber({ value: 1, unit: 'px' }),
+ }),
+ }),
+ ],
+ }),
+ ])
+ })
+ })
})