UNPKG

3.06 kBJavaScriptView Raw
1/*
2 * Copyright 2019 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12const assert = require('assert');
13const unified = require('unified');
14const stringify = require('remark-stringify');
15const inspect = require('unist-util-inspect');
16const select = require('unist-util-select');
17const path = require('path');
18const gfm = require('remark-gfm');
19const readdirp = require('readdirp');
20const { loader } = require('../lib/schemaProxy');
21const traverse = require('../lib/traverseSchema');
22
23function 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
76async 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 // eslint-disable-next-line global-require, import/no-dynamic-require
84 require(fullPath), fullPath,
85 )));
86}
87
88module.exports = { assertMarkdown, loadschemas };