Commit 0d9659e23045d218b41e945ebb6a50c7391a14da
Split out testEnvironment from Environment
Alex Beregszaszi committed on 8/16/2016, 9:31:11 PMParent: 1fff96f44dcc50430b7bfd09cd20493184489746
Files changed
environment.js | changed |
testEnvironment.js | added |
environment.js | ||
---|---|---|
@@ -24,43 +24,11 @@ | ||
24 | 24 | // more output calls |
25 | 25 | returnValue: new Uint8Array() |
26 | 26 | } |
27 | 27 | |
28 | - const self = this | |
29 | 28 | this.state = new Map() |
30 | 29 | |
31 | - if (data) { | |
32 | - data = JSON.parse(data) | |
33 | - } else { | |
34 | - data = {} | |
35 | - } | |
36 | - | |
37 | - Object.assign(this, defaults, data) | |
38 | - if (data.accounts) { | |
39 | - data.accounts.forEach((account) => { | |
40 | - self.state.set(new Uint8Array(account[0]).toString(), account[1]) | |
41 | - }) | |
42 | - } | |
43 | - | |
44 | - if (data.address) { | |
45 | - this.address = new Uint8Array(data.address) | |
46 | - } | |
47 | - | |
48 | - if (data.origin) { | |
49 | - this.origin = new Uint8Array(data.origin) | |
50 | - } | |
51 | - | |
52 | - if (data.caller) { | |
53 | - this.caller = new Uint8Array(data.caller) | |
54 | - } | |
55 | - | |
56 | - if (data.callValue) { | |
57 | - this.callValue = new Uint8Array(data.callValue) | |
58 | - } | |
59 | - | |
60 | - if (data.callData) { | |
61 | - this.callData = hexStr2arrayBuf(data.callData) | |
62 | - } | |
30 | + Object.assign(this, defaults, data || {}) | |
63 | 31 | } |
64 | 32 | |
65 | 33 | getBalance (address) { |
66 | 34 | return this.state.get(address.toString()).balance |
@@ -88,19 +56,4 @@ | ||
88 | 56 | // STUB |
89 | 57 | return // result |
90 | 58 | } |
91 | 59 | } |
92 | - | |
93 | -function hexStr2arrayBuf (string) { | |
94 | - const ab = new ArrayBuffer(string.length / 2) | |
95 | - const view = new Uint8Array(ab) | |
96 | - string = [...string] | |
97 | - let temp = '' | |
98 | - string.forEach((el, i) => { | |
99 | - temp += el | |
100 | - if (i % 2) { | |
101 | - view[(i + 1) / 2 - 1] = parseInt(temp, 16) | |
102 | - temp = '' | |
103 | - } | |
104 | - }) | |
105 | - return ab | |
106 | -} |
testEnvironment.js | ||
---|---|---|
@@ -1,0 +1,54 @@ | ||
1 | +const Environment = require('./environment.js') | |
2 | + | |
3 | +module.exports = class TestEnvironment extends Environment { | |
4 | + constructor (data) { | |
5 | + super() | |
6 | + | |
7 | + if (typeof data === 'string') { | |
8 | + data = JSON.parse(data) | |
9 | + } | |
10 | + | |
11 | + let self = this | |
12 | + | |
13 | + if (data.accounts) { | |
14 | + data.accounts.forEach((account) => { | |
15 | + self.state.set(new Uint8Array(account[0]).toString(), account[1]) | |
16 | + }) | |
17 | + } | |
18 | + | |
19 | + if (data.address) { | |
20 | + self.address = new Uint8Array(data.address) | |
21 | + } | |
22 | + | |
23 | + if (data.origin) { | |
24 | + self.origin = new Uint8Array(data.origin) | |
25 | + } | |
26 | + | |
27 | + if (data.caller) { | |
28 | + self.caller = new Uint8Array(data.caller) | |
29 | + } | |
30 | + | |
31 | + if (data.callValue) { | |
32 | + self.callValue = new Uint8Array(data.callValue) | |
33 | + } | |
34 | + | |
35 | + if (data.callData) { | |
36 | + self.callData = hexStr2arrayBuf(data.callData) | |
37 | + } | |
38 | + } | |
39 | +} | |
40 | + | |
41 | +function hexStr2arrayBuf (string) { | |
42 | + const ab = new ArrayBuffer(string.length / 2) | |
43 | + const view = new Uint8Array(ab) | |
44 | + string = [...string] | |
45 | + let temp = '' | |
46 | + string.forEach((el, i) => { | |
47 | + temp += el | |
48 | + if (i % 2) { | |
49 | + view[(i + 1) / 2 - 1] = parseInt(temp, 16) | |
50 | + temp = '' | |
51 | + } | |
52 | + }) | |
53 | + return ab | |
54 | +} |
Built with git-ssb-web