github.com/igggame/nebulas-go@v2.1.0+incompatible/nf/nvm/test/test_storage.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  'use strict';
    19  if (typeof ContractStorage === 'undefined') {
    20      throw new Error("ContractStorage is undefined.");
    21  }
    22  
    23  var err = new Error("ContractStorage should accept _storage_handlers member only.");
    24  try {
    25      new ContractStorage();
    26      throw err;
    27  } catch (e) {
    28      if (e == err) {
    29          throw e;
    30      }
    31  }
    32  
    33  // disable gcs according to https://github.com/nebulasio/go-nebulas/issues/23
    34  var _e = new Error('GlobalContractStorage should be disabled.');
    35  try {
    36      GlobalContractStorage.put('k1', 'v1');
    37      throw _e;
    38  } catch (e) {
    39      if (e == _e) {
    40          throw e;
    41      } else {
    42          // pass.
    43      }
    44  }
    45  
    46  [
    47      [LocalContractStorage, 'LocalContractStorage']
    48      // disable gcs according to https://github.com/nebulasio/go-nebulas/issues/23
    49      // [GlobalContractStorage, 'GlobalContractStorage']
    50  ].forEach(function (item) {
    51      var stor = item[0];
    52      var name = item[1];
    53      if (typeof stor === 'undefined') {
    54          throw new Error(name + " is undefined.");
    55      }
    56  
    57      var v1 = 'now is v1';
    58      stor.put('k1', v1);
    59      if (stor.get('k1') !== v1) {
    60          throw new Error("key k1 should return string [" + v1 + "].")
    61      }
    62  
    63      stor.del('k1');
    64      if (stor.get('k1') !== null) {
    65          throw new Error("key k1 should not exist, return null when get.")
    66      }
    67  
    68      stor.del('k2');
    69  
    70      ["haha", 123, false, true, null, {
    71          x: 1,
    72          y: {
    73              a: 2
    74          },
    75          z: ["zzz", 3, 4]
    76      }].forEach(function (v, idx) {
    77          var key = 'key-' + idx;
    78          stor.put(key, v);
    79          var val = stor.get(key);
    80          if (!Object.is(val, v)) {
    81              if (typeof v === 'object') {
    82                  if (JSON.stringify(v) == JSON.stringify(val)) {
    83                      // pass.
    84                      return;
    85                  }
    86              }
    87              throw new Error("ContractStorage should support value type " + typeof v + "; expected is " + v + ", actual is " + val);
    88          }
    89      });
    90  });