github.com/igggame/nebulas-go@v2.1.0+incompatible/nf/nvm/test/test_storage_class.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 NativeStorage === "undefined") { 20 throw new Error("NativeStorage is undefined."); 21 } 22 if (typeof NativeStorage !== "function") { 23 throw new Error("NativeStorage is not a function."); 24 } 25 26 try { 27 new NativeStorage({}); 28 throw new Error("NativeStorage should accept _storage_handlers member only."); 29 } catch (e) { 30 // pass. 31 } 32 33 // disable gcs according to https://github.com/nebulasio/go-nebulas/issues/23 34 var _e = new Error('_native_storage_handlers.gcs should be disabled.'); 35 try { 36 _native_storage_handlers.gcs.put('k1', 'v1'); 37 throw _e; 38 } catch (e) { 39 if (e == _e) { 40 throw e; 41 } else { 42 // pass. 43 } 44 } 45 46 // [_native_storage_handlers.lcs, _native_storage_handlers.gcs].forEach(function (handler) { 47 [_native_storage_handlers.lcs].forEach(function (handler) { 48 var stor = new NativeStorage(handler); 49 if (stor.get("non-exist-key") !== null) { 50 throw new Error("get non-exist-key should return null."); 51 } 52 53 var v1 = 'this is v1'; 54 if (stor.put('k1', v1) != 0) { 55 throw new Error("put k1 failed."); 56 } 57 58 if (stor.get('k1') !== v1) { 59 throw new Error("key k1 should return string [" + v1 + "].") 60 } 61 62 if (stor.del('k1') != 0) { 63 throw new Error("del k1 failed."); 64 } 65 66 if (stor.get('k1') !== null) { 67 throw new Error("key k1 should not exist, return null when get.") 68 } 69 70 if (stor.del('k2') != 0) { 71 throw new Error("del k2 failed."); 72 } 73 74 [123, {}, function () {}, undefined].forEach(function (v) { 75 var err = new Error('value of put should be string, not support ' + typeof v); 76 var err1 = new Error("put k3 failed."); 77 try { 78 if (stor.put('k3', v) != 0) { 79 throw err1; 80 } 81 throw err; 82 } catch (e) { 83 if (e == err || e == err1) { 84 throw e; 85 } 86 } 87 }); 88 });