(function (root, factory) { 'use strict'; if (typeof module === 'object' && module.exports) { // Node. Does not work with strict CommonJS, but only CommonJS-like // environments that support module.exports, like Node. factory(require('./x2js'), require('qunit-cli')); } else { // Browser globals (root is window) factory(root.X2JS, root.QUnit); } })(this, function (X2JS, QUnit) { 'use strict'; QUnit.module('Converting JavaScript objects to XML'); QUnit.test('Element with attribute', function (assert) { var js = { 'document': { 'element': { '_attribute': 'value' } } }; var x = new X2JS(); var xml = x.js2xml(js); var expected = '' + '' + ''; // Implementation does not guarantee formatting so the test is somewhat fragile. assert.strictEqual(xml, expected); }); QUnit.test('Element with attribute and selfClosingElements set to false', function (assert) { var js = { 'document': { 'element': { '_attribute': 'value' } } }; var x = new X2JS({ 'selfClosingElements': false }); var xml = x.js2xml(js); var expected = '' + '' + ''; // Implementation does not guarantee formatting so the test is somewhat fragile. assert.strictEqual(xml, expected); }); QUnit.test('Element with attribute and selfClosingElements set to true', function (assert) { var js = { 'document': { 'element': { '_attribute': 'value' } } }; var x = new X2JS({ 'selfClosingElements': true }); var xml = x.js2xml(js); var expected = '' + '' + ''; // Implementation does not guarantee formatting so the test is somewhat fragile. assert.strictEqual(xml, expected); }); QUnit.test('Element with attribute containing XML characters', function (assert) { var js = { 'document': { 'element': { '_attribute': 'va&lue<' } } }; var x = new X2JS(); var xml = x.js2xml(js); var expected = '' + '' + ''; // Implementation does not guarantee formatting so the test is somewhat fragile. assert.strictEqual(xml, expected); }); QUnit.test('Just a string', function (assert) { var js = { 'document': { 'elementY': 'hello there' } }; var x = new X2JS(); var xml = x.js2xml(js); var expected = '' + 'hello there' + ''; // Implementation does not guarantee formatting so the test is somewhat fragile. assert.strictEqual(xml, expected); }); QUnit.test('String with XML characters', function (assert) { var js = { 'document': { 'elementY': 'hello &there<' } }; var x = new X2JS(); var xml = x.js2xml(js); var expected = '' + 'hello &there<' + ''; // Implementation does not guarantee formatting so the test is somewhat fragile. assert.strictEqual(xml, expected); }); QUnit.test('CDATA', function (assert) { var js = { 'document': { 'elementZ': { '__cdata': 'hello again' } } }; var x = new X2JS(); var xml = x.js2xml(js); var expected = '' + '' + ''; // Implementation does not guarantee formatting so the test is somewhat fragile. assert.strictEqual(xml, expected); }); QUnit.test('CDATA with XML characters', function (assert) { var js = { 'document': { 'elementZ': { '__cdata': 'hello &again<' } } }; var x = new X2JS(); var xml = x.js2xml(js); var expected = '' + '' + ''; // Implementation does not guarantee formatting so the test is somewhat fragile. assert.strictEqual(xml, expected); }); QUnit.test('Empty string as value', function (assert) { var js = { 'document': { 'elementU': '' } }; var x = new X2JS(); var xml = x.js2xml(js); var expected = '' + '' + ''; // Implementation does not guarantee formatting so the test is somewhat fragile. assert.strictEqual(xml, expected); }); QUnit.test('Zero as value', function (assert) { var js = { 'document': { 'element': 0 } }; var x = new X2JS(); var xml = x.js2xml(js); var expected = '' + '0' + ''; assert.strictEqual(xml, expected); }); QUnit.test('Empty string as value with selfClosingElements set to false', function (assert) { var js = { 'document': { 'elementU': '' } }; var x = new X2JS({ 'selfClosingElements': false }); var xml = x.js2xml(js); var expected = '' + '' + ''; // Implementation does not guarantee formatting so the test is somewhat fragile. assert.strictEqual(xml, expected); }); QUnit.test('Basic array', function (assert) { var js = { 'document': { 'elementV': [ { 'x': 't' }, { 'm': 'n' } ] } }; var x = new X2JS(); var xml = x.js2xml(js); var expected = '' + 't' + 'n' + ''; // Implementation does not guarantee formatting so the test is somewhat fragile. assert.strictEqual(xml, expected); }); QUnit.test('Array of empty strings', function (assert) { var js = { 'document': { 'elementX': ['', ''] } }; var x = new X2JS(); var xml = x.js2xml(js); var expected = '' + '' + '' + ''; // Implementation does not guarantee formatting so the test is somewhat fragile. assert.strictEqual(xml, expected); }); QUnit.test('Array of empty strings with selfClosingElements set to false', function (assert) { var js = { 'document': { 'elementX': ['', ''] } }; var x = new X2JS({ 'selfClosingElements': false }); var xml = x.js2xml(js); var expected = '' + '' + '' + ''; // Implementation does not guarantee formatting so the test is somewhat fragile. assert.strictEqual(xml, expected); }); QUnit.test('Attributes in array', function (assert) { var js = { 'document': { 'elementV': [ { 'x': 't', '_a': 'a' }, { 'm': 'n', '_b': 'b' } ] } }; var x = new X2JS(); var xml = x.js2xml(js); var expected = '' + 't' + 'n' + ''; // Implementation does not guarantee formatting so the test is somewhat fragile. assert.strictEqual(xml, expected); }); QUnit.test('Falsey element values + attributes', function (assert) { var js = { 'document': { 'elementV': [ { 'm': { '__text': 'n', '_a': 'ns' } }, { 'm': { '__text': 0, '_a': 'ns' } }, { 'm': { '__text': false, '_a': 'ns' } } ] } }; var x = new X2JS(); var xml = x.js2xml(js); var expected = '' + 'n' + '0' + 'false' + ''; // Implementation does not guarantee formatting so the test is somewhat fragile. assert.strictEqual(xml, expected); }); QUnit.test('Namespaces', function (assert) { var js = { 'document': { '__prefix': 'ns', '_ns:xmlns': 'http://example.com', 'elementV': [ { '__prefix': 'ns', 'x': 't', '_a': 'a' }, { 'm': { '__text': 'n', '__prefix': 'ns' }, '_b': 'b' } ] } }; var x = new X2JS(); var xml = x.js2xml(js); var expected = '' + 't' + 'n' + ''; // Implementation does not guarantee formatting so the test is somewhat fragile. assert.strictEqual(xml, expected); }); QUnit.test('Filter out', function (assert) { var js = { 'document': { 'elementV': [ { 'x': 't' }, { 'password': 'n' } ], 'password': 'n' } }; var x = new X2JS({ 'jsAttributeFilter': function (name, value) { return name === 'password'; } }); var xml = x.js2xml(js); var expected = '' + 't' + '' + ''; // Implementation does not guarantee formatting so the test is somewhat fragile. assert.strictEqual(xml, expected); }); QUnit.test('Attribute converter', function (assert) { var js = { 'document': { 'elementV': [ { 'x': 't' }, { 'password': 'n' } ], 'password': 'n' } }; var x = new X2JS({ 'jsAttributeConverter': function (name, value) { return name === 'password' ? '***' : value; } }); var xml = x.js2xml(js); var expected = '' + 't' + '***' + '***' + ''; // Implementation does not guarantee formatting so the test is somewhat fragile. assert.strictEqual(xml, expected); }); QUnit.test('UTC dates', function (assert) { var date = new Date(); var js = { 'date': date }; var x = new X2JS({ 'jsDateUTC': true }); var xml = x.js2xml(js); var expected = '' + date.toUTCString() + ''; // Implementation does not guarantee formatting so the test is somewhat fragile. assert.strictEqual(xml, expected); }); QUnit.test('ISO dates', function (assert) { var date = new Date(); var js = { 'date': date }; var x = new X2JS(); var xml = x.js2xml(js); var expected = '' + date.toISOString() + ''; // Implementation does not guarantee formatting so the test is somewhat fragile. assert.strictEqual(xml, expected); }); });