Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Prev Previous commit
Next Next commit
Refactor utility functions
  • Loading branch information
vmuriart committed Jan 31, 2017
commit 072219cc6dc17f546a67ed50c004dd7f446d3d88
97 changes: 6 additions & 91 deletions src/tests/leaktest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@

import clr
import gc
import sys

import System

from _compat import range
from utils import (CallableHandler, ClassMethodHandler, GenericHandler,
HelloClass, StaticMethodHandler, VarCallableHandler,
VariableArgsHandler, hello_func)


class LeakTest(object):
Expand Down Expand Up @@ -53,7 +57,6 @@ def run(self):
self.testDelegates()

def report(self):
import sys, gc
gc.collect()
dicttype = type({})
for item in gc.get_objects():
Expand All @@ -78,7 +81,6 @@ def testModules(self):
def testClasses(self):
from System.Collections import Hashtable
from Python.Test import StringDelegate
from System import Int32

self.notify("Running class leak check...")

Expand All @@ -91,7 +93,7 @@ def testClasses(self):
del x

# Value type
x = Int32(99)
x = System.Int32(99)
del x

# Delegate type
Expand All @@ -101,7 +103,7 @@ def testClasses(self):
self.end_test()

def testEnumerations(self):
from Python import Test
import Python.Test as Test

self.notify("Running enum leak check...")

Expand Down Expand Up @@ -215,7 +217,6 @@ def handler(sender, args, dict=dict):

def testDelegates(self):
from Python.Test import DelegateTest, StringDelegate
import System

self.notify("Running delegate leak check...")

Expand Down Expand Up @@ -326,92 +327,6 @@ def testDelegates(self):
self.end_test()


class GenericHandler(object):
"""A generic handler to test event callbacks."""

def __init__(self):
self.value = None

def handler(self, sender, args):
self.value = args.value


class VariableArgsHandler(object):
"""A variable args handler to test event callbacks."""

def __init__(self):
self.value = None

def handler(self, *args):
ob, eventargs = args
self.value = eventargs.value


class CallableHandler(object):
"""A callable handler to test event callbacks."""

def __init__(self):
self.value = None

def __call__(self, sender, args):
self.value = args.value


class VarCallableHandler(object):
"""A variable args callable handler to test event callbacks."""

def __init__(self):
self.value = None

def __call__(self, *args):
ob, eventargs = args
self.value = eventargs.value


class StaticMethodHandler(object):
"""A static method handler to test event callbacks."""

value = None

def handler(sender, args):
StaticMethodHandler.value = args.value

handler = staticmethod(handler)


class ClassMethodHandler(object):
"""A class method handler to test event callbacks."""

value = None

def handler(cls, sender, args):
cls.value = args.value

handler = classmethod(handler)


class HelloClass(object):
def hello(self):
return "hello"

def __call__(self):
return "hello"

def s_hello():
return "hello"

s_hello = staticmethod(s_hello)

def c_hello(cls):
return "hello"

c_hello = classmethod(c_hello)


def hello_func():
return "hello"


if __name__ == '__main__':
test = LeakTest()
test.run()
Expand Down
20 changes: 10 additions & 10 deletions src/tests/test_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@
from _compat import DictProxyType, range


class ClassicClass:
def kind(self):
return 'classic'


class NewStyleClass(object):
def kind(self):
return 'new-style'


class ClassTests(unittest.TestCase):
"""Test CLR class support."""

Expand Down Expand Up @@ -273,15 +283,5 @@ def PyCallback(self, self2):
self.assertTrue(testobj.SameReference)


class ClassicClass:
def kind(self):
return 'classic'


class NewStyleClass(object):
def kind(self):
return 'new-style'


def test_suite():
return unittest.makeSuite(ClassTests)
54 changes: 20 additions & 34 deletions src/tests/test_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,18 @@
import unittest

from _compat import ClassType, PY2, PY3, range
from utils import isCLRClass, isCLRModule, isCLRRootModule


class CompatibilityTests(unittest.TestCase):
"""
Backward-compatibility tests for deprecated features.
"""

def isCLRModule(self, object):
return type(object).__name__ == 'ModuleObject'

def isCLRRootModule(self, object):
if PY3:
# in Python 3 the clr module is a normal python module
return object.__name__ == "clr"
elif PY2:
return type(object).__name__ == 'CLRModule'

def isCLRClass(self, object):
return type(object).__name__ == 'CLR Metatype' # for now
"""Backward-compatibility tests for deprecated features."""

# Tests for old-style CLR-prefixed module naming.

def testSimpleImport(self):
"""Test simple import."""
import CLR
self.assertTrue(self.isCLRRootModule(CLR))
self.assertTrue(isCLRRootModule(CLR))
self.assertTrue(CLR.__name__ == 'clr')

import sys
Expand All @@ -49,7 +35,7 @@ def testSimpleImport(self):
def testSimpleImportWithAlias(self):
"""Test simple import with aliasing."""
import CLR as myCLR
self.assertTrue(self.isCLRRootModule(myCLR))
self.assertTrue(isCLRRootModule(myCLR))
self.assertTrue(myCLR.__name__ == 'clr')

