DEFINITIONS
This source file includes following functions.
1 # You need RubyUnit and MS Excel and MSI to run this test script
2
3 require 'runit/testcase'
4 require 'runit/cui/testrunner'
5
6 require 'win32ole'
7 require 'oleserver'
8
9 module EXCEL_CONST
10 end
11
12 module CONST1
13 end
14
15 module CONST2
16 end
17
18 module CONST3
19 end
20
21 class TestWin32OLE < RUNIT::TestCase
22 include OLESERVER
23 def setup
24 @excel = WIN32OLE.new("Excel.Application")
25 @excel.visible = true
26 end
27 def test_s_new
28 assert_instance_of(WIN32OLE, @excel)
29 end
30 def test_s_new_DCOM
31 rexcel = WIN32OLE.new("Excel.Application", "localhost")
32 assert_instance_of(WIN32OLE, rexcel)
33 rexcel.visible = true
34 rexcel.quit
35 end
36 def test_s_new_from_clsid
37 excel = WIN32OLE.new("{00024500-0000-0000-C000-000000000046}")
38 assert_instance_of(WIN32OLE, excel)
39 excel.quit
40 exc = assert_exception(WIN32OLERuntimeError) {
41 WIN32OLE.new("{000}")
42 }
43 assert_match(/Unknown OLE server : `\{000\}'/, exc.message)
44 end
45 def test_s_connect
46 excel2 = WIN32OLE.connect('Excel.Application')
47 assert_instance_of(WIN32OLE, excel2)
48 end
49
50 def test_s_const_load
51 assert(!defined?(EXCEL_CONST::XlTop))
52 WIN32OLE.const_load(@excel, EXCEL_CONST)
53 assert_equal(-4160, EXCEL_CONST::XlTop)
54
55 assert(!defined?(CONST1::XlTop))
56 WIN32OLE.const_load(MS_EXCEL_TYPELIB, CONST1)
57 assert_equal(-4160, CONST1::XlTop)
58 end
59
60 def test_get_win32ole_object
61 workbooks = @excel.Workbooks;
62 assert_instance_of(WIN32OLE, workbooks)
63 end
64 def test_each
65 workbooks = @excel.Workbooks
66 assert_no_exception {
67 i = 0;
68 workbooks.each do |workbook|
69 print i += 1
70 end
71 }
72 workbooks.add
73 workbooks.add
74 i = 0
75 workbooks.each do |workbook|
76 i+=1
77 end
78 assert_equal(2, i)
79 workbooks.each do |workbook|
80 workbook.saved = true
81 end
82 end
83 def test_setproperty_bracket
84 book = @excel.workbooks.add
85 sheet = book.worksheets(1)
86 begin
87 sheet.range("A1")['Value'] = 10
88 assert_equal(10, sheet.range("A1").value)
89 sheet['Cells', 1, 2] = 10
90 assert_equal(10, sheet.range("B1").value)
91 ensure
92 book.saved = true
93 end
94 end
95 def test_convert_bignum
96 book = @excel.workbooks.add
97 sheet = book.worksheets(1)
98 begin
99 sheet.range("A1").value = 999999999
100 sheet.range("A2").value = 9999999999
101 sheet.range("A3").value = "=A1*10 + 9"
102 assert_equal(9999999999, sheet.range("A2").value)
103 assert_equal(9999999999, sheet.range("A3").value)
104
105 ensure
106 book.saved = true
107 end
108 end
109
110 def test_ole_invoke_with_named_arg
111 book = @excel.workbooks.add
112 sheets = book.worksheets
113 sheet = book.worksheets(1)
114 num = sheets.count
115 begin
116 sheets.add({'count' => 2, 'after'=>sheet})
117 assert_equal(2, sheets.count - num);
118 ensure
119 book.saved = true
120 end
121 end
122
123 def test_ole_invoke_with_named_arg_last
124 book = @excel.workbooks.add
125 sheets = book.worksheets
126 sheet = book.worksheets(1)
127 num = sheets.count
128 begin
129 sheets.add(sheet, {'count' => 2})
130 assert_equal(2, sheets.count - num);
131 ensure
132 book.saved = true
133 end
134 end
135
136 def test_setproperty
137 @excel.setproperty('Visible', false)
138 assert_equal(false, @excel.Visible)
139 @excel.setproperty('Visible', true)
140 assert_equal(true, @excel.Visible)
141 book = @excel.workbooks.add
142 sheet = book.worksheets(1)
143 begin
144 sheet.setproperty('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_no_exist_property
151 isok = false
152 begin
153 @excel.unknown_prop = 1
154 rescue WIN32OLERuntimeError
155 isok = true
156 end
157 assert(isok)
158
159 isok = false
160 begin
161 @excel['unknown_prop'] = 2
162 rescue WIN32OLERuntimeError
163 isok = true
164 end
165 assert(isok)
166 end
167
168 def test_setproperty_with_equal
169 book = @excel.workbooks.add
170 sheet = book.worksheets(1)
171 begin
172 sheet.range("B1").value = 10
173 assert_equal(10, sheet.range("B1").value)
174 sheet.range("C1:D1").value = [11, 12]
175 assert_equal(11, sheet.range("C1").value)
176 assert_equal(12, sheet.range("D1").value)
177 ensure
178 book.saved = true
179 end
180 end
181 def test_invoke
182 workbooks = @excel.invoke( 'workbooks' )
183 assert_instance_of(WIN32OLE, workbooks)
184 book = workbooks.invoke( 'add' )
185 assert_instance_of(WIN32OLE, book)
186 end
187 def test_ole_methods
188 methods = @excel.ole_methods
189 method_names = methods.collect{|m| m.name}
190 assert(method_names.include?("Quit"))
191 end
192 def test_ole_method_help
193 quit_info = @excel.ole_method_help("Quit")
194 assert_equal(0, quit_info.size_params)
195 assert_equal(0, quit_info.size_opt_params)
196
197 workbooks = @excel.Workbooks
198 add_info = workbooks.ole_method_help("Add")
199 assert_equal(1, add_info.size_params)
200 assert_equal(1, add_info.size_opt_params)
201 assert(add_info.params[0].input?)
202 assert(add_info.params[0].optional?)
203 assert_equal('VARIANT', add_info.params[0].ole_type)
204 end
205 # def test_ole_put_methods
206 # methods_list = @excel.ole_put_methods
207 # puts methods_list
208 # end
209 def teardown
210 @excel.quit
211 @excel = nil
212 GC.start
213 end
214 end
215
216 class TestWin32OLE_WITH_MSI < RUNIT::TestCase
217 def setup
218 installer = WIN32OLE.new("WindowsInstaller.Installer")
219 @record = installer.CreateRecord(2)
220 end
221
222 # Sorry, this test fails.
223 # Win32OLE does not support this style to set property.
224 # Use Win32OLE#setproperty or Win32OLE#[]= .
225 # def test_invoke
226 # @record.invoke("StringData", 1, 'cccc')
227 # assert_equal('cccc', @record.StringData(1))
228 # end
229
230 def test_setproperty
231 @record.setproperty( "StringData", 1, 'dddd')
232 assert_equal('dddd', @record.StringData(1))
233 end
234 def test_bracket_equal_with_arg
235 @record[ "StringData", 1 ] = 'ffff'
236 assert_equal('ffff', @record.StringData(1))
237 end
238 end
239
240 # ---------------------
241 #
242 # a subclass of Win32OLE
243 # override new() and connect()
244 class MyExcel<WIN32OLE
245 def MyExcel.new
246 super "Excel.Application"
247 end
248 def MyExcel.connect
249 super "Excel.Application"
250 end
251 end
252
253 class TestMyExcel < TestWin32OLE
254 #
255 # because we overrided new() and connect()
256 # we need to change the test.
257 # also, because the class will be different
258 #
259 def setup
260 @excel = MyExcel.new
261 @excel.visible = true
262 end
263 def test_s_new
264 assert_instance_of(MyExcel, @excel)
265 end
266 def test_s_connect
267 excel2 = MyExcel.connect
268 assert_instance_of(MyExcel, excel2)
269 end
270 #
271 # const_load didn't like to be called twice,
272 # and I don't know how to undefine something in Ruby yet
273 # so, hide the test.
274 #
275 private :test_s_const_load
276 end
277
278 if $0 == __FILE__
279 puts "Now Test Win32OLE version #{WIN32OLE::VERSION}"
280 if ARGV.size == 0
281 suite = RUNIT::TestSuite.new
282 suite.add_test(TestWin32OLE.suite)
283 suite.add_test(TestMyExcel.suite)
284 begin
285 installer = WIN32OLE.new("WindowsInstaller.Installer")
286 suite.add_test(TestWin32OLE_WITH_MSI.suite)
287 rescue
288 puts "Skip some test with MSI"
289 end
290 else
291 suite = RUNIT::TestSuite.new
292 ARGV.each do |testmethod|
293 suite.add_test(TestWin32OLE.new(testmethod))
294 end
295 end
296 RUNIT::CUI::TestRunner.quiet_mode = true
297 RUNIT::CUI::TestRunner.run(suite)
298 end