def test_base_interfaces_are_honored(): class I(System.IComparable, System.IConvertible): __metaclass__ = ClrInterface _clrnamespace = get_unique_namespace() AssertContains(GetClrType(I).GetInterfaces(), System.IComparable) AssertContains(GetClrType(I).GetInterfaces(), System.IConvertible)
def __getattr__(name): name = utils.python_name_to_csharp_name(name) _original_class = getattr(System.Windows.Forms, name) _original_clr_class = GetClrType(_original_class) if (_original_clr_class.IsSubclassOf(System.Windows.Forms.Control) and not _original_clr_class.IsSubclassOf(System.Windows.Forms.Form)) \ or _original_clr_class.IsSubclassOf(System.ComponentModel.Component): # Either this is a control that isn't a form (which are available in controls.py), # or its a component (which are available in components.py). raise AttributeError(name) return get_wrapper_class(_original_class)
def test_namespace(metaclass): n = get_unique_namespace() class T(object): __metaclass__ = metaclass _clrnamespace = n AreEqual(GetClrType(T).FullName, n + ".T")
def test_return_type(metaclass): for t, v in types_and_values: class T(object): __metaclass__ = metaclass _clrnamespace = get_unique_namespace() @accepts() @returns(t) def Foo(self): return v foo = GetClrType(T).GetMethod("Foo") AreEqual(foo.ReturnType, GetClrType(t)) if metaclass is ClrClass: AreEqual(v, foo.Invoke(T(), None))
def test_argument_types(metaclass): for t, v in types_and_values: class T(object): __metaclass__ = metaclass _clrnamespace = get_unique_namespace() @accepts(t) @returns() def Foo(self, a): AreEqual(a, v) foo = GetClrType(T).GetMethod("Foo") p0 = foo.GetParameters()[0] AreEqual(p0.ParameterType, GetClrType(t)) if metaclass is ClrClass: foo.Invoke(T(), System.Array[object]([v]))
def test_clr_get_clr_type(): """Test clr.GetClrType().""" from clr import GetClrType import System from System import IComparable from System import ArgumentException assert GetClrType(System.String).FullName == "System.String" comparable = GetClrType(IComparable) assert comparable.FullName == "System.IComparable" assert comparable.IsInterface assert GetClrType(int).FullName == "System.Int32" assert GetClrType(str).FullName == "System.String" assert GetClrType(float).FullName == "System.Double" dblarr = System.Array[System.Double] assert GetClrType(dblarr).FullName == "System.Double[]" with pytest.raises(TypeError): GetClrType(1) with pytest.raises(TypeError): GetClrType("thiswillfail")
from collections import defaultdict from expanded_clr.utils import wrap_csharp_method, python_name_to_csharp_name from clr import System, GetClrType from . import components, controls, drawing from .forms import Form __ARGUMENT_TYPES = defaultdict(list) for method in GetClrType(System.Windows.Forms.Application).GetMethods(): __ARGUMENT_TYPES[method.Name].append( tuple(param_info.ParameterType for param_info in method.GetParameters())) def __getattr__(name): """This module is also an entry point for items found in System.Windows.Forms.Application""" name = python_name_to_csharp_name(name) _original_method = getattr(System.Windows.Forms.Application, name) if not _original_method or name not in __ARGUMENT_TYPES: raise AttributeError(name) return wrap_csharp_method(_original_method, __ARGUMENT_TYPES[name])
def test_clrtype_interface_can_inherit_clrtype_interface(): class I(ClrTypeInterface): __metaclass__ = ClrInterface _clrnamespace = get_unique_namespace() AssertContains(GetClrType(I).GetInterfaces(), GetClrType(ClrTypeInterface))
raise RuntimeError("this should not get called") class ClrTypeClass(ClrTypeInterface): __metaclass__ = ClrClass def InterfaceMethod(self, s=""): return "ClrTypeClass-InterfaceMethod-" + s @accepts(str) @returns(str) def ClassMethod(self, s=""): return "ClrTypeClass-ClassMethod-" + s ct = GetClrType(ClrTypeClass) # Ensures that the CLR types have unique names global namespace_count namespace_count = 0 def get_unique_namespace(): global namespace_count namespace_count += 1 return "test_clrclass_namespace_" + str(namespace_count) def for_interface_and_class(f): def test_interface_and_class(): for metaclass in [ClrInterface, ClrClass]:
def test_can_implement_clrtype_interface(): class C(ClrTypeInterface): pass AssertContains(GetClrType(C).GetInterfaces(), ClrTypeInterface)
def test_base_type_is_honored(): AreEqual(ct.BaseType, GetClrType(object))