import test from 'ava'; import xml from '../lib/xml'; test('no elements', t => { t.is(xml(), ''); t.is(xml([]), ''); t.is(xml('test'), 'test'); t.is(xml('scotch & whisky'), 'scotch & whisky'); t.is(xml('bob\'s escape character'), 'bob's escape character'); }); test('simple options', t => { t.is(xml([{a: {}}]), ''); t.is(xml([{a: null}]), ''); t.is(xml([{a: []}]), ''); t.is(xml([{a: -1}]), '-1'); t.is(xml([{a: false}]), 'false'); t.is(xml([{a: 'test'}]), 'test'); t.is(xml({a: {}}), ''); t.is(xml({a: null}), ''); t.is(xml({a: []}), ''); t.is(xml({a: -1}), '-1'); t.is(xml({a: false}), 'false'); t.is(xml({a: 'test'}), 'test'); t.is(xml([{a: 'test'}, {b: 123}, {c: -0.5}]), 'test123-0.5'); }); test('deeply nested objects', t => { t.is(xml([{a: [{b: [{c: 1}, {c: 2}, {c: 3}]}]}]), '123'); }); test('indents property', t => { t.is(xml([{a: [{b: [{c: 1}, {c: 2}, {c: 3}]}]}], true), '\n \n 1\n 2\n 3\n \n'); t.is(xml([{a: [{b: [{c: 1}, {c: 2}, {c: 3}]}]}], ' '), '\n \n 1\n 2\n 3\n \n'); t.is(xml([{a: [{b: [{c: 1}, {c: 2}, {c: 3}]}]}], '\t'), '\n\t\n\t\t1\n\t\t2\n\t\t3\n\t\n'); t.is(xml({guid: [{_attr: {premalink: true}}, 'content']}, true), 'content'); }); test('supports xml attributes', t => { t.is(xml([{b: {_attr: {}}}]), ''); t.is(xml([{ a: { _attr: { attribute1: 'some value', attribute2: 12345 } } }]), ''); t.is(xml([{ a: [{ _attr: { attribute1: 'some value', attribute2: 12345 } }] }]), ''); t.is(xml([{ a: [{ _attr: { attribute1: 'some value', attribute2: 12345 } }, 'content'] }]), 'content'); }); test('supports cdata', t => { t.is(xml([{a: {_cdata: 'This is some CDATA'}}]), 'CDATA]]>'); t.is(xml([{ a: { _attr: {attribute1: 'some value', attribute2: 12345}, _cdata: 'This is some CDATA' } }]), 'CDATA]]>'); t.is(xml([{a: {_cdata: 'This is some CDATA with ]]> and then again ]]>'}}]), 'CDATA with ]]]]> and then again ]]]]>]]>'); }); test('supports encoding', t => { t.is(xml([{ a: [{ _attr: { anglebrackets: 'this is strong', url: 'http://google.com?s=opower&y=fun' } }, 'text'] }]), 'text'); }); test('supports stream interface', t => { const elem = xml.element({_attr: {decade: '80s', locale: 'US'}}); const xmlStream = xml({toys: elem}, {stream: true}); const results = ['', 'Transformers', 'He-man', 'GI Joe', '']; elem.push({toy: 'Transformers'}); elem.push({toy: [{name: 'He-man'}]}); elem.push({toy: 'GI Joe'}); elem.close(); xmlStream.on('data', stanza => { t.is(stanza, results.shift()); }); return new Promise( (resolve, reject) => { xmlStream.on('close', () => { t.same(results, []); resolve(); }); xmlStream.on('error', reject); }); }); test('streams end properly', t => { const elem = xml.element({ _attr: { decade: '80s', locale: 'US'} }); const xmlStream = xml({ toys: elem }, { stream: true }); let gotData; t.plan(7); elem.push({ toy: 'Transformers' }); elem.push({ toy: 'GI Joe' }); elem.push({ toy: [{name:'He-man'}] }); elem.close(); xmlStream.on('data', data => { t.ok(data); gotData = true; }); xmlStream.on('end', () => { t.ok(gotData); }); return new Promise( (resolve, reject) => { xmlStream.on('close', () => { t.ok(gotData); resolve(); }); xmlStream.on('error', reject); }); }); test('xml declaration options', t => { t.is(xml([{a: 'test'}], {declaration: true}), 'test'); t.is(xml([{a: 'test'}], {declaration: {encoding: 'foo'}}), 'test'); t.is(xml([{a: 'test'}], {declaration: {standalone: 'yes'}}), 'test'); t.is(xml([{a: 'test'}], {declaration: false}), 'test'); t.is(xml([{a: 'test'}], {declaration: true, indent: '\n'}), '\ntest'); t.is(xml([{a: 'test'}], {}), 'test'); });