test_widgetbase_tpc.py :  » PDF » ReportLab » ReportLab_2_4 » tests » 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 » PDF » ReportLab 
ReportLab » ReportLab_2_4 » tests » test_widgetbase_tpc.py
#Copyright ReportLab Europe Ltd. 2000-2008
#see license.txt for license details
"""
Tests for TypedPropertyCollection class.
"""
__version__='''$Id: test_widgetbase_tpc.py 3288 2008-09-15 11:03:17Z rgbecker $'''
from reportlab.lib.testutils import setOutDir,makeSuiteForClasses,printLocation
setOutDir(__name__)
import os, sys, copy
from os.path import join,basename,splitext
import unittest
from reportlab.graphics.widgetbase import PropHolder,TypedPropertyCollection
from reportlab.lib.attrmap import AttrMap,AttrMapValue
from reportlab.lib.validators import isNumber


TPC = TypedPropertyCollection


class PH(PropHolder):
    _attrMap = AttrMap(
        a = AttrMapValue(isNumber),
        b = AttrMapValue(isNumber)
        )


class APH(PH):
    def __init__(self):
        self.a = 1


class BPH(APH):
    def __init__(self):
        APH.__init__(self)

    def __getattr__(self,name):
        if name=='b': return -1
        raise AttributeError


class TPCTestCase(unittest.TestCase):
    "Test TypedPropertyCollection class."

    def test0(self):
        "Test setting an invalid collective attribute."

        t = TPC(PH)
        try:
            t.c = 42
        except AttributeError:
            pass


    def test1(self):
        "Test setting a valid collective attribute."

        t = TPC(PH)
        t.a = 42
        assert t.a == 42


    def test2(self):
        "Test setting a valid collective attribute with an invalid value."

        t = TPC(PH)
        try:
            t.a = 'fourty-two'
        except AttributeError:
            pass


    def test3(self):
        "Test setting a valid collective attribute with a convertible invalid value."

        t = TPC(PH)
        t.a = '42'
        assert t.a == '42' # Or should it rather be an integer?


    def test4(self):
        "Test accessing an unset collective attribute."

        t = TPC(PH)
        try:
            t.a
        except AttributeError:
            pass


    def test5(self):
        "Test overwriting a collective attribute in one slot."

        t = TPC(PH)
        t.a = 42
        t[0].a = 4242
        assert t[0].a == 4242


    def test6(self):
        "Test overwriting a one slot attribute with a collective one."

        t = TPC(PH)
        t[0].a = 4242
        t.a = 42
        assert t[0].a == 4242


    def test7(self):
        "Test to ensure we can handle classes with __getattr__ methods"

        a=TypedPropertyCollection(APH)
        b=TypedPropertyCollection(BPH)

        a.a=3
        b.a=4
        try:
            a.b
            assert 1, "Shouldn't be able to see a.b"
        except AttributeError:
            pass
        a.b=0
        assert a.b==0, "Wrong value for "+str(a.b)
        assert b.b==-1, "This should call __getattr__ special"
        b.b=0
        assert a[0].b==0
        assert b[0].b==-1, "Class __getattr__ should return -1"


def makeSuite():
    return makeSuiteForClasses(TPCTestCase)


#noruntests
if __name__ == "__main__":
    unittest.TextTestRunner().run(makeSuite())
    printLocation()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.