Skip to content

adding parseStringSync + simple tests (continued) #319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/bom.js

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

2 changes: 1 addition & 1 deletion lib/processors.js

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

36 changes: 29 additions & 7 deletions lib/xml2js.js

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

18 changes: 18 additions & 0 deletions src/xml2js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,20 @@ class exports.Parser extends events.EventEmitter
else if @saxParser.ended
throw err

parseStringSync: (str) =>
#assumption 1: sax events block when given a string instead of a stream
retval = undefined
err = undefined
cb = (_err, _retval) ->
retval = _retval
err = _err

#assumption 2: parseString will implicitly call cb (thus setting closure retval) before returning
@parseString str, cb
if err
throw err
retval

exports.parseString = (str, a, b) ->
# let's determine what we got as arguments
if b?
Expand All @@ -419,3 +433,7 @@ exports.parseString = (str, a, b) ->
# the rest is super-easy
parser = new exports.Parser options
parser.parseString str, cb

exports.parseStringSync = (str) ->
parser = new exports.Parser {}
parser.parseStringSync str
17 changes: 17 additions & 0 deletions test/parser.test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,23 @@ module.exports =
equ r.sample.$$[10].$$[5]['#name'], 'three'
equ r.sample.$$[10].$$[5]._, '6')

'test non-async parsing': (test) ->
data = fs.readFileSync fileName, 'utf8'
xmldoc = xml2js.parseStringSync data
assert.notEqual xmldoc, null
equ xmldoc.sample.listtest[0].item[0].subitem[0], 'Foo(1)'
test.finish()

'test non-async with bad input': (test) ->
data = fs.readFileSync fileName, 'utf8'
err = null
try
xmldoc = xml2js.parseStringSync "< a moose bit my sister>";
catch _err
err = _err
assert.notEqual err, null
test.finish()

'test parse with explicitChildren and charsAsChildren and preserveChildrenOrder': skeleton(explicitChildren: true, preserveChildrenOrder: true, charsAsChildren: true, (r) ->
console.log 'Result object: ' + util.inspect r, false, 10
equ r.sample.$$[10]['#name'], 'ordertest'
Expand Down