Files
Zos/Skills/@be/node_modules/jibo-node-xml/tests/index.js

112 lines
5.2 KiB
JavaScript

const Node = require('../').default;
const { NodeType, Parser } = require('../');
describe('packages', () => {
describe('jibo-node-xml', () => {
it('Load library', function () {
let pack = require('../package.json');
let lib = require('../' + pack.main);
assert.equal(lib.default, Node);
});
describe('#parseXML', function() {
it('should always return a root node even if no children.', () => {
let text = '';
let result = Parser.parseXML(text);
expect(result instanceof Node).to.be.true;
expect(result.type).to.equal(NodeType.ROOT);
});
it('should interpret XML nodes that have more than 1 parent', () => {
let text = `test 1 test 2<bla>test 3 <bla1> test 4</bla1></bla>`;
let node = Parser.parseXML(text);
expect(node.children.length).to.equal(2);
expect(node.children[0].type).to.equal('TEXT');
expect(node.children[0].value).to.equal('test 1 test 2');
expect(node.children[1].type).to.equal('bla');
expect(node.children[1].children.length).to.equal(2);
expect(node.children[1].children[0].value).to.equal('test 3 ');
expect(node.children[1].children[1].type).to.equal('bla1');
expect(node.children[1].children[1].children[0].value).to.equal(' test 4');
});
it('should properly store tag attributes in `.att` property', () => {
let text = `<bla test="20"></bla>`;
let node = Parser.parseXML(text);
expect(node.children[0].att.get('test')).to.equal('20');
});
it('Should not care about white spaces between xml tags', () => {
let text1 = `I am <anim cat="1">a minimum example.</anim> <anim cat="2">Broken, phenomenom?</anim> <anim cat="3">It is.</anim>`;
let text2 = `I am <anim cat="1">a minimum example.</anim><anim cat="2">Broken, phenomenom?</anim><anim cat="3">It is.</anim>`;
let node1 = Parser.parseXML(text1);
let node2 = Parser.parseXML(text2);
expect(node1.toString(true)).to.equal(node2.toString(true));
});
it('should properly parse self closing XML tags', () => {
let text = `bla bla<bla/> bla bla`;
let node = Parser.parseXML(text);
expect(node.children.length).to.equal(3);
expect(node.children[0].value).to.equal('bla bla');
expect(node.children[2].value).to.equal(' bla bla');
expect(node.children[1].type).to.equal('bla');
expect(node.children[1].children.length).to.equal(0);
});
it('Should handle tag names with dashes', () => {
let text = '<say-as type="spell"">Test</say-as>';
let node = Parser.parseXML(text);
expect(node.children[0].type).to.equal('say-as');
});
describe('#toString', () => {
it('should print a string representation of the tree.', () => {
let node = Parser.parseXML('hello <emphasis>world</emphasis>!');
let result = node.toString(true);
expect(typeof result).to.equal('string');
let expectedOutput =
`<ROOT>\n` +
` <TEXT value="hello"/>\n` +
` <emphasis>\n` +
` <TEXT value="world"/>\n` +
` </emphasis>\n` +
` <TEXT value="!"/>\n` +
'</ROOT>';
expect(result).to.equal(expectedOutput);
});
});
describe('.tagName', () => {
it('should return the name of the XML tag', () => {
let node1 = Parser.parseXML('<foo>world</foo>!');
let result1 = node1.children[0].tagName;
expect(result1).to.equal('foo');
let node2 = Parser.parseXML('<bar/>');
let result2 = node2.children[0].tagName;
expect(result2).to.equal('bar');
let node3 = Parser.parseXML('<baz:hello/>');
let result3 = node3.children[0].tagName;
expect(result3).to.equal('baz');
});
});
describe('.getAttribute()', () => {
it('should return the attribes of XML tag', () => {
let root = Parser.parseXML('<foo a="bla" b=30>world</foo>!');
let foo = root.children[0];
expect(foo.getAttribute('a')).to.equal('bla');
expect(foo.getAttribute('b')).to.equal('30');
expect(foo.getAttribute('b')).to.not.equal(30);
expect(foo.getNumberAtt('b')).to.equal(30);
foo.modifyAttribute('b', (old) => Number.parseFloat(old));
expect(foo.getAttribute('b')).to.equal(30);
});
});
});
});
});