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

@ -1,3 +1,7 @@
Sat Jun 1 19:20:07 2002 Masaki Suketa <masaki.suketa@nifty.ne.jp>
* ext/win32ole: merge from rough
Thu May 30 12:52:42 2002 Yukihiro Matsumoto <matz@ruby-lang.org>
* range.c (range_step): iteration done using "+" if elements are

15
LEGAL
View file

@ -306,3 +306,18 @@ ext/socket/getnameinfo.c:
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
ext/win32ole/win32ole.c:
You can apply the Artistic License to this file. (or GPL,
alternatively)
(c) 1995 Microsoft Corporation. All rights reserved.
Developed by ActiveWare Internet Corp., http://www.ActiveWare.com
Other modifications Copyright (c) 1997, 1998 by Gurusamy Sarathy
<gsar@umich.edu> and Jan Dubois <jan.dubois@ibm.net>
You may distribute under the terms of either the GNU General Public
License or the Artistic License, as specified in the README file
of the Perl distribution.

View file

@ -24,3 +24,4 @@
#syslog
#tcltklib
#tk
#win32ole

View file

@ -24,3 +24,4 @@ strscan
#syslog
#tcltklib
#tk
#win32ole

View file

@ -24,3 +24,4 @@ strscan
#syslog
#tcltklib
#tk
#win32ole

View file

@ -24,3 +24,4 @@ strscan
#syslog
#tcltklib
#tk
win32ole

View file

@ -24,3 +24,4 @@ strscan
#syslog
#tcltklib
#tk
#win32ole

2
ext/win32ole/.cvsignore Normal file
View file

@ -0,0 +1,2 @@
Makefile
mkmf.log

24
ext/win32ole/MANIFEST Normal file
View file

@ -0,0 +1,24 @@
MANIFEST
depend
doc/win32ole.rd
extconf.rb
lib/win32ole/property.rb
sample/excel1.rb
sample/excel2.rb
sample/excel3.rb
sample/ie.rb
sample/ieconst.rb
sample/ienavi.rb
sample/oledirs.rb
sample/olegen.rb
sample/xml.rb
tests/oleserver.rb
tests/testOLEEVENT.rb
tests/testOLEMETHOD.rb
tests/testOLEPARAM.rb
tests/testOLETYPE.rb
tests/testOLEVARIABLE.rb
tests/testVARIANT.rb
tests/testWIN32OLE.rb
tests/testall.rb
win32ole.c

1
ext/win32ole/depend Normal file
View file

@ -0,0 +1 @@
win32ole.o : $(hdrdir)/ruby.h $(hdrdir)/config.h $(hdrdir)/defines.h

View file

