1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

merge win32ole from rough

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@2508 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
suke 2002-06-01 12:34:30 +00:00
parent 95a5a67142
commit f14180707d
32 changed files with 14163 additions and 0 deletions

View file

@ -0,0 +1,10 @@
require 'win32ole'
def oletypelib_name(pat)
WIN32OLE_TYPE.typelibs.each do |lib|
return lib if pat =~ lib
end
end
module OLESERVER
MS_EXCEL_TYPELIB = oletypelib_name(/^Microsoft Excel .* Object Library$/)
MS_XML_TYPELIB = oletypelib_name(/^Microsoft XML/)
end

View file

@ -0,0 +1,33 @@
require 'rubyunit'
require 'win32ole'
class TestWIN32OLE_EVENT < RUNIT::TestCase
def setup
@excel = WIN32OLE.new("Excel.Application")
@excel.visible = true
end
def test_on_event
book = @excel.workbooks.Add
value = ""
begin
ev = WIN32OLE_EVENT.new(book, 'WorkbookEvents')
ev.on_event('SheetChange'){|arg1, arg2|
begin
value = arg1.value
rescue
value = $!.message
end
}
book.Worksheets(1).Range("A1").value = "OK"
ensure
book.saved = true
end
assert_equal("OK", value)
end
def teardown
@excel.quit
@excel = nil
GC.start
end
end

View file

@ -0,0 +1,83 @@
# You need RubyUnit and MS Excel and MSI to run this test script
require 'rubyunit'
require 'win32ole'
require 'oleserver'
class TestOLEMETHOD < RUNIT::TestCase
include OLESERVER
def setup
@excel_app = WIN32OLE_TYPE.new(MS_EXCEL_TYPELIB, 'Application')
end
def test_s_new
m = WIN32OLE_METHOD.new(@excel_app, 'Quit')
assert_instance_of(WIN32OLE_METHOD, m)
m = WIN32OLE_METHOD.new(@excel_app, 'WorkbookOpen')
assert_instance_of(WIN32OLE_METHOD, m)
m = WIN32OLE_METHOD.new(@excel_app, 'workbookopen')
assert_instance_of(WIN32OLE_METHOD, m)
end
def test_name
m = WIN32OLE_METHOD.new(@excel_app, 'Quit')
assert_equal('Quit', m.name)
end
def test_return_type
m = WIN32OLE_METHOD.new(@excel_app, 'ActiveCell')
assert_equal('Range', m.return_type)
m = WIN32OLE_METHOD.new(@excel_app, 'ActivePrinter')
assert_equal('BSTR', m.return_type)
end
def test_return_vtype
m = WIN32OLE_METHOD.new(@excel_app, 'ActiveCell')
assert_equal(WIN32OLE::VARIANT::VT_PTR, m.return_vtype)
m = WIN32OLE_METHOD.new(@excel_app, 'ActivePrinter')
assert_equal(WIN32OLE::VARIANT::VT_BSTR, m.return_vtype)
end
def test_return_type_detail
m = WIN32OLE_METHOD.new(@excel_app, 'ActiveCell')
assert_equal(['PTR', 'USERDEFINED', 'Range'], m.return_type_detail)
m = WIN32OLE_METHOD.new(@excel_app, 'ActivePrinter')
assert_equal(['BSTR'], m.return_type_detail)
end
def test_invoke_kind
m = WIN32OLE_METHOD.new(@excel_app, 'ActiveCell')
assert_equal('PROPERTYGET', m.invoke_kind)
end
def test_visible
m = WIN32OLE_METHOD.new(@excel_app, 'ActiveCell')
assert(m.visible?)
m = WIN32OLE_METHOD.new(@excel_app, 'AddRef')
assert(!m.visible?)
end
def test_event
m = WIN32OLE_METHOD.new(@excel_app, 'WorkbookOpen')
assert(m.event?)
m = WIN32OLE_METHOD.new(@excel_app, 'ActiveCell')
assert(!m.event?)
end
def test_event_interface
m = WIN32OLE_METHOD.new(@excel_app, 'WorkbookOpen')
assert_equal('AppEvents', m.event_interface)
m = WIN32OLE_METHOD.new(@excel_app, 'ActiveCell')
assert_nil(m.event_interface)
end
def test_helpstring
domdoc = WIN32OLE_TYPE.new(MS_XML_TYPELIB, 'DOMDocument')
m = WIN32OLE_METHOD.new(domdoc, 'abort')
assert_equal('abort an asynchronous download', m.helpstring)
end
def test_helpfile
m = WIN32OLE_METHOD.new(@excel_app, 'ActiveCell')
assert_match(/VBAXL.*\.(HLP|CHM)$/i, m.helpfile)
end
def test_helpcontext
m = WIN32OLE_METHOD.new(@excel_app, 'ActiveCell')
assert(m.helpcontext > 0)
end
def test_offset_vtbl
m = WIN32OLE_METHOD.new(@excel_app, 'QueryInterface')
assert_equal(0, m.offset_vtbl)
end
end

