node.mixin(process, require('helpers.js'));

describe("SAX Push Parser", function() {
  var callbacks = {};

  function createParser(parserType) {
    parser = new libxml[parserType](function(cb) {
      function argsToArray(args) {
        var ary = [];
        var i;
        for (i = 0; i < args.length; i++)
          ary.push(args[i]);
        return ary;
      };

      cb.onStartDocument(function() {
        callbacks.startDocument.push(argsToArray(arguments));
      });

      cb.onEndDocument(function() {
        callbacks.endDocument.push(argsToArray(arguments));
      });

      cb.onStartElementNS(function(elem, attrs, prefix, uri, namespaces) {
        callbacks.startElementNS.push(argsToArray(arguments));
      });

      cb.onEndElementNS(function(elem, prefix, uri) {
        callbacks.endElementNS.push(argsToArray(arguments));
      });

      cb.onCharacters(function(chars) {
        if (chars[0] && !chars[0].match(/^[\s\n\r]+$/))
          callbacks.characters.push(argsToArray(arguments));
      });

      cb.onCdata(function(cdata) {
        callbacks.cdata.push(argsToArray(arguments));
      });

      cb.onComment(function(msg) {
        callbacks.comment.push(argsToArray(arguments));
      });

      cb.onWarning(function(msg) {
        callbacks.warning.push(argsToArray(arguments));
      });

      cb.onError(function(msg) {
        callbacks.error.push(argsToArray(arguments));
      });
    });
    return parser;
  };

  beforeEach(function() {
    callbacks = clone(callbackTest);
  });

  it('will properly parse a regular string', function() {
    var str = node.fs.cat(node.path.dirname(__filename)+'/fixtures/sax_parser_test.xml').wait();
    parser = createParser('SaxParser');
    parser.parseString(str);
    assertEqual(JSON.stringify(callbackControl), JSON.stringify(callbacks));
  });

  it('will properly parse a string chunk by chunk', function() {
    var str_ary = node.fs.cat(node.path.dirname(__filename)+'/fixtures/sax_parser_test.xml').wait().split("\n");
    parser = createParser('SaxPushParser');
    var i;
    for (i = 0; i < str_ary.length; i++)
      parser.push(str_ary[i], (i+1 == str_ary.length));

    var control = clone(callbackControl);
    control.error = [["Extra content at the end of the document\n"]];
    assertEqual(JSON.stringify(control), JSON.stringify(callbacks));
  });

  it('will properly parse a file', function() {
    parser = createParser('SaxParser');
    parser.parseFile(node.path.dirname(__filename)+'/fixtures/sax_parser_test.xml');

    var control = clone(callbackControl);
    assertEqual(JSON.stringify(control), JSON.stringify(callbacks));
  });
});

@@ -100,16 +100,96 @@
 ks = %7B%7D;
+%0A  var filename = node.path.dirname(__filename)+'/fixtures/sax_parser_test.xml';
 %0A%0A  func
@@ -1697,69 +1697,16 @@
 cat(
-node.path.dirname(__filename)+'/fixtures/sax_parser_test.xml'
+filename
 ).wa
@@ -1711,24 +1711,28 @@
 wait();%0A    
+var 
 parser = cre
@@ -1967,69 +1967,16 @@
 cat(
-node.path.dirname(__filename)+'/fixtures/sax_parser_test.xml'
+filename
 ).wa
@@ -1993,24 +1993,28 @@
 (%22%5Cn%22);%0A    
+var 
 parser = cre
@@ -2382,32 +2382,36 @@
 unction() %7B%0A    
+var 
 parser = createP
@@ -2455,69 +2455,16 @@
 ile(
-node.path.dirname(__filename)+'/fixtures/sax_parser_test.xml'
+filename
 );%0A%0A
@@ -2579,13 +2579,410 @@
 );%0A  %7D);
+%0A%0A  it('can can be reused as a string parser', function() %7B%0A    var str = node.fs.cat(filename).wait();%0A    var parser = createParser('SaxParser');%0A    %0A    for (var i=0; i%3C10; i++)%0A      parser.parseString(str);%0A%0A  %7D);%0A%0A  it('can can be reused as a string parser', function() %7B%0A    var parser = createParser('SaxParser');%0A    %0A    for (var i=0; i%3C10; i++)%0A      parser.parseFile(filename);%0A%0A  %7D);
 %0A%7D);%0A

Add test case for SAX parser reuse