Skip to content

Commit

Permalink
Added unit test and fix in the constructor making the test pass
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonidas-from-XIV committed Jun 12, 2013
1 parent 87003ae commit 05c7f43
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/xml2js.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/xml2js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ class exports.ValidationError extends Error

class exports.Parser extends events.EventEmitter
constructor: (opts) ->
# if this was called without 'new', create an instance with new and return
return new exports.Parser opts unless @ instanceof exports.Parser
# copy this versions default options
@options = {}
@options[key] = value for own key, value of exports.defaults["0.2"]
Expand Down
11 changes: 11 additions & 0 deletions test/xml2js.test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -308,3 +308,14 @@ module.exports =
xml2js.parseString html, strict: false, (err, parsed) ->
equ err, null
test.finish()

'test construction with new and without': (test) ->
demo = '<xml><foo>Bar</foo></xml>'
withNew = new xml2js.Parser
withNew.parseString demo, (err, resWithNew) ->
equ err, undefined
withoutNew = xml2js.Parser()
withoutNew.parseString demo, (err, resWithoutNew) ->
equ err, undefined
assert.deepEqual resWithNew, resWithoutNew
test.done()

0 comments on commit 05c7f43

Please sign in to comment.