Skip to content

Commit 5d6bde0

Browse files
authored
Merge pull request #1444 from UziTech/normalize-tests
Normalize tests
2 parents ca8fb56 + 4760772 commit 5d6bde0

36 files changed

+835
-1305
lines changed

package-lock.json

+258-314
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,17 @@
3030
],
3131
"devDependencies": {
3232
"commonmark": "0.x",
33-
"eslint": "^5.12.0",
33+
"eslint": "^5.15.1",
3434
"eslint-config-standard": "^12.0.0",
35-
"eslint-plugin-import": "^2.14.0",
35+
"eslint-plugin-import": "^2.16.0",
3636
"eslint-plugin-node": "^8.0.1",
3737
"eslint-plugin-promise": "^4.0.1",
3838
"eslint-plugin-standard": "^4.0.0",
3939
"eslint-plugin-vuln-regex-detector": "^1.0.4",
4040
"front-matter": "^3.0.1",
4141
"glob-to-regexp": "^0.4.0",
42-
"@markedjs/html-differ": "^2.0.0",
42+
"@markedjs/html-differ": "^2.0.1",
4343
"jasmine": "^3.3.1",
44-
"jasmine2-custom-message": "^0.9.3",
4544
"markdown": "0.x",
4645
"markdown-it": "8.x",
4746
"uglify-js": "^3.4.9"
@@ -56,6 +55,7 @@
5655
"test:old": "node test",
5756
"test:lint": "eslint bin/marked .",
5857
"test:redos": "eslint --plugin vuln-regex-detector --rule '\"vuln-regex-detector/no-vuln-regex\": 2' lib/marked.js",
58+
"test:node4": "npx node@4 ./node_modules/jasmine/bin/jasmine.js --config=jasmine.json",
5959
"bench": "node test --bench",
6060
"lint": "eslint --fix bin/marked .",
6161
"build": "uglifyjs lib/marked.js -cm --comments /Copyright/ -o marked.min.js",

test/helpers/helpers.js

+41-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,44 @@
1-
var marked = require('../../lib/marked.js');
1+
const marked = require('../../');
2+
const HtmlDiffer = require('@markedjs/html-differ').HtmlDiffer;
3+
const htmlDiffer = new HtmlDiffer({ignoreSelfClosingSlash: true});
24

3-
beforeEach(function () {
5+
const EXCERPT_LENGTH = 30;
6+
7+
beforeEach(() => {
48
marked.setOptions(marked.getDefaults());
9+
10+
jasmine.addMatchers({
11+
toRender: () => {
12+
return {
13+
compare: (spec, expected) => {
14+
const result = {};
15+
const actual = marked(spec.markdown, spec.options);
16+
result.pass = htmlDiffer.isEqual(expected, actual);
17+
18+
if (result.pass) {
19+
result.message = spec.markdown + '\n------\n\nExpected: Should Fail';
20+
} else {
21+
var expectedHtml = expected.replace(/\s/g, '');
22+
var actualHtml = actual.replace(/\s/g, '');
23+
24+
for (var i = 0; i < expectedHtml.length; i++) {
25+
if (actualHtml[i] !== expectedHtml[i]) {
26+
actualHtml = actualHtml.substring(
27+
Math.max(i - EXCERPT_LENGTH, 0),
28+
Math.min(i + EXCERPT_LENGTH, actualHtml.length));
29+
30+
expectedHtml = expectedHtml.substring(
31+
Math.max(i - EXCERPT_LENGTH, 0),
32+
Math.min(i + EXCERPT_LENGTH, expectedHtml.length));
33+
34+
break;
35+
}
36+
}
37+
result.message = 'Expected:\n' + expectedHtml + '\n\nActual:\n' + actualHtml;
38+
}
39+
return result;
40+
}
41+
};
42+
}
43+
});
544
});

test/index.js

+20-17
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,17 @@ function testFile(engine, file, filename, index) {
176176

177177
l = html.length;
178178

179+
if (l === 0 && text.length > 0) {
180+
text = text.substring(0, Math.min(30, text.length));
181+
182+
console.log(' failed in %dms at offset %d. Near: "%s".\n', prettyElapsedTime(elapsed), 0, text);
183+
184+
console.log('\nActual:\n%s\n', text.trim() || text);
185+
console.log('\nExpected:\n\n');
186+
187+
return false;
188+
}
189+
179190
for (j = 0; j < l; j++) {
180191
if (text[j] !== html[j]) {
181192
text = text.substring(
@@ -188,7 +199,7 @@ function testFile(engine, file, filename, index) {
188199

189200
console.log(' failed in %dms at offset %d. Near: "%s".\n', prettyElapsedTime(elapsed), j, text);
190201

191-
console.log('\nGot:\n%s\n', text.trim() || text);
202+
console.log('\nActual:\n%s\n', text.trim() || text);
192203
console.log('\nExpected:\n%s\n', html.trim() || html);
193204

194205
return false;
@@ -346,11 +357,11 @@ function time(options) {
346357
*/
347358

348359
function fix() {
349-
['compiled_tests', 'original', 'new'].forEach(function(dir) {
360+
['compiled_tests', 'original', 'new', 'redos'].forEach(function(dir) {
350361
try {
351362
fs.mkdirSync(path.resolve(__dirname, dir));
352363
} catch (e) {
353-
;
364+
// directory already exists
354365
}
355366
});
356367

@@ -393,20 +404,6 @@ function fix() {
393404
.replace(/&__QUOT__;/g, '"')
394405
.replace(/&__APOS__;/g, '\'');
395406

396-
// add heading id's
397-
html = html.replace(/<(h[1-6])>([^<]+)<\/\1>/g, function(s, h, text) {
398-
var id = text
399-
.replace(/&#39;/g, '\'')
400-
.replace(/&quot;/g, '"')
401-
.replace(/&gt;/g, '>')
402-
.replace(/&lt;/g, '<')
403-
.replace(/&amp;/g, '&');
404-
405-
id = id.toLowerCase().replace(/[^\w]+/g, '-');
406-
407-
return '<' + h + ' id="' + id + '">' + text + '</' + h + '>';
408-
});
409-
410407
fs.writeFileSync(file, html);
411408
});
412409

@@ -435,6 +432,12 @@ function fix() {
435432
fs.writeFileSync(path.resolve(__dirname, 'compiled_tests', file),
436433
fs.readFileSync(path.resolve(__dirname, 'new', file)));
437434
});
435+
436+
// cp redos/* tests/
437+
fs.readdirSync(path.resolve(__dirname, 'redos')).forEach(function(file) {
438+
fs.writeFileSync(path.resolve(__dirname, 'compiled_tests', file),
439+
fs.readFileSync(path.resolve(__dirname, 'redos', file)));
440+
});
438441
}
439442

440443
/**

test/json-to-files.js

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const path = require('path');
2+
const fs = require('fs');
3+
4+
const folder = process.argv[2];
5+
const jsonFile = process.argv[3];
6+
7+
if (!folder || !jsonFile) {
8+
console.log('node ./json-to-files.js {path to folder} {path to json file}');
9+
process.exit(1);
10+
}
11+
12+
const specs = require(jsonFile);
13+
14+
const files = specs.reduce((obj, spec) => {
15+
if (!obj[spec.section]) {
16+
obj[spec.section] = {
17+
md: [],
18+
html: [],
19+
options: {}
20+
};
21+
}
22+
23+
obj[spec.section].md.push(spec.markdown);
24+
obj[spec.section].html.push(spec.html);
25+
Object.assign(obj[spec.section].options, spec.options);
26+
27+
return obj;
28+
}, {});
29+
30+
try {
31+
fs.mkdirSync(folder, {recursive: true});
32+
} catch (ex) {
33+
// already exists
34+
}
35+
36+
for (const section in files) {
37+
const file = files[section];
38+
const name = section.toLowerCase().replace(' ', '_');
39+
const frontMatter = Object.keys(file.options).map(opt => {
40+
let value = file.options[opt];
41+
if (typeof value !== 'string') {
42+
value = JSON.stringify(value);
43+
}
44+
return `${opt}: ${value}`;
45+
}).join('\n');
46+
47+
let markdown = file.md.join('\n\n');
48+
if (frontMatter) {
49+
markdown = `---\n${frontMatter}\n---\n\n${markdown}`;
50+
}
51+
const html = file.html.join('\n\n');
52+
53+
const mdFile = path.resolve(folder, `${name}.md`);
54+
const htmlFile = path.resolve(folder, `${name}.html`);
55+
56+
if (fs.existsSync(mdFile) || fs.existsSync(htmlFile)) {
57+
throw new Error(`${name} already exists.`);
58+
}
59+
60+
fs.writeFileSync(mdFile, markdown);
61+
fs.writeFileSync(htmlFile, html);
62+
}

test/new/autolinks.html

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<p>(See <a href="https://www.example.com/fhqwhgads">https://www.example.com/fhqwhgads</a>.)</p>
2+
3+
<p>((<a href="http://foo.com">http://foo.com</a>))</p>
4+
5+
<p>((<a href="http://foo.com">http://foo.com</a>.))</p>
6+
7+
<p><a href="HTTP://FOO.COM">HTTP://FOO.COM</a></p>
8+
9+
<p><a href="hTtP://fOo.CoM">hTtP://fOo.CoM</a></p>
10+
11+
<p><del><a href="mailto:hello@email.com">hello@email.com</a></del></p>
12+
13+
<p><strong><a href="mailto:me@example.com">me@example.com</a></strong></p>
14+
15+
<p><strong><a href="mailto:test@test.com">test@test.com</a></strong></p>

test/new/autolinks.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
(See https://www.example.com/fhqwhgads.)
2+
3+
((http://foo.com))
4+
5+
((http://foo.com.))
6+
7+
HTTP://FOO.COM
8+
9+
hTtP://fOo.CoM
10+
11+
~~hello@email.com~~
12+
13+
**me@example.com**
14+
15+
__test@test.com__

test/new/code_spans.html

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<p><code>someone@example.com</code></p>
2+
3+
<p>``<em>test`</em></p>

test/new/code_spans.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
`someone@example.com`
2+
3+
``*test`*

test/new/emphasis_extra tests.html

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p><em>test</em>. <em>test</em>: <em>test</em>! <em>test</em>? <em>test</em>-</p>

test/new/emphasis_extra tests.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
_test_. _test_: _test_! _test_? _test_-

test/new/links.html

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
<p>URL</p>
2-
<p>URL</p>
3-
<p>URL</p>
4-
<p>URL</p>
5-
<p>URL</p>
1+
<p>Link: <a href="https://example.org/">constructor</a>.</p>
2+
3+
<p><a href="https://example.com/1">One</a> (<a href="https://example.com/2">Two</a>) <a href="https://example.com/3">Three</a></p>

test/new/links.md

+3-10
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
1-
---
2-
sanitize: true
3-
---
4-
[URL](javascript:alert)
1+
Link: [constructor][].
52

6-
[URL](vbscript:alert)
3+
[One](https://example.com/1) ([Two](https://example.com/2)) [Three](https://example.com/3)
74

8-
[URL](javascript&colon;alert&#40;1&#41;)
9-
10-
[URL](javascript&#58document;alert&#40;1&#41;)
11-
12-
[URL](data:text/html;base64,PHNjcmlwdD5hbGVydCgnWFNTJyk8L3NjcmlwdD4K)
5+
[constructor]: https://example.org/

test/new/redos_html_closing.html

Whitespace-only changes.

test/new/redos_nolink.html

Whitespace-only changes.

test/new/sanitize_links.html

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<p>URL</p>
2+
<p>URL</p>
3+
<p>URL</p>
4+
<p>URL</p>
5+
<p>URL</p>

test/new/sanitize_links.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
sanitize: true
3+
---
4+
[URL](javascript:alert)
5+
6+
[URL](vbscript:alert)
7+
8+
[URL](javascript&colon;alert&#40;1&#41;)
9+
10+
[URL](javascript&#58document;alert&#40;1&#41;)
11+
12+
[URL](data:text/html;base64,PHNjcmlwdD5hbGVydCgnWFNTJyk8L3NjcmlwdD4K)

test/new/table_cells.html

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<table><thead><tr><th>1</th></tr></thead><tbody><tr><td>1</td></tr></tbody></table>
2+
3+
<table><thead><tr><th>1</th></tr></thead><tbody><tr><td>|</td></tr></tbody></table>
4+
5+
<table><thead><tr><th>1</th></tr></thead><tbody><tr><td>1\1</td></tr></tbody></table>
6+
7+
<table><thead><tr><th>1</th></tr></thead><tbody><tr><td>\\</td></tr></tbody></table>
8+
9+
<table><thead><tr><th>1</th></tr></thead><tbody><tr><td>\\|</td></tr></tbody></table>
10+
11+
<table><thead><tr><th>1</th><th>2</th></tr></thead><tbody><tr><td></td><td>2</td></tr></tbody></table>
12+
13+
<table><thead><tr><th>1</th><th>2</th></tr></thead><tbody><tr><td>1|\</td><td>2|\</td></tr></tbody></table>
14+
15+
<table><thead><tr><th>1</th><th>2</th></tr></thead><tbody><tr><td></td><td>2</td></tr></tbody></table>
16+
17+
<table><thead><tr><th>1</th><th>2</th></tr></thead><tbody><tr><td>1|\</td><td>2|\</td></tr></tbody></table>
18+
19+
<table><thead><tr><th>1</th><th>2</th></tr></thead><tbody><tr><td></td><td>2</td></tr></tbody></table>
20+
21+
<table><thead><tr><th>1</th><th>2</th></tr></thead><tbody><tr><td>1</td><td>2|</td></tr></tbody></table>
22+
23+
<table><thead><tr><th>1</th><th>2</th></tr></thead><tbody><tr><td>1</td><td>2|</td></tr></tbody></table>
24+
25+
<table><thead><tr><th>1</th><th>2</th></tr></thead><tbody><tr><td>1</td><td>2|</td></tr></tbody></table>
26+
27+
<table><thead><tr><th>1</th><th>2</th></tr></thead><tbody><tr><td>1</td><td>2|</td></tr></tbody></table>

test/new/table_cells.md

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
|1|
2+
|-|
3+
|1|
4+
5+
|1|
6+
|-|
7+
|\||
8+
9+
|1|
10+
|-|
11+
|1\\1|
12+
13+
|1|
14+
|-|
15+
|\\\\||
16+
17+
|1|
18+
|-|
19+
|\\\\\||
20+
21+
|1|2|
22+
|-|-|
23+
||2|
24+
25+
|1|2|
26+
|-|-|
27+
|1\|\\|2\|\\|
28+
29+
|1|2|
30+
|-|-|
31+
| |2|
32+
33+
1|2
34+
-|-
35+
1\|\\|2\|\\
36+
37+
1|2
38+
-|-
39+
|2
40+
41+
1|2
42+
-|-
43+
1|2\|
44+
45+
1|2
46+
-|-
47+
1|2\|
48+
49+
|1|2|
50+
|-|-|
51+
|1|2\||
52+
53+
|1|2|
54+
|-|-|
55+
|1|2\||

0 commit comments

Comments
 (0)