UNPKG

6.24 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');
20const diff = require('jest-diff');
21
22const CiceroMarkTransformer = require('./CiceroMarkTransformer');
23
24// eslint-disable-next-line no-unused-vars
25let ciceroMarkTransformer = null;
26
27expect.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
62beforeAll(() => {
63 ciceroMarkTransformer = new CiceroMarkTransformer();
64});
65
66/**
67 * Get the name and contents of all markdown test files
68 * @returns {*} an array of name/contents tuples
69 */
70function 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
85describe('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
98describe('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 // console.log(JSON.stringify(json, null, 4));
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 // console.log(JSON.stringify(json, null, 4));
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 // console.log(JSON.stringify(json, null, 4));
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 // console.log(JSON.stringify(json, null, 4));
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 // console.log(JSON.stringify(json, null, 4));
140 expect(json).toMatchSnapshot();
141 const newMarkdown = ciceroMarkTransformer.toMarkdown(json,{unquoteVariables:true});
142 expect(newMarkdown).toMatchSnapshot();
143 });
144
145});