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

62 lines
2.5 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, ObjectPath } from '../../src/query/types.js'
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"')
const v = r => r.value[0][0].value
assert.strictEqual(v(tBool), true)
assert.strictEqual(v(fBool), false)
assert.strictEqual(v(uint), 5)
assert.strictEqual(v(posInt), 16)
assert.strictEqual(v(negInt), -710)
assert.strictEqual(v(ufloat), 2.1)
assert.strictEqual(v(posFloat), 0.1)
assert.strictEqual(v(negFloat), -12.01)
assert.strictEqual(v(string), '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"')
const v = r => r.value[0][0].value
assert.strictEqual(v(bool), false)
assert.strictEqual(v(uint), 11)
assert.strictEqual(v(float), 0.15)
assert.strictEqual(v(str), 'abc')
})
it('value should match variables', () => {
const identifier = parse(Common.baseValue, 'test')
const accessor = parse(Common.baseValue, 'test.value')
const v = r => r.value[0][0]
assert.strictEqual(v(identifier), 'test')
assert.deepEqual(v(accessor), new ObjectPath('test', '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')
const v = r => r.value[0][0]
assert.strictEqual(v(noAlias), 'test')
assert.deepEqual(v(aliased1), new Alias('crybaby', 'cb'))
assert.deepEqual(v(aliased2), new Alias(new ObjectPath('property', 'name'), 'name'))
})
})