Skip to content

Commit

Permalink
chore: split up agnostic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
CodyJasonBennett committed Nov 12, 2023
1 parent 5f49871 commit ca16ea5
Showing 1 changed file with 84 additions and 86 deletions.
170 changes: 84 additions & 86 deletions tests/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,100 +11,98 @@ import {
} from 'shaderkit'

describe('parser', () => {
describe('GLSL', () => {
it('parses identifiers', () => {
const identifier = parse('identifier')[0] as Identifier
expect(identifier).toBeInstanceOf(Identifier)
expect(identifier.value).toBe('identifier')
})
it('parses identifiers', () => {
const identifier = parse('identifier')[0] as Identifier
expect(identifier).toBeInstanceOf(Identifier)
expect(identifier.value).toBe('identifier')
})

it('parses literals', () => {
for (const value of ['true', 'false', '0', '0.0']) {
const literal = parse(value)[0] as Literal
expect(literal).toBeInstanceOf(Literal)
expect(literal.value).toBe(value)
}
})
it('parses literals', () => {
for (const value of ['true', 'false', '0', '0.0']) {
const literal = parse(value)[0] as Literal
expect(literal).toBeInstanceOf(Literal)
expect(literal.value).toBe(value)
}
})

it('parses unary expressions', () => {
for (const operator of ['+', '-', '~', '!', '++', '--']) {
const left = parse(`0${operator}`)[0] as UnaryExpression
expect(left).toBeInstanceOf(UnaryExpression)
expect(left.operator).toBe(operator)
expect(left.argument).toBeInstanceOf(Literal)
expect((left.argument as Literal).value).toBe('0')
it('parses unary expressions', () => {
for (const operator of ['+', '-', '~', '!', '++', '--']) {
const left = parse(`0${operator}`)[0] as UnaryExpression
expect(left).toBeInstanceOf(UnaryExpression)
expect(left.operator).toBe(operator)
expect(left.argument).toBeInstanceOf(Literal)
expect((left.argument as Literal).value).toBe('0')

const right = parse(`${operator}1`)[0] as UnaryExpression
expect(right).toBeInstanceOf(UnaryExpression)
expect(right.operator).toBe(operator)
expect(right.argument).toBeInstanceOf(Literal)
expect((right.argument as Literal).value).toBe('1')
}
})
const right = parse(`${operator}1`)[0] as UnaryExpression
expect(right).toBeInstanceOf(UnaryExpression)
expect(right.operator).toBe(operator)
expect(right.argument).toBeInstanceOf(Literal)
expect((right.argument as Literal).value).toBe('1')
}
})

it('parses binary expressions', () => {
const expression = parse('0 == 1')[0] as BinaryExpression
expect(expression).toBeInstanceOf(BinaryExpression)
expect(expression.operator).toBe('==')
expect(expression.left).toBeInstanceOf(Literal)
expect((expression.left as Literal).value).toBe('0')
expect(expression.right).toBeInstanceOf(Literal)
expect((expression.right as Literal).value).toBe('1')
})
it('parses binary expressions', () => {
const expression = parse('0 == 1')[0] as BinaryExpression
expect(expression).toBeInstanceOf(BinaryExpression)
expect(expression.operator).toBe('==')
expect(expression.left).toBeInstanceOf(Literal)
expect((expression.left as Literal).value).toBe('0')
expect(expression.right).toBeInstanceOf(Literal)
expect((expression.right as Literal).value).toBe('1')
})

it('parses ternary expressions', () => {
const expression = parse('true ? 0 : 1')[0] as TernaryExpression
expect(expression).toBeInstanceOf(TernaryExpression)
expect(expression.test).toBeInstanceOf(Literal)
expect((expression.test as Literal).value).toBe('true')
expect(expression.consequent).toBeInstanceOf(Literal)
expect((expression.consequent as Literal).value).toBe('0')
expect(expression.alternate).toBeInstanceOf(Literal)
expect((expression.alternate as Literal).value).toBe('1')
})
it('parses ternary expressions', () => {
const expression = parse('true ? 0 : 1')[0] as TernaryExpression
expect(expression).toBeInstanceOf(TernaryExpression)
expect(expression.test).toBeInstanceOf(Literal)
expect((expression.test as Literal).value).toBe('true')
expect(expression.consequent).toBeInstanceOf(Literal)
expect((expression.consequent as Literal).value).toBe('0')
expect(expression.alternate).toBeInstanceOf(Literal)
expect((expression.alternate as Literal).value).toBe('1')
})

it('parses call expressions', () => {
{
const expression = parse('main()')[0] as CallExpression
expect(expression).toBeInstanceOf(CallExpression)
expect(expression.callee).toBeInstanceOf(Identifier)
expect((expression.callee as Identifier).value).toBe('main')
expect(expression.args.length).toBe(0)
}
it('parses call expressions', () => {
{
const expression = parse('main()')[0] as CallExpression
expect(expression).toBeInstanceOf(CallExpression)
expect(expression.callee).toBeInstanceOf(Identifier)
expect((expression.callee as Identifier).value).toBe('main')
expect(expression.args.length).toBe(0)
}

{
const expression = parse('all(true, false)')[0] as CallExpression
expect(expression).toBeInstanceOf(CallExpression)
expect(expression.callee).toBeInstanceOf(Identifier)
expect((expression.callee as Identifier).value).toBe('all')
expect(expression.args.length).toBe(2)
expect(expression.args[0]).toBeInstanceOf(Literal)
expect((expression.args[0] as Literal).value).toBe('true')
expect(expression.args[1]).toBeInstanceOf(Literal)
expect((expression.args[1] as Literal).value).toBe('false')
}
})
{
const expression = parse('all(true, false)')[0] as CallExpression
expect(expression).toBeInstanceOf(CallExpression)
expect(expression.callee).toBeInstanceOf(Identifier)
expect((expression.callee as Identifier).value).toBe('all')
expect(expression.args.length).toBe(2)
expect(expression.args[0]).toBeInstanceOf(Literal)
expect((expression.args[0] as Literal).value).toBe('true')
expect(expression.args[1]).toBeInstanceOf(Literal)
expect((expression.args[1] as Literal).value).toBe('false')
}
})

it('parses member expressions', () => {
{
const expression = parse('foo.bar')[0] as MemberExpression
expect(expression).toBeInstanceOf(MemberExpression)
expect(expression.object).toBeInstanceOf(Identifier)
expect((expression.object as Identifier).value).toBe('foo')
expect(expression.property).toBeInstanceOf(Identifier)
expect((expression.property as Identifier).value).toBe('bar')
}
it('parses member expressions', () => {
{
const expression = parse('foo.bar')[0] as MemberExpression
expect(expression).toBeInstanceOf(MemberExpression)
expect(expression.object).toBeInstanceOf(Identifier)
expect((expression.object as Identifier).value).toBe('foo')
expect(expression.property).toBeInstanceOf(Identifier)
expect((expression.property as Identifier).value).toBe('bar')
}

{
const expression = parse('array.length()')[0] as CallExpression
expect(expression).toBeInstanceOf(CallExpression)
expect(expression.callee).toBeInstanceOf(MemberExpression)
expect((expression.callee as MemberExpression).object).toBeInstanceOf(Identifier)
expect(((expression.callee as MemberExpression).object as Identifier).value).toBe('array')
expect((expression.callee as MemberExpression).property).toBeInstanceOf(Identifier)
expect(((expression.callee as MemberExpression).property as Identifier).value).toBe('length')
expect(expression.args.length).toBe(0)
}
})
{
const expression = parse('array.length()')[0] as CallExpression
expect(expression).toBeInstanceOf(CallExpression)
expect(expression.callee).toBeInstanceOf(MemberExpression)
expect((expression.callee as MemberExpression).object).toBeInstanceOf(Identifier)
expect(((expression.callee as MemberExpression).object as Identifier).value).toBe('array')
expect((expression.callee as MemberExpression).property).toBeInstanceOf(Identifier)
expect(((expression.callee as MemberExpression).property as Identifier).value).toBe('length')
expect(expression.args.length).toBe(0)
}
})
})

0 comments on commit ca16ea5

Please sign in to comment.