ec736f8590
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
109 lines
3.5 KiB
TypeScript
109 lines
3.5 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 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(),',
|
|
'}'
|
|
)
|
|
})
|
|
})
|