Skip to content

Commit 9208971

Browse files
authored
Add logical operators to BoolValue (#62)
1 parent 789871f commit 9208971

File tree

2 files changed

+181
-1
lines changed

2 files changed

+181
-1
lines changed

src/spellbind/bool_values.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,35 @@
44
from abc import ABC
55
from typing import TypeVar, Generic
66

7-
from spellbind.values import Value, OneToOneValue, Constant, SimpleVariable
7+
from spellbind.values import Value, OneToOneValue, Constant, SimpleVariable, TwoToOneValue
88

99
_S = TypeVar('_S')
1010

11+
BoolLike = Value[bool] | bool
12+
1113

1214
class BoolValue(Value[bool], ABC):
1315
def logical_not(self) -> BoolValue:
1416
return NotBoolValue(self)
1517

18+
def __and__(self, other: BoolLike) -> BoolValue:
19+
return AndBoolValues(self, other)
20+
21+
def __rand__(self, other: bool) -> BoolValue:
22+
return AndBoolValues(other, self)
23+
24+
def __or__(self, other: BoolLike) -> BoolValue:
25+
return OrBoolValues(self, other)
26+
27+
def __ror__(self, other: bool) -> BoolValue:
28+
return OrBoolValues(other, self)
29+
30+
def __xor__(self, other: BoolLike) -> BoolValue:
31+
return XorBoolValues(self, other)
32+
33+
def __rxor__(self, other: bool) -> BoolValue:
34+
return XorBoolValues(other, self)
35+
1636

1737
class OneToBoolValue(OneToOneValue[_S, bool], BoolValue, Generic[_S]):
1838
pass
@@ -23,6 +43,21 @@ def __init__(self, value: Value[bool]):
2343
super().__init__(operator.not_, value)
2444

2545

46+
class AndBoolValues(TwoToOneValue[bool, bool, bool], BoolValue):
47+
def __init__(self, left: BoolLike, right: BoolLike):
48+
super().__init__(operator.and_, left, right)
49+
50+
51+
class OrBoolValues(TwoToOneValue[bool, bool, bool], BoolValue):
52+
def __init__(self, left: BoolLike, right: BoolLike):
53+
super().__init__(operator.or_, left, right)
54+
55+
56+
class XorBoolValues(TwoToOneValue[bool, bool, bool], BoolValue):
57+
def __init__(self, left: BoolLike, right: BoolLike):
58+
super().__init__(operator.xor, left, right)
59+
60+
2661
class BoolConstant(BoolValue, Constant[bool]):
2762
pass
2863

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
from spellbind.bool_values import BoolVariable
2+
3+
4+
def test_bool_variables_and_variable_true_variable_true():
5+
var1 = BoolVariable(True)
6+
var2 = BoolVariable(True)
7+
result = var1 & var2
8+
assert result.value
9+
10+
var1.value = False
11+
assert not result.value
12+
13+
14+
def test_bool_variables_and_variable_true_variable_false():
15+
var1 = BoolVariable(True)
16+
var2 = BoolVariable(False)
17+
result = var1 & var2
18+
assert not result.value
19+
20+
var2.value = True
21+
assert result.value
22+
23+
24+
def test_bool_variables_and_variable_false_variable_false():
25+
var1 = BoolVariable(False)
26+
var2 = BoolVariable(False)
27+
result = var1 & var2
28+
assert not result.value
29+
30+
var1.value = True
31+
assert not result.value
32+
33+
34+
def test_bool_variable_and_variable_true_literal_false():
35+
var = BoolVariable(True)
36+
result = var & False
37+
assert not result.value
38+
39+
var.value = False
40+
assert not result.value
41+
42+
43+
def test_bool_variable_and_literal_false_variable_true():
44+
var = BoolVariable(True)
45+
result = False & var
46+
assert not result.value
47+
48+
var.value = False
49+
assert not result.value
50+
51+
52+
def test_bool_variables_or_variable_true_variable_true():
53+
var1 = BoolVariable(True)
54+
var2 = BoolVariable(True)
55+
result = var1 | var2
56+
assert result.value
57+
58+
var1.value = False
59+
assert result.value
60+
61+
62+
def test_bool_variables_or_variable_true_variable_false():
63+
var1 = BoolVariable(True)
64+
var2 = BoolVariable(False)
65+
result = var1 | var2
66+
assert result.value
67+
68+
var1.value = False
69+
assert not result.value
70+
71+
72+
def test_bool_variables_or_variable_false_variable_false():
73+
var1 = BoolVariable(False)
74+
var2 = BoolVariable(False)
75+
result = var1 | var2
76+
assert not result.value
77+
78+
var2.value = True
79+
assert result.value
80+
81+
82+
def test_bool_variable_or_variable_false_literal_true():
83+
var = BoolVariable(False)
84+
result = var | True
85+
assert result.value
86+
87+
var.value = True
88+
assert result.value
89+
90+
91+
def test_bool_variable_or_literal_true_variable_false():
92+
var = BoolVariable(False)
93+
result = True | var
94+
assert result.value
95+
96+
var.value = True
97+
assert result.value
98+
99+
100+
def test_bool_variables_xor_variable_true_variable_true():
101+
var1 = BoolVariable(True)
102+
var2 = BoolVariable(True)
103+
result = var1 ^ var2
104+
assert not result.value
105+
106+
var1.value = False
107+
assert result.value
108+
109+
110+
def test_bool_variables_xor_variable_true_variable_false():
111+
var1 = BoolVariable(True)
112+
var2 = BoolVariable(False)
113+
result = var1 ^ var2
114+
assert result.value
115+
116+
var2.value = True
117+
assert not result.value
118+
119+
120+
def test_bool_variables_xor_variable_false_variable_false():
121+
var1 = BoolVariable(False)
122+
var2 = BoolVariable(False)
123+
result = var1 ^ var2
124+
assert not result.value
125+
126+
var1.value = True
127+
assert result.value
128+
129+
130+
def test_bool_variable_xor_variable_true_literal_true():
131+
var = BoolVariable(True)
132+
result = var ^ True
133+
assert not result.value
134+
135+
var.value = False
136+
assert result.value
137+
138+
139+
def test_bool_variable_xor_literal_true_variable_true():
140+
var = BoolVariable(True)
141+
result = True ^ var
142+
assert not result.value
143+
144+
var.value = False
145+
assert result.value

0 commit comments

Comments
 (0)