Class: TestWin32OLE

Inherits:
RUNIT::TestCase
  • Object
show all
Includes:
OLESERVER
Defined in:
tests/testWIN32OLE.rb

Direct Known Subclasses

TestMyExcel

Constant Summary

Constants included from OLESERVER

OLESERVER::MS_EXCEL_TYPELIB, OLESERVER::MS_XML_TYPELIB

Instance Method Summary collapse

Instance Method Details

#setupObject



23
24
25
26
# File 'tests/testWIN32OLE.rb', line 23

def setup
  @excel = WIN32OLE.new("Excel.Application")
  @excel.visible = true
end

#teardownObject



279
280
281
282
283
# File 'tests/testWIN32OLE.rb', line 279

def teardown
  @excel.quit
  @excel = nil
  GC.start
end

#test_const_CP_ACPObject



70
71
72
# File 'tests/testWIN32OLE.rb', line 70

def test_const_CP_ACP
  assert_equal(0, WIN32OLE::CP_ACP)
end

#test_const_CP_MACCPObject



78
79
80
# File 'tests/testWIN32OLE.rb', line 78

def test_const_CP_MACCP
  assert_equal(2, WIN32OLE::CP_MACCP)
end

#test_const_CP_OEMCPObject



74
75
76
# File 'tests/testWIN32OLE.rb', line 74

def test_const_CP_OEMCP
  assert_equal(1, WIN32OLE::CP_OEMCP)
end

#test_const_CP_SYMBOLObject



86
87
88
# File 'tests/testWIN32OLE.rb', line 86

def test_const_CP_SYMBOL
  assert_equal(42, WIN32OLE::CP_SYMBOL)
end

#test_const_CP_THREAD_ACPObject



82
83
84
# File 'tests/testWIN32OLE.rb', line 82

def test_const_CP_THREAD_ACP
  assert_equal(3, WIN32OLE::CP_THREAD_ACP)
end

#test_const_CP_UTF7Object



90
91
92
# File 'tests/testWIN32OLE.rb', line 90

def test_const_CP_UTF7
  assert_equal(65000, WIN32OLE::CP_UTF7)
end

#test_const_CP_UTF8Object



94
95
96
# File 'tests/testWIN32OLE.rb', line 94

def test_const_CP_UTF8
  assert_equal(65001, WIN32OLE::CP_UTF8)
end

#test_convert_bignumObject



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'tests/testWIN32OLE.rb', line 150

def test_convert_bignum
  book = @excel.workbooks.add
  sheet = book.worksheets(1)
  begin
    sheet.range("A1").value = 999999999
    sheet.range("A2").value = 9999999999
    sheet.range("A3").value = "=A1*10 + 9"
    assert_equal(9999999999, sheet.range("A2").value)
    assert_equal(9999999999, sheet.range("A3").value)
    sheet.range("A4").value = "2008/03/04"
    assert_equal("2008/03/04 00:00:00", sheet.range("A4").value)
  ensure
    book.saved = true
  end
end

#test_eachObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'tests/testWIN32OLE.rb', line 118

def test_each
  workbooks = @excel.Workbooks
  assert_no_exception {
    i = 0;
    workbooks.each do |workbook|
      print i += 1
    end
  }
  workbooks.add
  workbooks.add
  i = 0
  workbooks.each do |workbook|
    i+=1
  end
  assert_equal(2, i)
  workbooks.each do |workbook|
    workbook.saved = true
  end
end

#test_get_win32ole_objectObject



114
115
116
117
# File 'tests/testWIN32OLE.rb', line 114

def test_get_win32ole_object
  workbooks = @excel.Workbooks;
  assert_instance_of(WIN32OLE, workbooks)
end

#test_invokeObject



237
238
239
240
241
242
# File 'tests/testWIN32OLE.rb', line 237

def test_invoke
  workbooks = @excel.invoke( 'workbooks' )
  assert_instance_of(WIN32OLE, workbooks)
  book = workbooks.invoke( 'add' )
  assert_instance_of(WIN32OLE, book)
