2 # frozen_string_literal: false
10 PROGID_RBCOMTEST='RbComTest.ComSrvTest'
13 RbComTest.ComSrvTest is following VB.NET COM server(RbComTest solution).
14 (You must check COM interoperability.)
16 Imports System.Runtime.InteropServices
17 Public Class ComSrvTest
18 <StructLayout(LayoutKind.Sequential)> _
20 <MarshalAs(UnmanagedType.BStr)> _
21 Public title As String
22 Public cost As Integer
25 Public Function getBook() As Book
27 book.title = "The Ruby Book"
32 Public Function getBooks() As Book()
33 Dim book() As Book = {New Book, New Book}
34 book(0).title = "The CRuby Book"
36 book(1).title = "The JRuby Book"
41 Public Sub getBookByRefObject(ByRef obj As Object)
43 book.title = "The Ruby Reference Book"
48 Public Function getVer2BookByValBook(<MarshalAs(UnmanagedType.Struct)> ByVal book As Book) As Book
50 ret.title = book.title + " ver2"
51 ret.cost = book.cost * 1.1
55 Public Sub getBookByRefBook(<MarshalAs(UnmanagedType.LPStruct)> ByRef book As Book)
56 book.title = "The Ruby Reference Book2"
60 Public Sub getVer3BookByRefBook(<MarshalAs(UnmanagedType.LPStruct)> ByRef book As Book)
68 if defined?(WIN32OLE::Record)
69 class TestWIN32OLE_RECORD < Test::Unit::TestCase
70 def test_toplevel_constants_backward_compatibility
71 assert_equal(WIN32OLE::Record, ::WIN32OLE_RECORD)
76 WIN32OLE.new(PROGID_RBCOMTEST)
78 rescue WIN32OLE::RuntimeError
82 class TestWIN32OLE_RECORD_BY_RBCOMTEST < Test::Unit::TestCase
83 unless rbcomtest_exist?
84 def test_dummy_for_skip_message
85 omit "#{PROGID_RBCOMTEST} for WIN32OLE::Record test is not installed"
89 @obj = WIN32OLE.new(PROGID_RBCOMTEST)
92 def test_s_new_from_win32ole
93 rec = WIN32OLE::Record.new('Book', @obj)
95 assert_instance_of(WIN32OLE::Record, rec)
98 def test_s_new_from_win32ole_typelib
99 tlib = @obj.ole_typelib
100 rec = WIN32OLE::Record.new('Book', tlib)
102 assert_instance_of(WIN32OLE::Record, rec)
106 assert_raise(WIN32OLE::RuntimeError) {
107 WIN32OLE::Record.new('NonExistRecordName', @obj)
109 assert_raise(ArgumentError) {
112 assert_raise(ArgumentError) {
113 WIN32OLE::Record.new('NonExistRecordName')
118 rec = WIN32OLE::Record.new('Book', @obj)
119 assert_equal({'title'=>nil, 'cost'=>nil}, rec.to_h)
123 rec = WIN32OLE::Record.new('Book', @obj)
124 assert_equal('Book', rec.typename)
127 def test_method_missing_getter
128 rec = WIN32OLE::Record.new('Book', @obj)
129 assert_equal(nil, rec.title)
130 assert_raise(KeyError) {
135 def test_method_missing_setter
136 rec = WIN32OLE::Record.new('Book', @obj)
137 rec.title = "Ruby Book"
138 assert_equal("Ruby Book", rec.title)
141 def test_get_record_from_comserver
143 assert_instance_of(WIN32OLE::Record, rec)
144 assert_equal("The Ruby Book", rec.title)
145 assert_equal(20, rec.cost)
148 def test_get_record_array_from_comserver
150 assert_instance_of(Array, rec)
151 assert_equal(2, rec.size)
152 assert_instance_of(WIN32OLE::Record, rec[0])
153 assert_equal("The CRuby Book", rec[0].title)
154 assert_equal(30, rec[0].cost)
155 assert_instance_of(WIN32OLE::Record, rec[1])
156 assert_equal("The JRuby Book", rec[1].title)
157 assert_equal(40, rec[1].cost)
160 def test_pass_record_parameter
161 rec = WIN32OLE::Record.new('Book', @obj)
162 rec.title = "Ruby Book"
164 book = @obj.getVer2BookByValBook(rec)
165 assert_equal("Ruby Book ver2", book.title)
166 assert_equal(66, book.cost)
169 def test_pass_variant_parameter_byref
170 obj = WIN32OLE::Variant.new(nil, WIN32OLE::VARIANT::VT_VARIANT|WIN32OLE::VARIANT::VT_BYREF)
171 @obj.getBookByRefBook(obj)
172 assert_instance_of(WIN32OLE::Record, obj.value)
174 assert_equal("The Ruby Reference Book2", book.title)
175 assert_equal(44, book.cost)
178 def test_pass_record_parameter_byref
179 book = WIN32OLE::Record.new('Book', @obj)
180 @obj.getBookByRefBook(book)
181 assert_equal("The Ruby Reference Book2", book.title)
182 assert_equal(44, book.cost)
185 def test_pass_and_get_record_parameter_byref
186 book = WIN32OLE::Record.new('Book', @obj)
187 book.title = "Ruby Book"
189 @obj.getVer3BookByRefBook(book)
190 assert_equal("Ruby Book ver3", book.title)
191 assert_equal(72, book.cost)
194 def test_ole_instance_variable_get
195 obj = WIN32OLE::Record.new('Book', @obj)
196 assert_equal(nil, obj.ole_instance_variable_get(:title))
197 assert_equal(nil, obj.ole_instance_variable_get('title'))
200 def test_ole_instance_variable_set
201 book = WIN32OLE::Record.new('Book', @obj)
202 book.ole_instance_variable_set(:title, "Ruby Book")
203 assert_equal("Ruby Book", book.title)
204 book.ole_instance_variable_set('title', "Ruby Book2")
205 assert_equal("Ruby Book2", book.title)
209 book = WIN32OLE::Record.new('Book', @obj)
210 assert_equal(%q[#<WIN32OLE::Record(Book) {"title"=>nil, "cost"=>nil}>], book.inspect)