UNPKG

4.26 kBJavaScriptView Raw
1/*
2 * Licensed under the Apache License, Version 2.0 (the "License");
3 * you may not use this file except in compliance with the License.
4 * You may obtain a copy of the License at
5 *
6 * http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 */
14
15// @ts-nocheck
16/* eslint-disable no-undef */
17'use strict';
18
19const fs = require('fs');
20
21const CiceroMarkTransformer = require('./CiceroMarkTransformer');
22
23// eslint-disable-next-line no-unused-vars
24let ciceroMarkTransformer = null;
25
26expect.extend({
27 toMarkdownRoundtrip(markdownText) {
28 const json1 = ciceroMarkTransformer.fromMarkdown(markdownText, 'json', {ciceroEdit:true});
29 const newMarkdown = ciceroMarkTransformer.toMarkdown(json1);
30 const json2 = ciceroMarkTransformer.fromMarkdown(newMarkdown, 'json', {ciceroEdit:true});
31 const pass = JSON.stringify(json1) === JSON.stringify(json2);
32
33 const message = pass
34 ? () =>
35 this.utils.matcherHint(`toMarkdownRoundtrip - ${markdownText} -> ${newMarkdown}`, undefined, undefined, undefined) +
36 '\n\n' +
37 `Expected: ${this.utils.printExpected(json1)}\n` +
38 `Received: ${this.utils.printReceived(json2)}`
39 : () => {
40 const diffString = diff(json1, json2, {
41 expand: true,
42 });
43 return (
44 this.utils.matcherHint(`toMarkdownRoundtrip - ${JSON.stringify(markdownText)} -> ${JSON.stringify(newMarkdown)}`, undefined, undefined, undefined) +
45 '\n\n' +
46 (diffString && diffString.includes('- Expect')
47 ? `Difference:\n\n${diffString}`
48 : `Expected: ${this.utils.printExpected(json1)}\n` +
49 `Received: ${this.utils.printReceived(json2)}`)
50 );
51 };
52
53 return {actual: markdownText, message, pass};
54 },
55});
56
57beforeAll(() => {
58 ciceroMarkTransformer = new CiceroMarkTransformer();
59});
60
61/**
62 * Extracts all the test md snippets from a commonmark spec file
63 * @param {string} testfile the file to use
64 * @return {*} the examples
65 */
66function extractSpecTests(testfile) {
67 let data = fs.readFileSync(testfile, 'utf8');
68 let examples = [];
69 let current_section = '';
70 let example_number = 0;
71 let tests = data
72 .replace(/\r\n?/g, '\n') // Normalize newlines for platform independence
73 .replace(/^<!-- END TESTS -->(.|[\n])*/m, '');
74
75 tests.replace(/^`{32} example\n([\s\S]*?)^\.\n([\s\S]*?)^`{32}$|^#{1,6} *(.*)$/gm,
76 function(_, markdownSubmatch, htmlSubmatch, sectionSubmatch){
77 if (sectionSubmatch) {
78 current_section = sectionSubmatch;
79 } else {
80 example_number++;
81 examples.push({markdown: markdownSubmatch,
82 html: htmlSubmatch,
83 section: current_section,
84 number: example_number});
85 }
86 });
87 return examples;
88}
89
90/**
91 * Get the name and contents of all markdown snippets
92 * used in a commonmark spec file
93 * @returns {*} an array of name/contents tuples
94 */
95function getMarkdownSpecFiles() {
96 const result = [];
97 const specExamples = extractSpecTests(__dirname + '/../test/data/spec.txt');
98 specExamples.forEach(function(example) {
99 result.push([`${example.section}-${example.number}`, example.markdown]);
100 });
101
102 return result;
103}
104
105describe('markdown-spec', () => {
106 getMarkdownSpecFiles().forEach( ([file, markdownText]) => {
107 it(`converts ${file} to concerto`, () => {
108 const json = ciceroMarkTransformer.fromMarkdown(markdownText, 'json', {ciceroEdit:true});
109 expect(json).toMatchSnapshot();
110 });
111
112 // currently skipped because not all examples roundtrip
113 // needs more investigation!!
114 it.skip(`roundtrips ${file}`, () => {
115 expect(markdownText).toMarkdownRoundtrip();
116 });
117 });
118});