View file

@ -0,0 +1,67 @@
# You need RubyUnit and MS Excel and MSI to run this test script
require 'rubyunit'
require 'win32ole'
require 'oleserver'
class TestOLEPARAM < RUNIT::TestCase
include OLESERVER
def test_name
classes = WIN32OLE_TYPE.ole_classes(MS_EXCEL_TYPELIB)
sh = classes.find {|c| c.name == 'Worksheet'}
saveas = sh.ole_methods.find {|m| m.name == 'SaveAs'}
param_names = saveas.params.collect{|p| p.name}
assert(param_names.size > 0)
assert(param_names.include?('Filename'))
end
def test_ole_type
classes = WIN32OLE_TYPE.ole_classes(MS_EXCEL_TYPELIB)
methods = classes.find {|c| c.name == 'Worksheet'}.ole_methods
f = methods.find {|m| m.name == 'SaveAs'}
assert_equal('BSTR', f.params[0].ole_type)
methods = classes.find {|c| c.name == 'Workbook'}.ole_methods
f = methods.find {|m| m.name == 'SaveAs'}
assert_equal('XlSaveAsAccessMode', f.params[6].ole_type)
end
def test_ole_type_detail
classes = WIN32OLE_TYPE.ole_classes(MS_EXCEL_TYPELIB)
methods = classes.find {|c| c.name == 'Worksheet'}.ole_methods
f = methods.find {|m| m.name == 'SaveAs'}
assert_equal(['BSTR'], f.params[0].ole_type_detail)
methods = classes.find {|c| c.name == 'Workbook'}.ole_methods
f = methods.find {|m| m.name == 'SaveAs'}
assert_equal(['USERDEFINED', 'XlSaveAsAccessMode'], f.params[6].ole_type_detail)
end
def test_input
classes = WIN32OLE_TYPE.ole_classes(MS_EXCEL_TYPELIB)
methods = classes.find {|c| c.name == 'Worksheet'}.ole_methods
f = methods.find {|m| m.name == 'SaveAs'}
assert(f.params[0].input?)
end
def test_output
classes = WIN32OLE_TYPE.ole_classes(MS_EXCEL_TYPELIB)
methods = classes.find {|c| c.name == 'Worksheet'}.ole_methods
f = methods.find {|m| m.name == 'SaveAs'}
assert(!f.params[0].output?)
end
def test_optional
classes = WIN32OLE_TYPE.ole_classes(MS_EXCEL_TYPELIB)
methods = classes.find {|c| c.name == 'Worksheet'}.ole_methods
f = methods.find {|m| m.name == 'SaveAs'}
assert(!f.params[0].optional?)
methods = classes.find {|c| c.name == 'Workbook'}.ole_methods
f = methods.find {|m| m.name == 'SaveAs'}
assert(f.params[0].optional?)
end
def test_ole_type_detail
classes = WIN32OLE_TYPE.ole_classes(MS_EXCEL_TYPELIB)
methods = classes.find {|c| c.name == 'Worksheet'}.ole_methods
f = methods.find {|m| m.name == 'SaveAs'}
assert_equal(nil, f.params[0].default)
methods = classes.find {|c| c.name == 'Workbook'}.ole_methods
f = methods.find {|m| m.name == 'SaveAs'}
assert_equal(1, f.params[6].default)
end
end

View file