@ -0,0 +1,294 @@
=begin
= Win32OLE extension module
== WIN32OLE
=== Constants
: VERSION
The version number of WIN32OLE.
: ARGV
The argument of the method invoked recently.
This constant is used to get value of argument
when the argument is passed by reference.
=== Class Method
: connect(oleserver)
returns running OLE automation object or WIN32OLE object from moniker.
: const_load(ole [,obj])
defines the constants of OLE automation
server as 'obj' class constants. If 'obj' omitted, the default
is WIN32OLE.
: new(oleserver)
returns OLE Automation object.
: ole_free(obj)
invokes Release method of Dispatch interface of WIN32OLE object.
This method should not be used because this method exists for debugging WIN32OLE.
: ole_reference_count(obj)
returns reference counter of Dispatch interface.
This method should not be used because this method exists for debugging WIN32OLE.
: ole_show_help(info [,helpcontext])
displays helpfile.
The first argument specifies WIN32OLE_TYPE object or WIN32OLE_METHOD object
or helpfile.
=== Method
: self[property]
gets property of OLE object.
: self[property]=
sets property of OLE object.
: _invoke(dispid, args, types)
runs the early binding method.
The dispid specifies Dispatch ID, args specifies the array of arguments,
types specifies array of the type of arguments.
: each {...}
Iterates over each item of OLE collection which has IEnumVARIANT
interface.
: invoke(method, args,...)
runs OLE method.
: ole_func_methods
returns array of WIN32OLE_METHOD object which corresponds with function.
: ole_get_methods
returns array of WIN32OLE_METHOD object which corresponds with get properties.
: ole_method(method)
returns WIN32OLE_METHOD object which coreesponds with method
which specified by argument.
: ole_method_help(method)
alias of ole_method.
: ole_methods
returns WIN32OLE_METHOD object which coreesponds with method.
: ole_obj_help
returns WIN32OLE_TYPE object.
: ole_put_methods
returns array of WIN32OLE_METHOD object which corresponds with put properties.
: setproperty(property, key, val)
set property of OLE object.
This method is used when the property has argument.
For example, in VB
obj.item("key") = val
in Win32OLE
obj.setproperty("item", "key", val)
== WIN32OLE_EVENT class
=== Class Method
: new(ole, interface)
The new class method creates OLE event sink object to connect ole.
The ole must be WIN32OLE object, and interface is the interface
name of event.
: message_loop
The message_loop class method translates and dispatches Windows
message.
=== Method
: on_event([event]){...}
defines the callback of event.
If event omitted, defines the callback of all events.
: on_event_with_outargs([event]) {...}
defines the callback of event.
If you want modify argument in callback,
== WIN32OLE_METHOD
=== Class Methods
: new(win32ole_type, method)
creates WIN32OLE_METHOD object.
=== Methods
: dispid
returns Dispatch ID.
: event?
returns true if the method is event.
: event_interface
returns interface name of event if the method is event.
: helpcontext
returns help context.
: helpfile
returns help file.
: invkind
returns invkind.
: invoke_kind
returns invoke kind string.
: name
returns name of method.
: offset_vtbl
returns the offset of Vtbl.
: params
returns array of WIN32OLE_PARAM object.
: return_type
returns string of return value type of method.
: return_vtype
returns number of return value type of method.
: return_type_detail
returns detail information of return value type of method.
: size_params
returns the size of arguments.
: size_opt_params
returns the size of optional arguments.
: visible?
returns true if the method is public.
== WIN32OLE_PARAM
: default
returns default value.
: input?
returns true if argument is input.
: optional?
returns true if argument is optional.
: output?
returns true if argument is output.
: name
returns name.
: ole_type
returns type of argument.
: ole_type_detail
returns detail information of type of argument.
: retval?
returns true if argument is return value.
== WIN32OLE_TYPE
=== Class Methods
: new(typelibrary, class)
returns WIN32OLE_TYPE object.
: ole_classes(typelibrary)
returns array of WIN32OLE_TYPE objects defined by Type Library.
: progids
returns array of ProgID.
: typelibs
returns array of type libraries.
=== Methods
: guid
returns GUID.
: helpfile
returns helpfile.
: helpcontext
returns helpcontext.
: helpstring
returns help string.
: major_version
returns major version.
: minor_version
returns minor version.
: name
returns name.
: ole_methods
returns array of WIN32OLE_METHOD objects.
: ole_type
returns type of class.
: progid
returns ProgID if it exists. If not found, then returns nil.
: src_type
returns source class when the OLE class is 'Alias'.
: typekind
returns number which represents type.
: variables
returns array of variables defined in OLE class.
: visible?
returns true if the OLE class is public.
== WIN32OLE_VARIABLE
=== Methods
: name
returns the name.
: ole_type
returns type
: ole_type_detail
returns detail information of type.
: value
returns value.
: variable_kind
returns variable kind string.
: varkind
returns the number which represents variable kind.
== WIN32OLE::VARIANT
=== Constants
*VT_I4
*VT_R4
*VT_R8
*VT_CY
*VT_DATE
*VT_BSTR
*VT_USERDEFINED
*VT_PTR
*VT_DISPATCH
*VT_ERROR
*VT_BOOL
*VT_VARIANT
*VT_UNKNOWN
*VT_I1
*VT_UI1
*VT_UI2
*VT_UI4
*VT_INT
*VT_UINT
*VT_ARRAY
*VT_BYREF
=end

26
ext/win32ole/extconf.rb Normal file
View file

@ -0,0 +1,26 @@
#----------------------------------
# extconf.rb
# $Revision$
# $Date$
#----------------------------------
require 'mkmf'
def create_win32ole_makefile
if have_library("ole32") and
have_library("oleaut32") and
have_library("uuid") and
have_library("user32") and
have_library("advapi32")
create_makefile("win32ole")
end
end
case PLATFORM
when /mswin32/
$CFLAGS='/W3'
when /mingw/
$CFLAGS='-DNONAMELESSUNION'
when /cygwin/
$CFLAGS='-DNONAMELESSUNION'
end
create_win32ole_makefile

View file

@ -0,0 +1,16 @@
# OLEProperty
# helper class of Property with arguments.
class OLEProperty
def initialize(obj, dispid, gettypes, settypes)
@obj = obj
@dispid = dispid
@gettypes = gettypes
@settypes = settypes
end
def [](*args)
@obj._getproperty(@dispid, args, @gettypes)
end
def []=(*args)
@obj._setproperty(@dispid, args, @settypes)
end
end

