Exemple #1
0
    def test_from_derived_type(self):
        from Merlin.Testing.Property import DerivedClass
        from Merlin.Testing.TypeSample import SimpleClass, SimpleStruct
        t = DerivedClass
        x = t()
        a, b, c = 8, SimpleStruct(7), SimpleClass(6)

        t.StaticInt32Property  # read

        def f():
            t.StaticInt32Property = a

        self.assertRaisesRegex(
            AttributeError,
            "'DerivedClass' object has no attribute 'StaticInt32Property'",
            f)  # write

        x.InstanceInt32Property = a
        self.assertEqual(a, x.InstanceInt32Property)

        self.assertTrue('StaticSimpleStructProperty' not in t.__dict__)
        self.assertTrue('InstanceSimpleStructProperty' not in t.__dict__)
        p = t.__bases__[0].__dict__['InstanceSimpleStructProperty']
        p.SetValue(x, b)
        self.assertEqual(b.Flag, p.GetValue(x).Flag)

        self.assertTrue('StaticSimpleClassProperty' not in t.__dict__)
        self.assertTrue('InstanceSimpleClassProperty' not in t.__dict__)
        p = t.__bases__[0].__dict__['InstanceSimpleClassProperty']
        p.__set__(x, c)
        self.assertEqual(c, p.__get__(x))
    def test_basic(self):
        from Merlin.Testing.Indexer import ClassWithIndexer, StructWithIndexer
        from Merlin.Testing.TypeSample import SimpleClass, SimpleStruct
        for t in [
                ClassWithIndexer,
                StructWithIndexer,
        ]:
            x = t()
            x.Init()

            for y, z in zip(x, range(10)):
                self.assertEqual(y, z)

            a, b, c = 2, SimpleStruct(3), SimpleClass(4)

            x[1] = a
            self.assertEqual(x[1], a)

            x[2, "ab1"] = b
            self.assertEqual(x[12, "ab"].Flag, b.Flag)

            x['ab', 'c', 'd'] = c
            self.assertEqual(x['a', 'b', 'cd'], c)

            a, b, c = 5, SimpleStruct(6), SimpleClass(7)
            self.assertTrue(not hasattr(x, 'set_Item'))
            self.assertTrue(not hasattr(x, 'get_Item'))

            # bad arg count
            self.assertRaisesRegexp(TypeError, "expected int, got tuple",
                                    lambda: x[()])
            self.assertRaisesRegexp(
                TypeError,
                "__getitem__\(\) takes at most 3 arguments \(4 given\)",
                lambda: x[1, 2, 3, 4])

            # bad arg type
            self.assertRaisesRegexp(TypeError, "expected str, got int",
                                    lambda: x[1, 2, 3])

            # bad value type
            def f():
                x[1] = 'abc'

            self.assertRaisesRegexp(TypeError, "expected int, got str", f)
    def _test_set_by_type(self, current_type):
        import System
        from Merlin.Testing.FieldTest import EnumInt64
        current_type.SetStaticFields()

        # pass correct values
        current_type.StaticByteField = 5
        current_type.StaticSByteField = 10
        current_type.StaticUInt16Field = 20
        current_type.StaticInt16Field = 30
        current_type.StaticUInt32Field = 40
        current_type.StaticInt32Field = 50
        current_type.StaticUInt64Field = 60
        current_type.StaticInt64Field = 70
        current_type.StaticDoubleField = 80
        current_type.StaticSingleField = 90
        current_type.StaticDecimalField = 100
        current_type.StaticCharField = 'd'
        current_type.StaticBooleanField = False
        current_type.StaticStringField = 'testing'.upper()

        current_type.StaticObjectField = "number_to_string"
        current_type.StaticEnumField = EnumInt64.C
        current_type.StaticDateTimeField = System.DateTime(500000)
        current_type.StaticSimpleStructField = SimpleStruct(12340)
        current_type.StaticSimpleGenericStructField = SimpleGenericStruct[
            System.UInt16](320)
        current_type.StaticNullableStructNotNullField = None
        current_type.StaticNullableStructNullField = SimpleStruct(650)
        current_type.StaticSimpleClassField = SimpleClass(540)
        current_type.StaticSimpleGenericClassField = SimpleGenericClass[str](
            "string".upper())
        current_type.StaticSimpleInterfaceField = ClassImplementSimpleInterface(
            78)

        # verify
        self._test_verify(current_type)

        # set values which need conversion.
        current_type.StaticInt16Field = long(100)
        self.assertEqual(current_type.StaticInt16Field, 100)

        current_type.StaticBooleanField = 0
        self.assertEqual(current_type.StaticBooleanField, False)

        # set bad values
        def f1():
            current_type.StaticInt16Field = "abc"

        def f2():
            current_type.StaticCharField = "abc"

        def f3():
            current_type.StaticEnumField = EnumInt32.B

        for f in [f1, f2, f3]:
            self.assertRaises(TypeError, f)
