rice 4.3.3 → 4.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (237) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +86 -26
  3. data/CMakeLists.txt +31 -0
  4. data/CMakePresets.json +75 -0
  5. data/COPYING +3 -2
  6. data/FindRuby.cmake +437 -0
  7. data/README.md +7 -2
  8. data/Rakefile +12 -5
  9. data/include/rice/rice.hpp +9522 -4426
  10. data/include/rice/stl.hpp +2831 -1198
  11. data/lib/make_rice_headers.rb +79 -0
  12. data/lib/mkmf-rice.rb +40 -94
  13. data/lib/rice/version.rb +3 -0
  14. data/lib/rice.rb +1 -0
  15. data/lib/rubygems/builder.rb +11 -0
  16. data/lib/rubygems/cmake_builder.rb +113 -0
  17. data/lib/rubygems_plugin.rb +9 -0
  18. data/rice/Address_Registration_Guard.hpp +72 -3
  19. data/rice/Arg.hpp +26 -6
  20. data/rice/Arg.ipp +35 -2
  21. data/rice/Buffer.hpp +123 -0
  22. data/rice/Buffer.ipp +599 -0
  23. data/rice/Callback.hpp +21 -0
  24. data/rice/Callback.ipp +13 -0
  25. data/rice/Constructor.hpp +4 -27
  26. data/rice/Constructor.ipp +79 -0
  27. data/rice/Data_Object.hpp +73 -3
  28. data/rice/Data_Object.ipp +388 -96
  29. data/rice/Data_Type.hpp +214 -3
  30. data/rice/Data_Type.ipp +144 -67
  31. data/rice/Director.hpp +0 -2
  32. data/rice/Enum.hpp +4 -7
  33. data/rice/Enum.ipp +102 -55
  34. data/rice/Exception.hpp +62 -2
  35. data/rice/Exception.ipp +7 -12
  36. data/rice/Init.hpp +8 -0
  37. data/rice/Init.ipp +8 -0
  38. data/rice/JumpException.hpp +44 -0
  39. data/rice/JumpException.ipp +48 -0
  40. data/rice/MemoryView.hpp +11 -0
  41. data/rice/MemoryView.ipp +3 -0
  42. data/rice/Return.hpp +7 -27
  43. data/rice/Return.ipp +13 -13
  44. data/rice/cpp_api/Array.hpp +209 -0
  45. data/rice/cpp_api/Array.ipp +304 -0
  46. data/rice/cpp_api/Builtin_Object.hpp +31 -0
  47. data/rice/cpp_api/Builtin_Object.ipp +37 -0
  48. data/rice/cpp_api/Class.hpp +70 -0
  49. data/rice/cpp_api/Class.ipp +97 -0
  50. data/rice/cpp_api/Encoding.hpp +32 -0
  51. data/rice/cpp_api/Encoding.ipp +59 -0
  52. data/rice/cpp_api/Hash.hpp +194 -0
  53. data/rice/cpp_api/Hash.ipp +257 -0
  54. data/rice/{Identifier.hpp → cpp_api/Identifier.hpp} +2 -6
  55. data/rice/{Identifier.ipp → cpp_api/Identifier.ipp} +4 -2
  56. data/rice/cpp_api/Module.hpp +72 -0
  57. data/rice/cpp_api/Module.ipp +101 -0
  58. data/rice/cpp_api/Object.hpp +272 -0
  59. data/rice/cpp_api/Object.ipp +235 -0
  60. data/rice/cpp_api/String.hpp +74 -0
  61. data/rice/cpp_api/String.ipp +120 -0
  62. data/rice/cpp_api/Struct.hpp +113 -0
  63. data/rice/cpp_api/Struct.ipp +92 -0
  64. data/rice/cpp_api/Symbol.hpp +46 -0
  65. data/rice/cpp_api/Symbol.ipp +93 -0
  66. data/rice/cpp_api/shared_methods.hpp +134 -0
  67. data/rice/detail/DefaultHandler.hpp +12 -0
  68. data/rice/detail/DefaultHandler.ipp +8 -0
  69. data/rice/detail/HandlerRegistry.hpp +5 -35
  70. data/rice/detail/HandlerRegistry.ipp +7 -11
  71. data/rice/detail/InstanceRegistry.hpp +1 -4
  72. data/rice/detail/MethodInfo.hpp +12 -10
  73. data/rice/detail/MethodInfo.ipp +26 -21
  74. data/rice/detail/Native.hpp +33 -0
  75. data/rice/detail/Native.ipp +157 -0
  76. data/rice/detail/NativeAttributeGet.hpp +52 -0
  77. data/rice/detail/NativeAttributeGet.ipp +57 -0
  78. data/rice/detail/NativeAttributeSet.hpp +44 -0
  79. data/rice/detail/NativeAttributeSet.ipp +88 -0
  80. data/rice/detail/NativeCallbackFFI.hpp +55 -0
  81. data/rice/detail/NativeCallbackFFI.ipp +151 -0
  82. data/rice/detail/NativeCallbackSimple.hpp +30 -0
  83. data/rice/detail/NativeCallbackSimple.ipp +29 -0
  84. data/rice/detail/NativeFunction.hpp +33 -23
  85. data/rice/detail/NativeFunction.ipp +309 -70
  86. data/rice/detail/NativeIterator.hpp +9 -11
  87. data/rice/detail/NativeIterator.ipp +33 -31
  88. data/rice/detail/NativeRegistry.hpp +24 -15
  89. data/rice/detail/NativeRegistry.ipp +23 -48
  90. data/rice/detail/Proc.hpp +4 -0
  91. data/rice/detail/Proc.ipp +85 -0
  92. data/rice/detail/Registries.hpp +0 -7
  93. data/rice/detail/Registries.ipp +0 -18
  94. data/rice/detail/RubyFunction.hpp +0 -3
  95. data/rice/detail/RubyFunction.ipp +4 -8
  96. data/rice/detail/RubyType.hpp +16 -0
  97. data/rice/detail/RubyType.ipp +232 -0
  98. data/rice/detail/Type.hpp +7 -6
  99. data/rice/detail/Type.ipp +192 -45
  100. data/rice/detail/TypeRegistry.hpp +15 -7
  101. data/rice/detail/TypeRegistry.ipp +105 -12
  102. data/rice/detail/Wrapper.hpp +68 -32
  103. data/rice/detail/Wrapper.ipp +121 -109
  104. data/rice/detail/cpp_protect.hpp +5 -6
  105. data/rice/detail/default_allocation_func.ipp +0 -2
  106. data/rice/detail/from_ruby.hpp +38 -3
  107. data/rice/detail/from_ruby.ipp +1321 -492
  108. data/rice/detail/ruby.hpp +18 -0
  109. data/rice/detail/to_ruby.hpp +41 -3
  110. data/rice/detail/to_ruby.ipp +1424 -194
  111. data/rice/global_function.hpp +0 -4
  112. data/rice/global_function.ipp +0 -1
  113. data/rice/libc/file.hpp +11 -0
  114. data/rice/libc/file.ipp +32 -0
  115. data/rice/rice.hpp +116 -26
  116. data/rice/ruby_mark.hpp +4 -3
  117. data/rice/stl/complex.hpp +6 -0
  118. data/rice/stl/complex.ipp +93 -0
  119. data/rice/stl/exception.hpp +11 -0
  120. data/rice/stl/exception.ipp +29 -0
  121. data/rice/stl/exception_ptr.hpp +6 -0
  122. data/rice/stl/exception_ptr.ipp +27 -0
  123. data/rice/stl/map.hpp +12 -0
  124. data/rice/stl/map.ipp +469 -0
  125. data/rice/stl/monostate.hpp +6 -0
  126. data/rice/stl/monostate.ipp +80 -0
  127. data/rice/stl/multimap.hpp +14 -0
  128. data/rice/stl/multimap.ipp +448 -0
  129. data/rice/stl/optional.hpp +6 -0
  130. data/rice/stl/optional.ipp +118 -0
  131. data/rice/stl/pair.hpp +13 -0
  132. data/rice/stl/pair.ipp +155 -0
  133. data/rice/stl/reference_wrapper.hpp +6 -0
  134. data/rice/stl/reference_wrapper.ipp +41 -0
  135. data/rice/stl/set.hpp +12 -0
  136. data/rice/stl/set.ipp +495 -0
  137. data/rice/stl/shared_ptr.hpp +28 -0
  138. data/rice/stl/shared_ptr.ipp +224 -0
  139. data/rice/stl/string.hpp +6 -0
  140. data/rice/stl/string.ipp +158 -0
  141. data/rice/stl/string_view.hpp +6 -0
  142. data/rice/stl/string_view.ipp +65 -0
  143. data/rice/stl/tuple.hpp +6 -0
  144. data/rice/stl/tuple.ipp +128 -0
  145. data/rice/stl/type_index.hpp +6 -0
  146. data/rice/stl/type_index.ipp +30 -0
  147. data/rice/stl/type_info.hpp +6 -0
  148. data/rice/stl/type_info.ipp +29 -0
  149. data/rice/stl/unique_ptr.hpp +22 -0
  150. data/rice/stl/unique_ptr.ipp +139 -0
  151. data/rice/stl/unordered_map.hpp +12 -0
  152. data/rice/stl/unordered_map.ipp +469 -0
  153. data/rice/stl/variant.hpp +6 -0
  154. data/rice/stl/variant.ipp +242 -0
  155. data/rice/stl/vector.hpp +12 -0
  156. data/rice/stl/vector.ipp +590 -0
  157. data/rice/stl.hpp +11 -3
  158. data/rice/traits/attribute_traits.hpp +26 -0
  159. data/rice/traits/function_traits.hpp +95 -0
  160. data/rice/traits/method_traits.hpp +47 -0
  161. data/rice/traits/rice_traits.hpp +160 -0
  162. data/rice.gemspec +85 -0
  163. data/test/embed_ruby.cpp +7 -1
  164. data/test/extconf.rb +2 -0
  165. data/test/test_Address_Registration_Guard.cpp +5 -0
  166. data/test/test_Array.cpp +18 -4
  167. data/test/test_Attribute.cpp +136 -21
  168. data/test/test_Buffer.cpp +285 -0
  169. data/test/test_Builtin_Object.cpp +5 -0
  170. data/test/test_Callback.cpp +230 -0
  171. data/test/test_Class.cpp +5 -31
  172. data/test/test_Constructor.cpp +69 -6
  173. data/test/test_Data_Object.cpp +97 -38
  174. data/test/test_Data_Type.cpp +470 -65
  175. data/test/test_Director.cpp +17 -8
  176. data/test/test_Enum.cpp +155 -40
  177. data/test/test_Exception.cpp +235 -0
  178. data/test/test_File.cpp +70 -0
  179. data/test/test_From_Ruby.cpp +609 -0
  180. data/test/test_Hash.cpp +5 -0
  181. data/test/test_Identifier.cpp +5 -0
  182. data/test/test_Inheritance.cpp +6 -1
  183. data/test/test_Iterator.cpp +6 -1
  184. data/test/test_Jump_Exception.cpp +23 -0
  185. data/test/test_Keep_Alive.cpp +13 -19
  186. data/test/test_Keep_Alive_No_Wrapper.cpp +5 -1
  187. data/test/test_Memory_Management.cpp +5 -0
  188. data/test/test_Module.cpp +128 -67
  189. data/test/test_Native_Registry.cpp +2 -34
  190. data/test/test_Object.cpp +5 -0
  191. data/test/test_Overloads.cpp +806 -0
  192. data/test/test_Ownership.cpp +160 -54
  193. data/test/test_Proc.cpp +44 -0
  194. data/test/test_Self.cpp +9 -4
  195. data/test/test_Stl_Exception.cpp +109 -0
  196. data/test/test_Stl_Map.cpp +54 -42
  197. data/test/test_Stl_Multimap.cpp +693 -0
  198. data/test/test_Stl_Optional.cpp +5 -0
  199. data/test/test_Stl_Pair.cpp +14 -9
  200. data/test/test_Stl_Reference_Wrapper.cpp +9 -2
  201. data/test/test_Stl_Set.cpp +790 -0
  202. data/test/test_Stl_SharedPtr.cpp +458 -0
  203. data/test/test_Stl_String.cpp +5 -0
  204. data/test/test_Stl_String_View.cpp +5 -0
  205. data/test/test_Stl_Tuple.cpp +116 -0
  206. data/test/test_Stl_Type.cpp +147 -0
  207. data/test/test_Stl_UniquePtr.cpp +202 -0
  208. data/test/test_Stl_Unordered_Map.cpp +43 -38
  209. data/test/test_Stl_Variant.cpp +217 -84
  210. data/test/test_Stl_Vector.cpp +306 -58
  211. data/test/test_String.cpp +5 -0
  212. data/test/test_Struct.cpp +5 -0
  213. data/test/test_Symbol.cpp +5 -0
  214. data/test/test_Template.cpp +192 -0
  215. data/test/test_To_Ruby.cpp +524 -0
  216. data/test/test_Tracking.cpp +1 -0
  217. data/test/test_Type.cpp +171 -0
  218. data/test/test_global_functions.cpp +67 -7
  219. data/test/unittest.cpp +8 -0
  220. metadata +127 -26
  221. data/lib/version.rb +0 -3
  222. data/rice/Address_Registration_Guard_defn.hpp +0 -79
  223. data/rice/Data_Object_defn.hpp +0 -84
  224. data/rice/Data_Type_defn.hpp +0 -190
  225. data/rice/Exception_defn.hpp +0 -68
  226. data/rice/HandlerRegistration.hpp +0 -15
  227. data/rice/detail/ExceptionHandler.hpp +0 -8
  228. data/rice/detail/ExceptionHandler.ipp +0 -28
  229. data/rice/detail/ExceptionHandler_defn.hpp +0 -77
  230. data/rice/detail/Jump_Tag.hpp +0 -21
  231. data/rice/detail/NativeAttribute.hpp +0 -64
  232. data/rice/detail/NativeAttribute.ipp +0 -112
  233. data/rice/detail/from_ruby_defn.hpp +0 -38
  234. data/rice/detail/to_ruby_defn.hpp +0 -48
  235. data/test/test_Jump_Tag.cpp +0 -17
  236. data/test/test_Stl_SmartPointer.cpp +0 -283
  237. data/test/test_To_From_Ruby.cpp +0 -399
