123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643 |
- 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; i<len; i++){
- passFail(tests[i]);
- }
- };
-
- addTest({
- name: 'string',
- input: function(){
- return jstoxml.toXML('foo');
- },
- expectedOutput: 'foo'
- });
- addTest({
- name: 'boolean',
- input: function(){
- return jstoxml.toXML(true);
- },
- expectedOutput: 'true'
- });
-
- addTest({
- name: 'number',
- input: function(){
- return jstoxml.toXML(5);
- },
- expectedOutput: '5'
- });
-
- addTest({
- name: 'function',
- input: function(){
- return jstoxml.toXML(function(){
- return 999;
- });
- },
- expectedOutput: '999'
- });
-
- addTest({
- name: 'array',
- input: function(){
- return jstoxml.toXML([
- {foo: 'bar'},
- {foo2: 'bar2'}
- ]);
- },
- expectedOutput: '<foo>bar</foo><foo2>bar2</foo2>'
- });
-
- addTest({
- name: 'array-advanced',
- input: function(){
- return jstoxml.toXML([
- {foo: 'bar'},
- {foo2: 'bar2'}
- ]);
- },
- expectedOutput: '<foo>bar</foo><foo2>bar2</foo2>'
- });
-
- addTest({
- name: 'object',
- input: function(){
- return jstoxml.toXML({
- foo: 'bar',
- foo2: 'bar2'
- });
- },
- expectedOutput: '<foo>bar</foo><foo2>bar2</foo2>'
- });
-
- addTest({
- name: 'object-xml-header',
- input: function(){
- return jstoxml.toXML({
- foo: 'bar',
- foo2: 'bar2'
- }, {header: true});
- },
- expectedOutput: '<?xml version="1.0" encoding="UTF-8"?>\n<foo>bar</foo><foo2>bar2</foo2>'
- });
-
- addTest({
- name: 'object-with-tabs',
- input: function(){
- return jstoxml.toXML({
- a: {
- foo: 'bar',
- foo2: 'bar2'
- }
- }, {header: false, indent: ' '});
- },
- expectedOutput: '<a>\n <foo>bar</foo>\n <foo2>bar2</foo2>\n</a>\n'
- });
-
- addTest({
- name: 'object-with-attributes',
- input: function(){
- return jstoxml.toXML({
- _name: 'a',
- _attrs: {
- foo: 'bar',
- foo2: 'bar2'
- }
- });
- },
- expectedOutput: '<a foo="bar" foo2="bar2"/>'
- });
-
- addTest({
- name: 'object-with-attributes2',
- input: function(){
- return jstoxml.toXML({
- _name: 'a',
- _attrs: {
- foo: 'bar',
- foo2: 'bar2'
- },
- _content: 'la dee da'
- });
- },
- expectedOutput: '<a foo="bar" foo2="bar2">la dee da</a>'
- });
-
- 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: 'blah<foo>bar</foo>more blah<bar>0</bar>more more blah<baz>false</baz>'
- });
-
- addTest({
- name: 'object-nested',
- input: function(){
- return jstoxml.toXML({
- a: {
- foo: 'bar',
- foo2: 'bar2'
- }
- });
- },
- expectedOutput: '<a><foo>bar</foo><foo2>bar2</foo2></a>'
- });
-
- 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: '<a><b><c><d><e><f><g><h><i><j><k><l><m><foo>bar</foo></m></l></k></j></i></h></g></f></e></d></c></b></a>'
- });
- addTest({
- name: 'entities',
- input: function(){
- return jstoxml.toXML({
- foo: '<a>',
- bar: '"b"',
- baz: '\'&whee\''
- },
- {
- filter: {
- '<': '<',
- '>': '>',
- '"': '"',
- '\'': ''',
- '&': '&'
- }
- });
- },
- expectedOutput: '<foo><a></foo><bar>"b"</bar><baz>'&whee'</baz>'
- });
-
- addTest({
- name: 'example1-simple-object',
- input: function(){
- return jstoxml.toXML({
- foo: 'bar',
- foo2: 'bar2'
- });
- },
- expectedOutput: '<foo>bar</foo><foo2>bar2</foo2>'
- });
-
- addTest({
- name: 'example2-simple-array',
- input: function(){
- return jstoxml.toXML([
- {foo: 'bar'},
- {foo2: 'bar2'}
- ]);
- },
- expectedOutput: '<foo>bar</foo><foo2>bar2</foo2>'
- });
-
- addTest({
- name: 'example3-duplicate-tag-names',
- input: function(){
- return jstoxml.toXML([
- {foo: 'bar'},
- {foo: 'bar2'}
- ]);
- },
- expectedOutput: '<foo>bar</foo><foo>bar2</foo>'
- });
-
- addTest({
- name: 'example4-attributes',
- input: function(){
- return jstoxml.toXML({
- _name: 'foo',
- _content: 'bar',
- _attrs: {
- a: 'b',
- c: 'd'
- }
- });
- },
- expectedOutput: '<foo a="b" c="d">bar</foo>'
- });
-
- addTest({
- name: 'example5-tags-with-text-nodes',
- input: function(){
- return jstoxml.toXML({
- 'text1': null,
- foo: 'bar',
- 'text2': null
- });
- },
- expectedOutput: 'text1<foo>bar</foo>text2'
- });
-
- addTest({
- name: 'example6-nested-tags-with-indenting',
- input: function(){
- return jstoxml.toXML({
- a: {
- foo: 'bar',
- foo2: 'bar2'
- }
- }, {header: false, indent: ' '});
- },
- expectedOutput: '<a>\n <foo>bar</foo>\n <foo2>bar2</foo2>\n</a>\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: '<ooo>\n <foo a="b">\n <bar c="d"/>\n </foo>\n</ooo>\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: '<ooo>\n <foo a="b">\n <bar c="d"/>\n </foo>\n</ooo>\n'
- });
-
- addTest({
- name: 'example8-functions',
- input: function(){
- return jstoxml.toXML({
- onePlusTwo: function(){
- return 1 + 2;
- }
- });
- },
- expectedOutput: '<onePlusTwo>3</onePlusTwo>'
- });
-
- 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: '<?xml version="1.0" encoding="UTF-8"?>\n<rss version="2.0">\n <channel>\n <title>RSS Example</title>\n <description>Description</description>\n <link>google.com</link>\n <lastBuildDate>Sat Jul 30 2011 18:14:25 GMT+0900 (JST)</lastBuildDate>\n <pubDate>Sat Jul 30 2011 18:14:25 GMT+0900 (JST)</pubDate>\n <language>en</language>\n <item>\n <title>Item title</title>\n <link>Item link</link>\n <description>Item Description</description>\n <pubDate>Sat Jul 30 2011 18:33:47 GMT+0900 (JST)</pubDate>\n </item>\n <item>\n <title>Item2 title</title>\n <link>Item2 link</link>\n <description>Item2 Description</description>\n <pubDate>Sat Jul 30 2011 18:33:47 GMT+0900 (JST)</pubDate>\n </item>\n </channel>\n</rss>\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: '<?xml version="1.0" encoding="UTF-8"?>\n<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">\n <channel>\n <title>Title</title>\n <link>google.com</link>\n <language>en-us</language>\n <copyright>Copyright 2011</copyright>\n <itunes:subtitle>Subtitle</itunes:subtitle>\n <itunes:author>Author</itunes:author>\n <itunes:summary>Summary</itunes:summary>\n <description>Description</description>\n <itunes:owner>\n <itunes:name>Name</itunes:name>\n <itunes:email>Email</itunes:email>\n </itunes:owner>\n <itunes:image href="image.jpg"/>\n <itunes:category text="Technology">\n <itunes:category text="Gadgets"/>\n </itunes:category>\n <itunes:category text="TV & Film"/>\n <item>\n <title>Podcast Title</title>\n <itunes:author>Author</itunes:author>\n <itunes:subtitle>Subtitle</itunes:subtitle>\n <itunes:summary>Summary</itunes:summary>\n <itunes:image>image.jpg</itunes:image>\n <enclosure url="http://example.com/podcast.m4a" length="8727310" type="audio/x-m4a"/>\n <guid>http://example.com/archive/aae20050615.m4a</guid>\n <pubDate>Wed, 15 Jun 2011 19:00:00 GMT</pubDate>\n <itunes:duration>7:04</itunes:duration>\n <itunes:keywords>salt, pepper, shaker, exciting</itunes:keywords>\n </item>\n <item>\n <title>Podcast2 Title</title>\n <itunes:author>Author2</itunes:author>\n <itunes:subtitle>Subtitle2</itunes:subtitle>\n <itunes:summary>Summary2</itunes:summary>\n <itunes:image>image2.jpg</itunes:image>\n <enclosure url="http://example.com/podcast2.m4a" length="655555" type="audio/x-m4a"/>\n <guid>http://example.com/archive/aae2.m4a</guid>\n <pubDate>Wed, 15 Jul 2011 19:00:00 GMT</pubDate>\n <itunes:duration>11:20</itunes:duration>\n <itunes:keywords>foo, bar</itunes:keywords>\n </item>\n </channel>\n</rss>\n'
- });
- addTest({
- name: 'bug3',
- input: function(){
- return jstoxml.toXML({
- foo: true,
- bar: '',
- foo2: false,
- ok: 'This is ok',
- ok2: 'false',
- ok3: 'true'
- });
- },
- expectedOutput: '<foo>true</foo><bar></bar><foo2>false</foo2><ok>This is ok</ok><ok2>false</ok2><ok3>true</ok3>'
- });
- addTest({
- name: 'bug4a',
- input: function(){
- return jstoxml.toXML({
- foo: 4,
- bar: '&'
- });
- },
- expectedOutput: '<foo>4</foo><bar>&</bar>'
- });
- addTest({
- name: 'bug4b',
- input: function(){
- return jstoxml.toXML({
- foo: '&'
- },
- {
- filter: {
- '&': '&'
- }
- });
- },
- expectedOutput: '<foo>&</foo>'
- });
- addTest({
- name: 'headerNone',
- input: function(){
- return jstoxml.toXML({
- foo: 4
- },
- {
- header: false
- });
- },
- expectedOutput: '<foo>4</foo>'
- });
- addTest({
- name: 'headerDefault',
- input: function(){
- return jstoxml.toXML({
- foo: 4
- },
- {
- header: true
- });
- },
- expectedOutput: '<?xml version="1.0" encoding="UTF-8"?>\n<foo>4</foo>'
- });
- addTest({
- name: 'headerCustom',
- input: function(){
- return jstoxml.toXML({
- foo: 4
- },
- {
- header: '<Hooray For Captain Spaulding, the African Explorer>\n'
- });
- },
- expectedOutput: '<Hooray For Captain Spaulding, the African Explorer>\n<foo>4</foo>'
- });
- 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: [
- '<people>',
- '<students>',
- '<student><name>Joe</name></student>',
- '<student><name>Jane</name></student>',
- '</students>',
- '<teacher name="Yoda"/>',
- '</people>'
- ].join('')
- });
-
- runTests();
- showReport();
- // Exit with 1 to signal a failed build
- if(results.fail > 0) {
- process.exit(1);
- }
-
- })();
|