1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | 'use strict';
|
18 |
|
19 | const fs = require('fs');
|
20 |
|
21 | const CiceroMarkTransformer = require('./CiceroMarkTransformer');
|
22 |
|
23 |
|
24 | let ciceroMarkTransformer = null;
|
25 |
|
26 | expect.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 |
|
57 | beforeAll(() => {
|
58 | ciceroMarkTransformer = new CiceroMarkTransformer();
|
59 | });
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | function 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')
|
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 |
|
92 |
|
93 |
|
94 |
|
95 | function 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 |
|
105 | describe('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 |
|
113 |
|
114 | it.skip(`roundtrips ${file}`, () => {
|
115 | expect(markdownText).toMarkdownRoundtrip();
|
116 | });
|
117 | });
|
118 | });
|