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

« back to all changes in this revision

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