~ubuntu-branches/ubuntu/intrepid/ruby1.9/intrepid-updates

« back to all changes in this revision

Viewing changes to ext/win32ole/tests/testWIN32OLE.rb

  • Committer: Bazaar Package Importer
  • Author(s): akira yamada
  • Date: 2007-05-21 14:00:19 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070521140019-ui4zd0v80duktssk
Tags: 1.9.0+20070521-1
new upstream snapshot. (2006-07-21)  (Closes: #414856, #388344)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# You need RubyUnit and MS Excel and MSI to run this test script 
2
 
 
3
 
require 'test/unit'
4
 
require 'runit/testcase'
5
 
# require 'runit/cui/testrunner'
6
 
 
7
 
require 'win32ole'
8
 
require 'oleserver'
9
 
 
10
 
module EXCEL_CONST
11
 
end
12
 
 
13
 
module CONST1
14
 
end
15
 
 
16
 
module CONST2
17
 
end
18
 
 
19
 
module CONST3
20
 
end
21
 
 
22
 
class TestWin32OLE < RUNIT::TestCase
23
 
  include OLESERVER
24
 
  def setup
25
 
    @excel = WIN32OLE.new("Excel.Application")
26
 
    @excel.visible = true
27
 
  end
28
 
  def test_s_new
29
 
    assert_instance_of(WIN32OLE, @excel)
30
 
  end
31
 
  def test_s_new_DCOM
32
 
    rexcel = WIN32OLE.new("Excel.Application", "localhost")
33
 
    assert_instance_of(WIN32OLE, rexcel)
34
 
    rexcel.visible = true
35
 
    rexcel.quit
36
 
  end
37
 
  def test_s_new_from_clsid
38
 
    excel = WIN32OLE.new("{00024500-0000-0000-C000-000000000046}")
39
 
    assert_instance_of(WIN32OLE, excel)
40
 
    excel.quit
41
 
    exc = assert_exception(WIN32OLERuntimeError) {
42
 
      WIN32OLE.new("{000}")
43
 
    }
44
 
    assert_match(/unknown OLE server: `\{000\}'/, exc.message)
45
 
  end
46
 
  def test_s_connect
47
 
    excel2 = WIN32OLE.connect('Excel.Application')
48
 
    assert_instance_of(WIN32OLE, excel2)
49
 
  end
50
 
 
51
 
  def test_s_const_load
52
 
    assert(!defined?(EXCEL_CONST::XlTop))
53
 
    WIN32OLE.const_load(@excel, EXCEL_CONST)
54
 
    assert_equal(-4160, EXCEL_CONST::XlTop)
55
 
 
56
 
    assert(!defined?(CONST1::XlTop))
57
 
    WIN32OLE.const_load(MS_EXCEL_TYPELIB, CONST1)
58
 
    assert_equal(-4160, CONST1::XlTop)
59
 
  end
60
 
 
61
 
  def test_s_codepage
62
 
    assert_equal(WIN32OLE::CP_ACP, WIN32OLE.codepage)
63
 
  end
64
 
 
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
69
 
  end
70
 
 
71
 
  def test_const_CP_ACP
72
 
    assert_equal(0, WIN32OLE::CP_ACP)
73
 
  end
74
 
 
75
 
  def test_const_CP_OEMCP
76
 
    assert_equal(1, WIN32OLE::CP_OEMCP)
77
 
  end
78
 
 
79
 
  def test_const_CP_MACCP
80
 
    assert_equal(2, WIN32OLE::CP_MACCP)
81
 
  end
82
 
 
83
 
  def test_const_CP_THREAD_ACP
84
 
    assert_equal(3, WIN32OLE::CP_THREAD_ACP)
85
 
  end
86
 
 
87
 
  def test_const_CP_SYMBOL
88
 
    assert_equal(42, WIN32OLE::CP_SYMBOL)
89
 
  end
90
 
 
91
 
  def test_const_CP_UTF7
92
 
    assert_equal(65000, WIN32OLE::CP_UTF7)
93
 
  end
94
 
 
95
 
  def test_const_CP_UTF8
96
 
    assert_equal(65001, WIN32OLE::CP_UTF8)
97
 
  end
98
 
 
99
 
  def test_s_codepage_changed
100
 
    book = @excel.workbooks.add
101
 
    sheet = book.worksheets(1)
102
 
    begin
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)
110
 
    ensure
111
 
      book.saved = true
112
 
    end
113
 
  end
114
 
 
115
 
  def test_get_win32ole_object
116
 
    workbooks = @excel.Workbooks;
117
 
    assert_instance_of(WIN32OLE, workbooks)
118
 
  end
119
 
  def test_each
120
 
    workbooks = @excel.Workbooks
121
 
    assert_no_exception {
122
 
      i = 0;
123
 
      workbooks.each do |workbook|
124
 
        print i += 1
125
 
      end
126
 
    }
127
 
    workbooks.add
128
 
    workbooks.add
129
 
    i = 0
130
 
    workbooks.each do |workbook|
131
 
      i+=1
132
 
    end
133
 
    assert_equal(2, i)
134
 
    workbooks.each do |workbook|
135
 
      workbook.saved = true
136
 
    end
137
 
  end
138
 
  def test_setproperty_bracket
139
 
    book = @excel.workbooks.add
140
 
    sheet = book.worksheets(1)
141
 
    begin
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)
146
 
    ensure
147
 
      book.saved = true
148
 
    end
149
 
  end
150
 
  def test_convert_bignum
151
 
    book = @excel.workbooks.add
152
 
    sheet = book.worksheets(1)
153
 
    begin
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)
159
 
     
160
 
    ensure
161
 
      book.saved = true
162
 
    end
163
 
  end
164
 
 
165
 
  def test_ole_invoke_with_named_arg
166
 
    book = @excel.workbooks.add
167
 
    sheets = book.worksheets
168
 
    sheet = book.worksheets(1)
169
 
    num = sheets.count
170
 
    begin
171
 
      sheets.add({'count' => 2, 'after'=>sheet})
172
 
      assert_equal(2, sheets.count - num);
173
 
    ensure
174
 
      book.saved = true
175
 
    end
176
 
  end
177
 
 
178
 
  def test_ole_invoke_with_named_arg_last
179
 
    book = @excel.workbooks.add
180
 
    sheets = book.worksheets
181
 
    sheet = book.worksheets(1)
182
 
    num = sheets.count
183
 
    begin
184
 
      sheets.add(sheet, {'count' => 2})
185
 
      assert_equal(2, sheets.count - num);
186
 
    ensure
187
 
      book.saved = true
188
 
    end
189
 
  end
190
 
 
191
 
  def test_setproperty
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)
198
 
    begin
199
 
      sheet.setproperty('Cells', 1, 2, 10)
200
 
      assert_equal(10, sheet.range("B1").value)
201
 
    ensure
202
 
      book.saved = true
203
 
    end
204
 
  end
205
 
  def test_no_exist_property
206
 
    isok = false
207
 
    begin
208
 
      @excel.unknown_prop = 1
209
 
    rescue WIN32OLERuntimeError
210
 
      isok = true
211
 
    end
212
 
    assert(isok)
213
 
 
214
 
    isok = false
215
 
    begin
216
 
      @excel['unknown_prop'] = 2
217
 
    rescue WIN32OLERuntimeError
218
 
      isok = true
219
 
    end
220
 
    assert(isok)
221
 
  end
222
 
 
223
 
  def test_setproperty_with_equal
224
 
    book = @excel.workbooks.add
225
 
    sheet = book.worksheets(1)
226
 
    begin
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)
232
 
    ensure
233
 
      book.saved = true
234
 
    end
235
 
  end
236
 
  def test_invoke
237
 
    workbooks = @excel.invoke( 'workbooks' )
238
 
    assert_instance_of(WIN32OLE, workbooks)
239
 
    book = workbooks.invoke( 'add' )
240
 
    assert_instance_of(WIN32OLE, book)
241
 
  end
242
 
  def test_ole_type
243
 
    tobj = @excel.ole_type
244
 
    assert_equal('_Application', tobj.name)
245
 
  end
246
 
  def test_ole_obj_help
247
 
    tobj = @excel.ole_type
248
 
    assert_equal('_Application', tobj.name)
249
 
  end
250
 
  def test_ole_methods
251
 
    methods = @excel.ole_methods
252
 
    method_names = methods.collect{|m| m.name}
253
 
    assert(method_names.include?("Quit"))
254
 
  end
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"))
260
 
  end
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"))
266
 
  end
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"))
272
 
  end
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)
277
 
 
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)
285
 
  end
286
 
 
287
 
  def test_ole_typelib
288
 
    tlib = @excel.ole_typelib
289
 
    assert_equal(tlib.name, MS_EXCEL_TYPELIB);
290
 
  end
291
 
 
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}/,
295
 
                 guid)
296
 
  end
297
 
 
298
 
  def teardown
299
 
    @excel.quit
300
 
    @excel = nil
301
 
    GC.start
302
 
  end
303
 
end
304
 
 
305
 
# class TestWin32OLE_WITH_MSI < RUNIT::TestCase
306
 
class TestWin32OLE_WITH_MSI < Test::Unit::TestCase
307
 
  def setup
308
 
    installer = WIN32OLE.new("WindowsInstaller.Installer")
309
 
    @record = installer.CreateRecord(2)
310
 
  end
311
 
 
312
 
  # Sorry, this test fails. 
313
 
  # Win32OLE does not support this style to set property.
314
 
  # Use Win32OLE#setproperty or Win32OLE#[]= .
315
 
  # def test_invoke
316
 
  #   @record.invoke("StringData", 1, 'cccc')
317
 
  #   assert_equal('cccc', @record.StringData(1))
318
 
  # end
319
 
 
320
 
  def test_setproperty
321
 
    @record.setproperty( "StringData", 1, 'dddd')
322
 
    assert_equal('dddd', @record.StringData(1))
323
 
  end
324
 
  def test_bracket_equal_with_arg
325
 
    @record.StringData[1] =  'ffff'
326
 
    assert_equal('ffff', @record.StringData(1))
327
 
  end
328
 
 
329
 
  def test__invoke
330
 
    shell=WIN32OLE.new('Shell.Application')
331
 
    assert_equal(shell.NameSpace(0).title, shell._invoke(0x60020002, [0], [WIN32OLE::VARIANT::VT_VARIANT]).title)
332
 
  end
333
 
end
334
 
 
335
 
# ---------------------
336
 
#
337
 
# a subclass of Win32OLE
338
 
# override new() and connect()
339
 
class MyExcel<WIN32OLE
340
 
    def MyExcel.new 
341
 
        super "Excel.Application"
342
 
    end
343
 
    def MyExcel.connect
344
 
        super "Excel.Application"
345
 
    end
346
 
end
347
 
 
348
 
class TestMyExcel < TestWin32OLE
349
 
#
350
 
# because we overrided new() and connect()
351
 
# we need to change the test.
352
 
# also, because the class will be different
353
 
354
 
  def setup
355
 
    @excel = MyExcel.new
356
 
    @excel.visible = true
357
 
  end
358
 
  def test_s_new
359
 
    assert_instance_of(MyExcel, @excel)
360
 
  end
361
 
  def test_s_connect
362
 
    excel2 = MyExcel.connect
363
 
    assert_instance_of(MyExcel, excel2)
364
 
  end
365
 
#
366
 
# const_load didn't like to be called twice,
367
 
# and I don't know how to undefine something in Ruby yet 
368
 
# so, hide the test.
369
 
#
370
 
  private :test_s_const_load
371
 
end