Permalink
Browse files

lots of unit tests for the builder

  • Loading branch information...
1 parent c499afd commit f61b65281251ea62758586ac4149d849cc7fe0e4 @jacksenechal jacksenechal committed Oct 31, 2013
View
@@ -0,0 +1,96 @@
+# use zap to run tests, it also detects CoffeeScript files
+xml2js = require '../lib/xml2js'
+assert = require 'assert'
+fs = require 'fs'
+path = require 'path'
+diff = require 'diff'
+
+# fileName = path.join __dirname, '/fixtures/sample.xml'
+
+# shortcut, because it is quite verbose
+equ = assert.equal
+diffeq = (expected, actual) ->
+ diffless = "Index: test\n===================================================================\n--- test\texpected\n+++ test\tactual\n"
+ patch = diff.createPatch('test', expected, actual, 'expected', 'actual')
+ throw patch unless patch is diffless
+
+module.exports =
+ 'test building basic XML structure': (test) ->
+ expected = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><xml><Label></Label><MsgId>5850440872586764820</MsgId></xml>'
+ obj = {"xml":{"Label":[""],"MsgId":["5850440872586764820"]}}
+ builder = new xml2js.Builder({pretty: false})
+ actual = builder.buildObject obj
+ diffeq actual, expected
+ test.finish()
+
+ 'test setting XML declaration': (test) ->
+ expected = '<?xml version="1.0" encoding="WTF-8" standalone="no"?>'
+ opts = {
+ pretty: false
+ xmldec: {'version': '1.0', 'encoding': 'WTF-8', 'standalone': false}
+ }
+ builder = new xml2js.Builder(opts)
+ actual = builder.buildObject {}
+ diffeq actual, expected
+ test.finish()
+
+ 'test pretty by default': (test) ->
+ expected = """
+ <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+ <xml>
+ <MsgId>5850440872586764820</MsgId>
+ </xml>
+
+ """
+ builder = new xml2js.Builder()
+ obj = {"xml":{"MsgId":["5850440872586764820"]}}
+ actual = builder.buildObject obj
+ diffeq actual, expected
+ test.finish()
+
+ 'test explicit rootName is always used: 1. when there is only one element': (test) ->
+ expected = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><FOO><MsgId>5850440872586764820</MsgId></FOO>'
+ opts = { pretty: false, rootName: 'FOO' }
+ builder = new xml2js.Builder(opts)
+ obj = {"MsgId":["5850440872586764820"]}
+ actual = builder.buildObject obj
+ diffeq actual, expected
+ test.finish()
+
+ 'test explicit rootName is always used: 2. when there are multiple elements': (test) ->
+ expected = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><FOO><MsgId>5850440872586764820</MsgId></FOO>'
+ opts = { pretty: false, rootName: 'FOO' }
+ builder = new xml2js.Builder(opts)
+ obj = {"MsgId":["5850440872586764820"]}
+ actual = builder.buildObject obj
+ diffeq actual, expected
+ test.finish()
+
+ 'test default rootName is used when there is more than one element in the hash': (test) ->
+ expected = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><root><MsgId>5850440872586764820</MsgId><foo>bar</foo></root>'
+ opts = { pretty: false }
+ builder = new xml2js.Builder(opts)
+ obj = {"MsgId":["5850440872586764820"],"foo":"bar"}
+ actual = builder.buildObject obj
+ diffeq actual, expected
+ test.finish()
+
+ 'test when there is only one first-level element in the hash, that is used as root': (test) ->
+ expected = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><first><MsgId>5850440872586764820</MsgId><foo>bar</foo></first>'
+ opts = { pretty: false }
+ builder = new xml2js.Builder(opts)
+ obj = {"first":{"MsgId":["5850440872586764820"],"foo":"bar"}}
+ actual = builder.buildObject obj
+ diffeq actual, expected
+ test.finish()
+
+ 'test parser -> builder roundtrip': (test) ->
+ fileName = path.join __dirname, '/fixtures/build_sample.xml'
+ fs.readFile fileName, (err, xmlData) ->
+ xmlExpected = xmlData.toString()
+ xml2js.parseString xmlData, {'trim': true}, (err, obj) ->
+ equ err, null
+ builder = new xml2js.Builder({})
+ xmlActual = builder.buildObject obj
+ diffeq xmlExpected, xmlActual
+ test.finish()
@@ -0,0 +1,57 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<sample>
+ <chartest desc="Test for CHARs">Character data here!</chartest>
+ <nochartest desc="No data" misc="false"/>
+ <nochildrentest desc="No data" misc="false"/>
+ <listtest>
+ <item>
+ This is
+ character
+ data!
+ <subitem>Foo(1)</subitem>
+ <subitem>Foo(2)</subitem>
+ <subitem>Foo(3)</subitem>
+ <subitem>Foo(4)</subitem>
+ </item>
+ <item>Qux.</item>
+ <item>Quux.</item>
+ </listtest>
+ <arraytest>
+ <item>
+ <subitem>Baz.</subitem>
+ </item>
+ <item>
+ <subitem>Foo.</subitem>
+ <subitem>Bar.</subitem>
+ </item>
+ </arraytest>
+ <emptytest>
+
+ </emptytest>
+ <tagcasetest>
+ <tAg>something</tAg>
+ <TAG>something else</TAG>
+ <tag>something third</tag>
+ </tagcasetest>
+ <ordertest>
+ <one>1</one>
+ <one>4</one>
+ <two>2</two>
+ <two>5</two>
+ <three>3</three>
+ <three>6</three>
+ </ordertest>
+ <validatortest>
+ <emptyarray>
+
+ </emptyarray>
+ <oneitemarray>
+ <item>Bar.</item>
+ </oneitemarray>
+ <numbertest>42</numbertest>
+ <stringtest>43</stringtest>
+ </validatortest>
+ <pfx:top xmlns:pfx="http://foo.com" pfx:attr="baz">
+ <middle xmlns="http://bar.com"/>
+ </pfx:top>
+</sample>
File renamed without changes.

0 comments on commit f61b652

Please sign in to comment.