home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 June / maximum-cd-2009-06.iso / DiscContents / digsby_setup.exe / lib / protocols / tests / __init__.pyo (.txt) next >
Encoding:
Python Compiled Bytecode  |  2009-02-26  |  10.1 KB  |  278 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. from unittest import TestSuite, TestCase, makeSuite
  5. from protocols import adapt, advise, Interface, Attribute, declareAdapter
  6. from protocols import AbstractBase, AdaptationFailure
  7.  
  8. class APITests(TestCase):
  9.     
  10.     def checkAdaptTrapsTypeErrorsOnConform(self):
  11.         
  12.         class Conformer:
  13.             
  14.             def __conform__(self, ob):
  15.                 return []
  16.  
  17.  
  18.  
  19.     
  20.     def checkAdaptHandlesIsInstance(self):
  21.         pass
  22.  
  23.     
  24.     def checkAdviseFailsInCallContext(self):
  25.         
  26.         try:
  27.             advise()
  28.         except SyntaxError:
  29.             pass
  30.  
  31.         raise AssertionError("Should've got SyntaxError for advise() in function")
  32.  
  33.     
  34.     def checkAdviseClassKeywordsValidated(self):
  35.         
  36.         try:
  37.             
  38.             class X:
  39.                 advise(moduleProvides = list)
  40.  
  41.         except TypeError:
  42.             v = None
  43.  
  44.         raise AssertionError("Should've caught invalid keyword")
  45.  
  46.     
  47.     def checkAdviseClassKeywordsValidated(self):
  48.         
  49.         try:
  50.             
  51.             class X:
  52.                 advise(moduleProvides = list)
  53.  
  54.         except TypeError:
  55.             v = None
  56.  
  57.         raise AssertionError("Should've caught invalid keyword")
  58.  
  59.     
  60.     def checkAdviseModuleKeywordsValidated(self):
  61.         
  62.         try:
  63.             exec 'advise(instancesProvide=[IProtocol1])' in globals(), globals()
  64.         except TypeError:
  65.             v = None
  66.  
  67.         raise AssertionError("Should've caught invalid keyword")
  68.  
  69.     
  70.     def checkAdviseInClassExec(self):
  71.         d = {
  72.             'advise': advise,
  73.             'IProtocol1': IProtocol1 }
  74.         exec 'class Foo: advise(instancesProvide=[IProtocol1])' in d
  75.  
  76.     
  77.     def checkSimpleAdaptation(self):
  78.         
  79.         class Conformer:
  80.             
  81.             def __conform__(self, protocol):
  82.                 if protocol == 42:
  83.                     return ('hitchhiker', self)
  84.                 
  85.  
  86.  
  87.         
  88.         class AdaptingProtocol:
  89.             
  90.             def __adapt__(klass, ob):
  91.                 return ('adapted', ob)
  92.  
  93.             __adapt__ = classmethod(__adapt__)
  94.  
  95.         c = Conformer()
  96.  
  97.     
  98.     def checkAdaptFiltersTypeErrors(self):
  99.         
  100.         class Nonconformist:
  101.             
  102.             def __conform__(self, ob):
  103.                 raise TypeError('You got me!')
  104.  
  105.  
  106.         
  107.         class Unadaptive:
  108.             
  109.             def __adapt__(self, ob):
  110.                 raise TypeError('You got me!')
  111.  
  112.  
  113.         self.assertTypeErrorPassed(None, Unadaptive(), None)
  114.         self.assertTypeErrorPassed(Nonconformist(), None, None)
  115.         self.assertRaises(AdaptationFailure, adapt, None, None)
  116.  
  117.     
  118.     def assertTypeErrorPassed(self, *args):
  119.         
  120.         try:
  121.             adapt(*args)
  122.         except TypeError:
  123.             v = None
  124.  
  125.         raise AssertionError("Should've passed TypeError through")
  126.  
  127.     
  128.     def checkImplicationBug(self):
  129.         
  130.         class I1(Interface):
  131.             pass
  132.  
  133.         
  134.         class I2(I1):
  135.             pass
  136.  
  137.         declareAdapter((lambda o: o), provides = [
  138.             I1], forProtocols = [
  139.             I2])
  140.  
  141.     
  142.     def checkAttribute(self):
  143.         for Abstract in range(10) + [
  144.             None]:
  145.             i = None
  146.             ob = Abstract()
  147.             for j in range(10):
  148.                 ob.value = j
  149.             
  150.         
  151.  
  152.     
  153.     def checkStickyAdapter(self):
  154.         
  155.         class T:
  156.             pass
  157.  
  158.         t = T()
  159.         
  160.         class I(Interface):
  161.             pass
  162.  
  163.         StickyAdapter = StickyAdapter
  164.         import protocols.adapters
  165.         
  166.         class A('A', (StickyAdapter,)):
  167.             attachForProtocols = (I,)
  168.             advise(instancesProvide = [
  169.                 I], asAdapterForTypes = [
  170.                 T])
  171.  
  172.         a = I(t)
  173.         n = T()
  174.         a2 = I(n)
  175.  
  176.  
  177. from protocols import protocolForType, protocolForURI, sequenceOf, advise
  178. from protocols import declareImplementation, Variation
  179. from UserDict import UserDict
  180. IGetSetMapping = protocolForType(dict, [
  181.     '__getitem__',
  182.     '__setitem__'])
  183. IGetMapping = protocolForType(dict, [
  184.     '__getitem__'])
  185. ISimpleReadFile = protocolForType(file, [
  186.     'read'])
  187. IImplicitRead = protocolForType(file, [
  188.     'read'], implicit = True)
  189. IProtocol1 = protocolForURI('http://peak.telecommunity.com/PyProtocols')
  190. multimap = sequenceOf(IGetMapping)
  191. declareImplementation(UserDict, [
  192.     IGetSetMapping])
  193. IMyUnusualMapping = Variation(IGetSetMapping)
  194.  
  195. class MyUserMapping(object):
  196.     pass
  197.  
  198. declareImplementation(MyUserMapping, [
  199.     IMyUnusualMapping])
  200.  
  201. class GenerationTests(TestCase):
  202.     
  203.     def checkTypeSubset(self):
  204.         d = { }
  205.  
  206.     
  207.     def checkImplications(self):
  208.         d = UserDict()
  209.  
  210.     
  211.     def checkWeak(self):
  212.         StringIO = StringIO
  213.         import cStringIO
  214.         s = StringIO('foo')
  215.  
  216.     
  217.     def checkURI(self):
  218.         p = protocolForURI('http://www.python.org/')
  219.         p = protocolForURI('http://peak.telecommunity.com/PyProtocols')
  220.  
  221.     
  222.     def checkSequence(self):
  223.         d1 = { }
  224.         d2 = { }
  225.         seq = multimap([
  226.             d1,
  227.             d2])
  228.         
  229.         class ISequenceLike(Interface):
  230.             advise(protocolIsSubsetOf = [
  231.                 multimap])
  232.  
  233.         ISequenceLike([
  234.             d1,
  235.             d2])
  236.  
  237.     
  238.     def checkVariation(self):
  239.         d = { }
  240.         d = MyUserMapping()
  241.         self.assertEqual(repr(IMyUnusualMapping), "Variation(TypeSubset(<type 'dict'>,('__getitem__', '__setitem__')))")
  242.         self.assertEqual(repr(Variation(Interface, 42)), "Variation(<class 'protocols.interfaces.Interface'>,42)")
  243.  
  244.  
  245.  
  246. def test_suite():
  247.     test_advice = test_advice
  248.     test_direct = test_direct
  249.     test_classes = test_classes
  250.     import protocols.tests
  251.     tests = [
  252.         test_advice.test_suite(),
  253.         test_classes.test_suite(),
  254.         test_direct.test_suite(),
  255.         makeSuite(APITests, 'check'),
  256.         makeSuite(GenerationTests, 'check')]
  257.     
  258.     try:
  259.         import zope.interface as zope
  260.     except ImportError:
  261.         pass
  262.  
  263.     test_zope = test_zope
  264.     import protocols.tests
  265.     tests.append(test_zope.test_suite())
  266.     
  267.     try:
  268.         Interface = Interface
  269.         import twisted.python.components
  270.     except (ImportError, SystemError):
  271.         pass
  272.  
  273.     test_twisted = test_twisted
  274.     import protocols.tests
  275.     tests.append(test_twisted.test_suite())
  276.     return TestSuite(tests)
  277.  
  278.