Exemple #4
0
    def test_optional(self):
        import System
        from Merlin.Testing import Flag
        from Merlin.Testing.TypeSample import EnumInt32, SimpleClass, SimpleStruct
        #public void M231([Optional] int arg) { Flag.Set(arg); }  // not reset any
        #public void M232([Optional] bool arg) { Flag<bool>.Set(arg); }
        #public void M233([Optional] object arg) { Flag<object>.Set(arg); }
        #public void M234([Optional] string arg) { Flag<string>.Set(arg); }
        #public void M235([Optional] EnumInt32 arg) { Flag<EnumInt32>.Set(arg); }
        #public void M236([Optional] SimpleClass arg) { Flag<SimpleClass>.Set(arg); }
        #public void M237([Optional] SimpleStruct arg) { Flag<SimpleStruct>.Set(arg); }

        ## testing the passed in value, and the default values
        self.o.M231(12); Flag.Check(12)
        self.o.M231(); Flag.Check(0)

        self.o.M232(True); Flag[bool].Check(True)
        self.o.M232(); Flag[bool].Check(False)

        def t(): pass
        self.o.M233(t); Flag[object].Check(t)
        self.o.M233(); Flag[object].Check(System.Type.Missing.Value)

        self.o.M234("ironpython"); Flag[str].Check("ironpython")
        self.o.M234(); Flag[str].Check(None)

        self.o.M235(EnumInt32.B); Flag[EnumInt32].Check(EnumInt32.B)
        self.o.M235(); Flag[EnumInt32].Check(EnumInt32.A)

        x = SimpleClass(23)
        self.o.M236(x); Flag[SimpleClass].Check(x)
        self.o.M236(); Flag[SimpleClass].Check(None)

        x = SimpleStruct(24)
        self.o.M237(x); Flag[SimpleStruct].Check(x)
        self.o.M237(); self.assertEqual(Flag[SimpleStruct].Value1.Flag, 0)

        ## testing the argument style
        f = self.o.M231

        f(*()); Flag.Check(0)
        f(*(2, )); Flag.Check(2)
        f(arg = 3); Flag.Check(3)
        f(**{}); Flag.Check(0)
        f(*(), **{'arg':4}); Flag.Check(4)

        self.assertRaisesMessage(TypeError, "M231() takes at most 1 argument (2 given)", lambda: f(1, 2))  # msg
        self.assertRaisesMessage(TypeError, "M231() takes at most 1 argument (2 given)", lambda: f(1, **{'arg': 2}))  # msg
        self.assertRaisesMessage(TypeError, "M231() takes at most 1 argument (2 given)", lambda: f(arg = 3, **{'arg': 4}))  # msg
        self.assertRaisesMessage(TypeError, "M231() takes at most 1 argument (2 given)", lambda: f(arg = 3, **{'other': 4}))  # msg
    def _test_set_by_descriptor(self, current_type):
        import System
        from Merlin.Testing.FieldTest import EnumInt64
        current_type.SetStaticFields()
        o = current_type()

        # pass correct values
        current_type.__dict__['StaticByteField'].__set__(None, 5)
        current_type.__dict__['StaticSByteField'].__set__(None, 10)
        #current_type.__dict__['StaticSByteField'].__set__(o, 10)
        current_type.__dict__['StaticUInt16Field'].__set__(None, 20)
        current_type.__dict__['StaticInt16Field'].__set__(None, 30)
        current_type.__dict__['StaticUInt32Field'].__set__(None, 40)
        current_type.__dict__['StaticInt32Field'].__set__(None, 50)
        current_type.__dict__['StaticUInt64Field'].__set__(None, 60)
        current_type.__dict__['StaticInt64Field'].__set__(None, 70)
        current_type.__dict__['StaticDoubleField'].__set__(None, 80)
        current_type.__dict__['StaticSingleField'].__set__(None, 90)
        current_type.__dict__['StaticDecimalField'].__set__(None, 100)
        current_type.__dict__['StaticCharField'].__set__(None, 'd')
        current_type.__dict__['StaticBooleanField'].__set__(None, False)
        current_type.__dict__['StaticStringField'].__set__(None, 'TESTING')

        current_type.__dict__['StaticObjectField'].__set__(
            None, "number_to_string")
        current_type.__dict__['StaticEnumField'].__set__(None, EnumInt64.C)
        current_type.__dict__['StaticDateTimeField'].__set__(
            None, System.DateTime(500000))
        current_type.__dict__['StaticSimpleStructField'].__set__(
            None, SimpleStruct(12340))
        current_type.__dict__['StaticSimpleGenericStructField'].__set__(
            None, SimpleGenericStruct[System.UInt16](320))
        current_type.__dict__['StaticNullableStructNotNullField'].__set__(
            None, None)
        current_type.__dict__['StaticNullableStructNullField'].__set__(
            None, SimpleStruct(650))
        current_type.__dict__['StaticSimpleClassField'].__set__(
            None, SimpleClass(540))
        current_type.__dict__['StaticSimpleGenericClassField'].__set__(
            None, SimpleGenericClass[str]("STRING"))
        current_type.__dict__['StaticSimpleInterfaceField'].__set__(
            None, ClassImplementSimpleInterface(78))

        # verify
        self._test_verify(current_type)
