forked from vyperlang/vyper
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_block.py
More file actions
160 lines (148 loc) · 2.96 KB
/
Copy pathtest_block.py
File metadata and controls
160 lines (148 loc) · 2.96 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
import pytest
from pytest import (
raises,
)
from vyper import (
compiler,
)
from vyper.exceptions import (
TypeMismatchException,
)
fail_list = [
"""
@public
def foo() -> int128:
x: address = create_forwarder_to(
0x1234567890123456789012345678901234567890,
value=block.timestamp,
)
return 5
""",
"""
@public
def foo() -> int128[2]:
return [3,block.timestamp]
""",
"""
@public
def foo() -> timedelta[2]:
return [block.timestamp - block.timestamp, block.timestamp]
""",
"""
@public
def foo() -> decimal(wei / sec):
x: int128(wei) = as_wei_value(5, "finney")
y: int128 = block.timestamp + 50
return x / y
""",
"""
@public
def foo():
x: bytes[10] = slice("cow", start=0, len=block.timestamp)
""",
"""
@public
def foo():
x: int128 = 7
y: int128 = min(x, block.timestamp)
""",
"""
@public
def foo():
y = min(block.timestamp + 30 - block.timestamp, block.timestamp)
""",
"""
a: map(timestamp, int128)
@public
def add_record():
self.a[block.timestamp] = block.timestamp + 20
""",
"""
a: map(int128, timestamp)
@public
def add_record():
self.a[block.timestamp] = block.timestamp + 20
""",
"""
struct X:
x: timestamp
struct Y:
y: int128
@public
def add_record():
a: X = X({x: block.timestamp})
b: Y = Y({y: 5})
a.x = b.y
""",
"""
@public
def foo(inp: bytes[10]) -> bytes[3]:
return slice(inp, start=block.timestamp, len=3)
""",
"""
@public
def foo() -> address:
return as_unitless_number(block.coinbase)
""",
("""
@public
def foo() -> int128:
return block.fail
""", Exception)
]
@pytest.mark.parametrize('bad_code', fail_list)
def test_block_fail(bad_code):
if isinstance(bad_code, tuple):
with raises(bad_code[1]):
compiler.compile_code(bad_code[0])
else:
with raises(TypeMismatchException):
compiler.compile_code(bad_code)
valid_list = [
"""
a: map(timestamp, timestamp)
@public
def add_record():
self.a[block.timestamp] = block.timestamp + 20
""",
"""
@public
def foo() -> uint256(wei / sec):
x: uint256(wei) = as_wei_value(5, "finney")
y: uint256(sec) = block.timestamp + 50 - block.timestamp
return x / y
""",
"""
@public
def foo() -> timestamp[2]:
return [block.timestamp + 86400, block.timestamp]
""",
"""
@public
def foo():
y: timestamp = min(block.timestamp + 30, block.timestamp + 50)
""",
"""
@public
def foo() -> uint256:
return as_unitless_number(block.timestamp)
""",
"""
struct X:
x: timestamp
@public
def add_record():
a: X = X({x: block.timestamp})
a.x = 5
""",
"""
@public
def foo():
x: uint256 = block.difficulty + 185
if tx.origin == self:
y: bytes[35] = concat(block.prevhash, b"dog")
"""
]
@pytest.mark.parametrize('good_code', valid_list)
def test_block_success(good_code):
assert compiler.compile_code(good_code) is not None