mtcute/packages/tl-utils/tests/codegen/reader.spec.ts
teidesu 916c41e70c build(codegen): refactored reader codegen (removed the need in additional object), added tests for multiple flags fields
i honestly don't know why i did what i did, it's such an over-complication, lol.
2022-04-29 16:13:07 +03:00

124 lines
4.1 KiB
TypeScript

import { describe, it } from 'mocha'
import { expect } from 'chai'
import { generateReaderCodeForTlEntry } from '../../src/codegen/reader'
import { parseTlToEntries } from '../../src/parse'
describe('generateReaderCodeForTlEntry', () => {
const test = (tl: string, ...js: string[]) => {
const entry = parseTlToEntries(tl)[0]
expect(generateReaderCodeForTlEntry(entry)).eq(
`${entry.id}:function(r){${js.join('')}},`
)
}
it('generates code for constructors without arguments', () => {
test(
'topPeerCategoryBotsPM#ab661b5b = TopPeerCategory;',
"return{_:'topPeerCategoryBotsPM'}"
)
})
it('generates code for constructors with simple arguments', () => {
test(
'inputBotInlineMessageID#890c3d89 dc_id:int id:long access_hash:long = InputBotInlineMessageID;',
'return{',
"_:'inputBotInlineMessageID',",
'dcId:r.int(),',
'id:r.long(),',
'accessHash:r.long(),',
'}'
)
test(
'contact#145ade0b user_id:long mutual:Bool = Contact;',
'return{',
"_:'contact',",
'userId:r.long(),',
'mutual:r.boolean(),',
'}'
)
test(
'maskCoords#aed6dbb2 n:int x:double y:double zoom:double = MaskCoords;',
'return{',
"_:'maskCoords',",
'n:r.int(),',
'x:r.double(),',
'y:r.double(),',
'zoom:r.double(),',
'}'
)
})
it('generates code for constructors with true flags', () => {
test(
'messages.messageEditData#26b5dde6 flags:# caption:flags.0?true = messages.MessageEditData;',
'var flags=r.uint();',
'return{',
"_:'messages.messageEditData',",
'caption:!!(flags&1),',
'}'
)
})
it('generates code for constructors with optional arguments', () => {
test(
'updates.channelDifferenceEmpty#3e11affb flags:# final:flags.0?true pts:int timeout:flags.1?int = updates.ChannelDifference;',
'var flags=r.uint();',
'return{',
"_:'updates.channelDifferenceEmpty',",
'final:!!(flags&1),',
'pts:r.int(),',
'timeout:flags&2?r.int():void 0,',
'}'
)
})
it('generates code for constructors with multiple flags fields', () => {
test(
'updates.channelDifferenceEmpty#3e11affb flags:# final:flags.0?true pts:int timeout:flags.1?int flags2:# can_delete_channel:flags2.0?true = updates.ChannelDifference;',
'var flags=r.uint();',
'var flags2=r.uint();',
'return{',
"_:'updates.channelDifferenceEmpty',",
'final:!!(flags&1),',
'pts:r.int(),',
'timeout:flags&2?r.int():void 0,',
'canDeleteChannel:!!(flags2&1),',
'}'
)
})
it('generates code for constructors with vector arguments', () => {
test(
'contacts.resolvedPeer#7f077ad9 peer:Peer chats:Vector<Chat> users:Vector<User> = contacts.ResolvedPeer;',
'return{',
"_:'contacts.resolvedPeer',",
'peer:r.object(),',
'chats:r.vector(r.object),',
'users:r.vector(r.object),',
'}'
)
})
it('generates code for constructors with optional vector arguments', () => {
test(
'messages.getWebPagePreview#8b68b0cc flags:# message:string entities:flags.3?Vector<MessageEntity> = MessageMedia;',
'var flags=r.uint();',
'return{',
"_:'messages.getWebPagePreview',",
'message:r.string(),',
'entities:flags&8?r.vector(r.object):void 0,',
'}'
)
})
it('generates code for constructors with generics', () => {
test(
'invokeWithLayer#da9b0d0d {X:Type} layer:int query:!X = X;',
'return{',
"_:'invokeWithLayer',",
'layer:r.int(),',
'query:r.object(),',
'}'
)
})
})