FoundKey/test/mfm.ts

351 lines
10 KiB
TypeScript
Raw Normal View History

2018-10-15 21:37:21 +00:00
/*
* Tests of MFM
*/
2018-05-18 00:21:19 +00:00
import * as assert from 'assert';
2017-02-11 16:03:57 +00:00
import analyze from '../src/mfm/parse';
2018-09-16 17:45:30 +00:00
import toHtml from '../src/mfm/html';
import syntaxhighlighter from '../src/mfm/parse/core/syntax-highlighter';
2016-12-30 04:28:56 +00:00
describe('Text', () => {
2018-01-21 06:49:31 +00:00
it('can be analyzed', () => {
const tokens = analyze('@himawari @hima_sub@namori.net お腹ペコい :cat: #yryr');
2016-12-30 04:28:56 +00:00
assert.deepEqual([
{ type: 'mention', content: '@himawari', canonical: '@himawari', username: 'himawari', host: null },
{ type: 'text', content: ' '},
{ type: 'mention', content: '@hima_sub@namori.net', canonical: '@hima_sub@namori.net', username: 'hima_sub', host: 'namori.net' },
2016-12-30 04:28:56 +00:00
{ type: 'text', content: ' お腹ペコい ' },
2018-11-05 11:10:28 +00:00
{ type: 'emoji', content: ':cat:', name: 'cat'},
2017-03-01 03:15:45 +00:00
{ type: 'text', content: ' '},
2016-12-30 04:28:56 +00:00
{ type: 'hashtag', content: '#yryr', hashtag: 'yryr' }
], tokens);
});
2018-01-21 06:49:31 +00:00
it('can be inverted', () => {
const text = '@himawari @hima_sub@namori.net お腹ペコい :cat: #yryr';
2017-03-01 05:29:02 +00:00
assert.equal(analyze(text).map(x => x.content).join(''), text);
2017-02-11 16:01:35 +00:00
});
2017-03-01 05:29:02 +00:00
describe('elements', () => {
it('bold', () => {
const tokens = analyze('**Strawberry** Pasta');
assert.deepEqual([
{ type: 'bold', content: '**Strawberry**', bold: 'Strawberry' },
{ type: 'text', content: ' Pasta' }
], tokens);
});
2017-02-11 16:01:35 +00:00
2018-08-03 14:27:37 +00:00
it('big', () => {
const tokens = analyze('***Strawberry*** Pasta');
assert.deepEqual([
2018-08-05 15:31:09 +00:00
{ type: 'big', content: '***Strawberry***', big: 'Strawberry' },
2018-08-03 14:27:37 +00:00
{ type: 'text', content: ' Pasta' }
], tokens);
});
2018-08-05 03:33:51 +00:00
it('motion', () => {
const tokens1 = analyze('(((Strawberry))) Pasta');
2018-08-05 03:33:51 +00:00
assert.deepEqual([
2018-08-05 04:50:49 +00:00
{ type: 'motion', content: '(((Strawberry)))', motion: 'Strawberry' },
2018-08-05 03:33:51 +00:00
{ type: 'text', content: ' Pasta' }
], tokens1);
const tokens2 = analyze('<motion>Strawberry</motion> Pasta');
assert.deepEqual([
{ type: 'motion', content: '<motion>Strawberry</motion>', motion: 'Strawberry' },
{ type: 'text', content: ' Pasta' }
], tokens2);
2018-08-05 03:33:51 +00:00
});
2018-09-30 05:46:18 +00:00
describe('mention', () => {
it('local', () => {
const tokens = analyze('@himawari お腹ペコい');
assert.deepEqual([
{ type: 'mention', content: '@himawari', canonical: '@himawari', username: 'himawari', host: null },
2018-09-30 05:46:18 +00:00
{ type: 'text', content: ' お腹ペコい' }
], tokens);
});
2017-02-11 16:01:35 +00:00
2018-09-30 05:46:18 +00:00
it('remote', () => {
const tokens = analyze('@hima_sub@namori.net お腹ペコい');
assert.deepEqual([
{ type: 'mention', content: '@hima_sub@namori.net', canonical: '@hima_sub@namori.net', username: 'hima_sub', host: 'namori.net' },
{ type: 'text', content: ' お腹ペコい' }
], tokens);
});
it('remote punycode', () => {
const tokens = analyze('@hima_sub@xn--q9j5bya.xn--zckzah お腹ペコい');
assert.deepEqual([
{ type: 'mention', content: '@hima_sub@xn--q9j5bya.xn--zckzah', canonical: '@hima_sub@なもり.テスト', username: 'hima_sub', host: 'xn--q9j5bya.xn--zckzah' },
2018-09-30 05:46:18 +00:00
{ type: 'text', content: ' お腹ペコい' }
], tokens);
});
2018-09-30 05:46:18 +00:00
it('ignore', () => {
const tokens = analyze('idolm@ster');
assert.deepEqual([
{ type: 'text', content: 'idolm@ster' }
], tokens);
const tokens2 = analyze('@a\n@b\n@c');
assert.deepEqual([
{ type: 'mention', content: '@a', canonical: '@a', username: 'a', host: null },
2018-09-30 05:46:18 +00:00
{ type: 'text', content: '\n' },
{ type: 'mention', content: '@b', canonical: '@b', username: 'b', host: null },
2018-09-30 05:46:18 +00:00
{ type: 'text', content: '\n' },
{ type: 'mention', content: '@c', canonical: '@c', username: 'c', host: null }
2018-09-30 05:46:18 +00:00
], tokens2);
const tokens3 = analyze('**x**@a');
assert.deepEqual([
{ type: 'bold', content: '**x**', bold: 'x' },
{ type: 'mention', content: '@a', canonical: '@a', username: 'a', host: null }
2018-09-30 05:46:18 +00:00
], tokens3);
});
});
2017-03-01 05:29:02 +00:00
it('hashtag', () => {
2018-09-17 13:51:10 +00:00
const tokens1 = analyze('Strawberry Pasta #alice');
2017-03-01 05:29:02 +00:00
assert.deepEqual([
{ type: 'text', content: 'Strawberry Pasta ' },
{ type: 'hashtag', content: '#alice', hashtag: 'alice' }
2018-09-17 13:51:10 +00:00
], tokens1);
const tokens2 = analyze('Foo #bar, baz #piyo.');
assert.deepEqual([
{ type: 'text', content: 'Foo ' },
{ type: 'hashtag', content: '#bar', hashtag: 'bar' },
{ type: 'text', content: ', baz ' },
{ type: 'hashtag', content: '#piyo', hashtag: 'piyo' },
{ type: 'text', content: '.' }
], tokens2);
2018-10-20 00:03:04 +00:00
const tokens3 = analyze('#Foo!');
assert.deepEqual([
2018-10-20 00:14:16 +00:00
{ type: 'hashtag', content: '#Foo', hashtag: 'Foo' },
{ type: 'text', content: '!' },
2018-10-20 00:03:04 +00:00
], tokens3);
2017-03-01 05:29:02 +00:00
});
2017-02-11 16:01:35 +00:00
2018-09-19 21:27:41 +00:00
it('quote', () => {
2018-09-20 23:33:24 +00:00
const tokens1 = analyze('> foo\nbar\nbaz');
2018-09-19 21:27:41 +00:00
assert.deepEqual([
2018-09-20 23:33:24 +00:00
{ type: 'quote', content: '> foo\nbar\nbaz', quote: 'foo\nbar\nbaz' }
2018-09-19 21:27:41 +00:00
], tokens1);
2018-09-20 23:33:24 +00:00
const tokens2 = analyze('before\n> foo\nbar\nbaz\n\nafter');
2018-09-19 21:27:41 +00:00
assert.deepEqual([
{ type: 'text', content: 'before' },
2018-09-20 23:33:24 +00:00
{ type: 'quote', content: '\n> foo\nbar\nbaz\n\n', quote: 'foo\nbar\nbaz' },
2018-09-19 21:27:41 +00:00
{ type: 'text', content: 'after' }
], tokens2);
2018-09-20 23:33:24 +00:00
const tokens3 = analyze('piyo> foo\nbar\nbaz');
assert.deepEqual([
{ type: 'text', content: 'piyo> foo\nbar\nbaz' }
], tokens3);
const tokens4 = analyze('> foo\n> bar\n> baz');
assert.deepEqual([
{ type: 'quote', content: '> foo\n> bar\n> baz', quote: 'foo\nbar\nbaz' }
], tokens4);
2018-10-29 10:09:24 +00:00
const tokens5 = analyze('"\nfoo\nbar\nbaz\n"');
assert.deepEqual([
{ type: 'quote', content: '"\nfoo\nbar\nbaz\n"', quote: 'foo\nbar\nbaz' }
], tokens5);
2018-09-19 21:27:41 +00:00
});
describe('url', () => {
it('simple', () => {
const tokens = analyze('https://example.com');
assert.deepEqual([{
type: 'url',
content: 'https://example.com',
url: 'https://example.com'
}], tokens);
});
2018-11-16 12:30:01 +00:00
it('ignore trailing dot', () => {
const tokens = analyze('https://example.com.');
assert.deepEqual([{
type: 'url',
content: 'https://example.com',
url: 'https://example.com'
}, {
type: 'text', content: '.'
}], tokens);
});
2018-11-16 12:30:01 +00:00
it('with comma', () => {
const tokens = analyze('https://example.com/foo?bar=a,b');
assert.deepEqual([{
type: 'url',
content: 'https://example.com/foo?bar=a,b',
url: 'https://example.com/foo?bar=a,b'
}], tokens);
});
it('ignore trailing comma', () => {
const tokens = analyze('https://example.com/foo, bar');
assert.deepEqual([{
type: 'url',
content: 'https://example.com/foo',
url: 'https://example.com/foo'
}, {
type: 'text', content: ', bar'
}], tokens);
});
it('with brackets', () => {
const tokens = analyze('https://example.com/foo(bar)');
assert.deepEqual([{
type: 'url',
content: 'https://example.com/foo(bar)',
url: 'https://example.com/foo(bar)'
}], tokens);
});
it('ignore parent brackets', () => {
const tokens = analyze('(https://example.com/foo)');
assert.deepEqual([{
type: 'text', content: '('
}, {
type: 'url',
content: 'https://example.com/foo',
url: 'https://example.com/foo'
}, {
type: 'text', content: ')'
}], tokens);
});
2017-03-17 16:16:32 +00:00
});
it('link', () => {
const tokens = analyze('[ひまさく](https://himasaku.net)');
assert.deepEqual([{
type: 'link',
content: '[ひまさく](https://himasaku.net)',
title: 'ひまさく',
2017-03-17 16:29:21 +00:00
url: 'https://himasaku.net',
silent: false
2017-03-17 16:16:32 +00:00
}], tokens);
2017-03-01 05:29:02 +00:00
});
2017-03-01 03:15:45 +00:00
2017-03-01 05:29:02 +00:00
it('emoji', () => {
2018-11-03 13:35:24 +00:00
const tokens1 = analyze(':cat:');
2017-03-01 05:29:02 +00:00
assert.deepEqual([
2018-11-05 11:15:09 +00:00
{ type: 'emoji', content: ':cat:', name: 'cat' }
2018-11-03 13:35:24 +00:00
], tokens1);
const tokens2 = analyze(':cat::cat::cat:');
assert.deepEqual([
2018-11-05 11:15:09 +00:00
{ type: 'emoji', content: ':cat:', name: 'cat' },
{ type: 'emoji', content: ':cat:', name: 'cat' },
{ type: 'emoji', content: ':cat:', name: 'cat' }
2018-11-03 13:35:24 +00:00
], tokens2);
2018-11-05 11:14:49 +00:00
const tokens3 = analyze('🍎');
assert.deepEqual([
2018-11-05 11:40:39 +00:00
{ type: 'emoji', content: '🍎', emoji: '🍎' }
2018-11-05 11:14:49 +00:00
], tokens3);
2017-03-01 05:29:02 +00:00
});
2017-02-11 16:01:35 +00:00
2017-03-01 05:29:02 +00:00
it('block code', () => {
const tokens = analyze('```\nvar x = "Strawberry Pasta";\n```');
assert.equal(tokens[0].type, 'code');
assert.equal(tokens[0].content, '```\nvar x = "Strawberry Pasta";\n```');
});
it('inline code', () => {
const tokens = analyze('`var x = "Strawberry Pasta";`');
assert.equal(tokens[0].type, 'inline-code');
assert.equal(tokens[0].content, '`var x = "Strawberry Pasta";`');
});
2018-06-23 10:31:28 +00:00
2018-11-16 08:03:52 +00:00
it('math', () => {
const fomula = 'x = {-b \\pm \\sqrt{b^2-4ac} \\over 2a}.';
const text = `$${fomula}$`;
const tokens = analyze(text);
assert.deepEqual([
{ type: 'math', content: text, formula: fomula }
], tokens);
});
2018-06-23 10:31:28 +00:00
it('search', () => {
const tokens1 = analyze('a b c 検索');
assert.deepEqual([
2018-11-07 10:58:05 +00:00
{ type: 'search', content: 'a b c 検索', query: 'a b c' }
2018-06-23 10:31:28 +00:00
], tokens1);
const tokens2 = analyze('a b c Search');
assert.deepEqual([
2018-11-07 10:58:05 +00:00
{ type: 'search', content: 'a b c Search', query: 'a b c' }
2018-06-23 10:31:28 +00:00
], tokens2);
const tokens3 = analyze('a b c search');
assert.deepEqual([
2018-11-07 10:58:05 +00:00
{ type: 'search', content: 'a b c search', query: 'a b c' }
2018-06-23 10:31:28 +00:00
], tokens3);
const tokens4 = analyze('a b c SEARCH');
assert.deepEqual([
2018-11-07 10:58:05 +00:00
{ type: 'search', content: 'a b c SEARCH', query: 'a b c' }
2018-06-23 10:31:28 +00:00
], tokens4);
});
2018-06-26 09:42:00 +00:00
it('title', () => {
const tokens1 = analyze('【yee】\nhaw');
assert.deepEqual(
2018-11-07 10:58:05 +00:00
{ type: 'title', content: '【yee】\n', title: 'yee' }
2018-06-26 09:42:00 +00:00
, tokens1[0]);
const tokens2 = analyze('[yee]\nhaw');
assert.deepEqual(
2018-11-07 10:58:05 +00:00
{ type: 'title', content: '[yee]\n', title: 'yee' }
2018-06-26 09:42:00 +00:00
, tokens2[0]);
2018-11-03 06:28:00 +00:00
const tokens3 = analyze('a [a]\nb [b]\nc [c]');
assert.deepEqual(
{ type: 'text', content: 'a [a]\nb [b]\nc [c]' }
2018-11-03 06:28:11 +00:00
, tokens3[0]);
2018-11-07 10:58:05 +00:00
const tokens4 = analyze('foo\n【bar】\nbuzz');
assert.deepEqual([
2018-11-07 12:30:28 +00:00
{ type: 'text', content: 'foo' },
{ type: 'title', content: '\n【bar】\n', title: 'bar' },
{ type: 'text', content: 'buzz' },
2018-11-07 10:58:05 +00:00
], tokens4);
2018-06-26 09:42:00 +00:00
});
2017-02-11 16:01:35 +00:00
});
2017-02-28 12:00:59 +00:00
describe('syntax highlighting', () => {
2017-03-18 11:05:11 +00:00
it('comment', () => {
const html1 = syntaxhighlighter('// Strawberry pasta');
assert.equal(html1, '<span class="comment">// Strawberry pasta</span>');
const html2 = syntaxhighlighter('x // x\ny // y');
assert.equal(html2, 'x <span class="comment">// x\n</span>y <span class="comment">// y</span>');
});
2017-02-28 12:00:59 +00:00
it('regexp', () => {
const html = syntaxhighlighter('/.*/');
assert.equal(html, '<span class="regexp">/.*/</span>');
});
it('slash', () => {
const html = syntaxhighlighter('/');
assert.equal(html, '<span class="symbol">/</span>');
});
});
2018-09-16 17:45:30 +00:00
describe('toHtml', () => {
it('br', () => {
const input = 'foo\nbar\nbaz';
const output = '<p>foo<br>bar<br>baz</p>';
assert.equal(toHtml(analyze(input)), output);
});
});
2016-12-30 04:28:56 +00:00
});