summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorClaudiu Popa <pcmanticore@gmail.com>2014-08-13 18:07:11 +0300
committerClaudiu Popa <pcmanticore@gmail.com>2014-08-13 18:07:11 +0300
commitd8b19eae5e855b5453f399858aadeb7cf218ee70 (patch)
tree8361c6365dce7f5ccbbc50afff201b400c0af115 /test
parent52ae161026cdc83721d5c4af7d34a9f9131966f7 (diff)
downloadastroid-git-d8b19eae5e855b5453f399858aadeb7cf218ee70.tar.gz
The inference engine handles binary operations (add, mul etc.) between instances.
Diffstat (limited to 'test')
-rw-r--r--test/unittest_inference.py58
1 files changed, 58 insertions, 0 deletions
diff --git a/test/unittest_inference.py b/test/unittest_inference.py
index 06f5abb8..ea49e50a 100644
--- a/test/unittest_inference.py
+++ b/test/unittest_inference.py
@@ -1257,5 +1257,63 @@ def test(*args, **kwargs):
infered = func.infered()[0]
self.assertIsInstance(infered, UnboundMethod)
+ def test_instance_binary_operations(self):
+ code = dedent("""
+ class A(object):
+ def __mul__(self, other):
+ return 42
+ a = A()
+ b = A()
+ sub = a - b
+ mul = a * b
+ """)
+ astroid = builder.string_build(code, __name__, __file__)
+ sub = astroid['sub'].infered()[0]
+ mul = astroid['mul'].infered()[0]
+ self.assertIs(sub, YES)
+ self.assertIsInstance(mul, nodes.Const)
+ self.assertEqual(mul.value, 42)
+
+ def test_instance_binary_operations_parent(self):
+ code = dedent("""
+ class A(object):
+ def __mul__(self, other):
+ return 42
+ class B(A):
+ pass
+ a = B()
+ b = B()
+ sub = a - b
+ mul = a * b
+ """)
+ astroid = builder.string_build(code, __name__, __file__)
+ sub = astroid['sub'].infered()[0]
+ mul = astroid['mul'].infered()[0]
+ self.assertIs(sub, YES)
+ self.assertIsInstance(mul, nodes.Const)
+ self.assertEqual(mul.value, 42)
+
+ def test_instance_binary_operations_multiple_methods(self):
+ code = dedent("""
+ class A(object):
+ def __mul__(self, other):
+ return 42
+ class B(A):
+ def __mul__(self, other):
+ return [42]
+ a = B()
+ b = B()
+ sub = a - b
+ mul = a * b
+ """)
+ astroid = builder.string_build(code, __name__, __file__)
+ sub = astroid['sub'].infered()[0]
+ mul = astroid['mul'].infered()[0]
+ self.assertIs(sub, YES)
+ self.assertIsInstance(mul, nodes.List)
+ self.assertIsInstance(mul.elts[0], nodes.Const)
+ self.assertEqual(mul.elts[0].value, 42)
+
+
if __name__ == '__main__':
unittest_main()