var fs = require("fs");
var path = require("path");
var assert = require("chai").assert;
var Jison = require('../setup').Jison;
var Lexer = require('../setup').Lexer;
var helpers     = require('../../modules/helpers-lib');
var code_exec   = helpers.exec;


describe("Regression Checks", function () {
  it("GitHub JISON issue #13: https://github.com/GerHobbelt/jison/issues/13", function () {
    var filespec = {
      path: "tests/parser/github-issues-nr-13-gho.y"
    };
    var grammar = fs.readFileSync(filespec.path, "utf8");

    var options = {
      noMain: false,
    };
    //options.exportSourceCode = {};
    options.file = filespec.path;
    try {
      var parser = new Jison.Parser(grammar, options);

      assert.ok(false, "should never get here");
    } catch (ex) {
      console.log("ex:", ex);
      assert.ok(false);
    }
    code_exec(String(parser.parse), function test_exec() {
      if (typeof parser.main === 'function') {
        assert.ok(!parser.main(), 'main() is supposed to produce zero ~ success');
      } else if (filespec.inputs) {
        for (var i = 0, l = filespec.inputs.length; i < l; i++) {
          rv = parser.parse(filespec.inputs[i]);
          console.log('parse A: ', filespec.inputs[i], rv);
          assert.strictEqual(rv, true, 'parser.parse() is supposed to produce TRUE');
        }
      } else {
        rv = parser.parse('zz; yy; zz;zz ;');
        console.log('parse B: ', path.basename(filespec.path), rv);
        assert.strictEqual(rv, true, 'parser.parse() is supposed to produce TRUE');
      }
    }, {
      dumpSourceCodeOnFailure: true,
      throwErrorOnCompileFailure: true,
    }, "test");

    if (filespec.__check__) {
      filespec.__check__(parser, filespec, rv, options.exportAllTables);
    }
  });
});

@@ -250,16 +250,52 @@
 exec;%0A%0A%0A
+var original_cwd = process.cwd();%0A%0A%0A
 describe
@@ -430,50 +430,234 @@
     
-var filespec = %7B%0A      path: %22tests/parser
+// Change CWD to the directory where the source grammar resides: this helps us properly%0A    // %25include any files mentioned in the grammar with relative paths:%0A    process.chdir(__dirname);%0A%0A    var filespec = %7B%0A      path: %22.
 /git
@@ -683,24 +683,25 @@
 o.y%22%0A    %7D;%0A
+%0A
     var gram
@@ -1010,689 +1010,48 @@
     
-console.log(%22ex:%22, ex);%0A      assert.ok(false);%0A    %7D%0A    code_exec(String(parser.parse), function test_exec() %7B%0A      if (typeof parser.main === 'function') %7B%0A        assert.ok(!parser.main(), 'main() is supposed to produce zero ~ success');%0A      %7D else if (filespec.inputs) %7B%0A        for (var i = 0, l = filespec.inputs.length; i %3C l; i++) %7B%0A          rv = parser.parse(filespec.inputs%5Bi%5D);%0A          console.log('parse A: ', filespec.inputs%5Bi%5D, rv);%0A          assert.strictEqual(rv, true, 'parser.parse() is supposed to produce TRUE');%0A        %7D%0A      %7D else %7B%0A        rv = parser.parse('zz; yy; zz;zz ;');%0A        console.log('parse B: ', path.basename(filespec.path), rv
+throw new Error(%22ex:%22 + ex + ex.stack
 );%0A
-  
     
@@ -1063,288 +1063,15 @@
 ert.
-strictEqual(rv, true, 'parser.parse() is supposed to produce TRUE');%0A      %7D%0A    %7D, %7B%0A      dumpSourceCodeOnFailure: true,%0A      throwErrorOnCompileFailure: true,%0A    %7D, %22test%22);%0A%0A    if (filespec.__check__) %7B%0A      filespec.__check__(parser, filespec, rv, options.exportAllTables
+ok(true
 );%0A 

fix unit test (copy-pasta cruft removal)