-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathtest_class_methods.py
532 lines (451 loc) · 12.6 KB
/
test_class_methods.py
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
import unittest
import hypothesis
from hypothesis import given
from hypothesis import strategies as st
from .utils import eval_uplc_value
from . import PLUTUS_VM_PROFILE
from opshin.util import CompilerError
hypothesis.settings.load_profile(PLUTUS_VM_PROFILE)
class Classmethod_tests(unittest.TestCase):
@given(x=st.integers(), y=st.integers())
def test_accessible_attributes(self, x: int, y: int):
source_code = """
from opshin.prelude import *
@dataclass()
class Foo(PlutusData):
a: int
b: int
def sum(self) -> int:
return self.a + self.b
def validator(a: int, b: int) -> int:
foo = Foo(a, b)
return foo.sum()
"""
ret = eval_uplc_value(source_code, x, y)
self.assertEqual(ret, x + y)
def test_instance_method_only(self, x=5, y=6):
source_code = """
from opshin.prelude import *
@dataclass()
class Foo(PlutusData):
a: int
b: int
def sum(self) -> int:
return self.a + self.b
def validator(a: int, b: int) -> int:
return Foo.sum()
"""
with self.assertRaises(Exception):
ret = eval_uplc_value(source_code, x, y)
@given(x=st.integers(), y=st.integers())
def test_le_dunder(self, x: int, y: int):
source_code = """
from opshin.prelude import *
@dataclass()
class Foo(PlutusData):
a: int
def __le__(self, other:int) -> bool:
return self.a <= other
def validator(a: int, b: int) -> bool:
foo1 = Foo(a)
return foo1 <= b
"""
ret = eval_uplc_value(source_code, x, y)
self.assertEqual(ret, x <= y)
def test_invalid_python(self, x=5, y=6):
source_code = """
from opshin.prelude import *
@dataclass()
class Foo(PlutusData):
a: int
def __ge__(self, other: Foo) -> bool:
return self.a >= other.a
def validator(a: int, b: int) -> bool:
foo1 = Foo(a)
foo2 = Foo(b)
return foo1 >= foo2
"""
with self.assertRaises(CompilerError):
ret = eval_uplc_value(source_code, x, y)
@given(x=st.integers(), y=st.integers())
def test_Self_arguments(self, x: int, y: int):
source_code = """
from typing import Self
from opshin.prelude import *
@dataclass()
class Foo(PlutusData):
a: int
def __ge__(self, other: Self) -> bool:
return self.a >= other.a
def validator(a: int, b: int) -> bool:
foo1 = Foo(a)
foo2 = Foo(b)
return foo1 >= foo2
"""
ret = eval_uplc_value(source_code, x, y)
self.assertEqual(ret, x >= y)
def test_Self_return(self):
source_code = """
from typing import Self
from opshin.prelude import *
@dataclass()
class Foo(PlutusData):
a: int
def get_me(self) -> Self:
return self
def validator(b:int) -> int:
foo1 = Foo(b)
return foo1.get_me().a
"""
ret = eval_uplc_value(source_code, 5)
self.assertEqual(ret, 5)
@given(x=st.integers(), y=st.integers())
def test_externally_bound_variables_scope(self, x: int, y: int):
source_code = """
from typing import Self
from opshin.prelude import *
@dataclass()
class Foo(PlutusData):
a: int
def larger(self, other:Self) -> Self:
if self.a>other.a:
return self
else:
return other
def validator(a:int, b:int) -> int:
foo1 = Foo(a)
foo2 = Foo(b)
return foo1.larger(foo2).a
"""
ret = eval_uplc_value(source_code, x, y)
self.assertEqual(ret, max(x, y))
@given(x=st.integers(), y=st.integers())
def test_eq_dunder(self, x: int, y: int):
source_code = """
from typing import Self
from opshin.prelude import *
@dataclass()
class Foo(PlutusData):
a: int
def __eq__(self, other:Self) -> bool:
return self.a==other.a
def validator(a:int, b:int) -> bool:
foo1 = Foo(a)
foo2 = Foo(b)
return foo1 == foo2
"""
ret = eval_uplc_value(source_code, x, y)
self.assertEqual(ret, x == y)
@given(x=st.integers(), y=st.integers())
def test_ne_dunder(self, x: int, y: int):
source_code = """
from typing import Self
from opshin.prelude import *
@dataclass()
class Foo(PlutusData):
a: int
def __ne__(self, other:Self) -> bool:
return self.a!=other.a
def validator(a:int, b:int) -> bool:
foo1 = Foo(a)
foo2 = Foo(b)
return foo1 != foo2
"""
ret = eval_uplc_value(source_code, x, y)
self.assertEqual(ret, x != y)
@given(x=st.integers(), y=st.integers())
def test_lt_dunder(self, x: int, y: int):
source_code = """
from typing import Self
from opshin.prelude import *
@dataclass()
class Foo(PlutusData):
a: int
def __lt__(self, other:Self) -> bool:
return self.a<other.a
def validator(a:int, b:int) -> bool:
foo1 = Foo(a)
foo2 = Foo(b)
return foo1 < foo2
"""
ret = eval_uplc_value(source_code, x, y)
self.assertEqual(ret, x < y)
@given(x=st.integers(), y=st.integers())
def test_le_dunder(self, x: int, y: int):
source_code = """
from typing import Self
from opshin.prelude import *
@dataclass()
class Foo(PlutusData):
a: int
def __le__(self, other:Self) -> bool:
return self.a<=other.a
def validator(a:int, b:int) -> bool:
foo1 = Foo(a)
foo2 = Foo(b)
return foo1 <= foo2
"""
ret = eval_uplc_value(source_code, x, y)
self.assertEqual(ret, x <= y)
@given(x=st.integers(), y=st.integers())
def test_gt_dunder(self, x: int, y: int):
source_code = """
from typing import Self
from opshin.prelude import *
@dataclass()
class Foo(PlutusData):
a: int
def __gt__(self, other:Self) -> bool:
return self.a>other.a
def validator(a:int, b:int) -> bool:
foo1 = Foo(a)
foo2 = Foo(b)
return foo1 > foo2
"""
ret = eval_uplc_value(source_code, x, y)
self.assertEqual(ret, x > y)
@given(x=st.integers(), y=st.integers())
def test_ge_dunder(self, x: int, y: int):
source_code = """
from typing import Self
from opshin.prelude import *
@dataclass()
class Foo(PlutusData):
a: int
def __ge__(self, other:Self) -> bool:
return self.a>=other.a
def validator(a:int, b:int) -> bool:
foo1 = Foo(a)
foo2 = Foo(b)
return foo1 >= foo2
"""
ret = eval_uplc_value(source_code, x, y)
self.assertEqual(ret, x >= y)
@given(x=st.integers(), y=st.integers())
def test_add_dunder(self, x: int, y: int):
source_code = """
from typing import Self
from opshin.prelude import *
@dataclass()
class Foo(PlutusData):
a: int
def __add__(self, other:Self) -> int:
return self.a + other.a
def validator(a:int, b:int) -> int:
foo1 = Foo(a)
foo2 = Foo(b)
return foo1 + foo2
"""
ret = eval_uplc_value(source_code, x, y)
self.assertEqual(ret, x + y)
@given(x=st.integers(), y=st.integers())
def test_sub_dunder(self, x: int, y: int):
source_code = """
from typing import Self
from opshin.prelude import *
@dataclass()
class Foo(PlutusData):
a: int
def __sub__(self, other:Self) -> int:
return self.a - other.a
def validator(a:int, b:int) -> int:
foo1 = Foo(a)
foo2 = Foo(b)
return foo1 - foo2
"""
ret = eval_uplc_value(source_code, x, y)
self.assertEqual(ret, x - y)
@given(x=st.integers(), y=st.integers())
def test_mul_dunder(self, x: int, y: int):
source_code = """
from typing import Self
from opshin.prelude import *
@dataclass()
class Foo(PlutusData):
a: int
def __mul__(self, other:Self) -> int:
return self.a * other.a
def validator(a:int, b:int) -> int:
foo1 = Foo(a)
foo2 = Foo(b)
return foo1 * foo2
"""
ret = eval_uplc_value(source_code, x, y)
self.assertEqual(ret, x * y)
@given(x=st.integers(), y=st.integers().filter(lambda x: x != 0))
def test_floordiv_dunder(self, x: int, y: int):
source_code = """
from typing import Self
from opshin.prelude import *
@dataclass()
class Foo(PlutusData):
a: int
def __floordiv__(self, other:Self) -> int:
return self.a // other.a
def validator(a:int, b:int) -> int:
foo1 = Foo(a)
foo2 = Foo(b)
return foo1 // foo2
"""
ret = eval_uplc_value(source_code, x, y)
self.assertEqual(ret, x // y)
@given(x=st.integers(), y=st.integers().filter(lambda x: x != 0))
def test_mod_dunder(self, x: int, y: int):
source_code = """
from typing import Self
from opshin.prelude import *
@dataclass()
class Foo(PlutusData):
a: int
def __mod__(self, other:Self) -> int:
return self.a % other.a
def validator(a:int, b:int) -> int:
foo1 = Foo(a)
foo2 = Foo(b)
return foo1 % foo2
"""
ret = eval_uplc_value(source_code, x, y)
self.assertEqual(ret, x % y)
@given(
x=st.integers(min_value=-1000, max_value=1000),
y=st.integers(min_value=0, max_value=4),
)
def test_pow_dunder(self, x: int, y: int):
source_code = """
from typing import Self
from opshin.prelude import *
@dataclass()
class Foo(PlutusData):
a: int
def __pow__(self, other:Self) -> int:
return self.a ** other.a
def validator(a:int, b:int) -> int:
foo1 = Foo(a)
foo2 = Foo(b)
return foo1 ** foo2
"""
ret = eval_uplc_value(source_code, x, y)
self.assertEqual(ret, x**y)
@given(x=st.integers(), y=st.integers(), z=st.integers(), a=st.integers())
def test_matmul_dunder(self, x: int, y: int, z: int, a: int):
source_code = """
from typing import Self
from opshin.prelude import *
@dataclass()
class Foo(PlutusData):
a: int
b: int
def __matmul__(self, other:Self) -> int:
return self.a * other.a + self.b*other.b
def validator(a:int, b:int, c: int, d:int) -> int:
foo1 = Foo(a,b)
foo2 = Foo(c,d)
return foo1 @ foo2
"""
ret = eval_uplc_value(source_code, x, y, z, a)
self.assertEqual(ret, x * z + y * a)
def test_unsupported_dunder(
self,
):
source_code = """
from typing import Self
from opshin.prelude import *
@dataclass()
class Foo(PlutusData):
a: int
def __init__(self) -> None:
pass
def __add__(self, other:Self) -> int:
return self.a + other.a
def validator(a:int, b:int) -> int:
foo1 = Foo(a)
foo2 = Foo(b)
return foo1 + foo2
"""
with self.assertRaises(CompilerError):
ret = eval_uplc_value(source_code, 5, 6)
@given(x=st.integers(), y=st.integers().filter(lambda x: x != 0))
def test_truediv_dunder(self, x: int, y: int):
source_code = """
from typing import Self
from opshin.prelude import *
@dataclass()
class Foo(PlutusData):
a: int
def __truediv__(self, other:Self) -> int:
#map true div to floordiv
return self.a // other.a
def validator(a:int, b:int) -> int:
foo1 = Foo(a)
foo2 = Foo(b)
return foo1 / foo2
"""
ret = eval_uplc_value(source_code, x, y)
self.assertEqual(ret, x // y)
def test_not_dunder(self):
source_code = """
from typing import Self
from opshin.prelude import *
@dataclass()
class Foo(PlutusData):
a: int
def __bool__(self,) -> bool:
return self.a!=0
def validator(a: int) -> bool:
foo1 = Foo(a)
return not foo1
"""
ret = eval_uplc_value(source_code, 3)
self.assertEqual(ret, False)
ret = eval_uplc_value(source_code, 0)
self.assertEqual(ret, True)
@given(x=st.integers())
def test_neg_dunder(self, x: int):
source_code = """
from typing import Self
from opshin.prelude import *
@dataclass()
class Foo(PlutusData):
a: int
def __neg__(self,) -> int:
return -self.a
def validator(a: int) -> int:
foo1 = Foo(a)
return -foo1
"""
ret = eval_uplc_value(source_code, x)
self.assertEqual(ret, -x)
@given(x=st.integers(), y=st.integers(), z=st.integers())
def test_in_dunder(self, x: int, y: int, z: int):
source_code = """
from typing import Self
from opshin.prelude import *
@dataclass()
class Foo(PlutusData):
a: int
b: int
def __contains__(self, c: int) -> bool:
return self.a==c or self.b==c
def validator(a: int, b: int, c:int) -> bool:
foo1 = Foo(a, b)
return c in foo1
"""
ret = eval_uplc_value(source_code, x, y, z)
self.assertEqual(ret, z in [x, y])
@given(x=st.integers(), y=st.integers(), z=st.integers())
def test_Notin_dunder(self, x: int, y: int, z: int):
source_code = """
from typing import Self
from opshin.prelude import *
@dataclass()
class Foo(PlutusData):
a: int
b: int
def __contains__(self, c: int) -> bool:
return self.a==c or self.b==c
def validator(a: int, b: int, c:int) -> bool:
foo1 = Foo(a, b)
return c not in foo1
"""
ret = eval_uplc_value(source_code, x, y, z)
self.assertEqual(ret, z not in [x, y])