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 2test 3 test 4`; 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 = ``; 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 a minimum example. Broken, phenomenom? It is.`; let text2 = `I am a minimum example.Broken, phenomenom?It is.`; 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`; 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 = 'Test'; 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 world!'); let result = node.toString(true); expect(typeof result).to.equal('string'); let expectedOutput = `\n` + ` \n` + ` \n` + ` \n` + ` \n` + ` \n` + ''; expect(result).to.equal(expectedOutput); }); }); describe('.tagName', () => { it('should return the name of the XML tag', () => { let node1 = Parser.parseXML('world!'); let result1 = node1.children[0].tagName; expect(result1).to.equal('foo'); let node2 = Parser.parseXML(''); let result2 = node2.children[0].tagName; expect(result2).to.equal('bar'); let node3 = Parser.parseXML(''); 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('world!'); 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); }); }); }); }); });