From b3e147c1b1997295ee816fab078a83288450ef81 Mon Sep 17 00:00:00 2001 From: ache Date: Thu, 17 May 2018 07:30:03 +0200 Subject: Add tests with ava --- __tests__/index.js | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++++ index.js | 8 +++-- package.json | 11 +++++- 3 files changed, 116 insertions(+), 4 deletions(-) create mode 100644 __tests__/index.js 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": { -- cgit v1.2.3