AttributeTest.py :  » Ajax » pyjamas » src » examples » libtest » 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 » Ajax » pyjamas 
pyjamas » src » examples » libtest » AttributeTest.py
from UnitTest import UnitTest

name = 'Name'
prototype = 'Prototype'
call = 'Call'
apply = 'Apply'
constructor = 'Constructor'

class Foo:
    a = 1
    b = [1,2]
    name = "Foo"
    label = "label"

    def __init__(self, v):
        self.v = v

    def getV(self):
        return self.v

    def call(self, name):
        name = name.upper()
        prototype = self.name
        apply = self.name.lower()
        return (name, prototype, apply, self.name)

class AttributeTest(UnitTest):

    def testHasattr(self):
        self.assertEqual(hasattr(self, "getName"), True, "AttrTest should have method 'getName'")
        self.assertEqual(hasattr(self, "blah"), False, "AttrTest has no method 'getName'")

    def testGetattr(self):
        func = getattr(self, "getName")
        self.assertEqual(func(), "AttributeTest",
                         "getattr does not return correct value'")

        self.assertEqual(1, getattr(Foo, "notthere", 1))
        foo = Foo(1)
        self.assertEqual(foo.v, getattr(foo, "v"))

        # test on none object type
        self.assertEqual(getattr(1, 'x', 2), 2)
        self.assertEqual(getattr(None, 'x', 2), 2)

        try:
            self.assertEqual(1, getattr(foo, "vv"))
        except AttributeError, e:
            self.assertEqual(e.__class__.__name__, 'AttributeError')
            return
        self.fail("No AttributeError raised")

    def testSetAttr(self):

        f1 = Foo(1)
        self.assertEqual(f1.getV(), 1)

        f2 = Foo(2)
        self.assertEqual(f2.getV(), 2)

        f3 = Foo(3)
        self.assertEqual(f3.getV(), 3)

        # bound method
        setattr(f1, "getV", getattr(f2, "getV"))
        self.assertEqual(f1.getV(), 2)

        # unbound method
        setattr(f1, "getV", f3.getV) # reeallly need to have __getattr__
        self.assertEqual(f1.getV(), 3)

    def testDelAttr(self):

        foo = Foo(1)
        self.assertEqual(hasattr(foo, "v"), True)
        delattr(foo, "v")
        self.assertEqual(hasattr(foo, "v"), False)
        
        self.assertEqual(hasattr(foo, "getV"), True)
        try:
            delattr(foo, "getV")
            self.fail("No AttributeError raised")
        except AttributeError, e:
            self.assertEqual(str(e), "Foo instance has no attribute 'getV'")

    def testAttrErr(self):
        foo = Foo(1)
        try:
            v = foo.bar
            self.fail("No Error raised on foo.bar")
        except:
            self.assertTrue(True, "Exception raised")

    def testInstanceAttr(self):
        foo = Foo(1)
        foo_fn = foo.getV
        try:
            t = foo_fn()
        except:
            t = None
        self.assertEqual(t, 1)
        foo.getV = 2
        try:
            t = foo_fn()
        except:
            t = None
        self.assertEqual(t, 1)
        t = foo.a
        foo.a = 2
        self.assertEqual(t, 1)
        t = foo.b
        foo.b.append(3)
        self.assertEqual(t[2], 3)

    def testAttributMapping(self):
        f = Foo(1)
        self.assertEqual(Foo.name, 'Foo')
        self.assertEqual(f.name, 'Foo')
        name, prototype, apply, constructor = f.call('bAr')
        self.assertEqual(name, 'BAR')
        self.assertEqual(prototype, 'Foo')
        self.assertEqual(apply, 'foo')
        self.assertEqual(constructor, 'Foo')
        self.assertEqual(Foo.label, 'label')
        self.assertEqual(f.label, 'label')

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.