end

#test_no_exist_propertyObject



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'tests/testWIN32OLE.rb', line 206

def test_no_exist_property
  isok = false
  begin
    @excel.unknown_prop = 1
  rescue WIN32OLERuntimeError
    isok = true
  end
  assert(isok)

  isok = false
  begin
    @excel['unknown_prop'] = 2
  rescue WIN32OLERuntimeError
    isok = true
  end
  assert(isok)
end

#test_ole_func_methodsObject



248
249
250
251
252
253
# File 'tests/testWIN32OLE.rb', line 248

def test_ole_func_methods
  methods = @excel.ole_func_methods
  assert(methods.size > 0)
  method_names = methods.collect{|m| m.name}
  assert(method_names.include?("Quit"))
end

#test_ole_get_methodsObject



260
261
262
263
264
265
# File 'tests/testWIN32OLE.rb', line 260

def test_ole_get_methods
  methods = @excel.ole_get_methods
  assert(methods.size > 0)
  method_names = methods.collect{|m| m.name}
  assert(method_names.include?("Visible"))
end

#test_ole_invoke_with_named_argObject



166
167
168
169
170
171
172
173
174
175
176
177
# File 'tests/testWIN32OLE.rb', line 166

def test_ole_invoke_with_named_arg
  book = @excel.workbooks.add
  sheets = book.worksheets
  sheet = book.worksheets(1)
  num = sheets.count
  begin
    sheets.add({'count' => 2, 'after'=>sheet})
    assert_equal(2, sheets.count - num);
  ensure
    book.saved = true
  end
end

#test_ole_invoke_with_named_arg_lastObject



179
180
181
182
183
184
185
186
187
188
189
190
# File 'tests/testWIN32OLE.rb', line 179

def test_ole_invoke_with_named_arg_last
  book = @excel.workbooks.add
  sheets = book.worksheets
  sheet = book.worksheets(1)
  num = sheets.count
  begin
    sheets.add(sheet, {'count' => 2})
    assert_equal(2, sheets.count - num);
  ensure
    book.saved = true
  end
end

#test_ole_method_helpObject



266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'tests/testWIN32OLE.rb', line 266

def test_ole_method_help
  quit_info = @excel.ole_method_help("Quit")
  assert_equal(0, quit_info.size_params)
  assert_equal(0, quit_info.size_opt_params)

  workbooks = @excel.Workbooks
  add_info = workbooks.ole_method_help("Add")
  assert_equal(1, add_info.size_params)
  assert_equal(1, add_info.size_opt_params)
  assert(add_info.params[0].input?)
  assert(add_info.params[0].optional?)
  assert_equal('VARIANT', add_info.params[0].ole_type)
end

#test_ole_methodsObject



243
244
245
246
247
# File 'tests/testWIN32OLE.rb', line 243

def test_ole_methods
  methods = @excel.ole_methods
  method_names = methods.collect{|m| m.name}
  assert(method_names.include?("Quit"))
end

#test_ole_put_methodsObject



254
255
256
257
258
259
# File 'tests/testWIN32OLE.rb', line 254

def test_ole_put_methods
  methods = @excel.ole_put_methods
  assert(methods.size > 0)
  method_names = methods.collect{|m| m.name}
  assert(method_names.include?("Visible"))
end

#test_s_codepageObject



60
61
62
# File 'tests/testWIN32OLE.rb', line 60

def test_s_codepage
  assert_equal(WIN32OLE::CP_ACP, WIN32OLE.codepage)
end

#test_s_codepage_changedObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'tests/testWIN32OLE.rb', line 98

def test_s_codepage_changed
  book = @excel.workbooks.add
  sheet = book.worksheets(1)
  begin
    WIN32OLE.codepage = WIN32OLE::CP_UTF8
    sheet.range("A1").value = [0x3042].pack("U*")
    val = sheet.range("A1").value
    assert_equal("\343\201\202", val)
    WIN32OLE.codepage = WIN32OLE::CP_ACP
    val = sheet.range("A1").value
    assert_equal("\202\240", val)
  ensure
    book.saved = true
  end
