[rubygems/rubygems] Use a constant empty tar header to avoid extra allocations
[ruby.git] / test / win32ole / test_win32ole_record.rb
blob49205ce53da91cf72e3ffb9828770a70add901f8
1 # coding: us-ascii
2 # frozen_string_literal: false
4 begin
5   require 'win32ole'
6 rescue LoadError
7 end
8 require 'test/unit'
10 PROGID_RBCOMTEST='RbComTest.ComSrvTest'
12 =begin
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)> _
19     Public Structure Book
20         <MarshalAs(UnmanagedType.BStr)> _
21         Public title As String
22         Public cost As Integer
23     End Structure
25     Public Function getBook() As Book
26         Dim book As New Book
27         book.title = "The Ruby Book"
28         book.cost = 20
29         Return book
30     End Function
32     Public Function getBooks() As Book()
33         Dim book() As Book = {New Book, New Book}
34         book(0).title = "The CRuby Book"
35         book(0).cost = 30
36         book(1).title = "The JRuby Book"
37         book(1).cost = 40
38         Return book
39     End Function
41     Public Sub getBookByRefObject(ByRef obj As Object)
42         Dim book As New Book
43         book.title = "The Ruby Reference Book"
44         book.cost = 50
45         obj = book
46     End Sub
48     Public Function getVer2BookByValBook(<MarshalAs(UnmanagedType.Struct)> ByVal book As Book) As Book
49         Dim ret As New Book
50         ret.title = book.title + " ver2"
51         ret.cost = book.cost * 1.1
52         Return ret
53     End Function
55     Public Sub getBookByRefBook(<MarshalAs(UnmanagedType.LPStruct)> ByRef book As Book)
56         book.title = "The Ruby Reference Book2"
57         book.cost = 44
58     End Sub
60     Public Sub getVer3BookByRefBook(<MarshalAs(UnmanagedType.LPStruct)> ByRef book As Book)
61         book.title += " ver3"
62         book.cost *= 1.2
63     End Sub
64 End Class
65 =end
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)
72     end
73   end
75   def rbcomtest_exist?
76     WIN32OLE.new(PROGID_RBCOMTEST)
77     true
78   rescue WIN32OLE::RuntimeError
79     false
80   end
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"
86       end
87     else
88       def setup
89         @obj = WIN32OLE.new(PROGID_RBCOMTEST)
90       end
92       def test_s_new_from_win32ole
93         rec = WIN32OLE::Record.new('Book', @obj)
94         assert(rec)
95         assert_instance_of(WIN32OLE::Record, rec)
96       end
98       def test_s_new_from_win32ole_typelib
99         tlib = @obj.ole_typelib
100         rec = WIN32OLE::Record.new('Book', tlib)
101         assert(rec)
102         assert_instance_of(WIN32OLE::Record, rec)
103       end
105       def test_s_new_raise
106         assert_raise(WIN32OLE::RuntimeError) {
107           WIN32OLE::Record.new('NonExistRecordName', @obj)
108         }
109         assert_raise(ArgumentError) {
110           WIN32OLE::Record.new
111         }
112         assert_raise(ArgumentError) {
113           WIN32OLE::Record.new('NonExistRecordName')
114         }
115       end
117       def test_to_h
118         rec = WIN32OLE::Record.new('Book', @obj)
119         assert_equal({'title'=>nil, 'cost'=>nil}, rec.to_h)
120       end
122       def test_typename
123         rec = WIN32OLE::Record.new('Book', @obj)
124         assert_equal('Book', rec.typename)
125       end
127       def test_method_missing_getter
128         rec = WIN32OLE::Record.new('Book', @obj)
129         assert_equal(nil, rec.title)
130         assert_raise(KeyError) {
131           rec.non_exist_name
132         }
133       end
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)
139       end
141       def test_get_record_from_comserver
142         rec = @obj.getBook
143         assert_instance_of(WIN32OLE::Record, rec)
144         assert_equal("The Ruby Book", rec.title)
145         assert_equal(20, rec.cost)
146       end
148       def test_get_record_array_from_comserver
149         rec = @obj.getBooks
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)
158       end
160       def test_pass_record_parameter
161         rec = WIN32OLE::Record.new('Book', @obj)
162         rec.title = "Ruby Book"
163         rec.cost = 60
164         book = @obj.getVer2BookByValBook(rec)
165         assert_equal("Ruby Book ver2", book.title)
166         assert_equal(66, book.cost)
167       end
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)
173         book = obj.value
174         assert_equal("The Ruby Reference Book2", book.title)
175         assert_equal(44, book.cost)
176       end
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)
183       end
185       def test_pass_and_get_record_parameter_byref
186         book = WIN32OLE::Record.new('Book', @obj)
187         book.title = "Ruby Book"
188         book.cost = 60
189         @obj.getVer3BookByRefBook(book)
190         assert_equal("Ruby Book ver3", book.title)
191         assert_equal(72, book.cost)
192       end
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'))
198       end
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)
206       end
208       def test_inspect
209         book = WIN32OLE::Record.new('Book', @obj)
210         assert_equal(%q[#<WIN32OLE::Record(Book) {"title"=>nil, "cost"=>nil}>], book.inspect)
211       end
212     end
213   end