1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | 'use strict';
|
16 |
|
17 | const fs = require('fs');
|
18 | const path = require('path');
|
19 | const SlateTransformer = require('./SlateTransformer');
|
20 |
|
21 | let slateTransformer = null;
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | function normalizeNLs(input) {
|
32 |
|
33 | let text = input.replace(/\r/gm,'');
|
34 | return text;
|
35 | }
|
36 |
|
37 |
|
38 |
|
39 | beforeAll(() => {
|
40 | slateTransformer = new SlateTransformer();
|
41 | });
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 | function 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 |
|
61 | describe('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 |
|
68 | expect(ciceroMark).toMatchSnapshot();
|
69 |
|
70 |
|
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();
|
75 |
|
76 |
|
77 | const expectedSlateValue = slateTransformer.fromMarkdown(expectedMarkdown);
|
78 | expect(expectedSlateValue).toMatchSnapshot();
|
79 |
|
80 |
|
81 |
|
82 |
|
83 | const expectedCiceroMark = slateTransformer.toCiceroMark(expectedSlateValue, 'json');
|
84 | expect(expectedCiceroMark).toMatchSnapshot();
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 | expect(ciceroMark).toEqual(expectedCiceroMark);
|
91 |
|
92 |
|
93 | expect(expectedSlateValue).toEqual(value);
|
94 | });
|
95 | });
|
96 | });
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 | function 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 |
|
118 |
|
119 |
|
120 | function 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 |
|
134 | describe('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 |
|
141 | expect(ciceroMark).toMatchSnapshot();
|
142 |
|
143 |
|
144 | const expectedCiceroMark = JSON.parse(fs.readFileSync(__dirname + '/../test/data/ciceromark/' + file.replace(/_slate.json$/,'_ciceromark.json'), 'utf8'));
|
145 | expect(expectedCiceroMark).toMatchSnapshot();
|
146 |
|
147 |
|
148 | const expectedSlateValue = slateTransformer.fromCiceroMark(expectedCiceroMark);
|
149 | expect(expectedSlateValue).toMatchSnapshot();
|
150 |
|
151 |
|
152 | expect(ciceroMark).toEqual(expectedCiceroMark);
|
153 |
|
154 |
|
155 | expect(expectedSlateValue).toEqual(value);
|
156 | });
|
157 | });
|
158 | });
|
159 |
|
160 | describe('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 |
|
167 | expect(templateMark).toMatchSnapshot();
|
168 |
|
169 |
|
170 | const expectedTemplateMark = JSON.parse(fs.readFileSync(__dirname + '/../test/data/templatemark/' + file.replace(/_slate.json$/,'_templatemark.json'), 'utf8'));
|
171 | expect(expectedTemplateMark).toMatchSnapshot();
|
172 |
|
173 |
|
174 | const expectedSlateValue = slateTransformer.fromTemplateMark(expectedTemplateMark);
|
175 | expect(expectedSlateValue).toMatchSnapshot();
|
176 |
|
177 |
|
178 | expect(templateMark).toEqual(expectedTemplateMark);
|
179 |
|
180 |
|
181 | expect(expectedSlateValue).toEqual(value);
|
182 | });
|
183 | });
|
184 | });
|
185 |
|
186 | describe('slate -> markdown_cicero', () => {
|
187 | it('converts acceptance from slate to markdown cicero', () => {
|
188 |
|
189 | const slateDom = JSON.parse(fs.readFileSync(__dirname + '/../test/data/ciceromark/acceptance_slate.json', 'utf8'));
|
190 |
|
191 | const expectedMarkdownCicero = normalizeNLs(fs.readFileSync(__dirname + '/../test/data/ciceromark/acceptance.md', 'utf8'));
|
192 |
|
193 |
|
194 | const actualMarkdownCicero = slateTransformer.toMarkdownCicero(slateDom);
|
195 | expect(actualMarkdownCicero).toMatchSnapshot();
|
196 |
|
197 |
|
198 | expect(actualMarkdownCicero).toEqual(expectedMarkdownCicero);
|
199 | });
|
200 | });
|
201 |
|