[py-svn] r33659 - in py/dist/py/apigen: rest/testing tracer tracer/testing

fijal at codespeak.net fijal at codespeak.net
Tue Oct 24 17:00:16 CEST 2006


Author: fijal
Date: Tue Oct 24 17:00:14 2006
New Revision: 33659

Modified:
   py/dist/py/apigen/rest/testing/test_rest.py
   py/dist/py/apigen/tracer/model.py
   py/dist/py/apigen/tracer/testing/test_model.py
Log:
Really make the tests meaningfull, as well as extend model a bit.


Modified: py/dist/py/apigen/rest/testing/test_rest.py
==============================================================================
--- py/dist/py/apigen/rest/testing/test_rest.py	(original)
+++ py/dist/py/apigen/rest/testing/test_rest.py	Tue Oct 24 17:00:14 2006
@@ -256,7 +256,38 @@
         r = RestGen(ds, lg, DirWriter(tempdir))
         r.write()
         source = tempdir.join("function_blah.txt").open().read()
-        assert source.find("a :: <Int>") != -1
-        assert source.find("b :: <String>") != -1
-        assert source.find("c :: <Instance>") != -1
+        call_point = source.find("Call sites\:")
+        assert call_point != -1
+        assert source.find("a :: <Int>") < call_point
+        assert source.find("b :: <String>") < call_point
+        assert source.find("c :: <Instance of <Class(C)>>") < call_point
+        self.check_rest(tempdir)
+
+    def test_class_typedefs(self):
+        class A(object):
+            def __init__(self, x):
+                pass
+        
+        class B(object):
+            def __init__(self, y):
+                pass
+        
+        def xxx(x):
+            return x
+        
+        descs = {'A':A, 'B':B, 'xxx':xxx}
+        ds = DocStorage().from_dict(descs)
+        t = Tracer(ds)
+        t.start_tracing()
+        xxx(A(3))
+        xxx(B("f"))
+        t.end_tracing()
+        lg = DirectPaste()
+        tempdir = temppath.ensure("classargs", dir=True)
+        r = RestGen(ds, lg, DirWriter(tempdir))
+        r.write()
+        source = tempdir.join("function_xxx.txt").open().read()
+        call_point = source.find("Call sites\:")
+        assert call_point != -1
+        assert source.find("x :: <Instance of AnyOf(<Class(B)>,<Class(A)>)>") < call_point
         self.check_rest(tempdir)

Modified: py/dist/py/apigen/tracer/model.py
==============================================================================
--- py/dist/py/apigen/tracer/model.py	(original)
+++ py/dist/py/apigen/tracer/model.py	Tue Oct 24 17:00:14 2006
@@ -20,7 +20,7 @@
             return self
         if isinstance(other, SomeUnion):
             return other.unionof(self)
-        if self.gettypedef() is other.gettypedef():
+        if self == other:
             return self
         return SomeUnion([self, other])
     
@@ -46,6 +46,14 @@
             return SomeUnion(self.possibilities.union(other.possibilities))
         return SomeUnion(list(self.possibilities) + [other])
     
+    def __eq__(self, other):
+        if type(other) is not SomeUnion:
+            return False
+        return self.possibilities == other.possibilities
+    
+    def __ne__(self, other):
+        return not self == other
+    
     def __repr__(self):
         return "AnyOf(%s)" % ",".join([str(i) for i in list(self.possibilities)])
     
@@ -66,7 +74,26 @@
 
 class SomeClass(SomeObject):
     typedef = types.ClassType
-
+    
+    def __init__(self, cls):
+        self.cls = cls
+    
+    def __hash__(self):
+        return hash("Class") ^ hash(self.cls)
+    
+    def __eq__(self, other):
+        if type(other) is not SomeClass:
+            return False
+        return self.cls == other.cls
+    
+    def unionof(self, other):
+        if type(other) is not SomeClass or self.cls is not other.cls:
+            return super(SomeClass, self).unionof(other)
+        return self
+    
+    def __repr__(self):
+        return "<Class(%s)>" % self.cls.__name__
+    
 class SomeCode(SomeObject):
     typedef = types.CodeType
 
@@ -98,6 +125,27 @@
     typedef = types.GeneratorType
 
 class SomeInstance(SomeObject):
+    def __init__(self, classdef):
+        self.classdef = classdef
+        
+    def __hash__(self):
+        return hash("SomeInstance") ^ hash(self.classdef)
+    
+    def __eq__(self, other):
+        if type(other) is not SomeInstance:
+            return False
+        return other.classdef == self.classdef
+    
+    def unionof(self, other):
+        if type(other) is not SomeInstance:
+            return super(SomeInstance, self).unionof(other)
+        if self.classdef == other.classdef:
+            return self
+        return SomeInstance(unionof(self.classdef, other.classdef))
+    
+    def __repr__(self):
+        return "<Instance of %s>" % str(self.classdef)
+    
     typedef = types.InstanceType
 
 class SomeInt(SomeObject):
@@ -234,15 +282,15 @@
             result = SomeFunction()
         elif hasattr(x, '__class__'):
             if x.__class__ is type:
-                result = SomeClass()
+                result = SomeClass(x)
             else:
-                result = SomeInstance()
+                result = SomeInstance(SomeClass(x.__class__))
         elif tp is types.ClassType:
-            result = SomeClass()
+            result = SomeClass(x)
     elif x is None:
         return s_None
     elif hasattr(x, '__class__'):
-        result = SomeInstance()
+        result = SomeInstance(SomeClass(x.__class__))
     else:
         result = SomeObject()
     # XXX here we might want to consider stuff like

Modified: py/dist/py/apigen/tracer/testing/test_model.py
==============================================================================
--- py/dist/py/apigen/tracer/testing/test_model.py	(original)
+++ py/dist/py/apigen/tracer/testing/test_model.py	Tue Oct 24 17:00:14 2006
@@ -76,4 +76,24 @@
     assert len(ret.possibilities) == 4
 
 def test_union():
-    py.test.skip("We're not thinking about type unions right now")
+    class A(object):
+        pass
+    
+    class B(object):
+        pass
+    
+    f = guess_type(A).unionof(guess_type(A))
+    assert isinstance(f, SomeClass)
+    assert f.cls is A
+    f = guess_type(A).unionof(guess_type(B))
+    assert isinstance(f, SomeUnion)
+    assert len(f.possibilities) == 2
+    f = guess_type(A()).unionof(guess_type(A()))
+    assert isinstance(f, SomeInstance)
+    assert isinstance(f.classdef, SomeClass)
+    assert f.classdef.cls is A
+    f = guess_type(B()).unionof(guess_type(A()))
+    assert isinstance(f, SomeInstance)
+    assert isinstance(f.classdef, SomeUnion)
+    assert len(f.classdef.possibilities) == 2
+    



More information about the pytest-commit mailing list