aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorache <ache@ache.one>2018-05-27 01:51:44 +0200
committerache <ache@ache.one>2018-05-27 01:51:44 +0200
commit6008380353b97c96d8cbd4d2026f1d1bd3a1ca09 (patch)
tree1a1e63e88cfe2af3b6b1b1a95a1f05b2b6dcec78
parentAdd more comments (diff)
Test revision with parse5
-rw-r--r--__tests__/index.js257
-rw-r--r--package.json1
2 files changed, 56 insertions, 202 deletions
diff --git a/__tests__/index.js b/__tests__/index.js
index 45f1315..239e1aa 100644
--- a/__tests__/index.js
+++ b/__tests__/index.js
@@ -1,18 +1,17 @@
+'use strict';
+
+import unified from 'unified';
import {readFileSync as file} from 'fs';
import {join} from 'path';
-import unified from 'unified';
import test from 'ava';
import raw from 'rehype-raw';
import reParse from 'remark-parse';
import stringify from 'rehype-stringify';
import remark2rehype from 'remark-rehype';
import parse5 from 'parse5';
-import stream from 'stream';
import plugin from '..';
-const Stream = stream.Readable;
-
const renderDefault = text => unified()
.use(reParse)
.use(plugin)
@@ -35,6 +34,15 @@ const renderRaw = text => unified()
.use(stringify)
.processSync(text);
+const generateExtendParser = extendsOptions => text => unified()
+ .use(reParse)
+ .use(plugin, extendsOptions)
+ .use(remark2rehype)
+ .use(stringify)
+ .processSync(text);
+
+const parse = x => parse5.parse(x);
+
/*
* TODO :
* - Invalid scope
@@ -44,231 +52,78 @@ const renderRaw = text => unified()
const mainTestString = `Inline *test*{style="em:4"} paragraph. Use **multiple**{ style="color:pink"} inline ~~block~~ tag. Line \`tagCode\`{ style="color:yellow"}.`;
-function string2stream(string) {
- const stream = new Stream();
- stream.push(string);
- stream.push(null);
- return stream;
-}
-
-function propEgal(prop, attrs) {
- if (Object.getOwnPropertyNames(prop).length !== attrs.length) {
- return false;
- }
-
- attrs.forEach(e => {
- if (prop[e.name] !== e.value) {
- return false;
- }
- });
-
- return true;
-}
-function every(obj, fct) {
- Object.getOwnPropertyNames(obj).forEach(name => {
- if (!fct(obj[name])) {
- return false;
- }
- });
- return true;
-}
-
test('basic-default', t => {
const {contents} = renderDefault(mainTestString);
- const parser = new parse5.SAXParser();
-
- const nbTag = {em: 1, s: 1, code: 1, strong: 1, errorTag: 0};
- parser.on('startTag', name => {
- if (name in nbTag) {
- nbTag[name] -= 1;
- }
- });
- string2stream(contents).pipe(parser);
- t.true(every(nbTag, x => x === 0));
+ t.deepEqual(parse(contents), parse('<p>Inline <em style="em:4">test</em> paragraph. Use <strong style="color:pink">multiple</strong> inline <del>block</del> tag. Line <code style="color:yellow">tagCode</code>.</p>'));
});
test('basic', t => {
const {contents} = render(mainTestString);
- const parser = new parse5.SAXParser();
-
- const nbTag = {em: 1, s: 1, code: 1, strong: 1, errorTag: 0};
- parser.on('startTag', name => {
- if (name in nbTag) {
- nbTag[name] -= 1;
- }
- });
- string2stream(contents).pipe(parser);
- t.true(every(nbTag, x => x === 0));
+ t.deepEqual(parse(contents), parse(`
+<p>Inline <em style="em:4">test</em> paragraph. Use <strong style="color:pink">multiple</strong> inline <del>block</del> tag. Line <code style="color:yellow">tagCode</code>.</p>`));
});
test('basic-raw', t => {
const {contents} = renderRaw(mainTestString);
- const parser = new parse5.SAXParser();
-
- const nbTag = {em: 1, s: 1, code: 1, strong: 1, errorTag: 0};
- parser.on('startTag', name => {
- if (name in nbTag) {
- nbTag[name] -= 1;
- }
- });
- string2stream(contents).pipe(parser);
- t.true(every(nbTag, x => x === 0));
+ t.deepEqual(parse(contents), parse(`
+<p>Inline <em style="em:4">test</em> paragraph. Use <strong style="color:pink">multiple</strong> inline <del>block</del> tag. Line <code style="color:yellow">tagCode</code>.</p>`));
});
-test('em', async t => {
+test('em', t => {
const {contents} = render('textexamplenointerest **Important**{style=4em} still no interest');
- const parser = new parse5.SAXParser();
-
- parser.on('startTag', (name, attrs) => {
- if (name === 'strong') {
- t.true(propEgal({style: '4em'}, attrs));
- }
- });
-
- await string2stream(contents).pipe(parser);
+ t.deepEqual(parse(contents), parse(`<p>textexamplenointerest <strong style="4em">Important</strong> still no interest</p>`));
});
-test('readme-default', async t => {
+test('readme-default', t => {
const fileExample = file(join(__dirname, 'readMeTest.txt'));
const {contents} = renderDefault(fileExample);
- const parser = new parse5.SAXParser();
-
- parser.on('startTag', (name, attrs) => {
- switch (name) {
- case 'img':
- t.true(propEgal({height: 50, alt: 'alt', src: 'img'}, attrs));
- break;
- case 'a':
- t.true(propEgal({ref: 'external', src: 'https://rms.sexy'}, attrs));
- break;
- case 'h3':
- t.true(propEgal({style: 'color:red;'}, attrs));
- break;
- case 'em':
- t.true(propEgal({style: 'color:yellow;'}, attrs));
- break;
- case 'strong':
- t.true(propEgal({}, attrs));
- break;
- case 'del':
- t.true(propEgal({style: 'color: grey;'}, attrs));
- break;
- case 'code':
- t.true(propEgal({}, attrs));
- break;
- default:
- }
- });
-
- await string2stream(contents).pipe(parser);
+ t.deepEqual(parse(contents), parse(`
+<p><img src="img" alt="alt" height="50"></p>
+<p><a href="https://rms.sexy" rel="external">Hot babe with computer</a></p>
+<h3 style="color:red;">This is a title</h3>
+<p>Npm stand for <em style="color:yellow;">node</em> packet manager.</p>
+<p>This is a <strong>Unicorn</strong> !</p>
+<p>Your problem is <del style="color: grey;">at line 18</del>. My mistake, it's at line 14.</p>
+<p>You can use the <code>fprintf</code> function to format the output to a file.</p>`));
});
-test('readme', async t => {
+test('readme', t => {
const fileExample = file(join(__dirname, 'readMeTest.txt'));
const {contents} = render(fileExample);
- const parser = new parse5.SAXParser();
-
- parser.on('startTag', (name, attrs) => {
- switch (name) {
- case 'img':
- t.true(propEgal({height: 50, alt: 'alt', src: 'img'}, attrs));
- break;
- case 'a':
- t.true(propEgal({ref: 'external', src: 'https://rms.sexy'}, attrs));
- break;
- case 'h3':
- t.true(propEgal({style: 'color:red;'}, attrs));
- break;
- case 'em':
- t.true(propEgal({style: 'color:yellow;'}, attrs));
- break;
- case 'strong':
- t.true(propEgal({awesome: ''}, attrs));
- break;
- case 'del':
- t.true(propEgal({style: 'color: gray;'}, attrs));
- break;
- case 'code':
- t.true(propEgal({lang: 'c'}, attrs));
- break;
- default:
- }
- });
-
- await string2stream(contents).pipe(parser);
+ t.deepEqual(parse(contents), parse(`
+<p><img src="img" alt="alt" height="50"></p>
+<p><a href="https://rms.sexy" rel="external">Hot babe with computer</a></p>
+<h3 style="color:red;">This is a title</h3>
+<p>Npm stand for <em style="color:yellow;">node</em> packet manager.</p>
+<p>This is a <strong awesome="">Unicorn</strong> !</p>
+<p>Your problem is <del style="color: grey;">at line 18</del>. My mistake, it's at line 14.</p>
+<p>You can use the <code language="c">fprintf</code> function to format the output to a file.</p>`));
});
-test('extended', async t => {
- const renderExtended = text => unified()
- .use(reParse)
- .use(plugin, {extend: {image: ['quality']}})
- .use(remark2rehype)
- .use(stringify)
- .process(text, (err, file) => {
- const parser = new parse5.SAXParser();
-
- t.true(!err);
-
- parser.on('startTag', (name, attrs) => {
- if (name === 'img') {
- t.true(propEgal({alt: 'Awesome image', src: 'aws://image.jpg', quality: '80'}, attrs));
- }
- });
- string2stream(String(file)).pipe(parser);
- });
-
- await renderExtended(`
-*Wait* !
+test('extended', t => {
+ const renderExtended = generateExtendParser({extends: {image: ['quality']}});
+ const extentedString = `*Wait* !
This is an awesome image : ![Awesome image](aws://image.jpg){ quality="80" awesomeness="max" }
-`);
+`;
+ const {contents} = renderExtended(extentedString);
+ t.deepEqual(parse(contents), parse(`<p><em>Wait</em> !
+This is an awesome image : <img src="aws://image.jpg" alt="Awesome image"></p>`));
});
-test('extended Dangerous', async t => {
- const renderExtended = text => unified()
- .use(reParse)
- .use(plugin, {extend: {image: ['quality', 'onload']}})
- .use(remark2rehype)
- .use(stringify)
- .process(text, (err, file) => {
- const parser = new parse5.SAXParser();
-
- t.true(!err);
-
- parser.on('startTag', (name, attrs) => {
- if (name === 'img') {
- t.true(propEgal({alt: 'Awesome image', src: 'aws://image.jpg', quality: '80', onload: 'launchAwesomeFunction();'}, attrs));
- }
- });
- string2stream(String(file)).pipe(parser);
- });
-
- await renderExtended(`
-*Wait* !
+test('extended Dangerous', t => {
+ const renderExtended = generateExtendParser({extend: {image: ['quality', 'onload']}});
+ const dangerousString = `*Wait* !
This is an awesome image : ![Awesome image](aws://image.jpg){ quality="80" awesomeness="max" onload="launchAwesomeFunction();" }
-`);
+`;
+ const {contents} = renderExtended(dangerousString);
+ t.deepEqual(parse(contents), parse(`<p><em>Wait</em> !
+This is an awesome image : <img src="aws://image.jpg" alt="Awesome image" quality="80" onload="launchAwesomeFunction();"></p>`));
});
-test('extended-global', async t => {
- const renderExtended = text => unified()
- .use(reParse)
- .use(plugin, {extend: {'*': ['exAttr']}})
- .use(remark2rehype)
- .use(stringify)
- .process(text, (err, file) => {
- const parser = new parse5.SAXParser();
-
- t.true(!err);
-
- parser.on('startTag', (name, attrs) => {
- if (name === 'strong') {
- t.true(propEgal({exAttr: 'true'}, attrs));
- }
- });
- string2stream(String(file)).pipe(parser);
- });
-
- await renderExtended(`
-*Wait* ! You are **beautiful**{ exAttr="true" } !
-`);
+test('extended-global', t => {
+ const renderExtended = generateExtendParser({extend: {'*': ['exAttr']}});
+ const globalString = ` *Wait* ! You are **beautiful**{ exAttr="true" } !`;
+ const {contents} = renderExtended(globalString);
+ t.deepEqual(parse(contents), parse(`<p> <em>Wait</em> ! You are <strong ex-attr="true">beautiful</strong> !</p>`));
});
diff --git a/package.json b/package.json
index 0d7d4e0..8343994 100644
--- a/package.json
+++ b/package.json
@@ -29,7 +29,6 @@
"rehype-stringify": "^3.0.0",
"remark-parse": "^5.0.0",
"remark-rehype": "^3.0.0",
- "stream": "0.0.2",
"unified": "^7.0.0",
"xo": "^0.21.0"
},