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(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 |
|
58 | beforeAll(() => {
|
59 | ciceroMarkTransformer = new CiceroMarkTransformer();
|
60 | });
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | function 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 |
|
80 | describe('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 |
|
97 | describe('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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 | });
|