View file

@ -0,0 +1,22 @@
require 'win32ole'
#application = WIN32OLE.new('Excel.Application.5')
application = WIN32OLE.new('Excel.Application')
application.visible = TRUE
workbook = application.Workbooks.Add();
worksheet = workbook.Worksheets(1);
worksheet.Range("A1:D1").value = ["North","South","East","West"];
worksheet.Range("A2:B2").value = [5.2, 10];
worksheet.Range("C2").value = 8;
worksheet.Range("D2").value = 20;
range = worksheet.Range("A1:D2");
range.Select
chart = workbook.Charts.Add;
workbook.saved = TRUE;
application.ActiveWorkbook.Close(0);
application.Quit();

View file

@ -0,0 +1,30 @@
require 'win32ole'
# -4100 is the value for the Excel constant xl3DColumn.
ChartTypeVal = -4100;
# Creates OLE object to Excel
#excel = WIN32OLE.new("excel.application.5")
excel = WIN32OLE.new("excel.application")
# Create and rotate the chart
excel['Visible'] = TRUE;
excel.Workbooks.Add();
excel.Range("a1")['Value'] = 3;
excel.Range("a2")['Value'] = 2;
excel.Range("a3")['Value'] = 1;
excel.Range("a1:a3").Select();
excelchart = excel.Charts.Add();
excelchart['Type'] = ChartTypeVal;
i = 30
i.step(180, 10) do |rot|
# excelchart['Rotation'] = rot;
excelchart.rotation=rot;
end
# Done, bye
excel.ActiveWorkbook.Close(0);
excel.Quit();

View file

@ -0,0 +1,13 @@
require 'win32ole'
#application = WIN32OLE.new('Excel.Application.5')
application = WIN32OLE.new('Excel.Application')
application.visible = TRUE
workbook = application.Workbooks.Add();
sheet = workbook.Worksheets(1);
sheetS = workbook.Worksheets
puts "The number of sheets is #{sheetS.count}"
puts "Now add 2 sheets after of `#{sheet.name}`"
sheetS.add({'count'=>2, 'after'=>sheet})
puts "The number of sheets is #{sheetS.count}"

11
ext/win32ole/sample/ie.rb Normal file
View file

@ -0,0 +1,11 @@
require 'win32ole'
url = 'http://www.ruby-lang.org/'
ie = WIN32OLE.new('InternetExplorer.Application')
ie.visible = TRUE
ie.gohome
print "Now navigate Ruby home page... Please enter."
gets
ie.navigate(url)
print "Now quit Internet Explorer... Please enter."
gets
ie.Quit()

View file

@ -0,0 +1,32 @@
require 'win32ole'
ie = WIN32OLE.new('InternetExplorer.Application')
=begin
WIN32OLE.const_load(ie)
WIN32OLE.constants.sort.each do |c|
puts "#{c} = #{WIN32OLE.const_get(c)}"
end
=end
module IE_CONST
end
WIN32OLE.const_load(ie, IE_CONST)
IE_CONST.constants.sort.each do |c|
puts "#{c} = #{IE_CONST.const_get(c)}"
end
#------------------------------------------------------------
# Remark!!! CONSTANTS has not tested enoughly!!!
# CONSTANTS is alpha release.
# If there are constants which first letter is not [a-zA-Z],
# like a '_Foo', then maybe you can access the value by
# using CONSTANTS['_Foo']
#------------------------------------------------------------
IE_CONST::CONSTANTS.each do |k, v|
puts "#{k} = #{v}"
end
puts WIN32OLE::VERSION
ie.quit

View file

@ -0,0 +1,40 @@
require 'win32ole'
$urls = []
def navigate(url)
$urls << url
end
def stop_msg_loop
puts "Now Stop IE..."
$LOOP = FALSE;
end
def default_handler(event, *args)
case event
when "BeforeNavigate"
puts "Now Navigate #{args[0]}..."
end
end
ie = WIN32OLE.new('InternetExplorer.Application')
ie.visible = TRUE
ie.gohome
ev = WIN32OLE_EVENT.new(ie, 'DWebBrowserEvents')
ev.on_event {|*args| default_handler(*args)}
ev.on_event("NavigateComplete") {|url| navigate(url)}
ev.on_event("Quit") {|*args| stop_msg_loop}
$LOOP = TRUE
while ($LOOP)
WIN32OLE_EVENT.message_loop
end
puts "You Navigated the URLs ..."
$urls.each_with_index do |url, i|
puts "(#{i+1}) #{url}"
end

