graph-ecs/tests/query/common.test.js
2024-11-13 13:24:29 -06:00

59 lines
2.7 KiB
JavaScript

import { describe, it } from 'node:test'
import assert from 'node:assert'
import { parse } from '../../src/parser.js'
import * as Common from '../../src/query/common.js'
import { Alias, Identifier, Literal, ObjectPath } from '../../src/query/types.js'
const v = r => r.unwrap()[0][0]
describe('common parser library', () => {
it('literals should match literal types', () => {
const tBool = parse(Common.literal, 'true')
const fBool = parse(Common.literal, 'false')
const uint = parse(Common.literal, '5')
const posInt = parse(Common.literal, '+16')
const negInt = parse(Common.literal, '-710')
const ufloat = parse(Common.literal, '2.1')
const posFloat = parse(Common.literal, '+0.1')
const negFloat = parse(Common.literal, '-12.01')
const string = parse(Common.literal, '"akjsdfuaio"')
assert.deepEqual(v(tBool), new Literal(true))
assert.deepEqual(v(fBool), new Literal(false))
assert.deepEqual(v(uint), new Literal(5))
assert.deepEqual(v(posInt), new Literal(16))
assert.deepEqual(v(negInt), new Literal(-710))
assert.deepEqual(v(ufloat), new Literal(2.1))
assert.deepEqual(v(posFloat), new Literal(0.1))
assert.deepEqual(v(negFloat), new Literal(-12.01))
assert.deepEqual(v(string), new Literal('akjsdfuaio'))
})
it('value should match literals', () => {
const bool = parse(Common.baseValue, 'false')
const uint = parse(Common.baseValue, '11')
const float = parse(Common.baseValue, '0.15')
const str = parse(Common.baseValue, '"abc"')
assert.deepEqual(v(bool), new Literal(false))
assert.deepEqual(v(uint), new Literal(11))
assert.deepEqual(v(float), new Literal(0.15))
assert.deepEqual(v(str), new Literal('abc'))
})
it('value should match variables', () => {
const identifier = parse(Common.baseValue, 'test')
const accessor = parse(Common.baseValue, 'test.value')
assert.deepEqual(v(identifier), new Identifier('test'))
assert.deepEqual(v(accessor), new ObjectPath(new Identifier('test'), new Identifier('value')))
})
it('aliases should work i hope', () => {
const noAlias = parse(Common.value, 'test')
const aliased1 = parse(Common.value, 'crybaby AS cb')
const aliased2 = parse(Common.value, 'property.name AS name')
assert.deepEqual(v(noAlias), new Identifier('test'))
assert.deepEqual(v(aliased1), new Alias(new Identifier('crybaby'), new Identifier('cb')))
assert.deepEqual(v(aliased2), new Alias(new ObjectPath(new Identifier('property'), new Identifier('name')), new Identifier('name')))
})
})