Permalink
Browse files

Added unit test and fix in the constructor making the test pass

  • Loading branch information...
1 parent 87003ae commit 05c7f43750204de7626b9f8142624e0df62986f0 @Leonidas-from-XIV committed Jun 12, 2013
Showing with 16 additions and 0 deletions.
  1. +3 −0 lib/xml2js.js
  2. +2 −0 src/xml2js.coffee
  3. +11 −0 test/xml2js.test.coffee
View
3 lib/xml2js.js
@@ -72,6 +72,9 @@
this.parseString = __bind(this.parseString, this);
this.reset = __bind(this.reset, this);
var key, value, _ref;
+ if (!(this instanceof exports.Parser)) {
+ return new exports.Parser(opts);
+ }
this.options = {};
_ref = exports.defaults["0.2"];
for (key in _ref) {
View
2 src/xml2js.coffee
@@ -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"]
View
11 test/xml2js.test.coffee
@@ -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.