View file

@ -0,0 +1,23 @@
#
# You need WSH(Windows Scripting Host) to run this script.
#
require "win32ole"
def listup(items)
# items.each do |i|
for i in items
puts i.name
end
end
fs = WIN32OLE.new("Scripting.FileSystemObject")
folder = fs.GetFolder(".")
puts "--- folder of #{folder.path} ---"
listup(folder.SubFolders)
puts "--- files of #{folder.path} ---"
listup(folder.Files)

View file

@ -0,0 +1,348 @@
#-----------------------------
# olegen.rb
# $Date$
# $Revision$
#-----------------------------
require 'win32ole'
class WIN32COMGen
def initialize(typelib)
@typelib = typelib
@reciever = ""
end
attr_reader :typelib
def ole_classes(typelib)
begin
@ole = WIN32OLE.new(typelib)
[@ole.ole_obj_help]
rescue
WIN32OLE_TYPE.ole_classes(typelib)
end
end
def generate_args(method)
args = []
if method.size_opt_params >= 0
size_required_params = method.size_params - method.size_opt_params
else
size_required_params = method.size_params - 1
end
size_required_params.times do |i|
if method.params[i] && method.params[i].optional?
args.push "arg#{i}=nil"
else
args.push "arg#{i}"
end
end
if method.size_opt_params >= 0
method.size_opt_params.times do |i|
args.push "arg#{i + size_required_params}=nil"
end
else
args.push "*arg"
end
args.join(", ")
end
def generate_argtype(typedetails)
ts = ''
typedetails.each do |t|
case t
when 'CARRAY', 'VOID', 'UINT', 'RESULT', 'DECIMAL', 'I8', 'UI8'
# raise "Sorry type\"" + t + "\" not supported"
ts << "\"??? NOT SUPPORTED TYPE:`#{t}'\""
when 'USERDEFINED', 'Unknown Type 9'
ts << 'VT_DISPATCH'
break;
when 'SAFEARRAY'
ts << 'VT_ARRAY|'
when 'PTR'
ts << 'VT_BYREF|'
when 'INT'
ts << 'VT_I4'
else
if String === t
ts << 'VT_' + t
end
end
end
if ts.empty?
ts = 'VT_VARIANT'
elsif ts[-1] == ?|
ts += 'VT_VARIANT'
end
ts
end
def generate_argtypes(method, proptypes)
types = method.params.collect{|param|
generate_argtype(param.ole_type_detail)
}.join(", ")
if proptypes
types += ", " if types.size > 0
types += generate_argtype(proptypes)
end
types
end
def generate_method_body(method, disptype, types=nil)
" ret = #{@reciever}#{disptype}(#{method.dispid}, [" +
generate_args(method).gsub("=nil", "") +
"], [" +
generate_argtypes(method, types) +
"])\n" +
" @lastargs = WIN32OLE::ARGV\n" +
" ret"
end
def generate_method_help(method, type = nil)
str = " # "
if type
str += type
else
str += method.return_type
end
str += " #{method.name}"
if method.event?
str += " EVENT"
str += " in #{method.event_interface}"
end
if method.helpstring && method.helpstring != ""
str += "\n # "
str += method.helpstring
end
args_help = generate_method_args_help(method)
if args_help
str += "\n"
str += args_help
end
str
end
def generate_method_args_help(method)
args = []
method.params.each_with_index {|param, i|
h = " # #{param.ole_type} arg#{i} --- #{param.name}"
inout = []
inout.push "IN" if param.input?
inout.push "OUT" if param.output?
h += " [#{inout.join('/')}]"
h += " ( = #{param.default})" if param.default
args.push h
}
if args.size > 0
args.join("\n")
else
nil
end
end
def generate_method(method, disptype, io = STDOUT, types = nil)
io.puts "\n"
io.puts generate_method_help(method)
if method.invoke_kind == 'PROPERTYPUT'
io.print " def #{method.name}=("
else
io.print " def #{method.name}("
end
io.print generate_args(method)
io.puts ")"
io.puts generate_method_body(method, disptype, types)
io.puts " end"
end
def generate_propputref_methods(klass, io = STDOUT)
klass.ole_methods.select {|method|
method.invoke_kind == 'PROPERTYPUTREF' && method.visible?
}.each do |method|
generate_method(method, io)
end
end
def generate_properties_with_args(klass, io = STDOUT)
klass.ole_methods.select {|method|
method.invoke_kind == 'PROPERTYGET' &&
method.visible? &&
method.size_params > 0
}.each do |method|
types = method.return_type_detail
io.puts "\n"
io.puts generate_method_help(method, types[0])
io.puts " def #{method.name}"
if klass.ole_type == "Class"
io.print " OLEProperty.new(@dispatch, #{method.dispid}, ["
else
io.print " OLEProperty.new(self, #{method.dispid}, ["
end
io.print generate_argtypes(method, nil)
io.print "], ["
io.print generate_argtypes(method, types)
io.puts "])"
io.puts " end"
end
end
def generate_propput_methods(klass, io = STDOUT)
klass.ole_methods.select {|method|
method.invoke_kind == 'PROPERTYPUT' && method.visible? &&
method.size_params == 1
}.each do |method|
ms = klass.ole_methods.select {|m|
m.invoke_kind == 'PROPERTYGET' &&
m.dispid == method.dispid
}
types = []
if ms.size == 1
types = ms[0].return_type_detail
end
generate_method(method, '_setproperty', io, types)
end
end
def generate_propget_methods(klass, io = STDOUT)
klass.ole_methods.select {|method|
method.invoke_kind == 'PROPERTYGET' && method.visible? &&
method.size_params == 0
}.each do |method|
generate_method(method, '_getproperty', io)
end
end
def generate_func_methods(klass, io = STDOUT)
klass.ole_methods.select {|method|
method.invoke_kind == "FUNC" && method.visible?
}.each do |method|
generate_method(method, '_invoke', io)
end
end
def generate_methods(klass, io = STDOUT)
generate_propget_methods(klass, io)
generate_propput_methods(klass, io)
generate_properties_with_args(klass, io)
generate_func_methods(klass, io)
# generate_propputref_methods(klass, io)
end
def generate_constants(klass, io = STDOUT)
klass.variables.select {|v|
v.visible? && v.variable_kind == 'CONSTANT'
}.each do |v|
io.print " "
io.print v.name.sub(/^./){|c| c.upcase}
io.print " = "
io.puts v.value
end
end
def class_name(klass)
klass_name = klass.name
if klass.ole_type == "Class" &&
klass.guid &&
klass.progid
klass_name = klass.progid.gsub(/\./, '_')
end
if /^[A-Z]/ !~ klass_name || Module.constants.include?(klass_name)
klass_name = 'OLE' + klass_name
end
klass_name
end
def define_initialize(klass)
<<STR
def initialize(obj = nil)
@clsid = "#{klass.guid}"
@progid = "#{klass.progid}"
if obj.nil?
@dispatch = WIN32OLE.new @progid
else
@dispatch = obj
end
end
STR
end
def define_include
" include WIN32OLE::VARIANT"
end
def define_instance_variables
" attr_reader :lastargs"
end
def define_method_missing
<<STR
def method_missing(cmd, *arg)
@dispatch.method_missing(cmd, *arg)
end
STR
end
def define_class(klass, io = STDOUT)
io.puts "class #{class_name(klass)} # #{klass.name}"
io.puts define_include
io.puts define_instance_variables
io.puts " attr_reader :dispatch"
io.puts " attr_reader :clsid"
io.puts " attr_reader :progid"
io.puts define_initialize(klass)
io.puts define_method_missing
end
def define_module(klass, io = STDOUT)
io.puts "module #{class_name(klass)}"
io.puts define_include
io.puts define_instance_variables
end
def generate_class(klass, io = STDOUT)
io.puts "\n# #{klass.helpstring}"
if klass.ole_type == "Class" &&
klass.guid &&
klass.progid
@reciever = "@dispatch."
define_class(klass, io)
else
@reciever = ""
define_module(klass, io)
end
generate_constants(klass, io)
generate_methods(klass, io)
io.puts "end"
end
def generate(io = STDOUT)
io.puts "require 'win32ole'"
io.puts "require 'win32ole/property'"
ole_classes(typelib).select{|klass|
klass.visible? &&
(klass.ole_type == "Class" ||
klass.ole_type == "Interface" ||
klass.ole_type == "Dispatch" ||
klass.ole_type == "Enum")
}.each do |klass|
generate_class(klass, io)
end
begin
@ole.quit if @ole
rescue
end
end
end
require 'win32ole'
if __FILE__ == $0
if ARGV.size == 0
$stderr.puts "usage: #{$0} Type Library [...]"
exit 1
end
ARGV.each do |typelib|
comgen = WIN32COMGen.new(typelib)
comgen.generate
end
end

7306
ext/win32ole/sample/xml.rb Normal file

File diff suppressed because it is too large Load diff

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"

5292
ext/win32ole/win32ole.c Normal file

File diff suppressed because it is too large Load diff