2021-11-23 00:03:59 +03:00
|
|
|
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,',
|
|
|
|
'}'
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2022-04-29 16:13:07 +03:00
|
|
|
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;',
|
2022-06-28 19:26:11 +03:00
|
|
|
'var flags=r.uint(),',
|
|
|
|
'pts=r.int(),',
|
|
|
|
'timeout=flags&2?r.int():void 0,',
|
|
|
|
'flags2=r.uint();',
|
2022-04-29 16:13:07 +03:00
|
|
|
'return{',
|
|
|
|
"_:'updates.channelDifferenceEmpty',",
|
|
|
|
'final:!!(flags&1),',
|
2022-06-28 19:23:32 +03:00
|
|
|
"pts:pts,",
|
|
|
|
"timeout:timeout,",
|
2022-04-29 16:13:07 +03:00
|
|
|
'canDeleteChannel:!!(flags2&1),',
|
|
|
|
'}'
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-11-23 00:03:59 +03:00
|
|
|
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(),',
|
|
|
|
'}'
|
|
|
|
)
|
|
|
|
})
|
2022-06-28 18:57:22 +03:00
|
|
|
|
|
|
|
it('generates code with raw flags for constructors with flags', () => {
|
|
|
|
const entry = parseTlToEntries('test flags:# flags2:# = Test;')[0]
|
|
|
|
expect(generateReaderCodeForTlEntry(entry, true)).eq(
|
|
|
|
`${entry.id}:function(r){${[
|
2022-06-28 19:26:11 +03:00
|
|
|
'var flags=r.uint(),',
|
|
|
|
'flags2=r.uint();',
|
2022-06-28 18:57:22 +03:00
|
|
|
'return{',
|
|
|
|
"_:'test',",
|
|
|
|
'flags:flags,',
|
|
|
|
'flags2:flags2,',
|
|
|
|
'}',
|
|
|
|
].join('')}},`
|
|
|
|
)
|
|
|
|
})
|
2021-11-23 00:03:59 +03:00
|
|
|
})
|