Skip to content

Commit

Permalink
Merge pull request #251 from evolvedbinary/test/round-trip
Browse files Browse the repository at this point in the history
Complete Round trip testing
  • Loading branch information
plutonik-a authored Aug 12, 2024
2 parents ad8f27c + 1482571 commit c5f5d80
Show file tree
Hide file tree
Showing 4 changed files with 2,816 additions and 4 deletions.
2 changes: 0 additions & 2 deletions packages/lwdita-xdita/src/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import { Attributes, BasicValue, TextNode, getNodeClass, JDita, BaseNode, Docume
import { InMemoryTextSimpleOutputStreamCollector } from "./stream";
import { XditaSerializer } from "./xdita-serializer";

/** TODO: Add tests for this module */

/**
* Converts XML to an AST document tree
*
Expand Down
41 changes: 40 additions & 1 deletion packages/lwdita-xdita/test/converter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.


import { expect } from 'chai';
import { jditaToAst, serializeToXdita, xditaToAst, xditaToJdita } from '../src/converter';
import { astToJdita, jditaToAst, serializeToXdita, xditaToAst, xditaToJdita } from '../src/converter';
import { DocumentNode } from "@evolvedbinary/lwdita-ast";
import { fullAstObject, fullJditaObject, fullXditaExample } from './test-utils';
import { InMemoryTextSimpleOutputStreamCollector } from '../src/stream';
import { XditaSerializer } from '../src/xdita-serializer';

describe('xditaToAst', () => {

Expand Down Expand Up @@ -258,4 +261,40 @@ describe('jditaToAst', () => {
expect(textNode.static.nodeName).to.equal('cdata');
expect(textNode.readProp("content")).to.equal('cdata');
})
});


describe('A round trip conversion between xdita, ast, and jdita', () => {
// This test ensures that the conversion between the three formats is lossless and reversible.

const xdita = fullXditaExample;

it('converts xdita to ast', async () => {
const ast = await xditaToAst(xdita);
expect(ast).to.deep.equal(fullAstObject);
});

it('converts ast to jdita', async () => {
const ast = await xditaToAst(xdita);
const jdita = astToJdita(ast);
expect(jdita).to.deep.equal(fullJditaObject);
});

it('converts jdita to ast', async () => {
const ast = await xditaToAst(xdita);
const jdita = astToJdita(ast);
const newAst = jditaToAst(jdita);
expect(newAst).to.deep.equal(fullAstObject);
});

it('converts ast to xdita', async () => {
const ast = await xditaToAst(xdita);
const jdita = astToJdita(ast);
const newAst = jditaToAst(jdita);
const outStream = new InMemoryTextSimpleOutputStreamCollector();
const serializer = new XditaSerializer(outStream);
serializer.serialize(newAst);
const newXdita = outStream.getText();
expect(newXdita).to.equal(xdita);
});
});
117 changes: 117 additions & 0 deletions packages/lwdita-xdita/test/sample.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE topic PUBLIC "-//OASIS//DTD LIGHTWEIGHT DITA Topic//EN" "lw-topic.dtd">
<topic id="fullTopic">
<title dir="ltr" translate="no" xml:lang="english">
<b>bold</b> and <em>emphasized</em> and <i>italic</i> and <ph>Phrase content</ph> and <strong>strong</strong>
and <sub>subscript</sub> and <sup>superscipt</sup> and <tt>tele type</tt> and <u>underline</u>
<image></image>
</title>
<shortdesc>Short description of the full topic.</shortdesc>
<prolog props="metadata">
<metadata>
<othermeta name="test" content="test"/>
</metadata>
</prolog>
<body outputclass="outputclass">
<!--
((%list-blocks;)*, section*, div?)
list-blocks = p|ul|ol|dl|pre|audio|video|example|simpletable|fig|note
-->
<p>Paragraph content</p>
<ul>
<li><p>Unordered list item</p></li>
</ul>
<ol>
<li><p>Ordered list item</p></li>
</ol>
<dl>
<dlentry>
<dt>Definition term</dt>
<dd><p>Definition description</p></dd>
</dlentry>
</dl>
<pre>Preformatted content</pre>
<audio autoplay="false"
controls="true"
loop="false"
muted="false">
<!--
((desc)?,(fallback)?,(media-source)*,(media-track)*)*
-->
<desc>Theme song for the LwDITA podcast</desc>
<fallback><p>The theme song is not available.</p></fallback>
<media-source href="theme-song.mp3"/>
<media-track srclang="en" href="theme-song.vtt"/>
</audio>
<video height="300px" width="400px" loop="false"
muted="false">
<!--
((desc)?,(fallback)?,(video-poster)?,(media-source)*,(media-track)*)* >
-->
<desc>Video about the Sensei Sushi promise.</desc>
<fallback>
<image href="video-not-available.png">
<alt>This video cannot be displayed.</alt>
</image>
</fallback>
<video-poster href="sensei-video.jpg"/>
<media-source href="sensei-video.mp4"/>
<media-source href="sensei-video.ogg"/>
<media-source href="sensei-video.webm"/>
<media-track srclang="en" href="sensei-video.vtt"/>
</video>
<example>
<title>title</title>
<!--
p|ul|ol|dl|pre|audio|video|simpletable|fig|note
-->

</example>
<simpletable>
<title>Table title</title>
<sthead>
<stentry><p>Header 1</p></stentry>
<stentry><p>Header 2</p></stentry>
</sthead>
<strow>
<stentry><p>Row 1, Cell 1</p></stentry>
<stentry><p>Row 1, Cell 2</p></stentry>
</strow>
<strow>
<stentry><p>Row 2, Cell 1</p></stentry>
<stentry><p>Row 2, Cell 2</p></stentry>
</strow>
</simpletable>
<fig>
<!--
(title?, desc?, (%fig-blocks;|image|xref)*)
-->
<title>Figure title</title>
<desc>Figure description</desc>
<image href="images/image.png"><alt>alt text</alt></image>
</fig>
<note type="note">
<!--
<!ENTITY % simple-blocks "p|ul|ol|dl|pre|audio|video|example|note">
-->
<p>Note content</p>
</note>
<section>
<title>Section title</title>
<!--
p|ul|ol|dl|pre|audio|video|example|simpletable|fig|note
-->
<p>Section content</p>
</section>
<div>
<!--
p|ul|ol|dl|pre|audio|video|example|simpletable|fig|note
-->
<fn id="footnote">
<!--
"p|ul|ol|dl"
-->
</fn>
</div>
</body>
</topic>
Loading

0 comments on commit c5f5d80

Please sign in to comment.