2024-11-10 10:15:43 +01:00
|
|
|
import { describe, it } from 'node:test'
|
|
|
|
import assert from 'node:assert'
|
2024-11-10 22:25:26 +01:00
|
|
|
import { parse } from '../../src/parser.js'
|
2024-11-10 10:15:43 +01:00
|
|
|
import * as Common from '../../src/query/common.js'
|
2024-11-13 20:24:29 +01:00
|
|
|
import { Alias, Identifier, Literal, ObjectPath } from '../../src/query/types.js'
|
2024-11-10 10:15:43 +01:00
|
|
|
|
2024-11-13 20:24:29 +01:00
|
|
|
const v = r => r.unwrap()[0][0]
|
2024-11-10 10:15:43 +01:00
|
|
|
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"')
|
|
|
|
|
2024-11-13 20:24:29 +01:00
|
|
|
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'))
|
2024-11-10 10:15:43 +01:00
|
|
|
})
|
2024-11-10 22:25:26 +01:00
|
|
|
|
|
|
|
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"')
|
|
|
|
|
2024-11-13 20:24:29 +01:00
|
|
|
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'))
|
2024-11-10 22:25:26 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it('value should match variables', () => {
|
|
|
|
const identifier = parse(Common.baseValue, 'test')
|
|
|
|
const accessor = parse(Common.baseValue, 'test.value')
|
2024-11-13 20:24:29 +01:00
|
|
|
assert.deepEqual(v(identifier), new Identifier('test'))
|
|
|
|
assert.deepEqual(v(accessor), new ObjectPath(new Identifier('test'), new Identifier('value')))
|
2024-11-10 22:25:26 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
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')
|
2024-11-13 20:24:29 +01:00
|
|
|
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')))
|
2024-11-10 22:25:26 +01:00
|
|
|
})
|
2024-11-10 10:15:43 +01:00
|
|
|
})
|
|
|
|
|