-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
executable file
·89 lines (71 loc) · 2.03 KB
/
test.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
#! /usr/bin/env python
import doctest
import unittest
from abc import ABC, abstractmethod
import dollar_lambda
from dollar_lambda import args, data_structures, decorators, parsers, result
def load_tests(_, tests, __):
parsers.TESTING = True
for mod in [
parsers,
decorators,
result,
args,
data_structures,
dollar_lambda,
]:
tests.addTests(doctest.DocTestSuite(mod))
return tests
class MonadLawTester(ABC):
@abstractmethod
def assertEqual(self, a, b):
raise NotImplementedError
def f1(self, x):
unwrapped = self.unwrap(x)
if isinstance(x, int):
return self.m(unwrapped + 1)
else:
return self.m(unwrapped)
def f2(self, x):
unwrapped = self.unwrap(x)
if isinstance(unwrapped, int):
return self.m(unwrapped * 2)
else:
return self.m(unwrapped)
@staticmethod
@abstractmethod
def m(a):
raise NotImplementedError
@staticmethod
@abstractmethod
def return_(a):
raise NotImplementedError
@staticmethod
def unwrapped_values():
return [1]
@staticmethod
@abstractmethod
def wrapped_values():
raise NotImplementedError
def test_law1(self):
for a in self.unwrapped_values():
x1 = self.return_(a) >= self.f1
x2 = self.m(self.f1(a))
self.assertEqual(self.unwrap(x1), self.unwrap(x2))
def test_law2(self):
for p in self.wrapped_values():
p = self.m(p)
a = p >= self.return_
self.assertEqual(self.unwrap(a), self.unwrap(p))
def test_law3(self):
for p in self.wrapped_values():
p = self.m(p)
x1 = p >= (lambda a: self.f1(a) >= self.f2)
x2 = (p >= self.f1) >= self.f2
self.assertEqual(self.unwrap(x1), self.unwrap(x2))
@staticmethod
@abstractmethod
def unwrap(x):
raise NotImplementedError
if __name__ == "__main__":
unittest.main()