1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
|
#
begin
require 'win32ole'
rescue LoadError
end
require 'test/unit'
if defined?(WIN32OLE)
module CONST1
end
module CONST2
end
module TestCaseForDict
def test_convert_bignum
@dict1.add("a", 9999999999)
@dict1.add("b", 999999999)
@dict1.add("c", @dict1.item("b") * 10 + 9)
assert_equal(9999999999, @dict1.item("a"))
assert_equal(9999999999, @dict1.item("c"))
end
def test_add
@dict1.add("a", 1000)
assert_equal(1000, @dict1.item("a"))
end
def test_setproperty_equal_ended
@dict1.compareMode = 1
@dict1.add("one", 1)
assert_equal(1, @dict1.item("ONE"))
@dict2.add("one", 1)
assert_nil(@dict2.item("ONE"))
assert_equal(1, @dict2.item("one"))
end
def test_non_exist_property
assert_raise(WIN32OLERuntimeError) {
@dict1.unknown_property = 1
}
end
def test_raise_message
exc = assert_raise(WIN32OLERuntimeError) {
@dict1.add
}
assert_match(/^\(in OLE method `add': \)/, exc.message)
exc = assert_raise(WIN32OLERuntimeError) {
@dict1._invoke(1, [], [])
}
assert_match(/^\(in OLE method `<dispatch id:1>': \)/, exc.message)
exc = assert_raise(WIN32OLERuntimeError) {
@dict1.compareMode = 100
}
assert_match(/^\(in setting property `compareMode': \)/, exc.message)
end
def test_ole_methods
methods = @dict1.ole_methods
mnames = methods.collect {|m|
m.name
}
assert(mnames.include?("Add"))
end
def test_ole_func_methods
methods = @dict1.ole_func_methods
mnames = methods.collect {|m|
m.name
}
assert(mnames.include?("Add"))
end
def test_ole_put_methods
methods = @dict1.ole_put_methods
mnames = methods.collect {|m|
m.name
}
assert(mnames.include?("CompareMode"))
end
def test_ole_get_methods
methods = @dict1.ole_get_methods
mnames = methods.collect {|m|
m.name
}
assert(mnames.include?("Count"))
end
def test_ole_mehtod_help
minfo = @dict1.ole_method_help("Add")
assert_equal(2, minfo.size_params)
end
def test_ole_typelib
tlib = @dict1.ole_typelib
assert_equal("Microsoft Scripting Runtime", tlib.name);
end
def test_each
@dict1.add("one", 1)
@dict1.add("two", 2)
i = 0
@dict1.keys.each do |item|
i += 1
end
assert_equal(2, i)
end
def test_bracket
@dict1.add("foo", "FOO")
assert_equal("FOO", @dict1.item("foo"))
assert_equal("FOO", @dict1["foo"])
end
def test_bracket_equal
@dict1.add("foo", "FOO")
@dict1["foo"] = "BAR"
assert_equal("BAR", @dict1["foo"])
end
end
class TestWin32OLE < Test::Unit::TestCase
include TestCaseForDict
def setup
@dict1 = WIN32OLE.new('Scripting.Dictionary')
@dict2 = WIN32OLE.new('Scripting.Dictionary')
end
def test_s_new
assert_instance_of(WIN32OLE, @dict1)
assert_instance_of(WIN32OLE, @dict2)
end
def test_s_new_DCOM
rshell = WIN32OLE.new("Shell.Application")
assert_instance_of(WIN32OLE, rshell)
end
def test_s_new_from_clsid
shell = WIN32OLE.new("{13709620-C279-11CE-A49E-444553540000}")
assert_instance_of(WIN32OLE, shell)
exc = assert_raise(WIN32OLERuntimeError) {
WIN32OLE.new("{000}")
}
assert_match(/unknown OLE server: `\{000\}'/, exc.message)
end
# test_s_connect was moved to test_word.rb
# def test_s_connect
# end
def test_invoke_accept_symbol_hash_key
fso = WIN32OLE.new('Scripting.FileSystemObject')
afolder = fso.getFolder(".")
bfolder = fso.getFolder({"FolderPath" => "."})
cfolder = fso.getFolder({:FolderPath => "."})
assert_equal(afolder.path, bfolder.path)
assert_equal(afolder.path, cfolder.path)
fso = nil
end
def test_setproperty
installer = WIN32OLE.new("WindowsInstaller.Installer")
record = installer.CreateRecord(2)
# this is the way to set property with argument in Win32OLE.
record.setproperty( "StringData", 1, 'dddd')
assert_equal('dddd', record.StringData(1))
end
def test_ole_type
fso = WIN32OLE.new('Scripting.FileSystemObject')
tobj = fso.ole_type
assert_match(/^IFileSystem/, tobj.name)
end
def test_ole_obj_help
fso = WIN32OLE.new('Scripting.FileSystemObject')
tobj = fso.ole_obj_help
assert_match(/^IFileSystem/, tobj.name)
end
def test_invoke_hash_key_non_str_sym
fso = WIN32OLE.new('Scripting.FileSystemObject')
begin
bfolder = fso.getFolder({1 => "."})
assert(false)
rescue TypeError
assert(true)
end
fso = nil
end
def test_get_win32ole_object
shell = WIN32OLE.new('Shell.Application')
folder = shell.nameSpace(0)
assert_instance_of(WIN32OLE, folder)
end
def test_invoke_accept_multi_hash_key
shell = WIN32OLE.new('Shell.Application')
folder = shell.nameSpace(0)
item = folder.items.item(0)
name = folder.getDetailsOf(item, 0)
assert_equal(item.name, name)
name = folder.getDetailsOf({:vItem => item, :iColumn => 0})
assert_equal(item.name, name)
name = folder.getDetailsOf({"vItem" => item, :iColumn => 0})
assert_equal(item.name, name)
end
def test_ole_invoke_with_named_arg_last
shell = WIN32OLE.new('Shell.Application')
folder = shell.nameSpace(0)
item = folder.items.item(0)
name = folder.getDetailsOf(item, {:iColumn => 0})
assert_equal(item.name, name)
end
def test__invoke
shell=WIN32OLE.new('Shell.Application')
assert_equal(shell.NameSpace(0).title, shell._invoke(0x60020002, [0], [WIN32OLE::VARIANT::VT_VARIANT]).title)
end
def test_s_const_load
assert(!defined?(CONST1::SsfWINDOWS))
shell=WIN32OLE.new('Shell.Application')
WIN32OLE.const_load(shell, CONST1)
assert_equal(36, CONST1::SsfWINDOWS)
assert(!defined?(CONST2::SsfWINDOWS))
WIN32OLE.const_load("Microsoft Shell Controls And Automation", CONST2)
assert_equal(36, CONST2::SsfWINDOWS)
end
def test_s_create_guid
guid = WIN32OLE.create_guid
assert_match(/^\{[A-Z0-9]{8}\-[A-Z0-9]{4}\-[A-Z0-9]{4}\-[A-Z0-9]{4}\-[A-Z0-9]{12}/,
guid)
end
def test_s_codepage
assert_equal(WIN32OLE::CP_ACP, WIN32OLE.codepage)
end
def test_s_codepage_set
WIN32OLE.codepage = WIN32OLE::CP_UTF8
assert_equal(WIN32OLE::CP_UTF8, WIN32OLE.codepage)
WIN32OLE.codepage = WIN32OLE::CP_ACP
end
def test_s_codepage_changed
fso = WIN32OLE.new("Scripting.FileSystemObject")
fname = fso.getTempName
begin
WIN32OLE.codepage = WIN32OLE::CP_UTF8
file = fso.opentextfile(fname, 2, true)
file.write [0x3042].pack("U*")
file.close
str = ""
open(fname) {|ifs|
str = ifs.read
}
assert_equal("\202\240", str)
WIN32OLE.codepage = WIN32OLE::CP_ACP
file = fso.opentextfile(fname, 2, true)
file.write [0x3042].pack("U*")
file.close
open(fname) {|ifs|
str = ifs.read
}
assert_equal("\343\201", str)
# This test fail if codepage 20932 (euc) is not installed.
begin
WIN32OLE.codepage = 20932
rescue WIN32OLERuntimeError
end
if (WIN32OLE.codepage == 20932)
file = fso.opentextfile(fname, 2, true)
file.write [164, 162].pack("c*")
file.close
open(fname) {|ifs|
str = ifs.read
}
assert_equal("\202\240", str)
end
ensure
WIN32OLE.codepage = WIN32OLE::CP_ACP
if (File.exist?(fname))
File.unlink(fname)
end
end
end
def test_const_CP_ACP
assert_equal(0, WIN32OLE::CP_ACP)
end
def test_const_CP_OEMCP
assert_equal(1, WIN32OLE::CP_OEMCP)
end
def test_const_CP_MACCP
assert_equal(2, WIN32OLE::CP_MACCP)
end
def test_const_CP_THREAD_ACP
assert_equal(3, WIN32OLE::CP_THREAD_ACP)
end
def test_const_CP_SYMBOL
assert_equal(42, WIN32OLE::CP_SYMBOL)
end
def test_const_CP_UTF7
assert_equal(65000, WIN32OLE::CP_UTF7)
end
def test_const_CP_UTF8
assert_equal(65001, WIN32OLE::CP_UTF8)
end
end
# test of subclass of WIN32OLE
class MyDict < WIN32OLE
def MyDict.new
super('Scripting.Dictionary')
end
end
class TestMyDict < Test::Unit::TestCase
include TestCaseForDict
def setup
@dict1 = MyDict.new
@dict2 = MyDict.new
end
def test_s_new
assert_instance_of(MyDict, @dict1)
assert_instance_of(MyDict, @dict2)
end
end
end
|