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 | const diff = require('jest-diff');
|
21 |
|
22 | const CiceroMarkTransformer = require('./CiceroMarkTransformer');
|
23 |
|
24 |
|
25 | let ciceroMarkTransformer = null;
|
26 |
|
27 | expect.extend({
|
28 | toMarkdownRoundtrip(ciceroEditText,markdownText,testName) {
|
29 | const jsonEdit = ciceroMarkTransformer.fromCiceroEdit(ciceroEditText);
|
30 | const jsonEditUnwrapped = ciceroMarkTransformer.toCiceroMarkUnwrapped(jsonEdit);
|
31 | const newMarkdownEdit = ciceroMarkTransformer.toMarkdownCicero(jsonEditUnwrapped);
|
32 | const jsonMark = ciceroMarkTransformer.fromMarkdownCicero(markdownText);
|
33 | const newMarkdownMark = ciceroMarkTransformer.toMarkdownCicero(jsonMark);
|
34 | const json1 = ciceroMarkTransformer.fromMarkdownCicero(newMarkdownEdit);
|
35 | const json2 = jsonMark;
|
36 | const pass = JSON.stringify(json1) === JSON.stringify(json2);
|
37 |
|
38 | const message = pass
|
39 | ? () =>
|
40 | this.utils.matcherHint(`toMarkdownRoundtrip - ${newMarkdownEdit} <-> ${newMarkdownMark}`, undefined, undefined, undefined) +
|
41 | '\n\n' +
|
42 | `Expected: ${this.utils.printExpected(json1)}\n` +
|
43 | `Received: ${this.utils.printReceived(json2)}`
|
44 | : () => {
|
45 | const diffString = diff(json1, json2, {
|
46 | expand: true,
|
47 | });
|
48 | return (
|
49 | this.utils.matcherHint(`toMarkdownRoundtrip - ${JSON.stringify(newMarkdownEdit)} -> ${JSON.stringify(newMarkdownMark)}`, undefined, undefined, undefined) +
|
50 | '\n\n' +
|
51 | (diffString && diffString.includes('- Expect')
|
52 | ? `Difference:\n\n${diffString}`
|
53 | : `Expected: ${this.utils.printExpected(json1)}\n` +
|
54 | `Received: ${this.utils.printReceived(json2)}`)
|
55 | );
|
56 | };
|
57 |
|
58 | return {actual: ciceroEditText, message, pass};
|
59 | },
|
60 | });
|
61 |
|
62 | beforeAll(() => {
|
63 | ciceroMarkTransformer = new CiceroMarkTransformer();
|
64 | });
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | function getMarkdownFiles() {
|
71 | const result = [];
|
72 | const files = fs.readdirSync(__dirname + '/../test/data/ciceroedit');
|
73 |
|
74 | files.forEach(function(file) {
|
75 | if(file.endsWith('.md')) {
|
76 | let contentsEdit = fs.readFileSync(__dirname + '/../test/data/ciceroedit/' + file, 'utf8');
|
77 | let contentsMark = fs.readFileSync(__dirname + '/../test/data/ciceromark/' + file, 'utf8');
|
78 | result.push([file, contentsEdit, contentsMark]);
|
79 | }
|
80 | });
|
81 |
|
82 | return result;
|
83 | }
|
84 |
|
85 | describe('markdown', () => {
|
86 | getMarkdownFiles().forEach( ([file, ciceroEditText, markdownText]) => {
|
87 | it(`converts ${file} to ciceromark`, () => {
|
88 | const json = ciceroMarkTransformer.fromCiceroEdit(ciceroEditText);
|
89 | expect(json).toMatchSnapshot();
|
90 | });
|
91 |
|
92 | it(`roundtrips ${file}`, () => {
|
93 | expect(ciceroEditText).toMarkdownRoundtrip(markdownText,file);
|
94 | });
|
95 | });
|
96 | });
|
97 |
|
98 | describe('acceptance', () => {
|
99 | it('converts acceptance to CommonMark DOM', () => {
|
100 | const markdownText = fs.readFileSync(__dirname + '/../test/data/ciceroedit/acceptance.md', 'utf8');
|
101 | const json = ciceroMarkTransformer.fromCiceroEdit(markdownText);
|
102 |
|
103 | expect(json).toMatchSnapshot();
|
104 | const jsonUnwrapped = ciceroMarkTransformer.toCiceroMarkUnwrapped(json);
|
105 | const newMarkdown = ciceroMarkTransformer.toMarkdownCicero(jsonUnwrapped);
|
106 | expect(newMarkdown).toMatchSnapshot();
|
107 | });
|
108 |
|
109 | it('converts acceptance to markdown string (unquoted)', () => {
|
110 | const markdownText = fs.readFileSync(__dirname + '/../test/data/ciceroedit/acceptance.md', 'utf8');
|
111 | const json = ciceroMarkTransformer.fromCiceroEdit(markdownText);
|
112 |
|
113 | expect(json).toMatchSnapshot();
|
114 | const newMarkdown = ciceroMarkTransformer.toMarkdown(json,{unquoteVariables:true});
|
115 | expect(newMarkdown).toMatchSnapshot();
|
116 | });
|
117 |
|
118 | it('converts acceptance-formula to markdown string', () => {
|
119 | const markdownText = fs.readFileSync(__dirname + '/../test/data/ciceroedit/acceptance-formula.md', 'utf8');
|
120 | const json = ciceroMarkTransformer.fromCiceroEdit(markdownText);
|
121 |
|
122 | expect(json).toMatchSnapshot();
|
123 | const newMarkdown = ciceroMarkTransformer.toMarkdown(json);
|
124 | expect(newMarkdown).toMatchSnapshot();
|
125 | });
|
126 |
|
127 | it('converts acceptance-formula to markdown string (unquoted)', () => {
|
128 | const markdownText = fs.readFileSync(__dirname + '/../test/data/ciceroedit/acceptance-formula.md', 'utf8');
|
129 | const json = ciceroMarkTransformer.fromCiceroEdit(markdownText);
|
130 |
|
131 | expect(json).toMatchSnapshot();
|
132 | const newMarkdown = ciceroMarkTransformer.toMarkdown(json,{unquoteVariables:true});
|
133 | expect(newMarkdown).toMatchSnapshot();
|
134 | });
|
135 |
|
136 | it('converts acceptance-notclause2 to markdown string', () => {
|
137 | const markdownText = fs.readFileSync(__dirname + '/../test/data/ciceroedit/acceptance-notclause2.md', 'utf8');
|
138 | const json = ciceroMarkTransformer.fromCiceroEdit(markdownText);
|
139 |
|
140 | expect(json).toMatchSnapshot();
|
141 | const newMarkdown = ciceroMarkTransformer.toMarkdown(json,{unquoteVariables:true});
|
142 | expect(newMarkdown).toMatchSnapshot();
|
143 | });
|
144 |
|
145 | });
|