mtcute/packages/tl-utils/tests/schema.spec.ts
teidesu ec736f8590 some changes
i've been wanting to name a commit like this for my entire life, lol. seriously though, a lot has changed:
 - extracted TL-related stuff to `@mtcute/tl-utils` and `@mtcute/tl-runtime`, rewrote codegen in TS
 - updated to layer 134, moved to int64 identifiers
 - rewritten networking (mtproto), rewritten updates handling
 - *lots* of refactoring

 still a very early version though, there are a lot of improvements to be made, but at least it runs, lol

 also tl-reference will not be updated anytime soon because i want to rewrite it
2021-11-23 00:03:59 +03:00

152 lines
4 KiB
TypeScript

import { describe, it } from 'mocha'
import { expect } from 'chai'
import { TlEntry } from '../src/types'
import { writeTlEntriesToString } from '../src/schema'
describe('writeTlEntriesToString', () => {
const test = (
entries: TlEntry[],
params: Parameters<typeof writeTlEntriesToString>[1],
...expected: string[]
) => {
expect(
writeTlEntriesToString(entries, {
omitPrimitives: true,
...params,
})
).eq(expected.join('\n'))
}
it('computes missing ids', () => {
const obj: TlEntry = {
kind: 'class',
name: 'error',
type: 'Error',
id: 0,
arguments: [
{
name: 'code',
type: 'int',
},
{
name: 'text',
type: 'string',
},
],
}
test(
[obj],
{ computeIds: false },
'error code:int text:string = Error;'
)
test([obj], {}, 'error#c4b9f9bb code:int text:string = Error;')
})
it('writes comments along with the constructor', () => {
const obj: TlEntry = {
kind: 'class',
name: 'error',
type: 'Error',
id: 0,
arguments: [
{
name: 'code',
type: 'int',
comment: 'Error code',
},
{
name: 'text',
type: 'string',
comment: 'Error description',
},
],
comment: 'An error',
}
test(
[obj],
{},
'// An error',
'error#c4b9f9bb code:int text:string = Error;'
)
obj.comment += '\nVery error'
test(
[obj],
{},
'// An error',
'//- Very error',
'error#c4b9f9bb code:int text:string = Error;'
)
})
it('writes tdlib-style comments', () => {
const obj: TlEntry = {
kind: 'class',
name: 'error',
type: 'Error',
id: 0,
arguments: [
{
name: 'code',
type: 'int',
comment: 'Error code',
},
{
name: 'text',
type: 'string',
comment: 'Error description',
},
],
comment: 'An error\nVery error',
}
test(
[obj],
{ tdlibComments: true },
'// @description An error',
'//- Very error',
'// @code Error code',
'// @text Error description',
'error#c4b9f9bb code:int text:string = Error;'
)
})
it('inserts kind annotation when kind changes', () => {
const cls: TlEntry = {
kind: 'class',
name: 'error',
type: 'Error',
id: 0,
arguments: [
{
name: 'code',
type: 'int',
},
{
name: 'text',
type: 'string',
},
],
}
const method: TlEntry = {
...cls,
kind: 'method'
}
test(
[method, cls, cls, method, cls],
{},
'---functions---',
'error#c4b9f9bb code:int text:string = Error;',
'---types---',
'error#c4b9f9bb code:int text:string = Error;',
'error#c4b9f9bb code:int text:string = Error;',
'---functions---',
'error#c4b9f9bb code:int text:string = Error;',
'---types---',
'error#c4b9f9bb code:int text:string = Error;',
)
})
})