end

#test_s_codepage_setObject



64
65
66
67
68
# File 'tests/testWIN32OLE.rb', line 64

def test_s_codepage_set
  WIN32OLE.codepage = WIN32OLE::CP_UTF8
  assert_equal(WIN32OLE::CP_UTF8, WIN32OLE.codepage)
  WIN32OLE.codepage = WIN32OLE::CP_ACP
end

#test_s_connectObject



45
46
47
48
# File 'tests/testWIN32OLE.rb', line 45

def test_s_connect
  excel2 = WIN32OLE.connect('Excel.Application')
  assert_instance_of(WIN32OLE, excel2)
end

#test_s_const_loadObject



50
51
52
53
54
55
56
57
58
# File 'tests/testWIN32OLE.rb', line 50

def test_s_const_load
  assert(!defined?(EXCEL_CONST::XlTop))
  WIN32OLE.const_load(@excel, EXCEL_CONST)
  assert_equal(-4160, EXCEL_CONST::XlTop)

  assert(!defined?(CONST1::XlTop))
  WIN32OLE.const_load(MS_EXCEL_TYPELIB, CONST1)
  assert_equal(-4160, CONST1::XlTop)
end

#test_s_newObject



27
28
29
# File 'tests/testWIN32OLE.rb', line 27

def test_s_new
  assert_instance_of(WIN32OLE, @excel)
end

#test_s_new_DCOMObject



30
31
32
33
34
35
# File 'tests/testWIN32OLE.rb', line 30

def test_s_new_DCOM
  rexcel = WIN32OLE.new("Excel.Application", "localhost")
  assert_instance_of(WIN32OLE, rexcel)
  rexcel.visible = true
  rexcel.quit
end

#test_s_new_from_clsidObject



36
37
38
39
40
41
42
43
44
# File 'tests/testWIN32OLE.rb', line 36

def test_s_new_from_clsid
  excel = WIN32OLE.new("{00024500-0000-0000-C000-000000000046}")
  assert_instance_of(WIN32OLE, excel)
  excel.quit
  exc = assert_exception(WIN32OLERuntimeError) {
    WIN32OLE.new("{000}")
  }
  assert_match(/unknown OLE server: `\{000\}'/, exc.message)
end

#test_setpropertyObject



192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'tests/testWIN32OLE.rb', line 192

def test_setproperty
  @excel.setproperty('Visible', false)
  assert_equal(false, @excel.Visible)
  @excel.setproperty('Visible', true)
  assert_equal(true, @excel.Visible)
  book = @excel.workbooks.add
  sheet = book.worksheets(1)
  begin
    sheet.setproperty('Cells', 1, 2, 10)
    assert_equal(10, sheet.range("B1").value)
  ensure
    book.saved = true
  end
end

#test_setproperty_bracketObject



137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'tests/testWIN32OLE.rb', line 137

def test_setproperty_bracket
  book = @excel.workbooks.add
  sheet = book.worksheets(1)
  begin
    sheet.range("A1")['Value'] = 10
    assert_equal(10, sheet.range("A1").value)
    sheet['Cells', 1, 2] = 10
    assert_equal(10, sheet.range("B1").value)
    assert_equal(10, sheet['Cells', 1, 2].value)
  ensure
    book.saved = true
  end
end

#test_setproperty_with_equalObject



224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'tests/testWIN32OLE.rb', line 224

def test_setproperty_with_equal
  book = @excel.workbooks.add
  sheet = book.worksheets(1)
  begin
    sheet.range("B1").value = 10
    assert_equal(10, sheet.range("B1").value)
    sheet.range("C1:D1").value = [11, 12]
    assert_equal(11, sheet.range("C1").value)
    assert_equal(12, sheet.range("D1").value)
  ensure
    book.saved = true
  end
end