BREAKING CHANGE(core): Implement custom XmlBuilder, remove xmlbuilder2, upgrade fast-xml-parser, update SmartXml API, tests and CI
This commit is contained in:
69
test/test.ts
69
test/test.ts
@@ -11,11 +11,11 @@ tap.test('should create an instance', async () => {
|
||||
tap.test('should create an xml string', async () => {
|
||||
const xmlResult = testSmartxml.createXmlFromObject({
|
||||
hello: {
|
||||
"@_xlmns:teststring": "hellothere",
|
||||
"@_xlmns:testnumber": 10,
|
||||
'@_xlmns:teststring': 'hellothere',
|
||||
'@_xlmns:testnumber': 10,
|
||||
wow: 'test',
|
||||
url: [{loc: 3},{loc: 3}]
|
||||
}
|
||||
url: [{ loc: 3 }, { loc: 3 }],
|
||||
},
|
||||
});
|
||||
console.log(xmlResult);
|
||||
testXml = xmlResult;
|
||||
@@ -25,8 +25,65 @@ tap.test('should parse an yml file', async () => {
|
||||
const jsObject = testSmartxml.parseXmlToObject(testXml);
|
||||
// console.log(JSON.stringify(jsObject, null, 2));
|
||||
expect(typeof jsObject).toEqual('object');
|
||||
expect(jsObject).arrayItem(1).property('hello').arrayItem(0).property('wow').arrayItem(0).property('#text').toEqual('test');
|
||||
expect(jsObject)
|
||||
.arrayItem(1)
|
||||
.property('hello')
|
||||
.arrayItem(0)
|
||||
.property('wow')
|
||||
.arrayItem(0)
|
||||
.property('#text')
|
||||
.toEqual('test');
|
||||
});
|
||||
|
||||
// Test XmlBuilder chainable API
|
||||
tap.test('should create XML using chainable API', async () => {
|
||||
const xml = testSmartxml.create()
|
||||
.ele('root', { att: 'val' })
|
||||
.ele('foo')
|
||||
.ele('bar').txt('foobar').up()
|
||||
.up()
|
||||
.ele('baz').up()
|
||||
.up()
|
||||
.end({ prettyPrint: true });
|
||||
|
||||
tap.start();
|
||||
console.log('Chainable XML:', xml);
|
||||
expect(xml).toContain('<root');
|
||||
expect(xml).toContain('att="val"');
|
||||
expect(xml).toContain('<foo>');
|
||||
expect(xml).toContain('<bar>foobar</bar>');
|
||||
expect(xml).toContain('<baz');
|
||||
});
|
||||
|
||||
tap.test('should create XML from object using XmlBuilder', async () => {
|
||||
const obj = {
|
||||
root: {
|
||||
'@_att': 'val',
|
||||
foo: {
|
||||
bar: 'foobar'
|
||||
},
|
||||
baz: {}
|
||||
}
|
||||
};
|
||||
|
||||
const xml = testSmartxml.create(obj).end({ prettyPrint: true });
|
||||
console.log('Object-based XML:', xml);
|
||||
expect(xml).toContain('<root');
|
||||
expect(xml).toContain('att="val"');
|
||||
expect(xml).toContain('<bar>foobar</bar>');
|
||||
});
|
||||
|
||||
tap.test('should add attributes using att() method', async () => {
|
||||
const xml = testSmartxml.create()
|
||||
.ele('data')
|
||||
.att('x', 1)
|
||||
.att('y', 2)
|
||||
.txt('value')
|
||||
.up()
|
||||
.end();
|
||||
|
||||
expect(xml).toContain('x="1"');
|
||||
expect(xml).toContain('y="2"');
|
||||
expect(xml).toContain('>value</data>');
|
||||
});
|
||||
|
||||
export default tap.start();
|
||||
|
||||
Reference in New Issue
Block a user