Exemple #6
0
 def f21():
     o.InitOnlySimpleClassField = SimpleClass(3000)
 def f22():
     t.__dict__['InstanceSimpleClassField'].__set__(v, SimpleClass(540))
 def f22():
     t.InstanceSimpleClassField.__set__(v, SimpleClass(540))
 def f22():
     v.InstanceSimpleClassField = SimpleClass(540)
Exemple #10
0
 def f21(): current_type.InitOnlySimpleClassField = SimpleClass(3000)
 def f22(): current_type.InitOnlySimpleGenericClassField = None
Exemple #11
0
 def f22(): v.InstanceSimpleClassField = SimpleClass(540)
 def f23(): v.InstanceSimpleGenericClassField = SimpleGenericClass[str]("string".upper())
Exemple #12
0
    def test_basic(self):
        from Merlin.Testing.Property import ClassWithProperties, StructWithProperties
        from Merlin.Testing.TypeSample import SimpleClass, SimpleStruct
        for t in [
                ClassWithProperties,
                StructWithProperties,
        ]:
            # very basic: object.InstanceProperty, Type.StaticProperty
            x, y = t(), t()
            a, b, c = 1234, SimpleStruct(23), SimpleClass(45)

            self.assertEqual(x.InstanceInt32Property, 0)
            x.InstanceInt32Property = a
            self.assertEqual(x.InstanceInt32Property, a)

            self.assertTrue(x.InstanceSimpleStructProperty.Flag == 0)
            x.InstanceSimpleStructProperty = b
            self.assertTrue(b == x.InstanceSimpleStructProperty)
            self.assertEqual(b.Flag, x.InstanceSimpleStructProperty.Flag)

            self.assertEqual(x.InstanceSimpleClassProperty, None)
            x.InstanceSimpleClassProperty = c
            self.assertEqual(c, x.InstanceSimpleClassProperty)

            self.assertEqual(t.StaticInt32Property, 0)
            t.StaticInt32Property = a
            self.assertEqual(t.StaticInt32Property, a)

            t.StaticSimpleStructProperty = b
            self.assertEqual(b.Flag, t.StaticSimpleStructProperty.Flag)

            t.StaticSimpleClassProperty = c
            self.assertEqual(c, t.StaticSimpleClassProperty)

            # Type.InstanceProperty: SetValue/GetValue (on x), __set__/__get__ (on y)
            a, b, c = 34, SimpleStruct(56), SimpleClass(78)

            p = t.InstanceInt32Property
            self.assertEqual(p.SetValue(x, a), None)
            self.assertEqual(p.GetValue(x), a)

            p.__set__(y, a)
            #self.assertEqual(p.__get__(y), a)

            p = t.InstanceSimpleStructProperty
            p.SetValue(x, b)
            self.assertEqual(p.GetValue(x).Flag, b.Flag)

            p.__set__(y, b)
            #self.assertEqual(p.__get__(y).Flag, b.Flag)

            p = t.InstanceSimpleClassProperty
            p.SetValue(x, c)
            self.assertEqual(p.GetValue(x), c)

            p.__set__(y, c)
            #self.assertEqual(p.__get__(y), c)

            # instance.StaticProperty
            a, b, c = 21, SimpleStruct(32), SimpleClass(43)

            # can read static properties through instances...
            self.assertEqual(x.StaticInt32Property, 1234)
            self.assertEqual(type(x.StaticSimpleStructProperty), SimpleStruct)
            self.assertEqual(type(x.StaticSimpleClassProperty), SimpleClass)

            def w1():
                x.StaticInt32Property = a

            def w2():
                x.StaticSimpleStructProperty = b

            def w3():
                x.StaticSimpleClassProperty = c

            for w in [w1, w2, w3]:
                self.assertRaisesRegex(
                    AttributeError,
                    "static property '.*' of '.*' can only be assigned to through a type, not an instance",
                    w)

            #
            # type.__dict__['xxxProperty']
            #
            x = t()
            a, b, c = 8, SimpleStruct(7), SimpleClass(6)

            p = t.__dict__['StaticInt32Property']
            #p.SetValue(None, a)                # bug 363241
            #self.assertEqual(a, p.GetValue(None))

            # static property against instance
            self.assertRaisesRegex(SystemError, "cannot set property",
                                   lambda: p.SetValue(x, a))
            #self.assertRaisesRegex(SystemError, "cannot get property", lambda: p.GetValue(x))  # bug 363242

            p = t.__dict__['InstanceInt32Property']
            p.SetValue(x, a)
            #self.assertEqual(p.GetValue(x), a)         # value type issue again

            # instance property against None
            self.assertRaisesRegex(SystemError, "cannot set property",
                                   lambda: p.SetValue(None, a))
            #self.assertRaisesRegex(SystemError, "cannot get property", lambda: p.GetValue(None))  # bug 363247

            p = t.__dict__['StaticSimpleStructProperty']
            p.__set__(None, b)
            #self.assertEqual(b.Flag, p.__get__(None).Flag)

            # do we care???
            #print p.__set__(x, b)
            #print p.__get__(x)

            p = t.__dict__['InstanceSimpleStructProperty']
            p.__set__(x, b)
            #self.assertEqual(b.Flag, p.__get__(x).Flag)

            # do we care?
            #print p.__set__(None, b)
            #print p.__get__(None)

            p = t.__dict__['StaticSimpleClassProperty']
            p.__set__(None, c)  # similar to bug 363241
            #self.assertEqual(c, p.__get__(None))

            p = t.__dict__['InstanceSimpleClassProperty']
            p.__set__(x, c)
 def f22():
     o.StaticSimpleClassField = SimpleClass(540)
Exemple #14
0
 def f22(): t.__dict__['InstanceSimpleClassField'].__set__(v, SimpleClass(540))
 def f23(): t.__dict__['InstanceSimpleGenericClassField'].__set__(v, SimpleGenericClass[str]("STRING"))
Exemple #15
0
 def f22(): t.InstanceSimpleClassField.__set__(v, SimpleClass(540))
 def f23(): t.InstanceSimpleGenericClassField.__set__(v, SimpleGenericClass[str]("string".upper()))
Exemple #16
0
 def f21():
     current_type.InitOnlySimpleClassField = SimpleClass(3000)
Exemple #17
0
 def f21(): o.InitOnlySimpleClassField = SimpleClass(3000)
 def f22(): o.InitOnlySimpleGenericClassField = None
Exemple #18
0
 def f22(): o.StaticSimpleClassField = SimpleClass(540)
 def f23(): o.StaticSimpleGenericClassField = SimpleGenericClass[str]("string".upper())