aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorache <ache@ache.one>2018-05-17 07:30:03 +0200
committerache <ache@ache.one>2018-05-17 07:30:03 +0200
commitb3e147c1b1997295ee816fab078a83288450ef81 (patch)
tree6910703d5983762bdcae2b3d42ce0e3453e5e584
parentAdd a LICENSE (diff)
Add tests with ava
-rw-r--r--__tests__/index.js101
-rw-r--r--index.js8
-rw-r--r--package.json11
3 files changed, 116 insertions, 4 deletions
diff --git a/__tests__/index.js b/__tests__/index.js
new file mode 100644
index 0000000..6a09cf9
--- /dev/null
+++ b/__tests__/index.js
@@ -0,0 +1,101 @@
+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 render = text => unified()
+ .use(reParse)
+ .use(plugin)
+ .use(remark2rehype)
+ .use(stringify)
+ .processSync(text);
+
+const renderRaw = text => unified()
+ .use(reParse)
+ .use(plugin)
+ .use(remark2rehype, {allowDangerousHTML: true})
+ .use(raw)
+ .use(stringify)
+ .processSync(text);
+
+const mainTestString = `Inline *test*{style="em:4"} paragraphe. 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', 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));
+});
+
+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));
+});
+
+test('em', async t => {
+ const {contents} = render('textexampleno interest **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);
+});
+
diff --git a/index.js b/index.js
index 57ff60c..71a4ed8 100644
--- a/index.js
+++ b/index.js
@@ -26,6 +26,8 @@ const DOMEventHandler = [
'onsubmit', 'onsuspend', 'ontimeupdate',
'ontoggle', 'onvolumechange', 'onwaiting',
];
+
+/* Table convertion between type and HTML tagName */
const convTypeTag = {
image: 'img',
link: 'a',
@@ -35,11 +37,12 @@ const convTypeTag = {
delete: 's',
inlineCode: 'code',
};
+
/* TODO :
- * - [ ] fencedCode // require('./tokenize/code-fenced'),
+ * - [~] fencedCode // require('./tokenize/code-fenced'),
- [x] atxHeading //require('./tokenize/heading-atx'),
- [ ] setextHeading //require('./tokenize/heading-setext'),
- - [ ] table //require('./tokenize/table'),
+ - [~] table //require('./tokenize/table'),
- [x] link //require('./tokenize/link'),
- [x] strong //require('./tokenize/strong'),
- [x] emphasis //require('./tokenize/emphasis'),
@@ -146,7 +149,6 @@ function remarkAttr(userConfig) {
const oldCodeInline = tokenizers.code;
const oldAtxHeader = tokenizersBlock.atxHeading;
- console.log(tokenizeGenerator('', oldLink, config));
const linkTokenize = tokenizeGenerator('', oldLink, config);
linkTokenize.locator = tokenizers.link.locator;
const strongTokenize = tokenizeGenerator('', oldStrong, config);
diff --git a/package.json b/package.json
index e3c1e2a..73a2c7e 100644
--- a/package.json
+++ b/package.json
@@ -4,7 +4,8 @@
"description": "Add support of custom attributes to Markdown syntax.",
"main": "index.js",
"scripts": {
- "test": "echo \"Error: no test specified\" && exit 1"
+ "pretest": "xo",
+ "test": "ava"
},
"repository": {
"type": "git",
@@ -22,6 +23,14 @@
},
"homepage": "https://github.com/arobase-che/remark-attr#readme",
"devDependencies": {
+ "ava": "^0.25.0",
+ "parse5": "^4.0.0",
+ "rehype-raw": "^2.0.0",
+ "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"
},
"xo": {