@ -0,0 +1,83 @@
# You need RubyUnit and MS Excel and MSI to run this test script
require 'rubyunit'
require 'win32ole'
require 'oleserver'
class TestOLETYPE < RUNIT::TestCase
include OLESERVER
def test_s_new
type = WIN32OLE_TYPE.new(MS_EXCEL_TYPELIB, 'Application')
assert_instance_of(WIN32OLE_TYPE, type)
end
def test_s_ole_classes
classes = WIN32OLE_TYPE.ole_classes(MS_EXCEL_TYPELIB)
assert(classes.size > 0)
end
def test_s_typelibs
libs = WIN32OLE_TYPE.typelibs
assert(libs.include?(MS_EXCEL_TYPELIB))
assert(libs.include?(MS_XML_TYPELIB))
end
def test_s_progids
progids = WIN32OLE_TYPE.progids
assert(progids.include?('Excel.Application'))
end
def test_name
classes = WIN32OLE_TYPE.ole_classes(MS_EXCEL_TYPELIB)
class_names = classes.collect{|c|
c.name
}
assert(class_names.include?('Application'))
end
def test_ole_type
classes = WIN32OLE_TYPE.ole_classes(MS_EXCEL_TYPELIB)
app = classes.find {|c| c.name == 'Application'}
assert_equal('Class', app.ole_type)
app = classes.find {|c| c.name == '_Application'}
assert_equal('Dispatch', app.ole_type)
end
def test_typekind
classes = WIN32OLE_TYPE.ole_classes(MS_EXCEL_TYPELIB)
app = classes.find {|c| c.name == 'Application'}
assert_equal(5, app.typekind)
end
def test_visible
classes = WIN32OLE_TYPE.ole_classes(MS_EXCEL_TYPELIB)
app = classes.find {|c| c.name == 'Application'}
assert(app.visible?)
app = classes.find {|c| c.name == 'IAppEvents'}
assert(!app.visible?)
end
def test_src_type
classes = WIN32OLE_TYPE.ole_classes(MS_XML_TYPELIB)
domnode = classes.find {|c| c.name == 'DOMNodeType'}
assert_equal('tagDOMNodeType', domnode.src_type)
end
def test_helpstring
classes = WIN32OLE_TYPE.ole_classes(MS_XML_TYPELIB)
domdoc = classes.find {|c| c.name == 'DOMDocument'}
assert_equal('W3C-DOM XML Document', domdoc.helpstring)
end
def test_variables
classes = WIN32OLE_TYPE.ole_classes(MS_EXCEL_TYPELIB)
xlchart = classes.find {|c| c.name == 'XlChartType'}
assert(xlchart.variables.size > 0)
end
def test_ole_methods
classes = WIN32OLE_TYPE.ole_classes(MS_EXCEL_TYPELIB)
worksheet = classes.find {|c| c.name == 'Worksheet'}
assert(worksheet.ole_methods.size > 0)
end
def test_helpfile
classes = WIN32OLE_TYPE.ole_classes(MS_EXCEL_TYPELIB)
worksheet = classes.find {|c| c.name == 'Worksheet'}
assert_match(/VBAXL.*\.(CHM|HLP)$/, worksheet.helpfile)
end
def test_helpcontext
classes = WIN32OLE_TYPE.ole_classes(MS_EXCEL_TYPELIB)
worksheet = classes.find {|c| c.name == 'Worksheet'}
assert_equal(131088, worksheet.helpcontext)
end
end

View file

@ -0,0 +1,42 @@
# You need RubyUnit and MS Excel and MSI to run this test script
require 'rubyunit'
require 'win32ole'
require 'oleserver'
class TestOLEVARIABLE < RUNIT::TestCase
include OLESERVER
def test_name
classes = WIN32OLE_TYPE.ole_classes(MS_EXCEL_TYPELIB)
chart = classes.find {|c| c.name == 'XlChartType'}
var_names = chart.variables.collect {|m| m.name}
assert(var_names.size > 0)
assert(var_names.include?('xl3DColumn'))
end
def test_ole_type
classes = WIN32OLE_TYPE.ole_classes(MS_EXCEL_TYPELIB)
chart = classes.find {|c| c.name == 'XlChartType'}
var = chart.variables.find {|m| m.name == 'xl3DColumn'}
assert_equal('INT', var.ole_type)
end
def test_ole_type_detail
classes = WIN32OLE_TYPE.ole_classes(MS_EXCEL_TYPELIB)
chart = classes.find {|c| c.name == 'XlChartType'}
var = chart.variables.find {|m| m.name == 'xl3DColumn'}
assert_equal(['INT'], var.ole_type_detail)
end
def test_value
classes = WIN32OLE_TYPE.ole_classes(MS_EXCEL_TYPELIB)
chart = classes.find {|c| c.name == 'XlChartType'}
var = chart.variables.find {|m| m.name == 'xl3DColumn'}
assert_equal(-4100, var.value)
end
def test_visible
classes = WIN32OLE_TYPE.ole_classes(MS_EXCEL_TYPELIB)
chart = classes.find {|c| c.name == 'XlChartType'}
var = chart.variables.find {|m| m.name == 'xl3DColumn'}
assert(var.visible?)
end
end

View file

@ -0,0 +1,32 @@
# You need RubyUnit and MS Excel and MSI to run this test script
require 'rubyunit'
require 'win32ole'
class TestWin32OLE_VARIANT < RUNIT::TestCase
include WIN32OLE::VARIANT
def test_variant
assert_equal(2, VT_I2)
assert_equal(3, VT_I4)
assert_equal(4, VT_R4)
assert_equal(5, VT_R8)
assert_equal(6, VT_CY)
assert_equal(7, VT_DATE)
assert_equal(8, VT_BSTR)
assert_equal(9, VT_DISPATCH)
assert_equal(10, VT_ERROR)
assert_equal(11, VT_BOOL)
assert_equal(12, VT_VARIANT)
assert_equal(13, VT_UNKNOWN)
assert_equal(16, VT_I1)
assert_equal(17, VT_UI1)
assert_equal(18, VT_UI2)
assert_equal(19, VT_UI4)
assert_equal(22, VT_INT)
assert_equal(23, VT_UINT)
assert_equal(0x2000, VT_ARRAY)
assert_equal(0x4000, VT_BYREF)
end
end