import sys as mySys
Expand All @@ -69,11 +55,11 @@ def testSimpleImportWithAlias(self):
def testDottedNameImport(self):
"""Test dotted-name import."""
import CLR.System
self.assertTrue(self.isCLRModule(CLR.System))
self.assertTrue(isCLRModule(CLR.System))
self.assertTrue(CLR.System.__name__ == 'System')

import System
self.assertTrue(self.isCLRModule(System))
self.assertTrue(isCLRModule(System))
self.assertTrue(System.__name__ == 'System')

self.assertTrue(System is CLR.System)
Expand All @@ -85,11 +71,11 @@ def testDottedNameImport(self):
def testDottedNameImportWithAlias(self):
"""Test dotted-name import with aliasing."""
import CLR.System as myCLRSystem
self.assertTrue(self.isCLRModule(myCLRSystem))
self.assertTrue(isCLRModule(myCLRSystem))
self.assertTrue(myCLRSystem.__name__ == 'System')

import System as mySystem
self.assertTrue(self.isCLRModule(mySystem))
self.assertTrue(isCLRModule(mySystem))
self.assertTrue(mySystem.__name__ == 'System')

self.assertTrue(mySystem is myCLRSystem)
Expand All @@ -101,7 +87,7 @@ def testDottedNameImportWithAlias(self):
def testSimpleImportFrom(self):
"""Test simple 'import from'."""
from CLR import System
self.assertTrue(self.isCLRModule(System))
self.assertTrue(isCLRModule(System))
self.assertTrue(System.__name__ == 'System')

from xml import dom
Expand All @@ -111,7 +97,7 @@ def testSimpleImportFrom(self):
def testSimpleImportFromWithAlias(self):
"""Test simple 'import from' with aliasing."""
from CLR import System as mySystem
self.assertTrue(self.isCLRModule(mySystem))
self.assertTrue(isCLRModule(mySystem))
self.assertTrue(mySystem.__name__ == 'System')

from xml import dom as myDom
Expand All @@ -121,11 +107,11 @@ def testSimpleImportFromWithAlias(self):
def testDottedNameImportFrom(self):
"""Test dotted-name 'import from'."""
from CLR.System import Xml
self.assertTrue(self.isCLRModule(Xml))
self.assertTrue(isCLRModule(Xml))
self.assertTrue(Xml.__name__ == 'System.Xml')

from CLR.System.Xml import XmlDocument
self.assertTrue(self.isCLRClass(XmlDocument))
self.assertTrue(isCLRClass(XmlDocument))
self.assertTrue(XmlDocument.__name__ == 'XmlDocument')

from xml.dom import pulldom
Expand All @@ -139,11 +125,11 @@ def testDottedNameImportFrom(self):
def testDottedNameImportFromWithAlias(self):
"""Test dotted-name 'import from' with aliasing."""
from CLR.System import Xml as myXml
self.assertTrue(self.isCLRModule(myXml))
self.assertTrue(isCLRModule(myXml))
self.assertTrue(myXml.__name__ == 'System.Xml')

from CLR.System.Xml import XmlDocument as myXmlDocument
self.assertTrue(self.isCLRClass(myXmlDocument))
self.assertTrue(isCLRClass(myXmlDocument))
self.assertTrue(myXmlDocument.__name__ == 'XmlDocument')

from xml.dom import pulldom as myPulldom
Expand All @@ -159,12 +145,12 @@ def testFromModuleImportStar(self):
count = len(locals().keys())
m = __import__('CLR.System.Management', globals(), locals(), ['*'])
self.assertTrue(m.__name__ == 'System.Management')
self.assertTrue(self.isCLRModule(m))
self.assertTrue(isCLRModule(m))
self.assertTrue(len(locals().keys()) > count + 1)

m2 = __import__('System.Management', globals(), locals(), ['*'])
self.assertTrue(m2.__name__ == 'System.Management')
self.assertTrue(self.isCLRModule(m2))
self.assertTrue(isCLRModule(m2))
self.assertTrue(len(locals().keys()) > count + 1)

self.assertTrue(m is m2)
Expand Down Expand Up @@ -193,7 +179,7 @@ def testImplicitLoadAlreadyValidNamespace(self):
# Python runtime to "do the right thing", allowing types from both
# assemblies to be found in the CLR.System module implicitly.
import CLR.System
self.assertTrue(self.isCLRClass(CLR.System.UriBuilder))
self.assertTrue(isCLRClass(CLR.System.UriBuilder))

def testImportNonExistantModule(self):
"""Test import failure for a non-existant module."""
Expand All @@ -211,7 +197,7 @@ def testLookupNoNamespaceType(self):
"""Test lookup of types without a qualified namespace."""
import CLR.Python.Test
import CLR
self.assertTrue(self.isCLRClass(CLR.NoNamespaceType))
self.assertTrue(isCLRClass(CLR.NoNamespaceType))

def testModuleLookupRecursion(self):
"""Test for recursive lookup handling."""
Expand All @@ -232,10 +218,10 @@ def testModuleGetAttr(self):
import CLR.System as System

int_type = System.Int32
self.assertTrue(self.isCLRClass(int_type))
self.assertTrue(isCLRClass(int_type))

module = System.Xml
self.assertTrue(self.isCLRModule(module))
self.assertTrue(isCLRModule(module))

def test():
spam = System.Spam
Expand Down
Loading