forked from gram-js/gramjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request gram-js#15 from watzon/master
Add HTMLParser and more tests
- Loading branch information
Showing
15 changed files
with
2,077 additions
and
452 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
/gramjs/tl/AllTLObjects.js | ||
/gramjs/errors/RPCErrorList.js | ||
/dist/ | ||
/coverage/ | ||
|
||
# User session | ||
*.session | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
__tests__/factorizator.spec.js → __tests__/crypto/factorizator.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
__tests__/readBuffer.spec.js → __tests__/crypto/readBuffer.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
const { HTMLParser } = require('../../gramjs/extensions/HTML') | ||
const types = require('../../gramjs/tl/types') | ||
|
||
describe('HTMLParser', () => { | ||
test('it should construct a new HTMLParser', () => { | ||
const parser = new HTMLParser('Hello world') | ||
expect(parser.text).toEqual('') | ||
expect(parser.entities).toEqual([]) | ||
}) | ||
|
||
describe('.parse', () => { | ||
test('it should parse bold entities', () => { | ||
const parser = new HTMLParser('Hello <strong>world</strong>') | ||
const [text, entities] = parser.parse() | ||
expect(text).toEqual('Hello world') | ||
expect(entities.length).toEqual(1) | ||
expect(entities[0]).toBeInstanceOf(types.MessageEntityBold) | ||
}) | ||
|
||
test('it should parse italic entities', () => { | ||
const parser = new HTMLParser('Hello <em>world</em>') | ||
const [text, entities] = parser.parse() | ||
expect(text).toEqual('Hello world') | ||
expect(entities.length).toEqual(1) | ||
expect(entities[0]).toBeInstanceOf(types.MessageEntityItalic) | ||
}) | ||
|
||
test('it should parse code entities', () => { | ||
const parser = new HTMLParser('Hello <code>world</code>') | ||
const [text, entities] = parser.parse() | ||
expect(text).toEqual('Hello world') | ||
expect(entities.length).toEqual(1) | ||
expect(entities[0]).toBeInstanceOf(types.MessageEntityCode) | ||
}) | ||
|
||
test('it should parse pre entities', () => { | ||
const parser = new HTMLParser('Hello <pre>world</pre>') | ||
const [text, entities] = parser.parse() | ||
expect(text).toEqual('Hello world') | ||
expect(entities.length).toEqual(1) | ||
expect(entities[0]).toBeInstanceOf(types.MessageEntityPre) | ||
}) | ||
|
||
test('it should parse strike entities', () => { | ||
const parser = new HTMLParser('Hello <del>world</del>') | ||
const [text, entities] = parser.parse() | ||
expect(text).toEqual('Hello world') | ||
expect(entities.length).toEqual(1) | ||
expect(entities[0]).toBeInstanceOf(types.MessageEntityStrike) | ||
}) | ||
|
||
test('it should parse link entities', () => { | ||
const parser = new HTMLParser('Hello <a href="https://hello.world">world</a>') | ||
const [text, entities] = parser.parse() | ||
expect(text).toEqual('Hello world') | ||
expect(entities.length).toEqual(1) | ||
expect(entities[0]).toBeInstanceOf(types.MessageEntityTextUrl) | ||
expect(entities[0].url).toEqual('https://hello.world') | ||
}) | ||
|
||
test('it should parse nested entities', () => { | ||
const parser = new HTMLParser('Hello <strong><em>world</em></strong>') | ||
const [text, entities] = parser.parse() | ||
expect(text).toEqual('Hello world') | ||
expect(entities.length).toEqual(2) | ||
expect(entities[0]).toBeInstanceOf(types.MessageEntityItalic) | ||
expect(entities[1]).toBeInstanceOf(types.MessageEntityBold) | ||
}) | ||
|
||
test('it should parse multiple entities', () => { | ||
const parser = new HTMLParser('<em>Hello</em> <strong>world</strong>') | ||
const [text, entities] = parser.parse() | ||
expect(text).toEqual('Hello world') | ||
expect(entities.length).toEqual(2) | ||
expect(entities[0]).toBeInstanceOf(types.MessageEntityItalic) | ||
expect(entities[1]).toBeInstanceOf(types.MessageEntityBold) | ||
}) | ||
}) | ||
|
||
describe('.unparse', () => { | ||
test('it should create a markdown string from raw text and entities', () => { | ||
const unparsed = '<strong>hello</strong> <em>hello</em> <del>hello</del> <code>hello</code> <pre>hello</pre> <a href="https://hello.world">hello</a>' | ||
const strippedText = 'hello hello hello hello hello hello' | ||
const rawEntities = [ | ||
new types.MessageEntityBold({ offset: 0, length: 5 }), | ||
new types.MessageEntityItalic({ offset: 6, length: 5 }), | ||
new types.MessageEntityStrike({ offset: 12, length: 5 }), | ||
new types.MessageEntityCode({ offset: 18, length: 5 }), | ||
new types.MessageEntityPre({ offset: 24, length: 5 }), | ||
new types.MessageEntityTextUrl({ offset: 30, length: 5, url: 'https://hello.world' }), | ||
] | ||
const text = HTMLParser.unparse(strippedText, rawEntities) | ||
expect(text).toEqual(unparsed) | ||
}) | ||
|
||
test('it should unparse nested entities', () => { | ||
const unparsed = '<strong><em>Hello world</em></strong>' | ||
const strippedText = 'Hello world' | ||
const rawEntities = [ | ||
new types.MessageEntityBold({ offset: 0, length: 11 }), | ||
new types.MessageEntityItalic({ offset: 0, length: 11 }), | ||
] | ||
const text = HTMLParser.unparse(strippedText, rawEntities) | ||
expect(text).toEqual(unparsed) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
const { MarkdownParser } = require('../../gramjs/extensions/Markdown') | ||
const types = require('../../gramjs/tl/types') | ||
|
||
describe('MarkdownParser', () => { | ||
test('it should construct a new MarkdownParser', () => { | ||
const parser = new MarkdownParser('Hello world') | ||
expect(parser.text).toEqual('') | ||
expect(parser.entities).toEqual([]) | ||
}) | ||
|
||
describe('.parse', () => { | ||
test('it should parse bold entities', () => { | ||
const parser = new MarkdownParser('Hello **world**') | ||
const [text, entities] = parser.parse() | ||
expect(text).toEqual('Hello world') | ||
expect(entities.length).toEqual(1) | ||
expect(entities[0]).toBeInstanceOf(types.MessageEntityBold) | ||
}) | ||
|
||
test('it should parse italic entities', () => { | ||
const parser = new MarkdownParser('Hello __world__') | ||
const [text, entities] = parser.parse() | ||
expect(text).toEqual('Hello world') | ||
expect(entities.length).toEqual(1) | ||
expect(entities[0]).toBeInstanceOf(types.MessageEntityItalic) | ||
}) | ||
|
||
test('it should parse code entities', () => { | ||
const parser = new MarkdownParser('Hello `world`') | ||
const [text, entities] = parser.parse() | ||
expect(text).toEqual('Hello world') | ||
expect(entities.length).toEqual(1) | ||
expect(entities[0]).toBeInstanceOf(types.MessageEntityCode) | ||
}) | ||
|
||
test('it should parse pre entities', () => { | ||
const parser = new MarkdownParser('Hello ```world```') | ||
const [text, entities] = parser.parse() | ||
expect(text).toEqual('Hello world') | ||
expect(entities.length).toEqual(1) | ||
expect(entities[0]).toBeInstanceOf(types.MessageEntityPre) | ||
}) | ||
|
||
test('it should parse strike entities', () => { | ||
const parser = new MarkdownParser('Hello ~~world~~') | ||
const [text, entities] = parser.parse() | ||
expect(text).toEqual('Hello world') | ||
expect(entities.length).toEqual(1) | ||
expect(entities[0]).toBeInstanceOf(types.MessageEntityStrike) | ||
}) | ||
|
||
test('it should parse link entities', () => { | ||
const parser = new MarkdownParser('Hello [world](https://hello.world)') | ||
const [text, entities] = parser.parse() | ||
expect(text).toEqual('Hello world') | ||
expect(entities.length).toEqual(1) | ||
expect(entities[0]).toBeInstanceOf(types.MessageEntityTextUrl) | ||
expect(entities[0].url).toEqual('https://hello.world') | ||
}) | ||
|
||
test('it should not parse nested entities', () => { | ||
const parser = new MarkdownParser('Hello **__world__**') | ||
const [text, entities] = parser.parse() | ||
expect(text).toEqual('Hello __world__') | ||
expect(entities.length).toEqual(1) | ||
expect(entities[0]).toBeInstanceOf(types.MessageEntityBold) | ||
}) | ||
|
||
test('it should parse multiple entities', () => { | ||
const parser = new MarkdownParser('__Hello__ **world**') | ||
const [text, entities] = parser.parse() | ||
expect(text).toEqual('Hello world') | ||
expect(entities.length).toEqual(2) | ||
expect(entities[0]).toBeInstanceOf(types.MessageEntityItalic) | ||
expect(entities[1]).toBeInstanceOf(types.MessageEntityBold) | ||
}) | ||
}) | ||
|
||
describe('.unparse', () => { | ||
test('it should create a markdown string from raw text and entities', () => { | ||
const unparsed = '**hello** __hello__ ~~hello~~ `hello` ```hello``` [hello](https://hello.world)' | ||
const strippedText = 'hello hello hello hello hello hello' | ||
const rawEntities = [ | ||
new types.MessageEntityBold({ offset: 0, length: 5 }), | ||
new types.MessageEntityItalic({ offset: 6, length: 5 }), | ||
new types.MessageEntityStrike({ offset: 12, length: 5 }), | ||
new types.MessageEntityCode({ offset: 18, length: 5 }), | ||
new types.MessageEntityPre({ offset: 24, length: 5 }), | ||
new types.MessageEntityTextUrl({ offset: 30, length: 5, url: 'https://hello.world' }), | ||
] | ||
const text = MarkdownParser.unparse(strippedText, rawEntities) | ||
expect(text).toEqual(unparsed) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
const Scanner = require('../../gramjs/extensions/Scanner') | ||
|
||
const helloScanner = new Scanner('Hello world') | ||
|
||
describe('Scanner', () => { | ||
beforeEach(() => helloScanner.reset()) | ||
|
||
test('it should construct a new Scanner', () => { | ||
expect(helloScanner.str).toEqual('Hello world') | ||
expect(helloScanner.pos).toEqual(0) | ||
expect(helloScanner.lastMatch).toBeNull() | ||
}) | ||
|
||
describe('.chr', () => { | ||
test('it should return the character at the current pos', () => { | ||
expect(helloScanner.chr).toEqual('H') | ||
}) | ||
}) | ||
|
||
describe('.peek', () => { | ||
test('it should return the character at the current pos', () => { | ||
expect(helloScanner.peek()).toEqual('H') | ||
}) | ||
|
||
test('it should return the next n characters', () => { | ||
expect(helloScanner.peek(3)).toEqual('Hel') | ||
expect(helloScanner.peek(5)).toEqual('Hello') | ||
}) | ||
}) | ||
|
||
describe('.consume', () => { | ||
test('it should consume the current character', () => { | ||
const char = helloScanner.consume() | ||
expect(char).toEqual('H') | ||
expect(helloScanner.pos).toEqual(1) | ||
}) | ||
|
||
test('it should consume the next n characters', () => { | ||
const chars = helloScanner.consume(5) | ||
expect(chars).toEqual('Hello') | ||
expect(helloScanner.pos).toEqual(5) | ||
}) | ||
}) | ||
|
||
describe('.reverse', () => { | ||
test('it should set pos back n characters', () => { | ||
helloScanner.consume(5) | ||
helloScanner.reverse(5) | ||
expect(helloScanner.pos).toEqual(0) | ||
}) | ||
|
||
test('it should not go back further than 0', () => { | ||
helloScanner.reverse(10) | ||
expect(helloScanner.pos).toEqual(0) | ||
}) | ||
}) | ||
|
||
describe('.scanUntil', () => { | ||
test('it should scan the string for a regular expression starting at the current pos', () => { | ||
helloScanner.scanUntil(/w/) | ||
expect(helloScanner.pos).toEqual(6) | ||
}) | ||
|
||
test('it should do nothing if the pattern is not found', () => { | ||
helloScanner.scanUntil(/G/) | ||
expect(helloScanner.pos).toEqual(0) | ||
}) | ||
}) | ||
|
||
describe('.rest', () => { | ||
test('it should return the unconsumed input', () => { | ||
helloScanner.consume(6) | ||
expect(helloScanner.rest).toEqual('world') | ||
}) | ||
}) | ||
|
||
describe('.reset', () => { | ||
test('it should reset the pos to 0', () => { | ||
helloScanner.consume(5) | ||
helloScanner.reset() | ||
expect(helloScanner.pos).toEqual(0) | ||
}) | ||
}) | ||
|
||
describe('.eof', () => { | ||
test('it should return true if the scanner has reached the end of the input', () => { | ||
expect(helloScanner.eof()).toBe(false) | ||
helloScanner.consume(11) | ||
expect(helloScanner.eof()).toBe(true) | ||
}) | ||
}) | ||
|
||
describe('.bof', () => { | ||
test('it should return true if pos is 0', () => { | ||
expect(helloScanner.bof()).toBe(true) | ||
helloScanner.consume(11) | ||
expect(helloScanner.bof()).toBe(false) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.