/
test__examples__StringStorage.js
138 lines (123 loc) · 4.01 KB
/
test__examples__StringStorage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
"use strict";
const StringStorage = artifacts.require("StringStorage");
//
contract("test/examples/StringStorage.sol", (accounts) => {
const owner = accounts[0];
const key = "key";
const value = "value";
//
afterEach(async () => {
const instance = await StringStorage.deployed();
if (await instance.has(key)) {
await instance.remove(key);
}
});
//
it("StringStorage.get returns expected value", async () => {
const instance = await StringStorage.deployed();
await instance.set(key, value, { from: owner });
const got_value = await instance.get(key);
return assert.equal(got_value, value, "Failed to get expected value");
});
//
it("StringStorage.has returns expected value", async () => {
const instance = await StringStorage.deployed();
assert.isFalse(await instance.has(key), "Failed to get expected value");
await instance.set(key, value, { from: owner });
return assert.isTrue(
await instance.has(key),
"Failed to get expected value"
);
});
//
it("StringStorage.listKeys returns expected value", async () => {
const instance = await StringStorage.deployed();
assert.deepEqual([], await instance.listKeys(), "Key list not empty");
await instance.set(key, value, { from: owner });
return assert.deepEqual(
[key],
await instance.listKeys(),
"Key list empty?"
);
});
//
it("StringStorage.remove allowed from owner", async () => {
const instance = await StringStorage.deployed();
await instance.set(key, value, { from: owner });
await instance.remove(key, { from: owner });
try {
await instance.remove(key, { from: owner });
} catch (error) {
if (
error.reason === "StringStorage.remove: value not defined"
) {
return assert.isTrue(true, "Wat!?");
}
console.error({ error });
}
const {
words: [got_size],
} = await instance.size();
return assert.equal(got_size, 0, "StringStorage size did not decrease");
});
//
it("StringStorage.remove disallowed from non-owner", async () => {
const instance = await StringStorage.deployed();
await instance.set(key, value, { from: owner });
try {
await instance.remove(key, { from: accounts[9] });
} catch (error) {
if (
error.reason === "StringStorage.remove: message sender not an owner"
) {
return assert.isTrue(true, "Wat!?");
}
console.error({ error });
}
return assert.isTrue(false, "Failed to catch expected error reason");
});
//
it("StringStorage.selfDestruct allowed from owner", async () => {
const instance = await StringStorage.new(owner);
await instance.selfDestruct(owner, { from: owner });
return assert.isTrue(true, "Wat!?");
});
//
it("StringStorage.selfDestruct disallowed from non-owner", async () => {
const instance = await StringStorage.new(owner);
return assert.isTrue(true, "Wat!?");
try {
await instance.selfDestruct(owner, { from: accounts[9] });
} catch (error) {
if (error.reason === "StringStorage.selfDestruct: message sender not an owner") {
return assert.isTrue(true, "Wat!?");
}
console.error({ error });
}
return assert.isTrue(false, "Failed to catch expected error reason");
});
//
it("StringStorage.set allowed from owner", async () => {
const instance = await StringStorage.deployed();
await instance.set(key, value, { from: owner });
return assert.isTrue(
await instance.has(key),
"Failed to set key/value pair"
);
});
//
it("StringStorage.set disallows from non-owner", async () => {
const instance = await StringStorage.deployed();
try {
await instance.set(key, value, {
from: accounts[9],
});
} catch (error) {
if (error.reason === "StringStorage.set: message sender not an owner") {
return assert.isTrue(true, "Wat!?");
}
console.error({ error });
}
return assert.isTrue(false, "Failed to catch expected error reason");
});
});