-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathtest_misc.py
3044 lines (2676 loc) · 83.5 KB
/
test_misc.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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import pycardano
import sys
import subprocess
import tempfile
import unittest
import frozendict
import frozenlist2
import hypothesis
from hypothesis import given
from hypothesis import strategies as st
from opshin import IndefiniteList
from parameterized import parameterized
from uplc import ast as uplc, eval as uplc_eval
from . import PLUTUS_VM_PROFILE
from opshin import prelude, builder, Purpose, PlutusContract
from .utils import eval_uplc_value, Unit, eval_uplc
from opshin.bridge import wraps_builtin
from opshin.compiler_config import OPT_O2_CONFIG, DEFAULT_CONFIG
hypothesis.settings.load_profile(PLUTUS_VM_PROFILE)
# these imports are required to eval the result of script context dumps
from opshin.ledger.api_v2 import *
from pycardano import RawPlutusData, RawCBOR
from cbor2 import CBORTag
DEFAULT_CONFIG_FORCE_THREE_PARAMS = DEFAULT_CONFIG.update(force_three_params=True)
DEFAULT_CONFIG_CONSTANT_FOLDING = DEFAULT_CONFIG.update(constant_folding=True)
ALL_EXAMPLES = [
os.path.join(root, f)
for root, dirs, files in os.walk("examples")
for f in files
if f.endswith(".py") and not f.startswith("broken") and not f.startswith("extract")
]
def fib(n):
a, b = 0, 1
for _ in range(n):
a, b = b, a + b
return a
@dataclass()
class A(PlutusData):
CONSTR_ID = 0
foo: int
@dataclass()
class B(PlutusData):
CONSTR_ID = 1
foobar: int
bar: int
a_or_b = st.sampled_from([A(0), B(1, 2)])
some_output = st.sampled_from([SomeOutputDatum(b"0"), SomeOutputDatumHash(b"1")])
class MiscTest(unittest.TestCase):
def test_assert_sum_contract_succeed(self):
input_file = "examples/smart_contracts/assert_sum.py"
with open(input_file) as fp:
source_code = fp.read()
ret = eval_uplc(
source_code,
20,
22,
uplc.data_from_cbor(
bytes.fromhex(
"d8799fd8799f9fd8799fd8799fd8799f582055d353acacaab6460b37ed0f0e3a1a0aabf056df4a7fa1e265d21149ccacc527ff01ffd8799fd8799fd87a9f581cdbe769758f26efb21f008dc097bb194cffc622acc37fcefc5372eee3ffd87a80ffa140a1401a00989680d87a9f5820dfab81872ce2bbe6ee5af9bbfee4047f91c1f57db5e30da727d5fef1e7f02f4dffd87a80ffffff809fd8799fd8799fd8799f581cdc315c289fee4484eda07038393f21dc4e572aff292d7926018725c2ffd87a80ffa140a14000d87980d87a80ffffa140a14000a140a1400080a0d8799fd8799fd87980d87a80ffd8799fd87b80d87a80ffff80a1d87a9fd8799fd8799f582055d353acacaab6460b37ed0f0e3a1a0aabf056df4a7fa1e265d21149ccacc527ff01ffffd87980a15820dfab81872ce2bbe6ee5af9bbfee4047f91c1f57db5e30da727d5fef1e7f02f4dd8799f581cdc315c289fee4484eda07038393f21dc4e572aff292d7926018725c2ffd8799f5820746957f0eb57f2b11119684e611a98f373afea93473fefbb7632d579af2f6259ffffd87a9fd8799fd8799f582055d353acacaab6460b37ed0f0e3a1a0aabf056df4a7fa1e265d21149ccacc527ff01ffffff"
)
),
config=DEFAULT_CONFIG.update(OPT_O2_CONFIG),
)
self.assertEqual(ret, uplc.PlutusConstr(0, []))
@unittest.expectedFailure
def test_assert_sum_contract_fail(self):
input_file = "examples/smart_contracts/assert_sum.py"
with open(input_file) as fp:
source_code = fp.read()
ret = eval_uplc(source_code, 0, 22, Unit())
@given(
a=st.integers(min_value=-10, max_value=10),
b=st.integers(min_value=0, max_value=10),
)
def test_mult_for(self, a: int, b: int):
input_file = "examples/mult_for.py"
with open(input_file) as fp:
source_code = fp.read()
ret = eval_uplc_value(source_code, a, b)
self.assertEqual(ret, a * b)
@given(
a=st.integers(min_value=-10, max_value=10),
b=st.integers(min_value=0, max_value=10),
)
def test_mult_for(self, a: int, b: int):
input_file = "examples/mult_for.py"
with open(input_file) as fp:
source_code = fp.read()
ret = eval_uplc_value(source_code, a, b)
self.assertEqual(ret, a * b)
@given(
a=st.integers(min_value=-10, max_value=10),
b=st.integers(min_value=0, max_value=10),
)
def test_mult_for_return(self, a: int, b: int):
source_code = """
def validator(a: int, b: int) -> int:
c = 0
i = 0
for i in range(b):
c += a
if i == 1:
return c
return c
"""
ret = eval_uplc_value(source_code, a, b)
self.assertEqual(ret, a * min(b, 2))
@given(
a=st.integers(min_value=-10, max_value=10),
b=st.integers(min_value=0, max_value=10),
)
def test_mult_while_return(self, a: int, b: int):
source_code = """
def validator(a: int, b: int) -> int:
c = 0
i = 0
while i < b:
c += a
i += 1
if i == 2:
return c
return c
"""
ret = eval_uplc_value(source_code, a, b)
self.assertEqual(ret, a * min(2, b))
@given(
a=st.integers(),
b=st.integers(),
)
def test_sum(self, a: int, b: int):
input_file = "examples/sum.py"
with open(input_file) as fp:
source_code = fp.read()
ret = eval_uplc_value(source_code, a, b)
self.assertEqual(ret, a + b)
def test_complex_datum_correct_vals(self):
input_file = "examples/complex_datum.py"
with open(input_file) as fp:
source_code = fp.read()
ret = eval_uplc_value(
source_code,
uplc.data_from_cbor(
bytes.fromhex(
"d8799fd8799fd8799f581c81aab0790f33d26bad68a6a13ae98562aa1366da48cdce20dec21acfffd8799fd8799fd8799f581c145db8343296bd214dde862a64d700c29ed8a71d58bcf865659f5463ffffffffd8799fd8799f581c81aab0790f33d26bad68a6a13ae98562aa1366da48cdce20dec21acfffd8799fd8799fd8799f581c145db8343296bd214dde862a64d700c29ed8a71d58bcf865659f5463ffffffffd87a80d8799f1a38220b0bff1a001e84801a001e8480582051176daeee7f2ce62963c50a16f641951e21b8522da262980d4dd361a9bf331b4e4d7565736c69537761705f414d4dff"
)
),
)
self.assertEqual(
bytes.fromhex("81aab0790f33d26bad68a6a13ae98562aa1366da48cdce20dec21acf"),
ret,
)
def test_hello_world(self):
input_file = "examples/hello_world.py"
with open(input_file) as fp:
source_code = fp.read()
ret = eval_uplc(source_code, Unit())
def test_list_datum_correct_vals(self):
input_file = "examples/list_datum.py"
with open(input_file) as fp:
source_code = fp.read()
ret = eval_uplc_value(
source_code, uplc.data_from_cbor(bytes.fromhex("d8799f9f41014102ffff"))
)
self.assertEqual(
1,
ret,
)
def test_showcase(self):
input_file = "examples/showcase.py"
with open(input_file) as fp:
source_code = fp.read()
ret = eval_uplc_value(source_code, 1)
self.assertEqual(
42,
ret,
)
@given(n=st.integers(min_value=0, max_value=5))
def test_fib_iter(self, n):
input_file = "examples/fib_iter.py"
with open(input_file) as fp:
source_code = fp.read()
ret = eval_uplc_value(source_code, n)
self.assertEqual(
fib(n),
ret,
)
@given(n=st.integers(min_value=0, max_value=5))
def test_fib_rec(self, n):
input_file = "examples/fib_rec.py"
with open(input_file) as fp:
source_code = fp.read()
ret = eval_uplc_value(source_code, n)
self.assertEqual(
fib(n),
ret,
)
def test_gift_contract_succeed(self):
input_file = "examples/smart_contracts/gift.py"
with open(input_file) as fp:
source_code = fp.read()
ret = eval_uplc(
source_code,
uplc.PlutusConstr(
0,
[
uplc.PlutusByteString(
bytes.fromhex(
"dc315c289fee4484eda07038393f21dc4e572aff292d7926018725c2"
)
)
],
),
uplc.PlutusConstr(0, []),
uplc.data_from_cbor(
bytes.fromhex(
(
"d8799fd8799f9fd8799fd8799fd8799f582055d353acacaab6460b37ed0f0e3a1a0aabf056df4a7fa1e265d21149ccacc527ff01ffd8799fd8799fd87a9f581cdbe769758f26efb21f008dc097bb194cffc622acc37fcefc5372eee3ffd87a80ffa140a1401a00989680d87a9f5820dfab81872ce2bbe6ee5af9bbfee4047f91c1f57db5e30da727d5fef1e7f02f4dffd87a80ffffff809fd8799fd8799fd8799f581cdc315c289fee4484eda07038393f21dc4e572aff292d7926018725c2ffd87a80ffa140a14000d87980d87a80ffd8799fd8799fd8799f581cdc315c289fee4484eda07038393f21dc4e572aff292d7926018725c2ffd87a80ffa140a1401a000f4240d87980d87a80ffffa140a14000a140a1400080a0d8799fd8799fd87a9f1b000001836ac117d8ffd87a80ffd8799fd87b80d87a80ffff9f581cdc315c289fee4484eda07038393f21dc4e572aff292d7926018725c2ffa1d87a9fd8799fd8799f582055d353acacaab6460b37ed0f0e3a1a0aabf056df4a7fa1e265d21149ccacc527ff01ffffd87980a15820dfab81872ce2bbe6ee5af9bbfee4047f91c1f57db5e30da727d5fef1e7f02f4dd8799f581cdc315c289fee4484eda07038393f21dc4e572aff292d7926018725c2ffd8799f5820c17c32f6433ae22c2acaebfb796bbfaee3993ff7ebb58a2bac6b4a3bdd2f6d28ffffd87a9fd8799fd8799f582055d353acacaab6460b37ed0f0e3a1a0aabf056df4a7fa1e265d21149ccacc527ff01ffffff"
)
)
),
)
self.assertEqual(ret, uplc.PlutusConstr(0, []))
@unittest.expectedFailure
def test_gift_contract_fail(self):
input_file = "examples/smart_contracts/gift.py"
with open(input_file) as fp:
source_code = fp.read()
# required sig missing int this script context
ret = eval_uplc(
source_code,
uplc.PlutusConstr(
0,
[
uplc.PlutusByteString(
bytes.fromhex(
"dc315c289fee4484eda07038393f21dc4e572aff292d7926018725c2"
)
)
],
),
uplc.PlutusConstr(0, []),
uplc.data_from_cbor(
bytes.fromhex(
(
"d8799fd8799f9fd8799fd8799fd8799f582055d353acacaab6460b37ed0f0e3a1a0aabf056df4a7fa1e265d21149ccacc527ff01ffd8799fd8799fd87a9f581cdbe769758f26efb21f008dc097bb194cffc622acc37fcefc5372eee3ffd87a80ffa140a1401a00989680d87a9f5820dfab81872ce2bbe6ee5af9bbfee4047f91c1f57db5e30da727d5fef1e7f02f4dffd87a80ffffff809fd8799fd8799fd8799f581cdc315c289fee4484eda07038393f21dc4e572aff292d7926018725c2ffd87a80ffa140a14000d87980d87a80ffffa140a14000a140a1400080a0d8799fd8799fd87a9f1b000001836ac117d8ffd87a80ffd8799fd87b80d87a80ffff80a1d87a9fd8799fd8799f582055d353acacaab6460b37ed0f0e3a1a0aabf056df4a7fa1e265d21149ccacc527ff01ffffd87980a15820dfab81872ce2bbe6ee5af9bbfee4047f91c1f57db5e30da727d5fef1e7f02f4dd8799f581cdc315c289fee4484eda07038393f21dc4e572aff292d7926018725c2ffd8799f5820797a1e1720b63621c6b185088184cb8e23af6e46b55bd83e7a91024c823a6c2affffd87a9fd8799fd8799f582055d353acacaab6460b37ed0f0e3a1a0aabf056df4a7fa1e265d21149ccacc527ff01ffffff"
)
)
),
)
def test_recursion_simple(self):
source_code = """
def validator(_: None) -> int:
def a(n: int) -> int:
if n == 0:
res = 0
else:
res = a(n-1)
return res
return a(1)
"""
ret = eval_uplc_value(source_code, Unit())
self.assertEqual(0, ret)
@unittest.expectedFailure
def test_recursion_illegal(self):
# this is now an illegal retyping because read variables dont match
source_code = """
def validator(_: None) -> int:
def a(n: int) -> int:
if n == 0:
res = 0
else:
res = a(n-1)
return res
b = a
def a(x: int) -> int:
return 100
return b(1)
"""
ret = eval_uplc_value(source_code, Unit())
self.assertEqual(100, ret)
def test_recursion_legal(self):
source_code = """
def validator(_: None) -> int:
def a(n: int) -> int:
if n == 0:
res = 0
else:
res = a(n-1)
return res
b = a
def a(n: int) -> int:
a
if 1 == n:
pass
return 100
return b(1)
"""
ret = eval_uplc_value(source_code, Unit())
self.assertEqual(100, ret)
@unittest.expectedFailure
def test_uninitialized_access(self):
source_code = """
def validator(_: None) -> int:
b = 1
def a(n: int) -> int:
b += 1
if b == 2:
return 0
else:
return a(n-1)
return a(1)
"""
builder._compile(source_code)
@unittest.expectedFailure
def test_illegal_bind(self):
source_code = """
def validator(_: None) -> int:
b = 1
def a(n: int) -> int:
if n == 0:
return 100
if b == 2:
return 0
b = 2
return a(n-1)
return a(2)
"""
builder._compile(source_code)
@unittest.expectedFailure
def test_type_reassignment_function_bound(self):
# changing the type of a variable should be disallowed if the variable is bound by a function
# it can be ok if the types can be merged (resulting in union type inside the function) but
# generally should be disallowed
source_code = """
def validator(_: None) -> int:
b = 1
def a(n: int) -> int:
return b
b = b''
return a(1)
"""
builder._compile(source_code)
@unittest.expectedFailure
def test_illegal_function_retype(self):
source_code = """
def validator(_: None) -> int:
def a(n: int) -> int:
if n == 0:
res = 0
else:
res = a(n-1)
return res
b = a
def a() -> int:
return 100
return b(1)
"""
builder._compile(source_code)
def test_datum_cast(self):
input_file = "examples/datum_cast.py"
with open(input_file) as fp:
source_code = fp.read()
ret = eval_uplc_value(
source_code,
uplc.data_from_cbor(
bytes.fromhex(
"d8799fd8799fd8799f581c81aab0790f33d26bad68a6a13ae98562aa1366da48cdce20dec21acfffd8799fd8799fd8799f581c145db8343296bd214dde862a64d700c29ed8a71d58bcf865659f5463ffffffffd8799fd8799f581c81aab0790f33d26bad68a6a13ae98562aa1366da48cdce20dec21acfffd8799fd8799fd8799f581c145db8343296bd214dde862a64d700c29ed8a71d58bcf865659f5463ffffffffd87a80d8799f1a38220b0bff1a001e84801a001e8480582051176daeee7f2ce62963c50a16f641951e21b8522da262980d4dd361a9bf331b4e4d7565736c69537761705f414d4dff"
)
),
uplc.PlutusByteString(b"test"),
)
self.assertEqual(
bytes.fromhex("81aab0790f33d26bad68a6a13ae98562aa1366da48cdce20dec21acf")
+ b"test",
ret,
)
def test_wrapping_contract_compile(self):
# TODO devise tests for this
input_file = "examples/smart_contracts/wrapped_token.py"
with open(input_file) as fp:
source_code = fp.read()
builder._compile(source_code, config=DEFAULT_CONFIG_FORCE_THREE_PARAMS)
def test_dual_use_compile(self):
# TODO devise tests for this
input_file = "examples/smart_contracts/dual_use.py"
with open(input_file) as fp:
source_code = fp.read()
builder._compile(source_code, config=DEFAULT_CONFIG_FORCE_THREE_PARAMS)
def test_marketplace_compile(self):
# TODO devise tests for this
input_file = "examples/smart_contracts/marketplace.py"
with open(input_file) as fp:
source_code = fp.read()
builder._compile(source_code)
@unittest.expectedFailure
def test_marketplace_compile_fail(self):
input_file = "examples/smart_contracts/marketplace.py"
with open(input_file) as fp:
source_code = fp.read()
builder._compile(source_code, config=DEFAULT_CONFIG_FORCE_THREE_PARAMS)
def test_parameterized_compile(self):
# TODO devise tests for this
input_file = "examples/smart_contracts/parameterized.py"
with open(input_file) as fp:
source_code = fp.read()
builder._compile(source_code, config=DEFAULT_CONFIG_FORCE_THREE_PARAMS)
def test_dict_datum(self):
input_file = "examples/dict_datum.py"
with open(input_file) as fp:
source_code = fp.read()
d = uplc.PlutusConstr(
0,
[
uplc.PlutusMap(
frozendict.frozendict(
{
uplc.PlutusConstr(
0,
frozenlist2.frozenlist(
[uplc.PlutusByteString(b"\x01")]
),
): uplc.PlutusInteger(2)
}
)
)
],
)
ret = eval_uplc(source_code, d)
self.assertTrue(bool(ret))
def test_dict_datum_wrong(self):
input_file = "examples/dict_datum.py"
with open(input_file) as fp:
source_code = fp.read()
d = uplc.PlutusConstr(
0,
[
uplc.PlutusMap(
frozendict.frozendict(
{
uplc.PlutusConstr(
0,
frozenlist2.frozenlist(
[uplc.PlutusByteString(b"\x02")]
),
): uplc.PlutusInteger(2)
}
)
)
],
)
ret = eval_uplc_value(source_code, d)
self.assertFalse(bool(ret))
def test_removedeadvar_noissue(self):
source_code = """
from opshin.prelude import *
def validator(x: Token) -> bool:
b = 4
a = b
return True
"""
ret = eval_uplc_value(source_code, Unit())
self.assertEqual(ret, True)
def test_removedeadvar_noissue2(self):
source_code = """
from opshin.prelude import *
def validator(x: Token) -> bool:
def foo(x: Token) -> bool:
b = 4
a = b
return True
return foo(x)
"""
ret = eval_uplc_value(source_code, Unit())
self.assertEqual(ret, True)
def test_removedeadvar_noissue3(self):
source_code = """
from opshin.prelude import *
def foo(x: Token) -> bool:
b = 4
a = b
return True
def validator(x: Token) -> bool:
return foo(x)
"""
ret = eval_uplc_value(source_code, Unit())
self.assertEqual(ret, True)
@unittest.expectedFailure
def test_overopt_removedeadvar(self):
source_code = """
from opshin.prelude import *
def validator(x: Token) -> bool:
a = x.policy_id
return True
"""
ret = eval_uplc(source_code, Unit())
@unittest.expectedFailure
def test_opt_shared_var(self):
source_code = """
from opshin.prelude import *
def validator(x: Token) -> bool:
if False:
y = x
else:
a = y
return True
"""
ret = eval_uplc(source_code, Unit())
def test_list_expr(self):
# this tests that the list expression is evaluated correctly
source_code = """
def validator(x: None) -> List[int]:
return [1, 2, 3, 4, 5]
"""
ret = eval_uplc_value(source_code, Unit())
ret = [x.value for x in ret]
self.assertEqual(ret, [1, 2, 3, 4, 5], "List expression incorrectly compiled")
def test_list_expr_not_const(self):
# this tests that the list expression is evaluated correctly (for non-constant expressions)
source_code = """
def validator(x: int) -> List[int]:
return [x, x+1, x+2, x+3, x+4]
"""
ret = eval_uplc_value(source_code, 1)
ret = [x.value for x in ret]
self.assertEqual(ret, [1, 2, 3, 4, 5], "List expression incorrectly compiled")
def test_dict_expr_not_const(self):
# this tests that the list expression is evaluated correctly (for non-constant expressions)
source_code = """
def validator(x: int) -> Dict[int, bytes]:
return {x: b"a", x+1: b"b"}
"""
ret = eval_uplc_value(source_code, 1)
ret = {x.value: y.value for x, y in ret.items()}
self.assertEqual(
ret, {1: b"a", 2: b"b"}, "Dict expression incorrectly compiled"
)
def test_redefine_poly_constr(self):
# this tests that classes defined by assignment inherit constructors
source_code = """
def validator(x: None) -> bytes:
a = bytes
return a([2, 3])
"""
ret = eval_uplc_value(source_code, Unit())
self.assertEqual(ret, bytes([2, 3]), "Re-assignment of global variable failed")
@given(st.booleans())
def test_redefine_constr(self, x):
# this tests that classes defined by assignment inherit constructors
source_code = """
from dataclasses import dataclass
from typing import Dict, List, Union
from pycardano import Datum as Anything, PlutusData
@dataclass()
class A(PlutusData):
CONSTR_ID = 0
foo: int
bar: int
def validator(x: int) -> int:
a = A
return a(x, 1).foo
"""
ret = eval_uplc_value(source_code, int(x))
self.assertEqual(ret, int(x), "Re-assignment of class constr failed")
def test_wrap_into_generic_data(self):
# this tests data is wrapped into Anything if a function accepts Anything
source_code = """
from opshin.prelude import *
def validator(_: None) -> SomeOutputDatum:
return SomeOutputDatum(b"a")
"""
ret = eval_uplc(source_code, Unit())
self.assertEqual(
ret,
uplc.data_from_cbor(prelude.SomeOutputDatum(b"a").to_cbor()),
"Wrapping to generic data failed",
)
def test_list_comprehension_even(self):
input_file = "examples/list_comprehensions.py"
with open(input_file) as fp:
source_code = fp.read()
ret = eval_uplc_value(source_code, 8, 1)
ret = [x.value for x in ret]
self.assertEqual(
ret,
[x * x for x in range(8) if x % 2 == 0],
"List comprehension with filter incorrectly evaluated",
)
def test_list_comprehension_all(self):
input_file = "examples/list_comprehensions.py"
with open(input_file) as fp:
source_code = fp.read()
ret = eval_uplc_value(source_code, 8, 0)
ret = [x.value for x in ret]
self.assertEqual(
ret,
[x * x for x in range(8)],
"List comprehension incorrectly evaluated",
)
def test_dict_comprehension_even(self):
input_file = "examples/dict_comprehensions.py"
with open(input_file) as fp:
source_code = fp.read()
ret = eval_uplc_value(source_code, 8, 1)
ret = {x.value: y.value for x, y in ret.items()}
self.assertEqual(
ret,
{x: x * x for x in range(8) if x % 2 == 0},
"Dict comprehension incorrectly evaluated",
)
def test_dict_comprehension_all(self):
input_file = "examples/dict_comprehensions.py"
with open(input_file) as fp:
source_code = fp.read()
ret = eval_uplc_value(source_code, 8, 0)
ret = {x.value: y.value for x, y in ret.items()}
self.assertEqual(
ret,
{x: x * x for x in range(8)},
"Dict comprehension incorrectly evaluated",
)
@hypothesis.given(some_output)
def test_union_type_attr_access_all_records(self, x):
source_code = """
from opshin.prelude import *
@dataclass()
class A(PlutusData):
CONSTR_ID = 0
foo: SomeOutputDatumHash
@dataclass()
class B(PlutusData):
CONSTR_ID = 1
foo: SomeOutputDatum
def validator(x: Union[A, B]) -> Union[SomeOutputDatumHash, SomeOutputDatum]:
return x.foo
"""
@dataclass()
class A(PlutusData):
CONSTR_ID = 0
foo: SomeOutputDatumHash
@dataclass()
class B(PlutusData):
CONSTR_ID = 1
foo: SomeOutputDatum
x = A(x) if isinstance(x, SomeOutputDatumHash) else B(x)
ret = eval_uplc(source_code, x)
self.assertEqual(ret, uplc.data_from_cbor(x.foo.to_cbor()))
@hypothesis.given(some_output, st.sampled_from([1, 2, 3]))
def test_union_type_attr_access_all_records_diff_pos(self, x, y):
source_code = """
from opshin.prelude import *
@dataclass()
class A(PlutusData):
CONSTR_ID = 0
foo: SomeOutputDatumHash
@dataclass()
class B(PlutusData):
CONSTR_ID = 1
foo: SomeOutputDatum
@dataclass()
class C(PlutusData):
CONSTR_ID = 2
bar: int
foo: SomeOutputDatum
@dataclass()
class D(PlutusData):
CONSTR_ID = 3
foobar: int
bar: int
foo: SomeOutputDatum
def validator(x: Union[A, B, C, D]) -> Union[SomeOutputDatumHash, SomeOutputDatum]:
return x.foo
"""
@dataclass()
class A(PlutusData):
CONSTR_ID = 0
foo: SomeOutputDatumHash
@dataclass()
class B(PlutusData):
CONSTR_ID = 1
foo: SomeOutputDatum
@dataclass()
class C(PlutusData):
CONSTR_ID = 2
bar: int
foo: SomeOutputDatum
@dataclass()
class D(PlutusData):
CONSTR_ID = 3
foobar: int
bar: int
foo: SomeOutputDatum
x = (
A(x)
if isinstance(x, SomeOutputDatumHash)
else B(x) if y == 1 else C(0, x) if y == 2 else D(0, 0, x)
)
ret = eval_uplc(source_code, x)
self.assertEqual(ret, uplc.data_from_cbor(x.foo.to_cbor()))
@unittest.expectedFailure
def test_union_type_all_records_same_constr(self):
source_code = """
from opshin.prelude import *
@dataclass()
class A(PlutusData):
CONSTR_ID = 0
foo: SomeOutputDatumHash
@dataclass()
class B(PlutusData):
CONSTR_ID = 0
foo: SomeOutputDatum
def validator(x: Union[A, B]) -> Union[SomeOutputDatumHash, SomeOutputDatum]:
return x.foo
"""
eval_uplc(source_code, Unit())
@unittest.expectedFailure
def test_union_type_attr_access_all_records_same_constr(self):
source_code = """
from opshin.prelude import *
@dataclass()
class A(PlutusData):
CONSTR_ID = 0
foo: Token
@dataclass()
class B(PlutusData):
CONSTR_ID = 1
foo: Address
def validator(x: Union[A, B]) -> int:
m = x.foo
if isinstance(m, Address):
k = 0
else:
k = 1
return k
"""
eval_uplc(source_code, Unit())
def test_union_type_attr_access_maximum_type(self):
source_code = """
from opshin.prelude import *
@dataclass()
class A(PlutusData):
CONSTR_ID = 0
foo: int
@dataclass()
class B(PlutusData):
CONSTR_ID = 1
foo: int
def validator(x: Union[A, B]) -> int:
return x.foo
"""
ret = eval_uplc_value(
source_code, uplc.PlutusConstr(0, [uplc.PlutusInteger(1)])
)
self.assertEqual(ret, 1)
def test_union_type_attr_anytype(self):
source_code = """
from opshin.prelude import *
@dataclass()
class A(PlutusData):
CONSTR_ID = 0
foo: bytes
@dataclass()
class B(PlutusData):
CONSTR_ID = 1
foo: int
def validator(x: Union[A, B]) -> Anything:
return x.foo
"""
ret = eval_uplc_value(
source_code, uplc.PlutusConstr(0, [uplc.PlutusByteString(b"")])
)
self.assertEqual(ret, b"")
def test_typecast_anything_int(self):
source_code = """
def validator(x: Anything) -> int:
b: int = x
return b
"""
ret = eval_uplc_value(source_code, 0)
self.assertEqual(ret, 0)
def test_typecast_int_anything(self):
# this should compile, it happens implicitly anyways when calling a function with Any parameters
source_code = """
def validator(x: int) -> Anything:
b: Anything = x
return b
"""
ret = eval_uplc_value(source_code, 0)
self.assertEqual(ret, 0)
def test_typecast_int_anything_int(self):
source_code = """
def validator(x: int) -> Anything:
b: Anything = x
c: int = b
return c + 1
"""
ret = eval_uplc_value(source_code, 0)
self.assertEqual(ret, 1)
def test_typecast_anything_int_anything(self):
source_code = """
def validator(x: Anything) -> Anything:
b: int = x
c: Anything = b + 1
return c
"""
ret = eval_uplc_value(source_code, 0)
self.assertEqual(ret, 1)
@unittest.expectedFailure
def test_typecast_int_str(self):
# this should not compile, the two types are unrelated and there is no meaningful way to cast them either direction
source_code = """
def validator(x: int) -> str:
b: str = x
return b
"""
builder._compile(source_code)
def test_typecast_int_int(self):
source_code = """
def validator(x: int) -> int:
b: int = x
return b
"""
ret = eval_uplc_value(source_code, 0)
self.assertEqual(ret, 0)
def test_zero_ary(self):
source_code = """
def a() -> None:
assert False, "Executed a"
def validator(x: None) -> int:
b = a
if False:
b()
return 2
"""
ret = eval_uplc_value(source_code, 0)
self.assertEqual(ret, 2, "Invalid return value")
@unittest.expectedFailure
def test_zero_ary_exec(self):
source_code = """
def a() -> None:
assert False, "Executed a"
def validator(x: None) -> None:
b = a
if True:
b()
"""
ret = eval_uplc_value(source_code, 0)
def test_zero_ary_method(self):
source_code = """
def validator(x: None) -> None:
b = b"\\xFF".decode
if False:
b()
"""
eval_uplc(source_code, 0)
@unittest.expectedFailure
def test_zero_ary_method_exec(self):
source_code = """
def validator(x: None) -> None:
b = b"\\xFF".decode
if True:
b()
"""
eval_uplc(source_code, 0)
def test_zero_ary_method_exec_suc(self):
source_code = """
def validator(x: None) -> str:
b = b"\\x32".decode
return b()
"""
res = eval_uplc_value(source_code, 0)
self.assertEqual(res, b"\x32")
def test_return_anything(self):
source_code = """
from opshin.prelude import *
def validator() -> Anything:
return b""
"""
res = eval_uplc(source_code, 0)
self.assertEqual(res, uplc.PlutusByteString(b""))