1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | const assert = require('assert');
|
13 | const unified = require('unified');
|
14 | const stringify = require('remark-stringify');
|
15 | const inspect = require('unist-util-inspect');
|
16 | const select = require('unist-util-select');
|
17 | const path = require('path');
|
18 | const gfm = require('remark-gfm');
|
19 | const readdirp = require('readdirp');
|
20 | const { loader } = require('../lib/schemaProxy');
|
21 | const traverse = require('../lib/traverseSchema');
|
22 |
|
23 | function assertMarkdown(node) {
|
24 | const processor = unified()
|
25 | .use(gfm)
|
26 | .use(stringify);
|
27 | try {
|
28 | const result = processor.stringify(node);
|
29 | const tester = {};
|
30 | tester.contains = (str) => {
|
31 | assert.ok(result.indexOf(str) >= 0, `Markdown output does not contain search string "${str}"
|
32 | ${result}`);
|
33 | return tester;
|
34 | };
|
35 | tester.doesNotContain = (str) => {
|
36 | assert.ok(result.indexOf(str) < 0, `Markdown output contains search string "${str}"
|
37 | ${result}`);
|
38 | return tester;
|
39 | };
|
40 | tester.matches = (re) => {
|
41 | assert.ok(result.match(re), `Markdown output does not match regex "${String(re)}"
|
42 | ${result}`);
|
43 | return tester;
|
44 | };
|
45 | tester.inspect = () => {
|
46 | console.log(inspect(node));
|
47 | return tester;
|
48 | };
|
49 | tester.print = () => {
|
50 | console.log(result);
|
51 | return tester;
|
52 | };
|
53 | tester.has = (selector) => {
|
54 | assert.ok(select.select(selector, node), `Markdown AST does not include node matching selector "${selector}"
|
55 | ${inspect(node)}`);
|
56 | return tester;
|
57 | };
|
58 | tester.equals = (selector, value) => {
|
59 | assert.deepStrictEqual(select.select(selector, node), value);
|
60 | return tester;
|
61 | };
|
62 | tester.fuzzy = (expr) => {
|
63 | const matches = expr.filter((line) => result.indexOf(line) >= 0);
|
64 | if (matches.length < expr.length) {
|
65 | assert.equal(result, expr.join(''));
|
66 | }
|
67 | return tester;
|
68 | };
|
69 | return tester;
|
70 | } catch (e) {
|
71 | assert.fail(`Invalid Markdown:\n${inspect(node)}\n${e}\n${JSON.stringify(node, undefined, 2)}`);
|
72 | }
|
73 | return null;
|
74 | }
|
75 |
|
76 | async function loadschemas(dir) {
|
77 | const schemaloader = loader();
|
78 | const schemadir = path.resolve(__dirname, 'fixtures', dir);
|
79 | const schemas = await readdirp.promise(schemadir, { fileFilter: '*.schema.json' });
|
80 |
|
81 | return traverse(schemas
|
82 | .map(({ fullPath }) => schemaloader(
|
83 |
|
84 | require(fullPath), fullPath,
|
85 | )));
|
86 | }
|
87 |
|
88 | module.exports = { assertMarkdown, loadschemas };
|