VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/python/test/test_isupports_primitives.py@ 63668

最後變更 在這個檔案從63668是 11746,由 vboxsync 提交於 16 年 前

export python lib to OSE

  • 屬性 svn:eol-style 設為 native
檔案大小: 8.6 KB
 
1# ***** BEGIN LICENSE BLOCK *****
2# Version: MPL 1.1/GPL 2.0/LGPL 2.1
3#
4# The contents of this file are subject to the Mozilla Public License Version
5# 1.1 (the "License"); you may not use this file except in compliance with
6# the License. You may obtain a copy of the License at
7# http://www.mozilla.org/MPL/
8#
9# Software distributed under the License is distributed on an "AS IS" basis,
10# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11# for the specific language governing rights and limitations under the
12# License.
13#
14# The Original Code is the Python XPCOM language bindings.
15#
16# The Initial Developer of the Original Code is
17# ActiveState Tool Corp.
18# Portions created by the Initial Developer are Copyright (C) 2000, 2001
19# the Initial Developer. All Rights Reserved.
20#
21# Contributor(s):
22# Mark Hammond <[email protected]> (original author)
23#
24# Alternatively, the contents of this file may be used under the terms of
25# either the GNU General Public License Version 2 or later (the "GPL"), or
26# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27# in which case the provisions of the GPL or the LGPL are applicable instead
28# of those above. If you wish to allow use of your version of this file only
29# under the terms of either the GPL or the LGPL, and not to allow others to
30# use your version of this file under the terms of the MPL, indicate your
31# decision by deleting the provisions above and replace them with the notice
32# and other provisions required by the GPL or the LGPL. If you do not delete
33# the provisions above, a recipient may use your version of this file under
34# the terms of any one of the MPL, the GPL or the LGPL.
35#
36# ***** END LICENSE BLOCK *****
37
38# Test our support for the interfaces defined in nsISupportsPrimitives.idl
39#
40# The framework supports nsISupportsCString and nsISupportsString, but
41# only if our class doesnt provide explicit support.
42
43from xpcom import components
44from xpcom import primitives
45import xpcom.server, xpcom.client
46from pyxpcom_test_tools import testmain
47import unittest
48
49class NoSupportsString:
50 _com_interfaces_ = [components.interfaces.nsISupports]
51
52class ImplicitSupportsString:
53 _com_interfaces_ = [components.interfaces.nsISupports]
54 def __str__(self):
55 return "<MyImplicitStrObject>"
56
57class ExplicitSupportsString:
58 _com_interfaces_ = [components.interfaces.nsISupportsPrimitive,
59 components.interfaces.nsISupportsCString]
60 type = components.interfaces.nsISupportsPrimitive.TYPE_CSTRING
61 test_data = "<MyExplicitStrObject>"
62 # __str__ will be ignored by XPCOM, as we have _explicit_ support.
63 def __str__(self):
64 return "<MyImplicitStrObject>"
65 # These are the ones that will be used.
66 def get_data(self):
67 return self.test_data
68 def toString(self):
69 return self.test_data
70
71class ImplicitSupportsUnicode:
72 _com_interfaces_ = [components.interfaces.nsISupports]
73 test_data = u"Copyright \xa9 the initial developer"
74 def __unicode__(self):
75 # An extended character in unicode tests can't hurt!
76 return self.test_data
77
78class ExplicitSupportsUnicode:
79 _com_interfaces_ = [components.interfaces.nsISupportsPrimitive,
80 components.interfaces.nsISupportsString]
81 type = components.interfaces.nsISupportsPrimitive.TYPE_STRING
82 # __unicode__ will be ignored by XPCOM, as we have _explicit_ support.
83 test_data = u"Copyright \xa9 the initial developer"
84 def __unicode__(self):
85 return self.test_data
86 def get_data(self):
87 return self.test_data
88
89class ImplicitSupportsInt:
90 _com_interfaces_ = [components.interfaces.nsISupports]
91 def __int__(self):
92 return 99
93
94class ExplicitSupportsInt:
95 _com_interfaces_ = [components.interfaces.nsISupportsPrimitive,
96 components.interfaces.nsISupportsPRInt32]
97 type = components.interfaces.nsISupportsPrimitive.TYPE_PRINT32
98 def get_data(self):
99 return 99
100
101class ImplicitSupportsLong:
102 _com_interfaces_ = [components.interfaces.nsISupports]
103 def __long__(self):
104 return 99L
105
106class ExplicitSupportsLong:
107 _com_interfaces_ = [components.interfaces.nsISupportsPrimitive,
108 components.interfaces.nsISupportsPRInt64]
109 type = components.interfaces.nsISupportsPrimitive.TYPE_PRINT64
110 def get_data(self):
111 return 99
112
113class ExplicitSupportsFloat:
114 _com_interfaces_ = [components.interfaces.nsISupportsPrimitive,
115 components.interfaces.nsISupportsDouble]
116 type = components.interfaces.nsISupportsPrimitive.TYPE_DOUBLE
117 def get_data(self):
118 return 99.99
119
120class ImplicitSupportsFloat:
121 _com_interfaces_ = [components.interfaces.nsISupports]
122 def __float__(self):
123 return 99.99
124
125class PrimitivesTestCase(unittest.TestCase):
126 def testNoSupports(self):
127 ob = xpcom.server.WrapObject( NoSupportsString(), components.interfaces.nsISupports)
128 if not str(ob).startswith("<XPCOM "):
129 raise RuntimeError, "Wrong str() value: %s" % (ob,)
130
131 def testImplicitString(self):
132 ob = xpcom.server.WrapObject( ImplicitSupportsString(), components.interfaces.nsISupports)
133 self.failUnlessEqual(str(ob), "<MyImplicitStrObject>")
134
135 def testExplicitString(self):
136 ob = xpcom.server.WrapObject( ExplicitSupportsString(), components.interfaces.nsISupports)
137 self.failUnlessEqual(str(ob), "<MyExplicitStrObject>")
138
139 def testImplicitUnicode(self):
140 ob = xpcom.server.WrapObject( ImplicitSupportsUnicode(), components.interfaces.nsISupports)
141 self.failUnlessEqual(unicode(ob), ImplicitSupportsUnicode.test_data)
142
143 def testExplicitUnicode(self):
144 ob = xpcom.server.WrapObject( ExplicitSupportsUnicode(), components.interfaces.nsISupports)
145 self.failUnlessEqual(unicode(ob), ExplicitSupportsUnicode.test_data)
146
147 def testConvertInt(self):
148 # Try our conversions.
149 ob = xpcom.server.WrapObject( ExplicitSupportsString(), components.interfaces.nsISupports)
150 self.failUnlessRaises( ValueError, int, ob)
151
152 def testExplicitInt(self):
153 ob = xpcom.server.WrapObject( ExplicitSupportsInt(), components.interfaces.nsISupports)
154 self.failUnlessAlmostEqual(float(ob), 99.0)
155 self.failUnlessEqual(int(ob), 99)
156
157 def testImplicitInt(self):
158 ob = xpcom.server.WrapObject( ImplicitSupportsInt(), components.interfaces.nsISupports)
159 self.failUnlessAlmostEqual(float(ob), 99.0)
160 self.failUnlessEqual(int(ob), 99)
161
162 def testExplicitLong(self):
163 ob = xpcom.server.WrapObject( ExplicitSupportsLong(), components.interfaces.nsISupports)
164 if long(ob) != 99 or not repr(long(ob)).endswith("L"):
165 raise RuntimeError, "Bad value: %s" % (repr(long(ob)),)
166 self.failUnlessAlmostEqual(float(ob), 99.0)
167
168 def testImplicitLong(self):
169 ob = xpcom.server.WrapObject( ImplicitSupportsLong(), components.interfaces.nsISupports)
170 if long(ob) != 99 or not repr(long(ob)).endswith("L"):
171 raise RuntimeError, "Bad value: %s" % (repr(long(ob)),)
172 self.failUnlessAlmostEqual(float(ob), 99.0)
173
174 def testExplicitFloat(self):
175 ob = xpcom.server.WrapObject( ExplicitSupportsFloat(), components.interfaces.nsISupports)
176 self.failUnlessEqual(float(ob), 99.99)
177 self.failUnlessEqual(int(ob), 99)
178
179 def testImplicitFloat(self):
180 ob = xpcom.server.WrapObject( ImplicitSupportsFloat(), components.interfaces.nsISupports)
181 self.failUnlessEqual(float(ob), 99.99)
182 self.failUnlessEqual(int(ob), 99)
183
184class PrimitivesModuleTestCase(unittest.TestCase):
185 def testExplicitString(self):
186 ob = xpcom.server.WrapObject( ExplicitSupportsString(), components.interfaces.nsISupports)
187 self.failUnlessEqual(primitives.GetPrimitive(ob), "<MyExplicitStrObject>")
188
189 def testExplicitUnicode(self):
190 ob = xpcom.server.WrapObject( ExplicitSupportsUnicode(), components.interfaces.nsISupports)
191 self.failUnlessEqual(primitives.GetPrimitive(ob), ExplicitSupportsUnicode.test_data)
192 self.failUnlessEqual(type(primitives.GetPrimitive(ob)), unicode)
193
194 def testExplicitInt(self):
195 ob = xpcom.server.WrapObject( ExplicitSupportsInt(), components.interfaces.nsISupports)
196 self.failUnlessEqual(primitives.GetPrimitive(ob), 99)
197
198 def testExplicitLong(self):
199 ob = xpcom.server.WrapObject( ExplicitSupportsLong(), components.interfaces.nsISupports)
200 self.failUnlessEqual(primitives.GetPrimitive(ob), 99)
201
202 def testExplicitFloat(self):
203 ob = xpcom.server.WrapObject( ExplicitSupportsFloat(), components.interfaces.nsISupports)
204 self.failUnlessEqual(primitives.GetPrimitive(ob), 99.99)
205
206if __name__=='__main__':
207 testmain()
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette