mtcute/packages/tl-utils/tests/merge.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

164 lines
5 KiB
TypeScript

import { describe, it } from 'mocha'
import { expect } from 'chai'
import { mergeTlEntries, mergeTlSchemas } from '../src/merge'
import { parseTlToEntries } from '../src/parse'
import { writeTlEntryToString } from '../src/stringify'
import { parseFullTlSchema, writeTlEntriesToString } from '../src/schema'
describe('mergeTlEntries', () => {
const test = (tl: string, expected: string) => {
const res = mergeTlEntries(parseTlToEntries(tl))
if (typeof res === 'string') {
expect(res).eq(expected)
} else {
expect(writeTlEntryToString(res)).eq(expected)
}
}
it('fails on conflicting kinds', () => {
test(
'test = Test;\n---functions---\ntest = Test;',
'basic info mismatch'
)
})
it('fails on conflicting names', () => {
test('test1 = Test;\ntest2 = Test;', 'basic info mismatch')
})
it('fails on conflicting types', () => {
test('test = Test1;\ntest = Test2;', 'basic info mismatch')
})
it('fails on conflicting ids', () => {
test('test = Test;\ntest foo:int = Test;', 'basic info mismatch')
})
it('fails on conflicting ids', () => {
test('test = Test;\ntest foo:int = Test;', 'basic info mismatch')
})
it('merges true flags', () => {
test(
'test = Test;\n' +
'test foo:flags.0?true = Test;\n' +
'test bar:flags.0?true = Test;\n' +
'test baz:flags.1?true = Test;',
'test#1c173316 foo:flags.0?true bar:flags.0?true baz:flags.1?true = Test;'
)
test(
'test foo:flags.0?true = Test;\n' +
'test bar:flags.0?true = Test;\n' +
'test baz:flags.1?true = Test;',
'test#1c173316 foo:flags.0?true bar:flags.0?true baz:flags.1?true = Test;'
)
test(
'test foo:flags.0?true = Test;\n' +
'test foo:flags.0?true bar:flags.0?true = Test;\n' +
'test baz:flags.1?true = Test;\n' +
'test bar:flags.0?true baz:flags.1?true = Test;',
'test#1c173316 foo:flags.0?true bar:flags.0?true baz:flags.1?true = Test;'
)
})
})
describe('mergeTlSchemas', () => {
const test = async (
schemas: string[][],
onConflict: number,
...expected: string[]
) => {
const res = await mergeTlSchemas(
schemas.map((tl) =>
parseFullTlSchema(parseTlToEntries(tl.join('\n')))
),
(opts) => opts[onConflict]
)
expect(
writeTlEntriesToString(res.entries, { omitPrimitives: true, tdlibComments: true })
).eq(expected.join('\n'))
}
it('merges different constructors', () => {
test(
[
['testClass = Test;'],
['testClass2 = Test;'],
['---functions---', 'testMethod = Test;'],
],
0,
'testClass = Test;',
'testClass2 = Test;',
'---functions---',
'testMethod = Test;'
)
})
it('merges true flags in constructors', () => {
test(
[
['test foo:flags.0?true = Test;'],
['test bar:flags.0?true = Test;'],
['test foo:flags.0?true bar:flags.0?true = Test;'],
],
0,
'test#1c173316 foo:flags.0?true bar:flags.0?true = Test;'
)
})
it('resolves conflict using user-provided option', () => {
test(
[
['test foo:int = Test;'],
['test bar:int = Test;'],
['test baz:int = Test;'],
],
0,
'test foo:int = Test;'
)
test(
[
['test foo:int = Test;'],
['test bar:int = Test;'],
['test baz:int = Test;'],
],
1,
'test foo:int = Test;'
)
test([['test foo:int = Test;'], [], ['test bar:int = Test;']], 1, '')
})
it('merges comments', () => {
test(
[
['test foo:flags.0?true = Test;'],
['// test ctor', 'test bar:flags.0?true = Test;'],
[
'// will be ignored',
'test foo:flags.0?true bar:flags.0?true = Test;',
],
],
0,
'// @description test ctor',
'test#1c173316 foo:flags.0?true bar:flags.0?true = Test;'
)
})
it('merges arguments comments', () => {
test(
[
['test foo:flags.0?true = Test;'],
['// @bar bar comment', 'test bar:flags.0?true = Test;'],
[
'// @foo foo comment',
'test foo:flags.0?true bar:flags.0?true = Test;',
],
],
0,
'// @foo foo comment',
'// @bar bar comment',
'test#1c173316 foo:flags.0?true bar:flags.0?true = Test;'
)
})
})