@@ -0,0 +1,806 @@
1
+ #include "unittest.hpp"
2
+ #include "embed_ruby.hpp"
3
+ #include <rice/rice.hpp>
4
+ #include <rice/stl.hpp>
5
+ #include <ruby/version.h>
6
+
7
+ using namespace Rice;
8
+
9
+ TESTSUITE(Overloads);
10
+
11
+ SETUP(Overloads)
12
+ {
13
+ embed_ruby();
14
+ }
15
+
16
+ namespace
17
+ {
18
+ class MyClass;
19
+ class MyClass2;
20
+ class MyClass3;
21
+ }
22
+
23
+ TEARDOWN(Overloads)
24
+ {
25
+ Data_Type<MyClass>::unbind();
26
+ Data_Type<MyClass2>::unbind();
27
+ Data_Type<MyClass3>::unbind();
28
+ Rice::detail::Registries::instance.types.remove<MyClass>();
29
+ Rice::detail::Registries::instance.types.remove<MyClass2>();
30
+ Rice::detail::Registries::instance.types.remove<MyClass3>();
31
+ rb_gc_start();
32
+ }
33
+
34
+ namespace
35
+ {
36
+ class Container
37
+ {
38
+ public:
39
+ size_t capacity()
40
+ {
41
+ return this->capacity_;
42
+ }
43
+
44
+ void capacity(size_t value)
45
+ {
46
+ this->capacity_ = value;
47
+ }
48
+
49
+ private:
50
+ size_t capacity_;
51
+ };
52
+ }
53
+
54
+ TESTCASE(OverloadsWithTemplateParameter)
55
+ {
56
+ Class c = define_class<Container>("Container")
57
+ .define_constructor(Constructor<Container>())
58
+ .define_method<size_t(Container::*)()>("capacity", &Container::capacity)
59
+ .define_method<void(Container::*)(size_t)>("capacity=", &Container::capacity);
60
+
61
+
62
+ Module m = define_module("Testing");
63
+
64
+ std::string code = R"(container = Container.new
65
+ container.capacity = 5
66
+ container.capacity)";
67
+
68
+ Object result = m.module_eval(code);
69
+ ASSERT_EQUAL(5, detail::From_Ruby<int>().convert(result.value()));
70
+ }
71
+
72
+ TESTCASE(OverloadsWithUsing)
73
+ {
74
+ using Getter_T = size_t(Container::*)();
75
+ using Setter_T = void(Container::*)(size_t);
76
+
77
+ Class c = define_class<Container>("Container")
78
+ .define_constructor(Constructor<Container>())
79
+ .define_method("capacity", (Getter_T)&Container::capacity)
80
+ .define_method("capacity=", (Setter_T)&Container::capacity);
81
+
82
+ Module m = define_module("Testing");
83
+
84
+ std::string code = R"(container = Container.new
85
+ container.capacity = 6
86
+ container.capacity)";
87
+
88
+ Object result = m.module_eval(code);
89
+ ASSERT_EQUAL(6, detail::From_Ruby<int>().convert(result.value()));
90
+ }
91
+
92
+ TESTCASE(OverloadsWithTypedef)
93
+ {
94
+ typedef size_t(Container::* Getter_T)();
95
+ typedef void (Container::* Setter_T)(size_t);
96
+
97
+ Class c = define_class<Container>("Container")
98
+ .define_constructor(Constructor<Container>())
99
+ .define_method("capacity", (Getter_T)&Container::capacity)
100
+ .define_method("capacity=", (Setter_T)&Container::capacity);
101
+
102
+ Module m = define_module("Testing");
103
+
104
+ std::string code = R"(container = Container.new
105
+ container.capacity = 7
106
+ container.capacity)";
107
+
108
+ Object result = m.module_eval(code);
109
+ ASSERT_EQUAL(7, detail::From_Ruby<int>().convert(result.value()));
110
+ }
111
+
112
+ namespace
113
+ {
114
+ std::string run()
115
+ {
116
+ return "run<void>";
117
+ }
118
+
119
+ std::string run(int a)
120
+ {
121
+ return "run<int>";
122
+ }
123
+
124
+ std::string run(float a)
125
+ {
126
+ return "run<float>";
127
+ }
128
+
129
+ std::string run(std::string a)
130
+ {
131
+ return "run<string>";
132
+ }
133
+
134
+ std::string run(int a, float b = 1.0)
135
+ {
136
+ return "run<int,float>";
137
+ }
138
+
139
+ std::string run(float a, int b = 1)
140
+ {
141
+ return "run<float,int>";
142
+ }
143
+ } // namespace
144
+
145
+ void create_globals()
146
+ {
147
+ define_global_function<std::string(*)()>("run", &run);
148
+ define_global_function<std::string(*)(std::string)>("run", &run);
149
+ define_global_function<std::string(*)(int, float)>("run", &run, Arg("a"), Arg("b") = 1.0f);
150
+ define_global_function<std::string(*)(float, int)>("run", &run, Arg("a"), Arg("b") = 1);
151
+ define_global_function<std::string(*)(int)>("run", &run);
152
+ define_global_function<std::string(*)(float)>("run", &run);
153
+ }
154
+
155
+ TESTCASE(function_zero_parameters)
156
+ {
157
+ Module m = define_module("Testing");
158
+ create_globals();
159
+
160
+ std::string code = R"(run())";
161
+
162
+ Rice::String result = m.module_eval(code);
163
+ ASSERT_EQUAL("run<void>", result.str());
164
+ }
165
+
166
+ TESTCASE(function_one_parameter)
167
+ {
168
+ Module m = define_module("Testing");
169
+ create_globals();
170
+
171
+ std::string code = R"(run(1))";
172
+
173
+ Rice::String result = m.module_eval(code);
174
+ ASSERT_EQUAL("run<int>", result.str());
175
+
176
+ code = R"(run(1.0))";
177
+
178
+ result = m.module_eval(code);
179
+ ASSERT_EQUAL("run<float>", result.str());
180
+
181
+ code = R"(run('hello'))";
182
+
183
+ result = m.module_eval(code);
184
+ ASSERT_EQUAL("run<string>", result.str());
185
+ }
186
+
187
+ TESTCASE(function_two_parameters)
188
+ {
189
+ Module m = define_module("Testing");
190
+ create_globals();
191
+
192
+ std::string code = R"(run(1, 1.0))";
193
+
194
+ Rice::String result = m.module_eval(code);
195
+ ASSERT_EQUAL("run<int,float>", result.str());
196
+
197
+ code = R"(run(1.0, 1))";
198
+
199
+ result = m.module_eval(code);
200
+ ASSERT_EQUAL("run<float,int>", result.str());
201
+ }
202
+
203
+ namespace
204
+ {
205
+ class MyClass
206
+ {
207
+ public:
208
+ MyClass() = default;
209
+
210
+ std::string run()
211
+ {
212
+ return "run<void>";
213
+ }
214
+
215
+ std::string run(int a)
216
+ {
217
+ return "run<int>";
218
+ }
219
+
220
+ std::string run(float a)
221
+ {
222
+ return "run<float>";
223
+ }
224
+
225
+ std::string run(std::string a)
226
+ {
227
+ return "run<string>";
228
+ }
229
+
230
+ std::string run(int a, float b = 1.0)
231
+ {
232
+ return "run<int,float>";
233
+ }
234
+
235
+ std::string run(float a, int b = 1)
236
+ {
237
+ return "run<float,int>";
238
+ }
239
+ };
240
+ } // namespace
241
+
242
+ Class createMyClass()
243
+ {
244
+ using Data_Type_T = Data_Type<MyClass>;
245
+ if (Data_Type_T::is_bound())
246
+ detail::Registries::instance.natives.reset(Data_Type_T::klass());
247
+
248
+ Class c = define_class<MyClass>("MyClass")
249
+ .define_constructor(Constructor<MyClass>())
250
+ .define_method<std::string(MyClass::*)()>("run", &MyClass::run)
251
+ .define_method<std::string(MyClass::*)(std::string)>("run", &MyClass::run)
252
+ .define_method<std::string(MyClass::*)(int, float)>("run", &MyClass::run, Arg("a"), Arg("b") = 1.0f)
253
+ .define_method<std::string(MyClass::*)(float, int)>("run", &MyClass::run, Arg("a"), Arg("b") = 1)
254
+ .define_method<std::string(MyClass::*)(int)>("run", &MyClass::run)
255
+ .define_method<std::string(MyClass::*)(float)>("run", &MyClass::run);
256
+
257
+ return c;
258
+ }
259
+
260
+ TESTCASE(method_zero_parameters)
261
+ {
262
+ Module m = define_module("Testing");
263
+ Class c = createMyClass();
264
+
265
+ std::string code = R"(my_class = MyClass.new
266
+ my_class.run())";
267
+
268
+ Rice::String result = m.module_eval(code);
269
+ ASSERT_EQUAL("run<void>", result.str());
270
+ }
271
+
272
+ TESTCASE(method_one_parameter)
273
+ {
274
+ Module m = define_module("Testing");
275
+ Class c = createMyClass();
276
+
277
+ std::string code = R"(my_class = MyClass.new
278
+ my_class.run(1))";
279
+
280
+ Rice::String result = m.module_eval(code);
281
+ ASSERT_EQUAL("run<int>", result.str());
282
+
283
+ code = R"(my_class = MyClass.new
284
+ my_class.run(1.0))";
285
+
286
+ result = m.module_eval(code);
287
+ ASSERT_EQUAL("run<float>", result.str());
288
+
289
+ code = R"(my_class = MyClass.new
290
+ my_class.run('hello'))";
291
+
292
+ result = m.module_eval(code);
293
+ ASSERT_EQUAL("run<string>", result.str());
294
+ }
295
+
296
+ TESTCASE(method_two_parameters)
297
+ {
298
+ Module m = define_module("Testing");
299
+ Class c = createMyClass();
300
+
301
+ std::string code = R"(my_class = MyClass.new
302
+ my_class.run(1, 1.0))";
303
+
304
+ Rice::String result = m.module_eval(code);
305
+ ASSERT_EQUAL("run<int,float>", result.str());
306
+
307
+ code = R"(my_class = MyClass.new
308
+ my_class.run(1.0, 1))";
309
+
310
+ result = m.module_eval(code);
311
+ ASSERT_EQUAL("run<float,int>", result.str());
312
+ }
313
+
314
+ TESTCASE(invalid_parameters)
315
+ {
316
+ Module m = define_module("Testing");
317
+ Class c = createMyClass();
318
+
319
+ std::string code = R"(my_class = MyClass.new
320
+ my_class.run("abc", "def"))";
321
+
322
+ std::string expected = R"(Could not resolve method call for MyClass#run
323
+ 6 overload(s) were evaluated based on the types of Ruby parameters provided:
324
+ std::string AnonymousNamespace::MyClass*::run(int, float)
325
+ std::string AnonymousNamespace::MyClass*::run(float, int)
326
+ std::string AnonymousNamespace::MyClass*::run()
327
+ std::string AnonymousNamespace::MyClass*::run(std::string)
328
+ std::string AnonymousNamespace::MyClass*::run(int)
329
+ std::string AnonymousNamespace::MyClass*::run(float))";
330
+
331
+ ASSERT_EXCEPTION_CHECK(
332
+ Exception,
333
+ Rice::String result = m.module_eval(code),
334
+ ASSERT_EQUAL(expected.c_str(), ex.what()));
335
+ }
336
+
337
+ namespace
338
+ {
339
+ class MyClass2
340
+ {
341
+ public:
342
+ MyClass2()
343
+ {
344
+ this->constructor = "constructor<void>";
345
+ }
346
+
347
+ MyClass2(int a)
348
+ {
349
+ this->constructor = "constructor<int>";
350
+ }
351
+
352
+ MyClass2(float a)
353
+ {
354
+ this->constructor = "constructor<float>";
355
+ }
356
+
357
+ MyClass2(std::string a)
358
+ {
359
+ this->constructor = "constructor<string>";
360
+ }
361
+
362
+ MyClass2(int a, float b)
363
+ {
364
+ this->constructor = "constructor<int,float>";
365
+ }
366
+
367
+ MyClass2(float a, int b)
368
+ {
369
+ this->constructor = "constructor<float,int>";
370
+ }
371
+
372
+ std::string constructor;
373
+ };
374
+ } // namespace
375
+
376
+ Class createMyClass2()
377
+ {
378
+ Class c = define_class<MyClass2>("MyClass")
379
+ .define_constructor(Constructor<MyClass2>())
380
+ .define_constructor(Constructor<MyClass2, int>())
381
+ .define_constructor(Constructor<MyClass2, float>())
382
+ .define_constructor(Constructor<MyClass2, std::string>())
383
+ .define_constructor(Constructor<MyClass2, int, float>())
384
+ .define_constructor(Constructor<MyClass2, float, int>())
385
+ .define_attr("constructor", &MyClass2::constructor);
386
+
387
+ return c;
388
+ }
389
+
390
+ TESTCASE(constructor_zero_parameters)
391
+ {
392
+ Module m = define_module("Testing");
393
+ Class c = createMyClass2();
394
+
395
+ std::string code = R"(MyClass.new)";
396
+
397
+ Rice::Object result = m.module_eval(code);
398
+ Rice::String constructor = result.call("constructor");
399
+ ASSERT_EQUAL("constructor<void>", constructor.str());
400
+ }
401
+
402
+ TESTCASE(constructor_one_parameter)
403
+ {
404
+ Module m = define_module("Testing");
405
+ Class c = createMyClass2();
406
+
407
+ std::string code = R"(MyClass.new(1))";
408
+
409
+ Rice::Object result = m.module_eval(code);
410
+ Rice::String constructor = result.call("constructor");
411
+ ASSERT_EQUAL("constructor<int>", constructor.str());
412
+
413
+ code = R"(my_class = MyClass.new(1.0))";
414
+
415
+ result = m.module_eval(code);
416
+ constructor = result.call("constructor");
417
+ ASSERT_EQUAL("constructor<float>", constructor.str());
418
+
419
+ code = R"(my_class = MyClass.new('hello'))";
420
+
421
+ result = m.module_eval(code);
422
+ constructor = result.call("constructor");
423
+ ASSERT_EQUAL("constructor<string>", constructor.str());
424
+ }
425
+
426
+ TESTCASE(constructor_two_parameters)
427
+ {
428
+ Module m = define_module("Testing");
429
+ Class c = createMyClass2();
430
+
431
+ std::string code = R"(MyClass.new(1, 1.0))";
432
+
433
+ Rice::Object result = m.module_eval(code);
434
+ Rice::String constructor = result.call("constructor");
435
+ ASSERT_EQUAL("constructor<int,float>", constructor.str());
436
+
437
+ code = R"(my_class = MyClass.new(1.0, 1))";
438
+
439
+ result = m.module_eval(code);
440
+ constructor = result.call("constructor");
441
+ ASSERT_EQUAL("constructor<float,int>", constructor.str());
442
+ }
443
+
444
+ namespace
445
+ {
446
+ class MyClass3
447
+ {
448
+ public:
449
+ std::string run(char value)
450
+ {
451
+ return "run<char>";
452
+ }
453
+
454
+ std::string run(unsigned char value)
455
+ {
456
+ return "run<unsigned char>";
457
+ }
458
+
459
+ std::string run(char* value)
460
+ {
461
+ return "run<char*>";
462
+ }
463
+
464
+ std::string run(unsigned char* value)
465
+ {
466
+ return "run<unsigned char*>";
467
+ }
468
+
469
+ std::string run(short value)
470
+ {
471
+ return "run<short>";
472
+ }
473
+
474
+ std::string run(int value)
475
+ {
476
+ return "run<int>";
477
+ }
478
+
479
+ std::string run(float value)
480
+ {
481
+ return "run<float>";
482
+ }
483
+
484
+ std::string run(double value)
485
+ {
486
+ return "run<double>";
487
+ }
488
+
489
+ std::string run(long value)
490
+ {
491
+ return "run<long>";
492
+ }
493
+
494
+ std::string run(long long value)
495
+ {
496
+ return "run<long long>";
497
+ }
498
+ };
499
+ } // namespace
500
+
501
+
502
+ TESTCASE(int_conversion_1)
503
+ {
504
+ Class c = define_class<MyClass3>("MyClass3").
505
+ define_constructor(Constructor<MyClass3>()).
506
+ define_method<std::string(MyClass3::*)(char*)>("run", &MyClass3::run).
507
+ define_method<std::string(MyClass3::*)(unsigned char*)>("run", &MyClass3::run).
508
+ define_method<std::string(MyClass3::*)(short)>("run", &MyClass3::run).
509
+ define_method<std::string(MyClass3::*)(long)>("run", &MyClass3::run).
510
+ define_method<std::string(MyClass3::*)(long long)>("run", &MyClass3::run);
511
+
512
+ Module m = define_module("Testing");
513
+
514
+ std::string code = R"(my_class = MyClass3.new
515
+ value = 1
516
+ my_class.run(value))";
517
+ String result = m.module_eval(code);
518
+ ASSERT_EQUAL("run<long>", result.str());
519
+
520
+ code = R"(my_class = MyClass3.new
521
+ value = 2**60
522
+ my_class.run(value))";
523
+ result = m.module_eval(code);
524
+
525
+ #ifdef _WIN32
526
+ const char* expected = "run<long long>";
527
+ #else
528
+ const char* expected = "run<long>";
529
+ #endif
530
+
531
+ ASSERT_EQUAL(expected, result.str());
532
+ }
533
+
534
+ TESTCASE(int_conversion_2)
535
+ {
536
+ Class c = define_class<MyClass3>("MyClass3").
537
+ define_constructor(Constructor<MyClass3>()).
538
+ define_method<std::string(MyClass3::*)(short)>("run", &MyClass3::run).
539
+ define_method<std::string(MyClass3::*)(float)>("run", &MyClass3::run);
540
+
541
+ Module m = define_module("Testing");
542
+
543
+ std::string code = R"(my_class = MyClass3.new
544
+ value = 1
545
+ my_class.run(value))";
546
+ String result = m.module_eval(code);
547
+ ASSERT_EQUAL("run<float>", result.str());
548
+
549
+ code = R"(my_class = MyClass3.new
550
+ value = 2**64
551
+ my_class.run(value))";
552
+
553
+ std::string expected = R"(Could not resolve method call for MyClass3#run
554
+ 2 overload(s) were evaluated based on the types of Ruby parameters provided:
555
+ std::string AnonymousNamespace::MyClass3*::run(short)
556
+ std::string AnonymousNamespace::MyClass3*::run(float))";
557
+
558
+ ASSERT_EXCEPTION_CHECK(
559
+ Exception,
560
+ result = m.module_eval(code),
561
+ ASSERT_EQUAL(expected.c_str(), ex.what()));
562
+ }
563
+
564
+ TESTCASE(int_conversion_3)
565
+ {
566
+ Class c = define_class<MyClass3>("MyClass3").
567
+ define_constructor(Constructor<MyClass3>()).
568
+ define_method<std::string(MyClass3::*)(short)>("run", &MyClass3::run).
569
+ define_method<std::string(MyClass3::*)(double)>("run", &MyClass3::run);
570
+
571
+ Module m = define_module("Testing");
572
+
573
+ std::string code = R"(my_class = MyClass3.new
574
+ value = 1
575
+ my_class.run(value))";
576
+ String result = m.module_eval(code);
577
+ ASSERT_EQUAL("run<double>", result.str());
578
+
579
+ code = R"(my_class = MyClass3.new
580
+ value = 2**32
581
+ my_class.run(value))";
582
+
583
+ result = m.module_eval(code);
584
+ ASSERT_EQUAL("run<double>", result.str());
585
+ }
586
+
587
+ TESTCASE(int_conversion_4)
588
+ {
589
+ Class c = define_class<MyClass3>("MyClass3").
590
+ define_constructor(Constructor<MyClass3>()).
591
+ define_method<std::string(MyClass3::*)(short)>("run", &MyClass3::run);
592
+
593
+ Module m = define_module("Testing");
594
+
595
+ std::string code = R"(my_class = MyClass3.new
596
+ value = 1
597
+ my_class.run(value))";
598
+ String result = m.module_eval(code);
599
+ ASSERT_EQUAL("run<short>", result.str());
600
+
601
+ code = R"(my_class = MyClass3.new
602
+ value = 2**42
603
+ my_class.run(value))";
604
+
605
+ #ifdef _WIN32
606
+
607
+ #if RUBY_API_VERSION_MAJOR == 3 && RUBY_API_VERSION_MINOR >= 4
608
+ const char* expected = "bignum too big to convert into 'long'";
609
+ #else
610
+ const char* expected = "bignum too big to convert into `long'";
611
+ #endif
612
+
613
+ #else
614
+
615
+ #if RUBY_API_VERSION_MAJOR == 3 && RUBY_API_VERSION_MINOR >= 4
616
+ const char* expected = "integer 4398046511104 too big to convert to 'short'";
617
+ #else
618
+ const char* expected = "integer 4398046511104 too big to convert to `short'";
619
+ #endif
620
+
621
+ #endif
622
+
623
+ ASSERT_EXCEPTION_CHECK(
624
+ Exception,
625
+ result = m.module_eval(code),
626
+ ASSERT_EQUAL(expected, ex.what()));
627
+ }
628
+
629
+ TESTCASE(int_conversion_5)
630
+ {
631
+ Class c = define_class<MyClass3>("MyClass3").
632
+ define_constructor(Constructor<MyClass3>()).
633
+ define_method<std::string(MyClass3::*)(unsigned char)>("run", &MyClass3::run).
634
+ define_method<std::string(MyClass3::*)(unsigned char*)>("run", &MyClass3::run);
635
+
636
+ Module m = define_module("Testing");
637
+
638
+ std::string code = R"(my_class = MyClass3.new
639
+ value = "54"
640
+ my_class.run(value))";
641
+ String result = m.module_eval(code);
642
+ ASSERT_EQUAL("run<unsigned char>", result.str());
643
+ }
644
+
645
+ TESTCASE(int_conversion_6)
646
+ {
647
+ Class c = define_class<MyClass3>("MyClass3").
648
+ define_constructor(Constructor<MyClass3>()).
649
+ define_method<std::string(MyClass3::*)(unsigned char)>("run", &MyClass3::run).
650
+ define_method<std::string(MyClass3::*)(unsigned char*)>("run", &MyClass3::run);
651
+
652
+ Module m = define_module("Testing");
653
+
654
+ std::string code = R"(my_class = MyClass3.new
655
+ buffer = Rice::Buffer≺unsigned char≻.new("54")
656
+ my_class.run(buffer))";
657
+ String result = m.module_eval(code);
658
+ ASSERT_EQUAL("run<unsigned char*>", result.str());
659
+ }
660
+
661
+ namespace
662
+ {
663
+ class MyClass4
664
+ {
665
+ public:
666
+ static const MyClass4& constRef()
667
+ {
668
+ if (!instance_)
669
+ {
670
+ instance_ = new MyClass4();
671
+ }
672
+ return *instance_;
673
+ }
674
+
675
+ private:
676
+ static inline MyClass4* instance_ = nullptr;
677
+ };
678
+
679
+ class MyClass5
680
+ {
681
+ public:
682
+ MyClass5(MyClass4& class4)
683
+ {
684
+ this->result = "non-const ref";
685
+ }
686
+
687
+ MyClass5(const MyClass4& class4)
688
+ {
689
+ this->result = "const ref";
690
+ }
691
+
692
+ public:
693
+ std::string result = "";
694
+ };
695
+ }
696
+
697
+ TESTCASE(NonConstRef)
698
+ {
699
+ Class c4 = define_class<MyClass4>("MyClass4").
700
+ define_constructor(Constructor<MyClass4>());
701
+
702
+ Class c5 = define_class<MyClass5>("MyClass5").
703
+ define_constructor(Constructor<MyClass5, const MyClass4&>()).
704
+ define_constructor(Constructor<MyClass5, MyClass4&>()).
705
+ define_attr("result", &MyClass5::result);
706
+
707
+ Module m = define_module("Testing");
708
+
709
+ std::string code = R"(my_class4 = MyClass4.new
710
+ my_class5 = MyClass5.new(my_class4)
711
+ my_class5.result)";
712
+ String result = m.module_eval(code);
713
+ ASSERT_EQUAL("non-const ref", result.str());
714
+ }
715
+
716
+ TESTCASE(ConstRef)
717
+ {
718
+ Class c4 = define_class<MyClass4>("MyClass4").
719
+ define_singleton_function("const_instance", MyClass4::constRef);
720
+
721
+ Class c5 = define_class<MyClass5>("MyClass5").
722
+ define_constructor(Constructor<MyClass5, const MyClass4&>()).
723
+ define_constructor(Constructor<MyClass5, MyClass4&>()).
724
+ define_attr("result", &MyClass5::result);
725
+
726
+ Module m = define_module("Testing");
727
+
728
+ std::string code = R"(my_class4 = MyClass4.const_instance
729
+ my_class5 = MyClass5.new(my_class4)
730
+ my_class5.result)";
731
+ String result = m.module_eval(code);
732
+ ASSERT_EQUAL("const ref", result.str());
733
+ }
734
+
735
+ namespace
736
+ {
737
+ class MyClass6
738
+ {
739
+ public:
740
+ static const MyClass6* constPointer()
741
+ {
742
+ if (!instance_)
743
+ {
744
+ instance_ = new MyClass6();
745
+ }
746
+ return instance_;
747
+ }
748
+
749
+ private:
750
+ static inline MyClass6* instance_ = nullptr;
751
+ };
752
+
753
+ class MyClass7
754
+ {
755
+ public:
756
+ MyClass7(MyClass6* class6)
757
+ {
758
+ this->result = "non-const pointer";
759
+ }
760
+
761
+ MyClass7(const MyClass6* class6)
762
+ {
763
+ this->result = "const pointer";
764
+ }
765
+ public:
766
+ std::string result = "";
767
+ };
768
+ }
769
+
770
+ TESTCASE(NonConstPointer)
771
+ {
772
+ Class c6 = define_class<MyClass6>("MyClass6").
773
+ define_constructor(Constructor<MyClass6>());
774
+
775
+ Class c7 = define_class<MyClass7>("MyClass7").
776
+ define_constructor(Constructor<MyClass7, const MyClass6*>()).
777
+ define_constructor(Constructor<MyClass7, MyClass6*>()).
778
+ define_attr("result", &MyClass7::result);
779
+
780
+ Module m = define_module("Testing");
781
+
782
+ std::string code = R"(my_class6 = MyClass6.new
783
+ my_class7 = MyClass7.new(my_class6)
784
+ my_class7.result)";
785
+ String result = m.module_eval(code);
786
+ ASSERT_EQUAL("non-const pointer", result.str());
787
+ }
788
+
789
+ TESTCASE(ConstPointer)
790
+ {
791
+ Class c6 = define_class<MyClass6>("MyClass6").
792
+ define_singleton_function("const_instance", MyClass6::constPointer);
793
+
794
+ Class c7 = define_class<MyClass7>("MyClass7").
795
+ define_constructor(Constructor<MyClass7, const MyClass6*>()).
796
+ define_constructor(Constructor<MyClass7, MyClass6*>()).
797
+ define_attr("result", &MyClass7::result);
798
+
799
+ Module m = define_module("Testing");
800
+
801
+ std::string code = R"(my_class6 = MyClass6.const_instance
802
+ my_class7 = MyClass7.new(my_class6)
803
+ my_class7.result)";
804
+ String result = m.module_eval(code);
805
+ ASSERT_EQUAL("const pointer", result.str());
806
+ }