Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Converts COM interop tests to using win32ole. Hardly any changes were…

… required! The only change required is that win32ole requires using parenthesis to access indexed properties, but the tests used square brackets. For now, I have added support to win32ole for square brackets instead of modifying the tests. Also, WIN32OLE_EVENT does not seem to have any way of unsubscribing from an event. adodb_spec has a weird failure only with non-managled names, and so I have left it using Type.GetTypeFromProgID for now
  • Loading branch information...
commit 5fb483dd13b8c0c950627e9624973e4fa60cf784 1 parent 7e0f79e
@shri-zz authored
View
24 Merlin/Main/Languages/Ruby/Libs/win32ole.rb
@@ -25,7 +25,11 @@ class WIN32OLE
def initialize(arg)
if arg.respond_to? :to_str
- type = System::Type.get_type_from_prog_i_d arg
+ if guid? arg
+ type = System::Type.GetTypeFromCLSID arg
+ else
+ type = System::Type.get_type_from_prog_i_d arg
+ end
@com_object = System::Activator.create_instance type
else
@com_object = arg
@@ -34,6 +38,10 @@ def initialize(arg)
attr :com_object # The wrapped RCW
+ def guid?(str)
+ /[{]?[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}[}]?|[0-9A-F]{32}/ =~ str
+ end
+
# The DLR COM binder only supports core types like System.String as arguments.
# Passing in a Ruby String will not work. So we convert the Ruby types to
# CLR COM interop types as expected by the DLR COM binder
@@ -56,8 +64,18 @@ def self.ruby_to_com_interop_types(args)
args.map { |arg| ruby_to_com_interop_type arg }
end
- def []=(name, value)
- method_missing "#{name}=", value
+ def []=(*args)
+ indices = args[0...-1]
+ value = args[-1]
+ if indices.size == 1 and indices[0].respond_to? to_str
+ name = indices[0]
+ method_missing "#{name}=", value
+ else
+ # TODO - MRI requires using parenthesis to access indexed properties.
+ # IronRuby allows square brackets because the tests currently use this syntax.
+ # This should be removed after modifying the tests to use parenthesis.
+ method_missing "[]=", *args
+ end
end
def each(&b)
View
8 Merlin/Main/Languages/Ruby/Tests/Interop/com/apps/adodb_spec.rb
@@ -3,18 +3,20 @@
describe "ADODB COM interop" do
it "allows assignment and reading without name mangling" do
obj = System::Activator.create_instance(System::Type.GetTypeFromProgID("ADODB.Connection"))
+ # This fails with : 'GetNativeVariantForObject': Value does not fall within the expected range. (ArgumentError)
+ # obj = WIN32OLE.new("ADODB.Connection")
obj.CommandTimeout = 40
obj.CommandTimeout.should == 40
- obj = System::Activator.create_instance(System::Type.GetTypeFromProgID("ADODB.Command"))
+ obj = WIN32OLE.new("ADODB.Command")
obj.CommandTimeout = 50
obj.CommandTimeout.should == 50
end
it "allows assignment and reading with name mangling" do
- obj = System::Activator.create_instance(System::Type.GetTypeFromProgID("ADODB.Connection"))
+ obj = WIN32OLE.new("ADODB.Connection")
obj.command_timeout = 40
obj.command_timeout.should == 40
- obj = System::Activator.create_instance(System::Type.GetTypeFromProgID("ADODB.Command"))
+ obj = WIN32OLE.new("ADODB.Command")
obj.command_timeout = 50
obj.command_timeout.should == 50
end
View
8 Merlin/Main/Languages/Ruby/Tests/Interop/com/apps/excel_spec.rb
@@ -1,11 +1,15 @@
require File.dirname(__FILE__) + "/../spec_helper"
module ExcelEventTracker
def add_event(ws)
- ws.SelectionChange.add(method(:handler))
+ e = WIN32OLE_EVENT.new(ws, "DocEvents")
+ e.on_event("SelectionChange") { |obj, event| handler(obj, event) }
end
def remove_event(ws)
- ws.SelectionChange.remove(method(:handler))
+ e = WIN32OLE_EVENT.new(ws, "DocEvents")
+ # WIN32OLE_EVENT does not document any way to unsubscribe from an event
+ # So we use this syntax
+ e.on_event("SelectionChange")
end
end
View
8 Merlin/Main/Languages/Ruby/Tests/Interop/com/apps/word_spec.rb
@@ -1,11 +1,15 @@
require File.dirname(__FILE__) + "/../spec_helper"
module WordEventTracker
def add_event(doc)
- doc.WindowSelectionChange.add(method(:handler))
+ e = WIN32OLE_EVENT.new(doc, "DocEvents")
+ e.on_event("WindowSelectionChange") { |obj, event| handler(obj, event) }
end
def remove_event(doc)
- doc.WindowSelectionChange.remove(method(:handler))
+ e = WIN32OLE_EVENT.new(doc, "DocEvents")
+ # WIN32OLE_EVENT does not document any way to unsubscribe from an event
+ # So we use this syntax
+ e.on_event("WindowSelectionChange")
end
end
View
19 Merlin/Main/Languages/Ruby/Tests/Interop/com/com_helper.rb
@@ -1,3 +1,5 @@
+require "win32ole"
+
# This is really just a rubified copy of IronPython's cominterop_util.py
module ComHelper
include Microsoft::Win32
@@ -27,25 +29,12 @@ def word_installed?
module_function :word_installed?
def create_app(prog_id_or_cls_id)
- if prog_id_or_cls_id.is_a? System::Guid
- type = System::Type.GetTypeFromCLSID(prog_id_or_cls_id)
- elsif guid? prog_id_or_cls_id
- type = System::Type.GetTypeFromCLSID(System::Guid.new(prog_id_or_cls_id))
- else
- type = System::Type.GetTypeFromProgID(prog_id_or_cls_id)
- end
- raise "Unknown PROGID '#{prog_id_or_cls_id}'" if type.nil?
- System::Activator.CreateInstance(type)
+ prog_id_or_cls_id = prog_id_or_cls_id.to_s if prog_id_or_cls_id.is_a? System::Guid
+ WIN32OLE.new prog_id_or_cls_id
end
private :create_app
module_function :create_app
- def guid?(str)
- /[{]?[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}[}]?|[0-9A-F]{32}/ =~ str
- end
- private :guid?
- module_function :guid?
-
def create_excel_app
create_app("Excel.Application")
end
Please sign in to comment.
Something went wrong with that request. Please try again.