var jstoxml = require('./jstoxml.js'); (function(){ var tests = []; var results = { testsRun: 0, pass: 0, fail: 0 }; var addTest = function(obj){ tests.push(obj); }; var showReport = function(){ console.log(results); }; var passFail = function(test){ var output; try { output = test.input(); results.testsRun++; if(output !== test.expectedOutput){ console.log('OUTPUT:\n' + output + '\n\nEXPECTED OUTPUT:\n' + test.expectedOutput); console.log(test.name + ' \033[1;31mFAILED\033[0m\n ' + results.testsRun + '/' + tests.length); results.fail++; } else { console.log(test.name + ' passed. ' + results.testsRun + '/' + tests.length); results.pass++; } } catch(e) { console.log(test.name + ' failed. ' + results.testsRun + '/' + tests.length); console.log(e); } }; var runTests = function(){ for(var i=0, len=tests.length; ibarbar2' }); addTest({ name: 'array-advanced', input: function(){ return jstoxml.toXML([ {foo: 'bar'}, {foo2: 'bar2'} ]); }, expectedOutput: 'barbar2' }); addTest({ name: 'object', input: function(){ return jstoxml.toXML({ foo: 'bar', foo2: 'bar2' }); }, expectedOutput: 'barbar2' }); addTest({ name: 'object-xml-header', input: function(){ return jstoxml.toXML({ foo: 'bar', foo2: 'bar2' }, {header: true}); }, expectedOutput: '\nbarbar2' }); addTest({ name: 'object-with-tabs', input: function(){ return jstoxml.toXML({ a: { foo: 'bar', foo2: 'bar2' } }, {header: false, indent: ' '}); }, expectedOutput: '\n bar\n bar2\n\n' }); addTest({ name: 'object-with-attributes', input: function(){ return jstoxml.toXML({ _name: 'a', _attrs: { foo: 'bar', foo2: 'bar2' } }); }, expectedOutput: '' }); addTest({ name: 'object-with-attributes2', input: function(){ return jstoxml.toXML({ _name: 'a', _attrs: { foo: 'bar', foo2: 'bar2' }, _content: 'la dee da' }); }, expectedOutput: 'la dee da' }); addTest({ name: 'object-mixed', input: function(){ return jstoxml.toXML({ 'blah': null, foo: 'bar', 'more blah': null, bar: 0, 'more more blah': null, baz: false }); }, expectedOutput: 'blahbarmore blah0more more blahfalse' }); addTest({ name: 'object-nested', input: function(){ return jstoxml.toXML({ a: { foo: 'bar', foo2: 'bar2' } }); }, expectedOutput: 'barbar2' }); addTest({ name: 'object-deep-nesting', input: function(){ return jstoxml.toXML({ a: { b: { c: { d: { e: { f: { g: { h: { i: { j: { k: { l: { m: { foo: 'bar' } } } } } } } } } } } } } }); }, expectedOutput: 'bar' }); addTest({ name: 'entities', input: function(){ return jstoxml.toXML({ foo: '', bar: '"b"', baz: '\'&whee\'' }, { filter: { '<': '<', '>': '>', '"': '"', '\'': ''', '&': '&' } }); }, expectedOutput: '<a>"b"'&whee'' }); addTest({ name: 'example1-simple-object', input: function(){ return jstoxml.toXML({ foo: 'bar', foo2: 'bar2' }); }, expectedOutput: 'barbar2' }); addTest({ name: 'example2-simple-array', input: function(){ return jstoxml.toXML([ {foo: 'bar'}, {foo2: 'bar2'} ]); }, expectedOutput: 'barbar2' }); addTest({ name: 'example3-duplicate-tag-names', input: function(){ return jstoxml.toXML([ {foo: 'bar'}, {foo: 'bar2'} ]); }, expectedOutput: 'barbar2' }); addTest({ name: 'example4-attributes', input: function(){ return jstoxml.toXML({ _name: 'foo', _content: 'bar', _attrs: { a: 'b', c: 'd' } }); }, expectedOutput: 'bar' }); addTest({ name: 'example5-tags-with-text-nodes', input: function(){ return jstoxml.toXML({ 'text1': null, foo: 'bar', 'text2': null }); }, expectedOutput: 'text1bartext2' }); addTest({ name: 'example6-nested-tags-with-indenting', input: function(){ return jstoxml.toXML({ a: { foo: 'bar', foo2: 'bar2' } }, {header: false, indent: ' '}); }, expectedOutput: '\n bar\n bar2\n\n' }); addTest({ name: 'example7-nested-tags-with-attributes', input: function(){ return jstoxml.toXML({ ooo: { _name: 'foo', _attrs: { a: 'b' }, _content: { _name: 'bar', _attrs: { c: 'd' } } } }, {header: false, indent: ' '}); }, expectedOutput: '\n \n \n \n\n' }); addTest({ name: 'example7b', input: function(){ var bar = { _name: 'bar', _attrs: { c: 'd' } }; var foo = { _name: 'foo', _attrs: { a: 'b' }, _content: bar }; return jstoxml.toXML({ ooo: foo }, {header: false, indent: ' '}); }, expectedOutput: '\n \n \n \n\n' }); addTest({ name: 'example8-functions', input: function(){ return jstoxml.toXML({ onePlusTwo: function(){ return 1 + 2; } }); }, expectedOutput: '3' }); addTest({ name: 'example9-rss-feed', input: function(){ return jstoxml.toXML({ _name: 'rss', _attrs: { version: '2.0' }, _content: { channel: [ {title: 'RSS Example'}, {description: 'Description'}, {link: 'google.com'}, {lastBuildDate: function(){ return 'Sat Jul 30 2011 18:14:25 GMT+0900 (JST)'; }}, {pubDate: function(){ return 'Sat Jul 30 2011 18:14:25 GMT+0900 (JST)'; }}, {language: 'en'}, {item: { title: 'Item title', link: 'Item link', description: 'Item Description', pubDate: function(){ return 'Sat Jul 30 2011 18:33:47 GMT+0900 (JST)'; } }}, {item: { title: 'Item2 title', link: 'Item2 link', description: 'Item2 Description', pubDate: function(){ return 'Sat Jul 30 2011 18:33:47 GMT+0900 (JST)'; } }} ] } }, {header: true, indent: ' '}); }, expectedOutput: '\n\n \n RSS Example\n Description\n google.com\n Sat Jul 30 2011 18:14:25 GMT+0900 (JST)\n Sat Jul 30 2011 18:14:25 GMT+0900 (JST)\n en\n \n Item title\n Item link\n Item Description\n Sat Jul 30 2011 18:33:47 GMT+0900 (JST)\n \n \n Item2 title\n Item2 link\n Item2 Description\n Sat Jul 30 2011 18:33:47 GMT+0900 (JST)\n \n \n\n' }); addTest({ name: 'example10-podcast', input: function(){ return jstoxml.toXML({ _name: 'rss', _attrs: { 'xmlns:itunes': 'http://www.itunes.com/dtds/podcast-1.0.dtd', version: '2.0' }, _content: { channel: [ {title: 'Title'}, {link: 'google.com'}, {language: 'en-us'}, {copyright: 'Copyright 2011'}, {'itunes:subtitle': 'Subtitle'}, {'itunes:author': 'Author'}, {'itunes:summary': 'Summary'}, {description: 'Description'}, {'itunes:owner': { 'itunes:name': 'Name', 'itunes:email': 'Email' }}, { _name: 'itunes:image', _attrs: { href: 'image.jpg' } }, { _name: 'itunes:category', _attrs: { text: 'Technology' }, _content: { _name: 'itunes:category', _attrs: { text: 'Gadgets' } } }, { _name: 'itunes:category', _attrs: { text: 'TV & Film' } }, { item: [ {title: 'Podcast Title'}, {'itunes:author': 'Author'}, {'itunes:subtitle': 'Subtitle'}, {'itunes:summary': 'Summary'}, {'itunes:image': 'image.jpg'}, { _name: 'enclosure', _attrs: { url: 'http://example.com/podcast.m4a', length: '8727310', type: 'audio/x-m4a' } }, {guid: 'http://example.com/archive/aae20050615.m4a'}, {pubDate: 'Wed, 15 Jun 2011 19:00:00 GMT'}, {'itunes:duration': '7:04'}, {'itunes:keywords': 'salt, pepper, shaker, exciting'} ] }, { item: [ {title: 'Podcast2 Title'}, {'itunes:author': 'Author2'}, {'itunes:subtitle': 'Subtitle2'}, {'itunes:summary': 'Summary2'}, {'itunes:image': 'image2.jpg'}, { _name: 'enclosure', _attrs: { url: 'http://example.com/podcast2.m4a', length: '655555', type: 'audio/x-m4a' } }, {guid: 'http://example.com/archive/aae2.m4a'}, {pubDate: 'Wed, 15 Jul 2011 19:00:00 GMT'}, {'itunes:duration': '11:20'}, {'itunes:keywords': 'foo, bar'} ] } ] } }, {header: true, indent: ' '}); }, expectedOutput: '\n\n \n Title\n google.com\n en-us\n Copyright 2011\n Subtitle\n Author\n Summary\n Description\n \n Name\n Email\n \n \n \n \n \n \n \n Podcast Title\n Author\n Subtitle\n Summary\n image.jpg\n \n http://example.com/archive/aae20050615.m4a\n Wed, 15 Jun 2011 19:00:00 GMT\n 7:04\n salt, pepper, shaker, exciting\n \n \n Podcast2 Title\n Author2\n Subtitle2\n Summary2\n image2.jpg\n \n http://example.com/archive/aae2.m4a\n Wed, 15 Jul 2011 19:00:00 GMT\n 11:20\n foo, bar\n \n \n\n' }); addTest({ name: 'bug3', input: function(){ return jstoxml.toXML({ foo: true, bar: '', foo2: false, ok: 'This is ok', ok2: 'false', ok3: 'true' }); }, expectedOutput: 'truefalseThis is okfalsetrue' }); addTest({ name: 'bug4a', input: function(){ return jstoxml.toXML({ foo: 4, bar: '&' }); }, expectedOutput: '4&' }); addTest({ name: 'bug4b', input: function(){ return jstoxml.toXML({ foo: '&' }, { filter: { '&': '&' } }); }, expectedOutput: '&' }); addTest({ name: 'headerNone', input: function(){ return jstoxml.toXML({ foo: 4 }, { header: false }); }, expectedOutput: '4' }); addTest({ name: 'headerDefault', input: function(){ return jstoxml.toXML({ foo: 4 }, { header: true }); }, expectedOutput: '\n4' }); addTest({ name: 'headerCustom', input: function(){ return jstoxml.toXML({ foo: 4 }, { header: '\n' }); }, expectedOutput: '\n4' }); addTest({ name: 'nested-elements-with-self-closing-sibling', input: function(){ return jstoxml.toXML({ people: { students: [ { student: { name: 'Joe' } }, { student: { name: 'Jane' } } ], teacher: { _selfCloseTag: true, _attrs: { 'name': 'Yoda' } } } }); }, expectedOutput: [ '', '', 'Joe', 'Jane', '', '', '' ].join('') }); runTests(); showReport(); // Exit with 1 to signal a failed build if(results.fail > 0) { process.exit(1); } })();