<commit_before>var TestCase = require("mocha/lib/test.js");
var Suite = require("mocha/lib/suite.js");
var Deployer = require("truffle-migrate-legacy/node_modules/truffle-deployer");
var find_contracts = require("truffle-contract-sources");
var compile = require("truffle-migrate-legacy/node_modules/truffle-compile");
var series = require("async").series;
var path = require("path");
var SolidityCoder = require("truffle-migrate-legacy/node_modules/web3/lib/solidity/coder.js");
//const semver = require("semver"); <--- for use when BYOC enabled

let SafeSend;

var SolidityTest = {
  define: function(abstraction, dependency_paths, runner, mocha) {
    var self = this;

    var suite = new Suite(abstraction.contract_name, {});
    suite.timeout(runner.BEFORE_TIMEOUT);

    // Set up our runner's needs first.
    suite.beforeAll("prepare suite", function(done) {
      series(
        [
          runner.initialize.bind(runner),
          self.compileNewAbstractInterface.bind(this, runner),
          self.deployTestDependencies.bind(
            this,
            abstraction,
            dependency_paths,
            runner
          )
        ],
        done
      );
    });

    suite.beforeEach("before test", function(done) {
      runner.startTest(this, done);
    });

    // Function that decodes raw logs from unlinked third party assertion
    // libraries and returns usable TestEvent logs
    function decodeTestEvents(result) {
      if (result.logs.length) return result.logs;

      var logs = [];
      var signature = web3.sha3("TestEvent(bool,string)");

      result.receipt.logs.forEach(function(log) {
        if (log.topics.length === 2 && log.topics[0] === signature) {
          var decoded = {
            event: "TestEvent",
            args: {
              result: SolidityCoder.decodeParams(
                ["bool"],
                log.topics[1].replace("0x", "")
              )[0],
              message: SolidityCoder.decodeParams(
                ["string"],
                log.data.replace("0x", "")
              )[0]
            }
          };
          logs.push(decoded);
        }
      });
      return logs;
    }

    // Function that checks transaction logs to see if a test failed.
    function processResult(result) {
      result.logs = decodeTestEvents(result);

      result.logs.forEach(function(log) {
        if (log.event == "TestEvent" && log.args.result == false) {
          throw new Error(log.args.message);
        }
      });
    }

    // Add functions from test file.
    abstraction.abi.forEach(function(item) {
      if (item.type != "function") return;

      ["beforeAll", "beforeEach", "afterAll", "afterEach"].forEach(function(
        fn_type
      ) {
        if (item.name.indexOf(fn_type) == 0) {
          suite[fn_type](item.name, function() {
            return abstraction
              .deployed()
              .then(function(deployed) {
                return deployed[item.name]();
              })
              .then(processResult);
          });
        }
      });

      if (item.name.indexOf("test") == 0) {
        var test = new TestCase(item.name, function() {
          return abstraction
            .deployed()
            .then(function(deployed) {
              return deployed[item.name]();
            })
            .then(processResult);
        });

        test.timeout(runner.TEST_TIMEOUT);
        suite.addTest(test);
      }
    });

    suite.afterEach("after test", function(done) {
      runner.endTest(this, done);
    });

    mocha.suite.addSuite(suite);
  },

  compileNewAbstractInterface: function(runner, callback) {
    find_contracts(runner.config.contracts_directory, function(err) {
      if (err) return callback(err);

      // TODO (make legacy testing work w/ BYOC, compilerSupplier, solc ^0.5.0
      SafeSend = "OldSafeSend.sol";
      /*
        const config = runner.config;
        if (!config.compilers.solc.version) SafeSend = "NewSafeSend.sol";
        else if (semver.lt(semver.coerce(config.compilers.solc.version), "0.5.0"))
        SafeSend = "OldSafeSend.sol";
        else SafeSend = "NewSafeSend.sol";
      */

      compile.with_dependencies(
        runner.config.with({
          paths: [
            path.join(__dirname, "Assert.sol"),
            path.join(__dirname, "AssertAddress.sol"),
            path.join(__dirname, "AssertAddressArray.sol"),
            // path.join(__dirname, "AssertAddressPayableArray.sol"), only compatible w/ ^0.5.0<commit_msg>update soli testing for BYOC<commit_after>var TestCase = require("mocha/lib/test.js");
var Suite = require("mocha/lib/suite.js");
var Deployer = require("truffle-migrate-legacy/node_modules/truffle-deployer");
var find_contracts = require("truffle-contract-sources");
var compile = require("truffle-compile-legacy");
var series = require("async").series;
var path = require("path");
var SolidityCoder = require("truffle-migrate-legacy/node_modules/web3/lib/solidity/coder.js");
const semver = require("semver");

let SafeSend;

var SolidityTest = {
  define: function(abstraction, dependency_paths, runner, mocha) {
    var self = this;

    var suite = new Suite(abstraction.contract_name, {});
    suite.timeout(runner.BEFORE_TIMEOUT);

    // Set up our runner's needs first.
    suite.beforeAll("prepare suite", function(done) {
      series(
        [
          runner.initialize.bind(runner),
          self.compileNewAbstractInterface.bind(this, runner),
          self.deployTestDependencies.bind(
            this,
            abstraction,
            dependency_paths,
            runner
          )
        ],
        done
      );
    });

    suite.beforeEach("before test", function(done) {
      runner.startTest(this, done);
    });

    // Function that decodes raw logs from unlinked third party assertion
    // libraries and returns usable TestEvent logs
    function decodeTestEvents(result) {
      if (result.logs.length) return result.logs;

      var logs = [];
      var signature = web3.sha3("TestEvent(bool,string)");

      result.receipt.logs.forEach(function(log) {
        if (log.topics.length === 2 && log.topics[0] === signature) {
          var decoded = {
            event: "TestEvent",
            args: {
              result: SolidityCoder.decodeParams(
                ["bool"],
                log.topics[1].replace("0x", "")
              )[0],
              message: SolidityCoder.decodeParams(
                ["string"],
                log.data.replace("0x", "")
              )[0]
            }
          };
          logs.push(decoded);
        }
      });
      return logs;
    }

    // Function that checks transaction logs to see if a test failed.
    function processResult(result) {
      result.logs = decodeTestEvents(result);

      result.logs.forEach(function(log) {
        if (log.event == "TestEvent" && log.args.result == false) {
          throw new Error(log.args.message);
        }
      });
    }

    // Add functions from test file.
    abstraction.abi.forEach(function(item) {
      if (item.type != "function") return;

      ["beforeAll", "beforeEach", "afterAll", "afterEach"].forEach(function(
        fn_type
      ) {
        if (item.name.indexOf(fn_type) == 0) {
          suite[fn_type](item.name, function() {
            return abstraction
              .deployed()
              .then(function(deployed) {
                return deployed[item.name]();
              })
              .then(processResult);
          });
        }
      });

      if (item.name.indexOf("test") == 0) {
        var test = new TestCase(item.name, function() {
          return abstraction
            .deployed()
            .then(function(deployed) {
              return deployed[item.name]();
            })
            .then(processResult);
        });

        test.timeout(runner.TEST_TIMEOUT);
        suite.addTest(test);
      }
    });

    suite.afterEach("after test", function(done) {
      runner.endTest(this, done);
    });

    mocha.suite.addSuite(suite);
  },

  compileNewAbstractInterface: function(runner, callback) {
    find_contracts(runner.config.contracts_directory, function(err) {
      if (err) return callback(err);

      const config = runner.config;
      if (!config.compilers.solc.version) SafeSend = "NewSafeSend.sol";
      else if (semver.lt(semver.coerce(config.compilers.solc.version), "0.5.0"))
        SafeSend = "OldSafeSend.sol";
      else SafeSend = "NewSafeSend.sol";

      compile.with_dependencies(
        runner.config.with({
          paths: [
            path.join(__dirname, "Assert.sol"),
            path.join(__dirname, "AssertAddress.sol"),
            path.join(__dirname, "AssertAddressArray.sol"),
            // path.join(__dirname, "AssertAddressPayableArray.sol"), only compatible w/ ^0.5.0
{'fraction_non_alphanumeric': 0.13326539855072464, 'fraction_numerical': 0.004415760869565217, 'mean_word_length': 2.1580264569181264, 'pattern_counts': {'":': 0, '<': 4, '<?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': '1368398', 'n_tokens_mistral': 2584, 'n_tokens_neox': 2519, 'n_words': 486}