forked from OpenZeppelin/contracts-wizard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontract.test.ts
More file actions
195 lines (166 loc) · 5.86 KB
/
Copy pathcontract.test.ts
File metadata and controls
195 lines (166 loc) · 5.86 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import test from 'ava';
import { ContractBuilder } from './contract';
import { printContract } from './print';
import { TAG_SECURITY_CONTACT } from './set-info';
const toContractReference = (name: string) => {
return {
name: name,
};
};
const toParentContract = (name: string, path: string) => {
return {
name: name,
path: path,
};
};
test('contract basics', t => {
const Foo = new ContractBuilder('Foo');
t.snapshot(printContract(Foo));
});
test('contract name is unicodeSafe', t => {
const Foo = new ContractBuilder('Footeć');
t.snapshot(printContract(Foo));
});
test('contract with a parent', t => {
const Foo = new ContractBuilder('Foo');
const Bar = toParentContract('Bar', './Bar.sol');
Foo.addParent(Bar);
t.snapshot(printContract(Foo));
});
test('contract with two parents', t => {
const Foo = new ContractBuilder('Foo');
const Bar = toParentContract('Bar', './Bar.sol');
const Quux = toParentContract('Quux', './Quux.sol');
Foo.addParent(Bar);
Foo.addParent(Quux);
t.snapshot(printContract(Foo));
});
test('contract with a parent with parameters', t => {
const Foo = new ContractBuilder('Foo');
const Bar = toParentContract('Bar', './Bar.sol');
Foo.addParent(Bar, ['param1', 'param2']);
t.snapshot(printContract(Foo));
});
test('contract with two parents only one with parameters', t => {
const Foo = new ContractBuilder('Foo');
const Bar = toParentContract('Bar', './Bar.sol');
const Quux = toParentContract('Quux', './Quux.sol');
Foo.addParent(Bar, ['param1', 'param2']);
Foo.addParent(Quux);
t.snapshot(printContract(Foo));
});
test('contract with one override', t => {
const Foo = new ContractBuilder('Foo');
const _beforeTokenTransfer = {
name: '_beforeTokenTransfer',
kind: 'internal' as const,
args: [
{ name: 'from', type: 'address' },
{ name: 'to', type: 'address' },
{ name: 'amount', type: 'uint256' },
],
};
Foo.addOverride(toContractReference('ERC20'), _beforeTokenTransfer);
t.snapshot(printContract(Foo));
});
test('contract with two overrides', t => {
const Foo = new ContractBuilder('Foo');
Foo.addOverride(toContractReference('ERC20'), _beforeTokenTransfer);
Foo.addOverride(toContractReference('ERC20Snapshot'), _beforeTokenTransfer);
t.snapshot(printContract(Foo));
});
test('contract with two different overrides', t => {
const Foo = new ContractBuilder('Foo');
Foo.addOverride(toContractReference('ERC20'), _beforeTokenTransfer);
Foo.addOverride(toContractReference('OtherParent'), _beforeTokenTransfer);
Foo.addOverride(toContractReference('ERC20'), _otherFunction);
Foo.addOverride(toContractReference('OtherParent'), _otherFunction);
t.snapshot(printContract(Foo));
});
test('contract with a modifier', t => {
const Foo = new ContractBuilder('Foo');
Foo.addModifier('whenNotPaused', _otherFunction);
t.snapshot(printContract(Foo));
});
test('contract with a modifier and override', t => {
const Foo = new ContractBuilder('Foo');
Foo.addModifier('whenNotPaused', _otherFunction);
Foo.addOverride(toContractReference('ERC20'), _otherFunction);
Foo.addOverride(toContractReference('OtherParent'), _otherFunction);
t.snapshot(printContract(Foo));
});
test('contract with constructor code', t => {
const Foo = new ContractBuilder('Foo');
Foo.addConstructorCode('_mint(msg.sender, 10 ether);');
t.snapshot(printContract(Foo));
});
test('contract with constructor code and a parent', t => {
const Foo = new ContractBuilder('Foo');
const Bar = toParentContract('Bar', './Bar.sol');
Foo.addParent(Bar, ['param1', 'param2']);
Foo.addConstructorCode('_mint(msg.sender, 10 ether);');
t.snapshot(printContract(Foo));
});
test('contract with function code', t => {
const Foo = new ContractBuilder('Foo');
Foo.addFunctionCode('_mint(msg.sender, 10 ether);', _otherFunction);
t.snapshot(printContract(Foo));
});
test('contract with overridden function with code', t => {
const Foo = new ContractBuilder('Foo');
Foo.addOverride(toContractReference('Bar'), _otherFunction);
Foo.addFunctionCode('_mint(msg.sender, 10 ether);', _otherFunction);
t.snapshot(printContract(Foo));
});
test('contract with one variable', t => {
const Foo = new ContractBuilder('Foo');
Foo.addStateVariable('uint value = 42;', false);
t.snapshot(printContract(Foo));
});
test('contract with two variables', t => {
const Foo = new ContractBuilder('Foo');
Foo.addStateVariable('uint value = 42;', false);
Foo.addStateVariable('string name = "john";', false);
t.snapshot(printContract(Foo));
});
test('contract with immutable', t => {
const Foo = new ContractBuilder('Foo');
Foo.addConstantOrImmutableOrErrorDefinition('uint immutable value = 42;');
t.snapshot(printContract(Foo));
});
test('contract with constant and comment', t => {
const Foo = new ContractBuilder('Foo');
Foo.addConstantOrImmutableOrErrorDefinition('uint constant value = 42;', ['// This is a comment']);
t.snapshot(printContract(Foo));
});
test('contract with variable and upgradeable - error', t => {
const Foo = new ContractBuilder('Foo');
const error = t.throws(() => Foo.addStateVariable('uint value = 42;', true));
t.is(
(error as Error).message,
'State variables should not be used when upgradeable is true. Set namespaced storage instead.',
);
});
test('name with special characters', t => {
const Foo = new ContractBuilder('foo bar baz');
t.snapshot(printContract(Foo));
});
test('contract with info', t => {
const Foo = new ContractBuilder('Foo');
Foo.addNatspecTag(TAG_SECURITY_CONTACT, 'security@example.com');
t.snapshot(printContract(Foo));
});
const _beforeTokenTransfer = {
name: '_beforeTokenTransfer',
kind: 'internal' as const,
args: [
{ name: 'from', type: 'address' },
{ name: 'to', type: 'address' },
{ name: 'amount', type: 'uint256' },
],
};
const _otherFunction = {
name: '_otherFunction',
kind: 'internal' as const,
args: [],
};