graph-ecs/tests/query-parser/common.test.js

84 lines
3 KiB
JavaScript
Raw Normal View History

2024-11-10 10:15:43 +01:00
import { describe, it } from 'node:test'
2024-11-15 07:36:19 +01:00
import assert from '../assert.js'
2024-11-28 08:25:49 +01:00
import { Alias, Identifier, Literal, ObjectPath, Property } from '../../src/query-parser/types.js'
2024-11-20 20:21:41 +01:00
import { baseValue, literal, value } from '../../src/query-parser/common.js'
2024-11-10 10:15:43 +01:00
describe('common parser library', () => {
2024-11-15 07:36:19 +01:00
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'],
]
2024-11-10 10:15:43 +01:00
it('literals should match literal types', () => {
2024-11-15 07:36:19 +01:00
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))
})
})
2024-11-10 10:15:43 +01:00
})
2024-11-10 22:25:26 +01:00
2024-11-15 07:36:19 +01:00
it('baseValue should parse identifiers', () => {
assert.parseOk(baseValue, 'red', ([actual]) => {
assert.deepEqual(actual, new Identifier('red'))
})
2024-11-10 22:25:26 +01:00
2024-11-15 07:36:19 +01:00
assert.parseOk(baseValue, 'ginger.snaps', ([actual]) => {
2024-11-28 08:25:49 +01:00
assert.deepEqual(actual, new ObjectPath(new Identifier('ginger'), new Property(new Identifier('snaps'))))
2024-11-15 07:36:19 +01:00
})
2024-11-10 22:25:26 +01:00
})
2024-11-15 07:36:19 +01:00
it('value should parse literals', () => {
literals.forEach(([input, expected]) => {
assert.parseOk(value, input, ([actual]) => {
assert.deepEqual(actual, new Literal(expected))
})
})
2024-11-10 22:25:26 +01:00
})
2024-11-15 07:36:19 +01:00
it('value should parse identifiers', () => {
assert.parseOk(baseValue, 'violet', ([actual]) => {
assert.deepEqual(actual, new Identifier('violet'))
})
assert.parseOk(baseValue, 'monster.girl', ([actual]) => {
2024-11-28 08:25:49 +01:00
assert.deepEqual(actual, new ObjectPath(new Identifier('monster'), new Property(new Identifier('girl'))))
2024-11-15 07:36:19 +01:00
})
})
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]) => {
2024-11-28 08:25:49 +01:00
const obj = new ObjectPath(new Identifier('rowan'), new Property(new Identifier('containment')), new Property(new Identifier('isBreached')))
2024-11-15 07:36:19 +01:00
const alias = new Alias(obj, new Identifier('rawrnEscaped'))
assert.deepEqual(actual, alias)
})
2024-11-10 22:25:26 +01:00
})
2024-11-10 10:15:43 +01:00
})