test_saferef.py :  » Development » Python-Dispatcher » PyDispatcher-2.0.1 » tests » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » Python Dispatcher 
Python Dispatcher » PyDispatcher 2.0.1 » tests » test_saferef.py
from pydispatch.saferef import *

import unittest
class Test1( object):
  def x( self ):
    pass
def test2(obj):
  pass
class Test2( object ):
  def __call__( self, obj ):
    pass
class Tester (unittest.TestCase):
  def setUp (self):
    ts = []
    ss = []
    for x in xrange( 5000 ):
      t = Test1()
      ts.append( t)
      s = safeRef(t.x, self._closure )
      ss.append( s)
    ts.append( test2 )
    ss.append( safeRef(test2, self._closure) )
    for x in xrange( 30 ):
      t = Test2()
      ts.append( t)
      s = safeRef(t, self._closure )
      ss.append( s)
    self.ts = ts
    self.ss = ss
    self.closureCount = 0
  def tearDown( self ):
    del self.ts
    del self.ss
  def testIn(self):
    """Test the "in" operator for safe references (cmp)"""
    for t in self.ts[:50]:
      assert safeRef(t.x) in self.ss
  def testValid(self):
    """Test that the references are valid (return instance methods)"""
    for s in self.ss:
      assert s()
  def testShortCircuit (self):
    """Test that creation short-circuits to reuse existing references"""
    sd = {}
    for s in self.ss:
      sd[s] = 1
    for t in self.ts:
      if hasattr( t, 'x'):
        assert sd.has_key( safeRef(t.x))
      else:
        assert sd.has_key( safeRef(t))
  def testRepresentation (self):
    """Test that the reference object's representation works

    XXX Doesn't currently check the results, just that no error
      is raised
    """
    repr( self.ss[-1] )
    
  def test(self):
    self.closureCount = 0
    wholeI = len(self.ts)
    for i in xrange( len(self.ts)-1, -1, -1):
      del self.ts[i]
      if wholeI-i != self.closureCount:
        """Unexpected number of items closed, expected %s, got %s closed"""%( wholeI-i,self.closureCount)
    
  def _closure(self, ref):
    """Dumb utility mechanism to increment deletion counter"""
    self.closureCount +=1

def getSuite():
  return unittest.makeSuite(Tester,'test')

if __name__ == "__main__":
  unittest.main ()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.