@@ -5,9 +5,12 @@ util = require 'util'
5
5
assert = require ' assert'
6
6
path = require ' path'
7
7
os = require ' os'
8
+ promisify = require ' util.promisify'
8
9
9
10
fileName = path .join __dirname , ' /fixtures/sample.xml'
10
11
12
+ readFilePromise = promisify fs .readFile
13
+
11
14
skeleton = (options , checks ) ->
12
15
(test ) ->
13
16
xmlString = options ? .__xmlString
@@ -574,3 +577,52 @@ module.exports =
574
577
console .log ' Result object: ' + util .inspect r, false , 10
575
578
equ r .hasOwnProperty (' SAMP' ), true
576
579
equ r .SAMP .hasOwnProperty (' TAGN' ), true )
580
+
581
+
582
+ ' test parseStringPromise parsing ' : (test ) ->
583
+ x2js = new xml2js.Parser ()
584
+ readFilePromise (fileName).then (data) ->
585
+ x2js .parseStringPromise data
586
+ .then (r) ->
587
+ # just a single test to check whether we parsed anything
588
+ equ r .sample .chartest [0 ]._ , ' Character data here!'
589
+ test .finish ()
590
+ .catch (err) ->
591
+ test .fail (' Should not error' )
592
+
593
+ ' test parseStringPromise with bad input ' : (test ) ->
594
+ x2js = new xml2js.Parser ()
595
+ x2js .parseStringPromise (" < a moose bit my sister>" ).then (r) ->
596
+ test .fail (' Should fail' )
597
+ .catch (err) ->
598
+ assert .notEqual err, null
599
+ test .finish ()
600
+
601
+ ' test global parseStringPromise parsing ' : (test ) ->
602
+ readFilePromise (fileName).then (data) ->
603
+ xml2js .parseStringPromise data
604
+ .then (r) ->
605
+ assert .notEqual r, null
606
+ equ r .sample .listtest [0 ].item [0 ].subitem [0 ], ' Foo(1)'
607
+ test .finish ()
608
+ .catch (err) ->
609
+ test .fail (' Should not error' )
610
+
611
+ ' test global parseStringPromise with options ' : (test ) ->
612
+ readFilePromise (fileName).then (data) ->
613
+ xml2js .parseStringPromise data,
614
+ trim : true
615
+ normalize : true
616
+ .then (r) ->
617
+ assert .notEqual r, null
618
+ equ r .sample .whitespacetest [0 ]._ , ' Line One Line Two'
619
+ test .finish ()
620
+ .catch (err) ->
621
+ test .fail (' Should not error' )
622
+
623
+ ' test global parseStringPromise with bad input ' : (test ) ->
624
+ xml2js .parseStringPromise (" < a moose bit my sister>" ).then (r) ->
625
+ test .fail (' Should fail' )
626
+ .catch (err) ->
627
+ assert .notEqual err, null
628
+ test .finish ()
0 commit comments