2023-11-09 00:20:43 +03:00
|
|
|
import { describe, expect, it } from 'vitest'
|
2022-06-30 16:32:56 +03:00
|
|
|
|
2023-11-09 00:20:43 +03:00
|
|
|
import { mergeTlEntries, mergeTlSchemas } from './merge.js'
|
|
|
|
import { parseTlToEntries } from './parse.js'
|
|
|
|
import { parseFullTlSchema, writeTlEntriesToString } from './schema.js'
|
|
|
|
import { writeTlEntryToString } from './stringify.js'
|
2021-11-23 00:03:59 +03:00
|
|
|
|
|
|
|
describe('mergeTlEntries', () => {
|
|
|
|
const test = (tl: string, expected: string) => {
|
|
|
|
const res = mergeTlEntries(parseTlToEntries(tl))
|
2023-06-05 03:30:48 +03:00
|
|
|
|
2021-11-23 00:03:59 +03:00
|
|
|
if (typeof res === 'string') {
|
2023-11-09 00:20:43 +03:00
|
|
|
expect(res).toEqual(expected)
|
2021-11-23 00:03:59 +03:00
|
|
|
} else {
|
2023-11-09 00:20:43 +03:00
|
|
|
expect(writeTlEntryToString(res)).toEqual(expected)
|
2021-11-23 00:03:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
it('fails on conflicting kinds', () => {
|
2023-09-24 19:56:13 +03:00
|
|
|
test('test = Test;\n---functions---\ntest = Test;', 'basic info mismatch - kind')
|
2021-11-23 00:03:59 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
it('fails on conflicting names', () => {
|
2023-09-24 19:56:13 +03:00
|
|
|
test('test1 = Test;\ntest2 = Test;', 'basic info mismatch - name')
|
2021-11-23 00:03:59 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
it('fails on conflicting types', () => {
|
2023-09-24 19:56:13 +03:00
|
|
|
test('test = Test1;\ntest = Test2;', 'basic info mismatch - type')
|
2021-11-23 00:03:59 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
it('fails on conflicting ids', () => {
|
2023-09-24 19:56:13 +03:00
|
|
|
test('test = Test;\ntest foo:int = Test;', 'basic info mismatch - id')
|
2021-11-23 00:03:59 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
it('merges true flags', () => {
|
|
|
|
test(
|
2022-04-29 16:36:41 +03:00
|
|
|
'test flags:# = Test;\n' +
|
|
|
|
'test flags:# foo:flags.0?true = Test;\n' +
|
|
|
|
'test flags:# bar:flags.0?true = Test;\n' +
|
|
|
|
'test flags:# baz:flags.1?true = Test;',
|
2023-06-05 03:30:48 +03:00
|
|
|
'test#e86481ba flags:# foo:flags.0?true bar:flags.0?true baz:flags.1?true = Test;',
|
2021-11-23 00:03:59 +03:00
|
|
|
)
|
2023-06-25 03:09:04 +03:00
|
|
|
// ordering of optional flags should not matter
|
2021-11-23 00:03:59 +03:00
|
|
|
test(
|
2022-04-29 16:36:41 +03:00
|
|
|
'test flags:# foo:flags.0?true = Test;\n' +
|
|
|
|
'test flags:# bar:flags.0?true = Test;\n' +
|
|
|
|
'test flags:# baz:flags.1?true = Test;',
|
2023-09-21 02:53:08 +03:00
|
|
|
'test#e86481ba flags:# foo:flags.0?true bar:flags.0?true baz:flags.1?true = Test;',
|
2021-11-23 00:03:59 +03:00
|
|
|
)
|
|
|
|
test(
|
2022-04-29 16:36:41 +03:00
|
|
|
'test flags:# foo:flags.0?true = Test;\n' +
|
|
|
|
'test flags:# foo:flags.0?true bar:flags.0?true = Test;\n' +
|
|
|
|
'test flags:# baz:flags.1?true = Test;\n' +
|
|
|
|
'test flags:# bar:flags.0?true baz:flags.1?true = Test;',
|
2023-09-21 02:53:08 +03:00
|
|
|
'test#e86481ba flags:# foo:flags.0?true bar:flags.0?true baz:flags.1?true = Test;',
|
2022-04-29 16:36:41 +03:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('merges true flags with multiple flags fields', () => {
|
|
|
|
test(
|
|
|
|
'test flags:# flags2:# = Test;\n' +
|
|
|
|
'test flags:# foo:flags.0?true flags2:# = Test;\n' +
|
|
|
|
'test flags:# flags2:# bar:flags2.0?true = Test;\n',
|
2023-06-05 03:30:48 +03:00
|
|
|
'test#5ca39a98 flags:# foo:flags.0?true flags2:# bar:flags2.0?true = Test;',
|
2021-11-23 00:03:59 +03:00
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('mergeTlSchemas', () => {
|
2023-09-24 01:32:22 +03:00
|
|
|
const test = async (schemas: string[][], onConflict: number, ...expected: string[]) => {
|
2021-11-23 00:03:59 +03:00
|
|
|
const res = await mergeTlSchemas(
|
2023-09-24 01:32:22 +03:00
|
|
|
schemas.map((tl) => parseFullTlSchema(parseTlToEntries(tl.join('\n')))),
|
2023-06-05 03:30:48 +03:00
|
|
|
(opts) => opts[onConflict],
|
2021-11-23 00:03:59 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
expect(
|
2022-06-30 16:32:56 +03:00
|
|
|
writeTlEntriesToString(res.entries, {
|
|
|
|
omitPrimitives: true,
|
|
|
|
tdlibComments: true,
|
2023-06-05 03:30:48 +03:00
|
|
|
}),
|
2023-11-09 00:20:43 +03:00
|
|
|
).toEqual(expected.join('\n'))
|
2021-11-23 00:03:59 +03:00
|
|
|
}
|
|
|
|
|
2023-09-03 02:37:51 +03:00
|
|
|
it('merges different constructors', async () => {
|
|
|
|
await test(
|
2023-09-24 01:32:22 +03:00
|
|
|
[['testClass = Test;'], ['testClass2 = Test;'], ['---functions---', 'testMethod = Test;']],
|
2021-11-23 00:03:59 +03:00
|
|
|
0,
|
2023-09-20 18:37:26 +03:00
|
|
|
'testClass#5d60a438 = Test;',
|
|
|
|
'testClass2#39c5c841 = Test;',
|
2021-11-23 00:03:59 +03:00
|
|
|
'---functions---',
|
2023-09-20 18:37:26 +03:00
|
|
|
'testMethod#87d8a7d2 = Test;',
|
2021-11-23 00:03:59 +03:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2023-09-03 02:37:51 +03:00
|
|
|
it('merges true flags in constructors', async () => {
|
|
|
|
await test(
|
2021-11-23 00:03:59 +03:00
|
|
|
[
|
|
|
|
['test foo:flags.0?true = Test;'],
|
|
|
|
['test bar:flags.0?true = Test;'],
|
|
|
|
['test foo:flags.0?true bar:flags.0?true = Test;'],
|
|
|
|
],
|
|
|
|
0,
|
2023-09-21 02:53:08 +03:00
|
|
|
'test#1c173316 foo:flags.0?true bar:flags.0?true = Test;',
|
2021-11-23 00:03:59 +03:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2023-09-03 02:37:51 +03:00
|
|
|
it('resolves conflict using user-provided option', async () => {
|
|
|
|
await test(
|
2023-09-24 01:32:22 +03:00
|
|
|
[['test foo:int = Test;'], ['test bar:int = Test;'], ['test baz:int = Test;']],
|
2021-11-23 00:03:59 +03:00
|
|
|
0,
|
2023-09-20 18:37:26 +03:00
|
|
|
'test#4f6455cd foo:int = Test;',
|
2021-11-23 00:03:59 +03:00
|
|
|
)
|
2023-09-03 02:37:51 +03:00
|
|
|
await test(
|
2023-09-24 01:32:22 +03:00
|
|
|
[['test foo:int = Test;'], ['test bar:int = Test;'], ['test baz:int = Test;']],
|
2021-11-23 00:03:59 +03:00
|
|
|
1,
|
2023-09-20 18:37:26 +03:00
|
|
|
'test#3e993a74 bar:int = Test;',
|
2021-11-23 00:03:59 +03:00
|
|
|
)
|
2023-09-24 01:32:22 +03:00
|
|
|
await test([['test foo:int = Test;'], [], ['test bar:int = Test;']], 1, '')
|
2021-11-23 00:03:59 +03:00
|
|
|
})
|
|
|
|
|
2023-09-03 02:37:51 +03:00
|
|
|
it('merges comments', async () => {
|
|
|
|
await test(
|
2021-11-23 00:03:59 +03:00
|
|
|
[
|
|
|
|
['test foo:flags.0?true = Test;'],
|
|
|
|
['// test ctor', 'test bar:flags.0?true = Test;'],
|
2023-09-24 01:32:22 +03:00
|
|
|
['// will be ignored', 'test foo:flags.0?true bar:flags.0?true = Test;'],
|
2021-11-23 00:03:59 +03:00
|
|
|
],
|
|
|
|
0,
|
|
|
|
'// @description test ctor',
|
2023-09-21 02:53:08 +03:00
|
|
|
'test#1c173316 foo:flags.0?true bar:flags.0?true = Test;',
|
2021-11-23 00:03:59 +03:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2023-09-03 02:37:51 +03:00
|
|
|
it('merges arguments comments', async () => {
|
|
|
|
await test(
|
2021-11-23 00:03:59 +03:00
|
|
|
[
|
|
|
|
['test foo:flags.0?true = Test;'],
|
2023-09-24 01:32:22 +03:00
|
|
|
['// @description test @bar bar comment', 'test bar:flags.0?true = Test;'],
|
|
|
|
['// @description test @foo foo comment', 'test foo:flags.0?true bar:flags.0?true = Test;'],
|
2021-11-23 00:03:59 +03:00
|
|
|
],
|
|
|
|
0,
|
2023-09-20 18:37:26 +03:00
|
|
|
'// @description test',
|
2021-11-23 00:03:59 +03:00
|
|
|
'// @foo foo comment',
|
|
|
|
'// @bar bar comment',
|
2023-06-05 03:30:48 +03:00
|
|
|
'test#1c173316 foo:flags.0?true bar:flags.0?true = Test;',
|
2021-11-23 00:03:59 +03:00
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|