github.com/igggame/nebulas-go@v2.1.0+incompatible/nf/nvm/test/instruction_counter_tests/with.js (about)

     1  // Copyright (C) 2017 go-nebulas authors
     2  //
     3  // This file is part of the go-nebulas library.
     4  //
     5  // the go-nebulas library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // the go-nebulas library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU General Public License
    16  // along with the go-nebulas library.  If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  const assert = require('assert.js');
    19  
    20  function assertEqual(func, args, expected, expected_count, msg) {
    21      const count_of_helper_statement = 46;
    22      var count = _instruction_counter.count;
    23      assert.equal(func.apply(null, args), expected);
    24      assert.equal(_instruction_counter.count - count - count_of_helper_statement, expected_count, msg);
    25  };
    26  
    27  // test1.
    28  var test1 = function (x) {
    29      with(x)
    30      return a + b;
    31  };
    32  assertEqual(test1, [{
    33      a: 1,
    34      b: 2
    35  }], 3, 3);
    36  
    37  // test2
    38  var test2 = function (x) {
    39      with(x[0]) {
    40          return a + b;
    41      }
    42  };
    43  assertEqual(test2, [
    44      [{
    45          a: 1,
    46          b: 2
    47      }]
    48  ], 3, 7);
    49  
    50  // test3
    51  var gen3X = function (a, b) {
    52      return {
    53          val: function () {
    54              return {
    55                  a: a,
    56                  b: b
    57              };
    58          }
    59      }
    60  };
    61  var test3 = function (x) {
    62      with(x.val()) {
    63          if (a < b) {
    64              return a + b;
    65          } else if (a > b) {
    66              return a * 2 + b;
    67          } else {
    68              return 0;
    69          }
    70      }
    71  };
    72  assertEqual(test3, [gen3X(3, 5)], 8, 18);
    73  assertEqual(test3, [gen3X(5, 3)], 13, 24);
    74  assertEqual(test3, [gen3X(4, 4)], 0, 18);