UNPKG

5.9 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(markdownText) {
29 const json1 = ciceroMarkTransformer.fromMarkdownCicero(markdownText);
30 const newMarkdown = ciceroMarkTransformer.toMarkdownCicero(json1);
31 const json2 = ciceroMarkTransformer.fromMarkdownCicero(newMarkdown);
32 const pass = JSON.stringify(json1) === JSON.stringify(json2);
33
34 const message = pass
35 ? () =>
36 this.utils.matcherHint(`toMarkdownRoundtrip - ${markdownText} -> ${newMarkdown}`, undefined, undefined, undefined) +
37 '\n\n' +
38 `Expected: ${this.utils.printExpected(json1)}\n` +
39 `Received: ${this.utils.printReceived(json2)}`
40 : () => {
41 const diffString = diff(json1, json2, {
42 expand: true,
43 });
44 return (
45 this.utils.matcherHint(`toMarkdownRoundtrip - ${JSON.stringify(markdownText)} -> ${JSON.stringify(newMarkdown)}`, undefined, undefined, undefined) +
46 '\n\n' +
47 (diffString && diffString.includes('- Expect')
48 ? `Difference:\n\n${diffString}`
49 : `Expected: ${this.utils.printExpected(json1)}\n` +
50 `Received: ${this.utils.printReceived(json2)}`)
51 );
52 };
53
54 return {actual: markdownText, message, pass};
55 },
56});
57
58beforeAll(() => {
59 ciceroMarkTransformer = new CiceroMarkTransformer();
60});
61
62/**
63 * Get the name and contents of all markdown test files
64 * @returns {*} an array of name/contents tuples
65 */
66function getMarkdownFiles() {
67 const result = [];
68 const files = fs.readdirSync(__dirname + '/../test/data/ciceromark');
69
70 files.forEach(function(file) {
71 if(file.endsWith('.md')) {
72 let contents = fs.readFileSync(__dirname + '/../test/data/ciceromark/' + file, 'utf8');
73 result.push([file, contents]);
74 }
75 });
76
77 return result;
78}
79
80describe('markdown', () => {
81 it('transformer should have a serializer', () => {
82 expect(ciceroMarkTransformer.getSerializer()).toBeTruthy();
83 });
84
85 getMarkdownFiles().forEach( ([file, markdownText]) => {
86 it(`converts ${file} to concerto`, () => {
87 const json = ciceroMarkTransformer.fromMarkdownCicero(markdownText, 'json');
88 expect(json).toMatchSnapshot();
89 });
90
91 it(`roundtrips ${file}`, () => {
92 expect(markdownText).toMarkdownRoundtrip();
93 });
94 });
95});
96
97describe('acceptance', () => {
98 it('converts acceptance to markdown string', () => {
99 const markdownText = fs.readFileSync(__dirname + '/../test/data/ciceromark/acceptance.md', 'utf8');
100 const json = ciceroMarkTransformer.fromMarkdownCicero(markdownText);
101 // console.log(JSON.stringify(json, null, 4));
102 expect(json).toMatchSnapshot();
103 const newMarkdown = ciceroMarkTransformer.toMarkdown(json);
104 expect(newMarkdown).toMatchSnapshot();
105 });
106
107 it('converts acceptance to markdown string (unquoted)', () => {
108 const markdownText = fs.readFileSync(__dirname + '/../test/data/ciceromark/acceptance.md', 'utf8');
109 const json = ciceroMarkTransformer.fromMarkdownCicero(markdownText);
110 // console.log(JSON.stringify(json, null, 4));
111 expect(json).toMatchSnapshot();
112 const newMarkdown = ciceroMarkTransformer.toMarkdown(json,{unquoteVariables:true});
113 expect(newMarkdown).toMatchSnapshot();
114 });
115
116 it('converts acceptance to markdown string (plaintext)', () => {
117 const markdownText = fs.readFileSync(__dirname + '/../test/data/ciceromark/acceptance.md', 'utf8');
118 const json = ciceroMarkTransformer.fromMarkdownCicero(markdownText);
119 // console.log(JSON.stringify(json, null, 4));
120 expect(json).toMatchSnapshot();
121 const newMarkdown = ciceroMarkTransformer.toMarkdown(json,{removeFormatting:true});
122 expect(newMarkdown).toMatchSnapshot();
123 });
124
125 it('converts acceptance to cicero markdown string', () => {
126 const markdownText = fs.readFileSync(__dirname + '/../test/data/ciceromark/acceptance.md', 'utf8');
127 const json = ciceroMarkTransformer.fromMarkdownCicero(markdownText);
128 // console.log(JSON.stringify(json, null, 4));
129 expect(json).toMatchSnapshot();
130 const newMarkdown = ciceroMarkTransformer.toMarkdownCicero(json);
131 expect(newMarkdown).toMatchSnapshot();
132 });
133
134 it('converts acceptance clause content to markdown string (getClauseText)', () => {
135 const markdownText = fs.readFileSync(__dirname + '/../test/data/ciceromark/acceptance.md', 'utf8');
136 const json = ciceroMarkTransformer.fromMarkdownCicero(markdownText);
137 // console.log(JSON.stringify(json, null, 4));
138 expect(json).toMatchSnapshot();
139 const clauseText = ciceroMarkTransformer.getClauseText(json.nodes[2]);
140 expect(clauseText).toMatchSnapshot();
141 expect((() => ciceroMarkTransformer.getClauseText(json.nodes[1]))).toThrow('Cannot apply getClauseText to non-clause node');
142 });
143
144});