Skip to content

Commit

Permalink
Add unit tests for tXml.js (#4437)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsilhavy committed Mar 22, 2024
1 parent dbdd14c commit ff66d15
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 5 deletions.
10 changes: 5 additions & 5 deletions externals/tXml.js
Expand Up @@ -52,19 +52,19 @@ export const XML_ENTITIES = {

/**
* Translates XML predefined entities and character references to their respective characters.
* @param {Object} entitiesList
* @param {String} str
* @param {Object} entitiesList
* @param {String} str
* @returns {String}
*/
function translateEntitiesAndCharacterReferences(entitiesList, str) {
export function translateEntitiesAndCharacterReferences(entitiesList, str) {
const entitySplit = str.split(/(&[#a-zA-Z0-9]+;)/);
if (entitySplit.length <= 1) { // No entities. Skip the rest of the function.
return str;
}

for (let i = 1; i < entitySplit.length; i += 2) {
const reference = entitySplit[i];

/*
* Check if it is a character reference of the form
* /&#[0-9]+;/ - Encoded in decimal, or
Expand All @@ -83,7 +83,7 @@ function translateEntitiesAndCharacterReferences(entitiesList, str) {
if (!isNaN(code) && code >= 0 && code <= 0x10FFFF) {
entitySplit[i] = String.fromCodePoint(code);
}
}
}
/*
* Translate entity references using a dictionary.
*/
Expand Down
62 changes: 62 additions & 0 deletions test/unit/externals.tXml.js
@@ -0,0 +1,62 @@
import {expect} from 'chai';
import {translateEntitiesAndCharacterReferences, XML_ENTITIES} from '../../externals/tXml.js'

describe('tXml', () => {

describe('translateEntitiesAndCharacterReferences', () => {

it('should not change string without XML entities', () => {
expect(translateEntitiesAndCharacterReferences(XML_ENTITIES, 'PT634')).to.be.equal('PT634')
})

it('should translate general entity amp', () => {
expect(translateEntitiesAndCharacterReferences(XML_ENTITIES, 'PT634&amp;')).to.be.equal('PT634&')
})

it('should translate general entity lt', () => {
expect(translateEntitiesAndCharacterReferences(XML_ENTITIES, 'PT634&lt;')).to.be.equal('PT634<')
})

it('should translate general entity gt', () => {
expect(translateEntitiesAndCharacterReferences(XML_ENTITIES, 'PT634&gt;')).to.be.equal('PT634>')
})

it('should translate general entity apos', () => {
expect(translateEntitiesAndCharacterReferences(XML_ENTITIES, 'PT634&apos;')).to.be.equal('PT634\'')
})

it('should translate general entity quot', () => {
expect(translateEntitiesAndCharacterReferences(XML_ENTITIES, 'PT634&quot;')).to.be.equal('PT634"')
})

it('should translate all general entities', () => {
const input = '&amp; &gt; &lt; &quot; &apos;';
const expectedOutput = '& > < " \'';
expect(translateEntitiesAndCharacterReferences(XML_ENTITIES, input)).to.be.equal(expectedOutput)
})

it('should correctly translate decimal character references', () => {
const input = '&#65; &#66; &#67;';
const expectedOutput = 'A B C';
expect(translateEntitiesAndCharacterReferences(XML_ENTITIES, input)).to.be.equal(expectedOutput)
})

it('should correctly translate hexadecimal character references', () => {
const input = '&#x41; &#x42; &#x43;';
const expectedOutput = 'A B C';
expect(translateEntitiesAndCharacterReferences(XML_ENTITIES, input)).to.be.equal(expectedOutput)
})

it('should correctly translate character references mixed with other text', () => {
const input = 'This is a test: &#65; &#66; &#67;';
const expectedOutput = 'This is a test: A B C';
expect(translateEntitiesAndCharacterReferences(XML_ENTITIES, input)).to.be.equal(expectedOutput)
})

it('should correctly translate character references mixed with general entities and other text', () => {
const input = 'This is a test: &#65; &#66; &#67; &amp; &gt; &lt; &quot; &apos;';
const expectedOutput = 'This is a test: A B C & > < " \'';
expect(translateEntitiesAndCharacterReferences(XML_ENTITIES, input)).to.be.equal(expectedOutput)
})
})
})

0 comments on commit ff66d15

Please sign in to comment.