View file

@ -0,0 +1,298 @@
# You need RubyUnit and MS Excel and MSI to run this test script
require 'runit/testcase'
require 'runit/cui/testrunner'
require 'win32ole'
require 'oleserver'
module EXCEL_CONST
end
module CONST1
end
module CONST2
end
module CONST3
end
class TestWin32OLE < RUNIT::TestCase
include OLESERVER
def setup
@excel = WIN32OLE.new("Excel.Application")
@excel.visible = true
end
def test_s_new
assert_instance_of(WIN32OLE, @excel)
end
def test_s_new_DCOM
rexcel = WIN32OLE.new("Excel.Application", "localhost")
assert_instance_of(WIN32OLE, rexcel)
rexcel.visible = true
rexcel.quit
end
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
def test_s_connect
excel2 = WIN32OLE.connect('Excel.Application')
assert_instance_of(WIN32OLE, excel2)
end
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
def test_get_win32ole_object
workbooks = @excel.Workbooks;
assert_instance_of(WIN32OLE, workbooks)
end
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
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)
ensure
book.saved = true
end
end
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)
ensure
book.saved = true
end
end
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
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
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
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
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
def test_invoke
workbooks = @excel.invoke( 'workbooks' )
assert_instance_of(WIN32OLE, workbooks)
book = workbooks.invoke( 'add' )
assert_instance_of(WIN32OLE, book)
end
def test_ole_methods
methods = @excel.ole_methods
method_names = methods.collect{|m| m.name}
assert(method_names.include?("Quit"))
end
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
# def test_ole_put_methods
# methods_list = @excel.ole_put_methods
# puts methods_list
# end
def teardown
@excel.quit
@excel = nil
GC.start
end
end
class TestWin32OLE_WITH_MSI < RUNIT::TestCase
def setup
installer = WIN32OLE.new("WindowsInstaller.Installer")
@record = installer.CreateRecord(2)
end
# Sorry, this test fails.
# Win32OLE does not support this style to set property.
# Use Win32OLE#setproperty or Win32OLE#[]= .
# def test_invoke
# @record.invoke("StringData", 1, 'cccc')
# assert_equal('cccc', @record.StringData(1))
# end
def test_setproperty
@record.setproperty( "StringData", 1, 'dddd')
assert_equal('dddd', @record.StringData(1))
end
def test_bracket_equal_with_arg
@record[ "StringData", 1 ] = 'ffff'
assert_equal('ffff', @record.StringData(1))
end
end
# ---------------------
#
# a subclass of Win32OLE
# override new() and connect()
class MyExcel<WIN32OLE
def MyExcel.new
super "Excel.Application"
end
def MyExcel.connect
super "Excel.Application"
end
end
class TestMyExcel < TestWin32OLE
#
# because we overrided new() and connect()
# we need to change the test.
# also, because the class will be different
#
def setup
@excel = MyExcel.new
@excel.visible = true
end
def test_s_new
assert_instance_of(MyExcel, @excel)
end
def test_s_connect
excel2 = MyExcel.connect
assert_instance_of(MyExcel, excel2)
end
#
# const_load didn't like to be called twice,
# and I don't know how to undefine something in Ruby yet
# so, hide the test.
#
private :test_s_const_load
end
if $0 == __FILE__
puts "Now Test Win32OLE version #{WIN32OLE::VERSION}"
if ARGV.size == 0
suite = RUNIT::TestSuite.new
suite.add_test(TestWin32OLE.suite)
suite.add_test(TestMyExcel.suite)
begin
installer = WIN32OLE.new("WindowsInstaller.Installer")
suite.add_test(TestWin32OLE_WITH_MSI.suite)
rescue
puts "Skip some test with MSI"
end
else
suite = RUNIT::TestSuite.new
ARGV.each do |testmethod|
suite.add_test(TestWin32OLE.new(testmethod))
end
end
RUNIT::CUI::TestRunner.quiet_mode = true
RUNIT::CUI::TestRunner.run(suite)
end

View file

@ -0,0 +1,11 @@
require 'rubyunit'
require 'win32ole'
puts "Now Test Win32OLE version #{WIN32OLE::VERSION}"
RUNIT::CUI::TestRunner.quiet_mode = true
require "testWIN32OLE"
require "testOLETYPE"
require "testOLEPARAM"
require "testOLEMETHOD"
require "testOLEVARIABLE"
require "testVARIANT"
require "testOLEEVENT"