1
# You need RubyUnit and MS Excel and MSI to run this test script
4
require 'runit/testcase'
5
# require 'runit/cui/testrunner'
22
class TestWin32OLE < RUNIT::TestCase
25
@excel = WIN32OLE.new("Excel.Application")
29
assert_instance_of(WIN32OLE, @excel)
32
rexcel = WIN32OLE.new("Excel.Application", "localhost")
33
assert_instance_of(WIN32OLE, rexcel)
37
def test_s_new_from_clsid
38
excel = WIN32OLE.new("{00024500-0000-0000-C000-000000000046}")
39
assert_instance_of(WIN32OLE, excel)
41
exc = assert_exception(WIN32OLERuntimeError) {
44
assert_match(/unknown OLE server: `\{000\}'/, exc.message)
47
excel2 = WIN32OLE.connect('Excel.Application')
48
assert_instance_of(WIN32OLE, excel2)
52
assert(!defined?(EXCEL_CONST::XlTop))
53
WIN32OLE.const_load(@excel, EXCEL_CONST)
54
assert_equal(-4160, EXCEL_CONST::XlTop)
56
assert(!defined?(CONST1::XlTop))
57
WIN32OLE.const_load(MS_EXCEL_TYPELIB, CONST1)
58
assert_equal(-4160, CONST1::XlTop)
62
assert_equal(WIN32OLE::CP_ACP, WIN32OLE.codepage)
65
def test_s_codepage_set
66
WIN32OLE.codepage = WIN32OLE::CP_UTF8
67
assert_equal(WIN32OLE::CP_UTF8, WIN32OLE.codepage)
68
WIN32OLE.codepage = WIN32OLE::CP_ACP
72
assert_equal(0, WIN32OLE::CP_ACP)
75
def test_const_CP_OEMCP
76
assert_equal(1, WIN32OLE::CP_OEMCP)
79
def test_const_CP_MACCP
80
assert_equal(2, WIN32OLE::CP_MACCP)
83
def test_const_CP_THREAD_ACP
84
assert_equal(3, WIN32OLE::CP_THREAD_ACP)
87
def test_const_CP_SYMBOL
88
assert_equal(42, WIN32OLE::CP_SYMBOL)
91
def test_const_CP_UTF7
92
assert_equal(65000, WIN32OLE::CP_UTF7)
95
def test_const_CP_UTF8
96
assert_equal(65001, WIN32OLE::CP_UTF8)
99
def test_s_codepage_changed
100
book = @excel.workbooks.add
101
sheet = book.worksheets(1)
103
WIN32OLE.codepage = WIN32OLE::CP_UTF8
104
sheet.range("A1").value = [0x3042].pack("U*")
105
val = sheet.range("A1").value
106
assert_equal("\343\201\202", val)
107
WIN32OLE.codepage = WIN32OLE::CP_ACP
108
val = sheet.range("A1").value
109
assert_equal("\202\240", val)
115
def test_get_win32ole_object
116
workbooks = @excel.Workbooks;
117
assert_instance_of(WIN32OLE, workbooks)
120
workbooks = @excel.Workbooks
121
assert_no_exception {
123
workbooks.each do |workbook|
130
workbooks.each do |workbook|
134
workbooks.each do |workbook|
135
workbook.saved = true
138
def test_setproperty_bracket
139
book = @excel.workbooks.add
140
sheet = book.worksheets(1)
142
sheet.range("A1").value = 10
143
assert_equal(10, sheet.range("A1").value)
144
sheet.cells[1, 2] = 10
145
assert_equal(10, sheet.range("B1").value)
150
def test_convert_bignum
151
book = @excel.workbooks.add
152
sheet = book.worksheets(1)
154
sheet.range("A1").value = 999999999
155
sheet.range("A2").value = 9999999999
156
sheet.range("A3").value = "=A1*10 + 9"
157
assert_equal(9999999999, sheet.range("A2").value)
158
assert_equal(9999999999, sheet.range("A3").value)
165
def test_ole_invoke_with_named_arg
166
book = @excel.workbooks.add
167
sheets = book.worksheets
168
sheet = book.worksheets(1)
171
sheets.add({'count' => 2, 'after'=>sheet})
172
assert_equal(2, sheets.count - num);
178
def test_ole_invoke_with_named_arg_last
179
book = @excel.workbooks.add
180
sheets = book.worksheets
181
sheet = book.worksheets(1)
184
sheets.add(sheet, {'count' => 2})
185
assert_equal(2, sheets.count - num);
192
@excel.setproperty('Visible', false)
193
assert_equal(false, @excel.Visible)
194
@excel.setproperty('Visible', true)
195
assert_equal(true, @excel.Visible)
196
book = @excel.workbooks.add
197
sheet = book.worksheets(1)
199
sheet.setproperty('Cells', 1, 2, 10)
200
assert_equal(10, sheet.range("B1").value)
205
def test_no_exist_property
208
@excel.unknown_prop = 1
209
rescue WIN32OLERuntimeError
216
@excel['unknown_prop'] = 2
217
rescue WIN32OLERuntimeError
223
def test_setproperty_with_equal
224
book = @excel.workbooks.add
225
sheet = book.worksheets(1)
227
sheet.range("B1").value = 10
228
assert_equal(10, sheet.range("B1").value)
229
sheet.range("C1:D1").value = [11, 12]
230
assert_equal(11, sheet.range("C1").value)
231
assert_equal(12, sheet.range("D1").value)
237
workbooks = @excel.invoke( 'workbooks' )
238
assert_instance_of(WIN32OLE, workbooks)
239
book = workbooks.invoke( 'add' )
240
assert_instance_of(WIN32OLE, book)
243
tobj = @excel.ole_type
244
assert_equal('_Application', tobj.name)
246
def test_ole_obj_help
247
tobj = @excel.ole_type
248
assert_equal('_Application', tobj.name)
251
methods = @excel.ole_methods
252
method_names = methods.collect{|m| m.name}
253
assert(method_names.include?("Quit"))
255
def test_ole_func_methods
256
methods = @excel.ole_func_methods
257
assert(methods.size > 0)
258
method_names = methods.collect{|m| m.name}
259
assert(method_names.include?("Quit"))
261
def test_ole_put_methods
262
methods = @excel.ole_put_methods
263
assert(methods.size > 0)
264
method_names = methods.collect{|m| m.name}
265
assert(method_names.include?("Visible"))
267
def test_ole_get_methods
268
methods = @excel.ole_get_methods
269
assert(methods.size > 0)
270
method_names = methods.collect{|m| m.name}
271
assert(method_names.include?("Visible"))
273
def test_ole_method_help
274
quit_info = @excel.ole_method_help("Quit")
275
assert_equal(0, quit_info.size_params)
276
assert_equal(0, quit_info.size_opt_params)
278
workbooks = @excel.Workbooks
279
add_info = workbooks.ole_method_help("Add")
280
assert_equal(1, add_info.size_params)
281
assert_equal(1, add_info.size_opt_params)
282
assert(add_info.params[0].input?)
283
assert(add_info.params[0].optional?)
284
assert_equal('VARIANT', add_info.params[0].ole_type)
288
tlib = @excel.ole_typelib
289
assert_equal(tlib.name, MS_EXCEL_TYPELIB);
292
def test_s_create_guid
293
guid = WIN32OLE.create_guid
294
assert_match(/^\{[A-Z0-9]{8}\-[A-Z0-9]{4}\-[A-Z0-9]{4}\-[A-Z0-9]{4}\-[A-Z0-9]{12}/,
305
# class TestWin32OLE_WITH_MSI < RUNIT::TestCase
306
class TestWin32OLE_WITH_MSI < Test::Unit::TestCase
308
installer = WIN32OLE.new("WindowsInstaller.Installer")
309
@record = installer.CreateRecord(2)
312
# Sorry, this test fails.
313
# Win32OLE does not support this style to set property.
314
# Use Win32OLE#setproperty or Win32OLE#[]= .
316
# @record.invoke("StringData", 1, 'cccc')
317
# assert_equal('cccc', @record.StringData(1))
321
@record.setproperty( "StringData", 1, 'dddd')
322
assert_equal('dddd', @record.StringData(1))
324
def test_bracket_equal_with_arg
325
@record.StringData[1] = 'ffff'
326
assert_equal('ffff', @record.StringData(1))
330
shell=WIN32OLE.new('Shell.Application')
331
assert_equal(shell.NameSpace(0).title, shell._invoke(0x60020002, [0], [WIN32OLE::VARIANT::VT_VARIANT]).title)
335
# ---------------------
337
# a subclass of Win32OLE
338
# override new() and connect()
339
class MyExcel<WIN32OLE
341
super "Excel.Application"
344
super "Excel.Application"
348
class TestMyExcel < TestWin32OLE
350
# because we overrided new() and connect()
351
# we need to change the test.
352
# also, because the class will be different
356
@excel.visible = true
359
assert_instance_of(MyExcel, @excel)
362
excel2 = MyExcel.connect
363
assert_instance_of(MyExcel, excel2)
366
# const_load didn't like to be called twice,
367
# and I don't know how to undefine something in Ruby yet
370
private :test_s_const_load