UNPKG

7.54 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'use strict';
16
17const fs = require('fs');
18const path = require('path');
19const SlateTransformer = require('./SlateTransformer');
20
21let slateTransformer = null;
22
23/* eslint-disable no-undef */
24// @ts-nocheck
25
26/**
27 * Prepare the text for parsing (normalizes new lines, etc)
28 * @param {string} input - the text for the clause
29 * @return {string} - the normalized text for the clause
30 */
31function normalizeNLs(input) {
32 // we replace all \r and \n with \n
33 let text = input.replace(/\r/gm,'');
34 return text;
35}
36
37// @ts-ignore
38// eslint-disable-next-line no-undef
39beforeAll(() => {
40 slateTransformer = new SlateTransformer();
41});
42
43/**
44 * Get the name and contents of all slate test files for Markdown
45 * @returns {*} an array of name/contents tuples
46 */
47function getMarkdownSlateFiles() {
48 const result = [];
49 const files = fs.readdirSync(__dirname + '/../test/data/markdown/');
50
51 files.forEach(function(file) {
52 if(file.endsWith('.json')) {
53 let contents = fs.readFileSync(__dirname + '/../test/data/markdown/' + file, 'utf8');
54 result.push([file, contents]);
55 }
56 });
57
58 return result;
59}
60
61describe('markdown <-> slate', () => {
62 getMarkdownSlateFiles().forEach( ([file, jsonText], index) => {
63 it(`converts ${file} to and from Markdown`, () => {
64 const value = JSON.parse(jsonText);
65 const ciceroMark = slateTransformer.toCiceroMark(value, 'json');
66
67 // check no changes to cicero mark
68 expect(ciceroMark).toMatchSnapshot(); // (1)
69
70 // load expected markdown
71 const extension = path.extname(file);
72 const mdFile = path.basename(file,extension);
73 const expectedMarkdown = fs.readFileSync(__dirname + '/../test/data/markdown/' + mdFile + '.md', 'utf8');
74 expect(expectedMarkdown).toMatchSnapshot(); // (2)
75
76 // convert the expected markdown to cicero mark and compare
77 const expectedSlateValue = slateTransformer.fromMarkdown(expectedMarkdown);
78 expect(expectedSlateValue).toMatchSnapshot(); // (3)
79 // if(mdFile === 'image') {
80 // console.log(JSON.stringify(expectedSlateValue, null, 4));
81 // }
82
83 const expectedCiceroMark = slateTransformer.toCiceroMark(expectedSlateValue, 'json');
84 expect(expectedCiceroMark).toMatchSnapshot(); // (4)
85 // if(mdFile === 'image') {
86 // console.log('Expected expectedCiceroMark', JSON.stringify(expectedCiceroMark, null, 4));
87 // }
88
89 // check that ast created from slate and from the expected md is the same
90 expect(ciceroMark).toEqual(expectedCiceroMark);
91
92 // check roundtrip
93 expect(expectedSlateValue).toEqual(value);
94 });
95 });
96});
97
98/**
99 * Get the name and contents of all slate test files for CiceroMark
100 * @returns {*} an array of name/contents tuples
101 */
102function getCiceroMarkSlateFiles() {
103 const result = [];
104 const files = fs.readdirSync(__dirname + '/../test/data/ciceromark/');
105
106 files.forEach(function(file) {
107 if(file.endsWith('_slate.json')) {
108 let contents = fs.readFileSync(__dirname + '/../test/data/ciceromark/' + file, 'utf8');
109 result.push([file, contents]);
110 }
111 });
112
113 return result;
114}
115
116/**
117 * Get the name and contents of all slate test files for TemplateMark
118 * @returns {*} an array of name/contents tuples
119 */
120function getTemplateMarkSlateFiles() {
121 const result = [];
122 const files = fs.readdirSync(__dirname + '/../test/data/templatemark/');
123
124 files.forEach(function(file) {
125 if(file.endsWith('_slate.json')) {
126 let contents = fs.readFileSync(__dirname + '/../test/data/templatemark/' + file, 'utf8');
127 result.push([file, contents]);
128 }
129 });
130
131 return result;
132}
133
134describe('ciceromark <-> slate', () => {
135 getCiceroMarkSlateFiles().forEach( ([file, jsonText], index) => {
136 it(`converts ${file} to and from CiceroMark`, () => {
137 const value = JSON.parse(jsonText);
138 const ciceroMark = slateTransformer.toCiceroMark(value, 'json');
139
140 // check no changes to ciceromark
141 expect(ciceroMark).toMatchSnapshot(); // (1)
142
143 // load expected ciceromark
144 const expectedCiceroMark = JSON.parse(fs.readFileSync(__dirname + '/../test/data/ciceromark/' + file.replace(/_slate.json$/,'_ciceromark.json'), 'utf8'));
145 expect(expectedCiceroMark).toMatchSnapshot(); // (2)
146
147 // convert the expected markdown to ciceromark and compare
148 const expectedSlateValue = slateTransformer.fromCiceroMark(expectedCiceroMark);
149 expect(expectedSlateValue).toMatchSnapshot(); // (3)
150
151 // check that ast created from slate and from the expected md is the same
152 expect(ciceroMark).toEqual(expectedCiceroMark);
153
154 // check roundtrip
155 expect(expectedSlateValue).toEqual(value);
156 });
157 });
158});
159
160describe('templatemark <-> slate', () => {
161 getTemplateMarkSlateFiles().forEach( ([file, jsonText], index) => {
162 it(`converts ${file} to and from TemplateMark`, () => {
163 const value = JSON.parse(jsonText);
164 const templateMark = slateTransformer.toTemplateMark(value, 'json');
165
166 // check no changes to templatemark
167 expect(templateMark).toMatchSnapshot(); // (1)
168
169 // load expected templatemark
170 const expectedTemplateMark = JSON.parse(fs.readFileSync(__dirname + '/../test/data/templatemark/' + file.replace(/_slate.json$/,'_templatemark.json'), 'utf8'));
171 expect(expectedTemplateMark).toMatchSnapshot(); // (2)
172
173 // convert the expected markdown to templatemark and compare
174 const expectedSlateValue = slateTransformer.fromTemplateMark(expectedTemplateMark);
175 expect(expectedSlateValue).toMatchSnapshot(); // (3)
176
177 // check that ast created from slate and from the expected md is the same
178 expect(templateMark).toEqual(expectedTemplateMark);
179
180 // check roundtrip
181 expect(expectedSlateValue).toEqual(value);
182 });
183 });
184});
185
186describe('slate -> markdown_cicero', () => {
187 it('converts acceptance from slate to markdown cicero', () => {
188 // load slate DOM
189 const slateDom = JSON.parse(fs.readFileSync(__dirname + '/../test/data/ciceromark/acceptance_slate.json', 'utf8'));
190 // load expected markdown cicero
191 const expectedMarkdownCicero = normalizeNLs(fs.readFileSync(__dirname + '/../test/data/ciceromark/acceptance.md', 'utf8'));
192
193 // convert the slate to markdown cicero and compare
194 const actualMarkdownCicero = slateTransformer.toMarkdownCicero(slateDom);
195 expect(actualMarkdownCicero).toMatchSnapshot(); // (3)
196
197 // check roundtrip
198 expect(actualMarkdownCicero).toEqual(expectedMarkdownCicero);
199 });
200});
201