<commit_before>/*global suite,test*/
require('source-map-support').install();
var horchata = require("../lib/index");
var _ = require("lodash");
var specOptions = require("../../../specs/options");
var misMatch = require("../../tacoscript-dev-utils").misMatch;
var mochaFixtures = require("mocha-fixtures-generic");

var coreSpecs = mochaFixtures(require("path").resolve(__dirname + "/../../../specs/core"),
  _.assign({}, specOptions.core, {
    skip: function(test, testPath) {
      return specOptions.core.skip(test, testPath) ||
      test.indexOf("invalid-") === 0 ||
      test.indexOf("unexpected-") === 0 ||
      test.indexOf("malformed-") === 0;
    }
  })
);

suite("taco-printer", function () {
  test("basic", function () {
    var code = "this\n";
    var ast = horchata.parse(code);
  });
});

function removeLocInfo(json) {
  if (Object.prototype.toString.call(json) === '[object Array]') {
    for (var i = 0, len = json.length; i < len; i++) {
      removeLocInfo(json[i]);
    }
  } else {
    delete json.start;
    delete json.end;
    delete json.loc;
    for (var k in json) {
      if (json[k] != null && Object.prototype.toString.call(json) === '[object Object]') {
        removeLocInfo(json[k]);
      }
    }
  }
  return json;
}

_.forOwn(coreSpecs, function(suites, setName) {
  suites.forEach(function (testSuite) {
    suite("tacoscript-generator: (preserve=false) core/" + setName + "/" + testSuite.title, function () {
      _.each(testSuite.tests, function (task) {
        // comment out the following line when generating new specs
        // if (!task.auto.code && !fs.existsSync(task.auto.loc.replace('expected.json/', ''))) { task.disabled = true; }
        test(task.title, !task.disabled && function () {
          // var taco = task.taco;
          var taco = task.auto;

          var ast = horchata.parse(taco.code);
          var expectedAst;
          try {
            expectedAst = removeLocInfo(JSON.parse(task.json.code));
            delete expectedAst.program.sourceType;
          } catch(e) {}
          var mismatchMessage = misMatch(expectedAst, ast);
          if (mismatchMessage) {
            console.log("code:");
            console.log(taco.code);
            console.dir(ast.program, {depth: null});
            throw new Error(mismatchMessage);
          }
        });
      });
    });
  });
});
<commit_msg>Add ast to basic test<commit_after>/*global suite,test*/
require('source-map-support').install();
var horchata = require("../lib/index");
var _ = require("lodash");
var specOptions = require("../../../specs/options");
var misMatch = require("../../tacoscript-dev-utils").misMatch;
var mochaFixtures = require("mocha-fixtures-generic");

var coreSpecs = mochaFixtures(require("path").resolve(__dirname + "/../../../specs/core"),
  _.assign({}, specOptions.core, {
    skip: function(test, testPath) {
      return specOptions.core.skip(test, testPath) ||
      test.indexOf("invalid-") === 0 ||
      test.indexOf("unexpected-") === 0 ||
      test.indexOf("malformed-") === 0;
    }
  })
);

suite("taco-printer", function () {
  test("basic", function () {
    var code = "this\n";
    var ast = horchata.parse(code);
    var mismatchMessage = misMatch({
      type: "File",
      program: {
        type: "Program",
        body: [
          {
            type: "ExpressionStatement",
            expression: {
              type: "ThisExpression"
            }
          }
        ]
      }
    }, ast);

    if (mismatchMessage) throw new Error(mismatchMessage);
  });
});

function removeLocInfo(json) {
  if (Object.prototype.toString.call(json) === '[object Array]') {
    for (var i = 0, len = json.length; i < len; i++) {
      removeLocInfo(json[i]);
    }
  } else {
    delete json.start;
    delete json.end;
    delete json.loc;
    for (var k in json) {
      if (json[k] != null && Object.prototype.toString.call(json) === '[object Object]') {
        removeLocInfo(json[k]);
      }
    }
  }
  return json;
}

_.forOwn(coreSpecs, function(suites, setName) {
  suites.forEach(function (testSuite) {
    suite("tacoscript-generator: (preserve=false) core/" + setName + "/" + testSuite.title, function () {
      _.each(testSuite.tests, function (task) {
        // comment out the following line when generating new specs
        // if (!task.auto.code && !fs.existsSync(task.auto.loc.replace('expected.json/', ''))) { task.disabled = true; }
        test(task.title, !task.disabled && function () {
          // var taco = task.taco;
          var taco = task.auto;

          var ast = horchata.parse(taco.code);
          var expectedAst;
          try {
            expectedAst = removeLocInfo(JSON.parse(task.json.code));
            delete expectedAst.program.sourceType;
          } catch(e) {}
          var mismatchMessage = misMatch(expectedAst, ast);
          if (mismatchMessage) {
            console.log("code:");
            console.log(taco.code);
            console.dir(ast.program, {depth: null});
            throw new Error(mismatchMessage);
          }
        });
      });
    });
  });
});

{'fraction_non_alphanumeric': 0.17384494909945183, 'fraction_numerical': 0.0015661707126076742, 'mean_word_length': 2.442722371967655, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7225558', 'n_tokens_mistral': 1609, 'n_tokens_neox': 1576, 'n_words': 312}