policySemantics.py :  » Windows » pyExcelerator » pywin32-214 » com » win32com » test » 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 » Windows » pyExcelerator 
pyExcelerator » pywin32 214 » com » win32com » test » policySemantics.py
import win32com.server.util
import win32com.client
import pythoncom
import winerror
import win32com.test.util

import unittest

class Error(Exception):
    pass

# An object representing a list of numbers
class PythonSemanticClass:
    _public_methods_ = ["In"]  # DISPIDs are allocated.
    _dispid_to_func_ = { 10: 'Add', 11:'Remove'} # DISPIDs specified by the object.
    def __init__(self):
        self.list = []
    def _NewEnum(self):
        return win32com.server.util.NewEnum(self.list)
    def _value_(self):
        # should return an array.
        return self.list
    def _Evaluate(self):
        # return the sum
        return sum(self.list)
    def In(self, value):
        return value in self.list
    def Add(self, value):
        self.list.append(value)
    def Remove(self, value):
        self.list.remove(value)

def DispExTest(ob):
    if not __debug__: print "WARNING: Tests dressed up as assertions are being skipped!"
    assert ob.GetDispID("Add", 0)==10, "Policy did not honour the dispid"
# Not impl
#       assert ob.GetMemberName(10, 0)=="add", "Policy did not give me the correct function for the dispid"
    assert ob.GetDispID("Remove", 0)==11, "Policy did not honour the dispid"
    assert ob.GetDispID("In", 0)==1000, "Allocated dispid unexpected value"
    assert ob.GetDispID("_NewEnum", 0)==pythoncom.DISPID_NEWENUM, "_NewEnum() got unexpected DISPID"
    dispids = []
    dispid = -1
    while 1:
        try:
            dispid = ob.GetNextDispID(0, dispid)
            dispids.append(dispid)
        except pythoncom.com_error, (hr, desc, exc, arg):
            assert hr==winerror.S_FALSE, "Bad result at end of enum"
            break
    dispids.sort()
    if dispids != [pythoncom.DISPID_EVALUATE, pythoncom.DISPID_NEWENUM, 10, 11, 1000]:
        raise Error("Got back the wrong dispids: %s" % dispids)

def SemanticTest(ob):
    # First just check our object "generally" as expected.
    ob.Add(1)
    ob.Add(2)
    ob.Add(3)
    # invoke _value_
    if ob() != (1,2,3):
        raise Error("Bad result - got %s" % (repr(ob())))

    dispob = ob._oleobj_

    rc = dispob.Invoke(pythoncom.DISPID_EVALUATE, 0, pythoncom.DISPATCH_METHOD|pythoncom.DISPATCH_PROPERTYGET, 1)
    if rc != 6:
        raise Error("Evaluate returned %d" % rc)


class Tester(win32com.test.util.TestCase):
    def setUp(self):
        debug=0
        import win32com.server.dispatcher
        if debug:
            dispatcher=win32com.server.dispatcher.DefaultDebugDispatcher
        else:
            dispatcher=None
        disp = win32com.server.util.wrap(PythonSemanticClass(), useDispatcher=dispatcher)
        self.ob = win32com.client.Dispatch(disp)
    def tearDown(self):
        self.ob = None
    def testSemantics(self):
        SemanticTest(self.ob)
    def testIDispatchEx(self):
        dispexob = self.ob._oleobj_.QueryInterface(pythoncom.IID_IDispatchEx)
        DispExTest(dispexob)

if __name__=='__main__':
    unittest.main()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.