graph-ecs/tests/query-parser/common.test.js
2024-11-20 03:54:35 -06:00

83 lines
2.9 KiB
JavaScript

import { describe, it } from 'node:test'
import assert from '../assert.js'
import { Alias, Identifier, Literal, ObjectPath } from '../../src/query/types.js'
import { baseValue, literal, value } from '../../src/query/common.js'
describe('common parser library', () => {
const literals = [
['true', true],
['false', false],
['5', 5],
['+16', 16],
['-710', -710],
['2.1', 2.1],
['+0.001', 0.001],
['-22.69', -22.69],
['"hellion"', 'hellion'],
]
it('literals should match literal types', () => {
literals.forEach(([input, expected]) => {
assert.parseOk(literal, input, ([actual]) => {
assert.deepEqual(actual, new Literal(expected))
})
})
})
it('baseValue should match literals', () => {
literals.forEach(([input, expected]) => {
assert.parseOk(baseValue, input, ([actual]) => {
assert.deepEqual(actual, new Literal(expected))
})
})
})
it('baseValue should parse identifiers', () => {
assert.parseOk(baseValue, 'red', ([actual]) => {
assert.deepEqual(actual, new Identifier('red'))
})
assert.parseOk(baseValue, 'ginger.snaps', ([actual]) => {
assert.deepEqual(actual, new ObjectPath(new Identifier('ginger'), new Identifier('snaps')))
})
})
it('value should parse literals', () => {
literals.forEach(([input, expected]) => {
assert.parseOk(value, input, ([actual]) => {
assert.deepEqual(actual, new Literal(expected))
})
})
})
it('value should parse identifiers', () => {
assert.parseOk(baseValue, 'violet', ([actual]) => {
assert.deepEqual(actual, new Identifier('violet'))
})
assert.parseOk(baseValue, 'monster.girl', ([actual]) => {
assert.deepEqual(actual, new ObjectPath(new Identifier('monster'), new Identifier('girl')))
})
})
it('value should parse aliases', () => {
assert.parseOk(value, '19589 AS sybil', ([actual]) => {
assert.deepEqual(actual, new Alias(new Literal(19589), new Identifier('sybil')))
})
assert.parseOk(value, '3.14159 AS PI', ([actual]) => {
assert.deepEqual(actual, new Alias(new Literal(3.14159), new Identifier('PI')))
})
assert.parseOk(value, 'crybaby AS cb', ([actual]) => {
assert.deepEqual(actual, new Alias(new Identifier('crybaby'), new Identifier('cb')))
})
assert.parseOk(value, 'rowan.containment.isBreached AS rawrnEscaped', ([actual]) => {
const obj = new ObjectPath(new Identifier('rowan'), new Identifier('containment'), new Identifier('isBreached'))
const alias = new Alias(obj, new Identifier('rawrnEscaped'))
assert.deepEqual(actual, alias)
})
})
})