wurlinc-rice 1.4.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (186) hide show
  1. data/COPYING +23 -0
  2. data/Doxyfile +1253 -0
  3. data/Makefile.am +26 -0
  4. data/README +1119 -0
  5. data/README.mingw +8 -0
  6. data/Rakefile +33 -0
  7. data/bootstrap +9 -0
  8. data/configure.ac +52 -0
  9. data/doxygen.ac +314 -0
  10. data/doxygen.am +186 -0
  11. data/extconf.rb +41 -0
  12. data/post-autoconf.rb +22 -0
  13. data/post-automake.rb +28 -0
  14. data/rice/Address_Registration_Guard.hpp +7 -0
  15. data/rice/Address_Registration_Guard.ipp +34 -0
  16. data/rice/Address_Registration_Guard_defn.hpp +65 -0
  17. data/rice/Allocation_Strategies.hpp +37 -0
  18. data/rice/Arg.hpp +8 -0
  19. data/rice/Arg_impl.hpp +127 -0
  20. data/rice/Arg_operators.cpp +21 -0
  21. data/rice/Arg_operators.hpp +19 -0
  22. data/rice/Array.hpp +220 -0
  23. data/rice/Array.ipp +263 -0
  24. data/rice/Builtin_Object.hpp +8 -0
  25. data/rice/Builtin_Object.ipp +50 -0
  26. data/rice/Builtin_Object_defn.hpp +51 -0
  27. data/rice/Class.cpp +57 -0
  28. data/rice/Class.hpp +8 -0
  29. data/rice/Class.ipp +6 -0
  30. data/rice/Class_defn.hpp +83 -0
  31. data/rice/Constructor.hpp +367 -0
  32. data/rice/Critical_Guard.hpp +40 -0
  33. data/rice/Critical_Guard.ipp +26 -0
  34. data/rice/Data_Object.hpp +8 -0
  35. data/rice/Data_Object.ipp +133 -0
  36. data/rice/Data_Object_defn.hpp +132 -0
  37. data/rice/Data_Type.cpp +54 -0
  38. data/rice/Data_Type.hpp +8 -0
  39. data/rice/Data_Type.ipp +365 -0
  40. data/rice/Data_Type_defn.hpp +261 -0
  41. data/rice/Data_Type_fwd.hpp +12 -0
  42. data/rice/Director.cpp +13 -0
  43. data/rice/Director.hpp +39 -0
  44. data/rice/Enum.hpp +117 -0
  45. data/rice/Enum.ipp +246 -0
  46. data/rice/Exception.cpp +59 -0
  47. data/rice/Exception.hpp +9 -0
  48. data/rice/Exception_Base.hpp +8 -0
  49. data/rice/Exception_Base.ipp +13 -0
  50. data/rice/Exception_Base_defn.hpp +27 -0
  51. data/rice/Exception_defn.hpp +69 -0
  52. data/rice/Hash.hpp +210 -0
  53. data/rice/Hash.ipp +338 -0
  54. data/rice/Identifier.cpp +8 -0
  55. data/rice/Identifier.hpp +50 -0
  56. data/rice/Identifier.ipp +33 -0
  57. data/rice/Jump_Tag.hpp +24 -0
  58. data/rice/Makefile.am +129 -0
  59. data/rice/Module.cpp +84 -0
  60. data/rice/Module.hpp +8 -0
  61. data/rice/Module.ipp +6 -0
  62. data/rice/Module_defn.hpp +88 -0
  63. data/rice/Module_impl.hpp +281 -0
  64. data/rice/Module_impl.ipp +348 -0
  65. data/rice/Object.cpp +160 -0
  66. data/rice/Object.hpp +8 -0
  67. data/rice/Object.ipp +19 -0
  68. data/rice/Object_defn.hpp +191 -0
  69. data/rice/Require_Guard.hpp +21 -0
  70. data/rice/String.cpp +94 -0
  71. data/rice/String.hpp +89 -0
  72. data/rice/Struct.cpp +117 -0
  73. data/rice/Struct.hpp +162 -0
  74. data/rice/Struct.ipp +26 -0
  75. data/rice/Symbol.cpp +25 -0
  76. data/rice/Symbol.hpp +66 -0
  77. data/rice/Symbol.ipp +44 -0
  78. data/rice/VM.cpp +92 -0
  79. data/rice/VM.hpp +32 -0
  80. data/rice/config.hpp.in +40 -0
  81. data/rice/detail/Arguments.hpp +118 -0
  82. data/rice/detail/Auto_Function_Wrapper.hpp +829 -0
  83. data/rice/detail/Auto_Function_Wrapper.ipp +3391 -0
  84. data/rice/detail/Auto_Member_Function_Wrapper.hpp +828 -0
  85. data/rice/detail/Auto_Member_Function_Wrapper.ipp +2503 -0
  86. data/rice/detail/Caster.hpp +103 -0
  87. data/rice/detail/Exception_Handler.hpp +8 -0
  88. data/rice/detail/Exception_Handler.ipp +68 -0
  89. data/rice/detail/Exception_Handler_defn.hpp +96 -0
  90. data/rice/detail/Iterator.hpp +93 -0
  91. data/rice/detail/Not_Copyable.hpp +25 -0
  92. data/rice/detail/Wrapped_Function.hpp +33 -0
  93. data/rice/detail/cfp.hpp +24 -0
  94. data/rice/detail/cfp.ipp +51 -0
  95. data/rice/detail/check_ruby_type.cpp +27 -0
  96. data/rice/detail/check_ruby_type.hpp +23 -0
  97. data/rice/detail/creation_funcs.hpp +37 -0
  98. data/rice/detail/creation_funcs.ipp +36 -0
  99. data/rice/detail/default_allocation_func.hpp +23 -0
  100. data/rice/detail/default_allocation_func.ipp +11 -0
  101. data/rice/detail/define_method_and_auto_wrap.hpp +31 -0
  102. data/rice/detail/define_method_and_auto_wrap.ipp +30 -0
  103. data/rice/detail/demangle.cpp +56 -0
  104. data/rice/detail/demangle.hpp +19 -0
  105. data/rice/detail/env.hpp +19 -0
  106. data/rice/detail/from_ruby.hpp +43 -0
  107. data/rice/detail/from_ruby.ipp +60 -0
  108. data/rice/detail/method_data.cpp +159 -0
  109. data/rice/detail/method_data.hpp +21 -0
  110. data/rice/detail/mininode.cpp +1220 -0
  111. data/rice/detail/mininode.hpp +320 -0
  112. data/rice/detail/node.hpp +13 -0
  113. data/rice/detail/object_call.hpp +68 -0
  114. data/rice/detail/object_call.ipp +131 -0
  115. data/rice/detail/protect.cpp +29 -0
  116. data/rice/detail/protect.hpp +34 -0
  117. data/rice/detail/ruby.hpp +93 -0
  118. data/rice/detail/ruby_version_code.hpp.in +6 -0
  119. data/rice/detail/rubysig.hpp +19 -0
  120. data/rice/detail/st.hpp +60 -0
  121. data/rice/detail/to_ruby.hpp +22 -0
  122. data/rice/detail/to_ruby.ipp +36 -0
  123. data/rice/detail/traits.hpp +43 -0
  124. data/rice/detail/win32.hpp +16 -0
  125. data/rice/detail/wrap_function.hpp +341 -0
  126. data/rice/detail/wrap_function.ipp +514 -0
  127. data/rice/global_function.hpp +33 -0
  128. data/rice/global_function.ipp +22 -0
  129. data/rice/protect.hpp +91 -0
  130. data/rice/protect.ipp +1133 -0
  131. data/rice/ruby_mark.hpp +13 -0
  132. data/rice/ruby_try_catch.hpp +86 -0
  133. data/rice/rubypp.rb +97 -0
  134. data/rice/to_from_ruby.hpp +8 -0
  135. data/rice/to_from_ruby.ipp +297 -0
  136. data/rice/to_from_ruby_defn.hpp +71 -0
  137. data/ruby/Makefile.am +1 -0
  138. data/ruby/lib/Makefile.am +3 -0
  139. data/ruby/lib/mkmf-rice.rb.in +216 -0
  140. data/ruby/lib/version.rb +3 -0
  141. data/ruby.ac +136 -0
  142. data/sample/Makefile.am +47 -0
  143. data/sample/enum/extconf.rb +3 -0
  144. data/sample/enum/sample_enum.cpp +54 -0
  145. data/sample/enum/test.rb +8 -0
  146. data/sample/inheritance/animals.cpp +98 -0
  147. data/sample/inheritance/extconf.rb +3 -0
  148. data/sample/inheritance/test.rb +7 -0
  149. data/sample/map/extconf.rb +3 -0
  150. data/sample/map/map.cpp +81 -0
  151. data/sample/map/test.rb +7 -0
  152. data/test/Makefile.am +78 -0
  153. data/test/ext/Makefile.am +43 -0
  154. data/test/ext/t1/Foo.hpp +10 -0
  155. data/test/ext/t1/extconf.rb +2 -0
  156. data/test/ext/t1/t1.cpp +15 -0
  157. data/test/ext/t2/extconf.rb +2 -0
  158. data/test/ext/t2/t2.cpp +11 -0
  159. data/test/test_Address_Registration_Guard.cpp +43 -0
  160. data/test/test_Allocation_Strategies.cpp +77 -0
  161. data/test/test_Array.cpp +241 -0
  162. data/test/test_Builtin_Object.cpp +72 -0
  163. data/test/test_Class.cpp +498 -0
  164. data/test/test_Constructor.cpp +128 -0
  165. data/test/test_Critical_Guard.cpp +51 -0
  166. data/test/test_Data_Object.cpp +275 -0
  167. data/test/test_Data_Type.cpp +348 -0
  168. data/test/test_Director.cpp +301 -0
  169. data/test/test_Enum.cpp +195 -0
  170. data/test/test_Exception.cpp +46 -0
  171. data/test/test_Hash.cpp +195 -0
  172. data/test/test_Identifier.cpp +70 -0
  173. data/test/test_Jump_Tag.cpp +17 -0
  174. data/test/test_Memory_Management.cpp +50 -0
  175. data/test/test_Module.cpp +481 -0
  176. data/test/test_Object.cpp +148 -0
  177. data/test/test_String.cpp +94 -0
  178. data/test/test_Struct.cpp +192 -0
  179. data/test/test_Symbol.cpp +63 -0
  180. data/test/test_To_From_Ruby.cpp +263 -0
  181. data/test/test_VM.cpp +26 -0
  182. data/test/test_global_functions.cpp +114 -0
  183. data/test/test_rice.rb +43 -0
  184. data/test/unittest.cpp +136 -0
  185. data/test/unittest.hpp +292 -0
  186. metadata +276 -0
@@ -0,0 +1,498 @@
1
+ #include "unittest.hpp"
2
+ #include "rice/Class.hpp"
3
+ #include "rice/Constructor.hpp"
4
+ #include "rice/protect.hpp"
5
+ #include "rice/Exception.hpp"
6
+ #include "rice/Array.hpp"
7
+ #include "rice/String.hpp"
8
+ #include "rice/Symbol.hpp"
9
+ #include <iostream>
10
+
11
+ using namespace Rice;
12
+ using namespace std;
13
+
14
+ TESTSUITE(Class);
15
+
16
+ SETUP(Class)
17
+ {
18
+ ruby_init();
19
+ }
20
+
21
+ TESTCASE(construct)
22
+ {
23
+ Class c(rb_cObject);
24
+ ASSERT_EQUAL(rb_cObject, c.value());
25
+ }
26
+
27
+ TESTCASE(undef_creation_funcs)
28
+ {
29
+ Class c(anonymous_class());
30
+ Class & c2(c.undef_creation_funcs());
31
+ ASSERT_EQUAL(&c, &c2);
32
+ ASSERT_EXCEPTION_CHECK(
33
+ Exception,
34
+ c.call("new"),
35
+ ASSERT_EQUAL(
36
+ Object(rb_eTypeError), // TODO: 1.6.x?
37
+ Object(CLASS_OF(ex.value()))
38
+ )
39
+ );
40
+ }
41
+
42
+ TESTCASE(include_module)
43
+ {
44
+ Class c(anonymous_class());
45
+ Class & c2(c.include_module(rb_mEnumerable));
46
+ ASSERT_EQUAL(&c, &c2);
47
+ Array ancestors(c.ancestors());
48
+ Array expected_ancestors;
49
+ expected_ancestors.push(c);
50
+ expected_ancestors.push(Module(rb_mEnumerable));
51
+ expected_ancestors.push(Module(rb_cObject));
52
+ expected_ancestors.push(Module(rb_mKernel));
53
+ #ifdef RUBY_VM
54
+ expected_ancestors.push(Module(rb_cBasicObject));
55
+ #endif
56
+ ASSERT_EQUAL(expected_ancestors, ancestors);
57
+ }
58
+
59
+ TESTCASE(const_set_get_by_id)
60
+ {
61
+ Class c(anonymous_class());
62
+ Object v = to_ruby(42);
63
+ Class & c2(c.const_set(rb_intern("FOO"), v));
64
+ ASSERT_EQUAL(&c, &c2);
65
+ ASSERT_EQUAL(v, c.const_get(rb_intern("FOO")));
66
+ }
67
+
68
+ TESTCASE(const_set_get_by_identifier)
69
+ {
70
+ Class c(anonymous_class());
71
+ Object v = to_ruby(42);
72
+ Class & c2(c.const_set(Identifier("FOO"), v));
73
+ ASSERT_EQUAL(&c, &c2);
74
+ ASSERT_EQUAL(v, c.const_get(Identifier("FOO")));
75
+ }
76
+
77
+ TESTCASE(const_set_get_by_string)
78
+ {
79
+ Class c(anonymous_class());
80
+ Object v = to_ruby(42);
81
+ Class & c2(c.const_set("FOO", v));
82
+ ASSERT_EQUAL(&c, &c2);
83
+ ASSERT_EQUAL(v, c.const_get("FOO"));
84
+ }
85
+
86
+ namespace
87
+ {
88
+
89
+ bool define_method_simple_ok;
90
+
91
+ void define_method_simple_helper()
92
+ {
93
+ define_method_simple_ok = true;
94
+ }
95
+
96
+ } // namespace
97
+
98
+ TESTCASE(define_method_simple)
99
+ {
100
+ Class c(anonymous_class());
101
+ c.define_method("foo", &define_method_simple_helper);
102
+ Object o = c.call("new");
103
+ define_method_simple_ok = false;
104
+ o.call("foo");
105
+ ASSERT(define_method_simple_ok);
106
+ }
107
+
108
+ TESTCASE(define_singleton_method_simple)
109
+ {
110
+ Class c(anonymous_class());
111
+ c.define_singleton_method("foo", &define_method_simple_helper);
112
+ define_method_simple_ok = false;
113
+ Object o = c.call("foo");
114
+ ASSERT(define_method_simple_ok);
115
+ }
116
+
117
+ TESTCASE(define_module_function_simple)
118
+ {
119
+ // module_function only works with Module, not Class
120
+ Class c(anonymous_class());
121
+ ASSERT_EXCEPTION_CHECK(
122
+ Exception,
123
+ c.define_module_function("foo", &define_method_simple_helper),
124
+ ASSERT_EQUAL(
125
+ Object(rb_eTypeError),
126
+ Object(CLASS_OF(ex.value()))
127
+ )
128
+ );
129
+ }
130
+
131
+ namespace
132
+ {
133
+ class RefTest
134
+ {
135
+ public:
136
+ RefTest() { }
137
+
138
+ static std::string& getReference() {
139
+ static std::string foo = "foo";
140
+ return foo;
141
+ }
142
+ };
143
+ }
144
+
145
+ TESTCASE(define_singleton_method_returning_reference)
146
+ {
147
+ Class c = define_class<RefTest>("RefTest")
148
+ .define_constructor(Constructor<RefTest>())
149
+ .define_singleton_method("get_reference", &RefTest::getReference);
150
+
151
+ Module m(anonymous_module());
152
+
153
+ Object result = m.instance_eval("RefTest.get_reference");
154
+ ASSERT_EQUAL(result, String("foo"));
155
+ }
156
+
157
+ namespace
158
+ {
159
+
160
+ int define_method_int_result;
161
+
162
+ class IntHelper {
163
+ public:
164
+ IntHelper() { }
165
+
166
+ void define_method_int_helper(int i)
167
+ {
168
+ define_method_int_result = i;
169
+ }
170
+ };
171
+
172
+ } // namespace
173
+
174
+ TESTCASE(define_method_int)
175
+ {
176
+ Class c =
177
+ define_class<IntHelper>("IntHelper")
178
+ .define_constructor(Constructor<IntHelper>())
179
+ .define_method("foo", &IntHelper::define_method_int_helper);
180
+
181
+ Object o = c.call("new");
182
+ define_method_int_result = 0;
183
+ o.call("foo", 42);
184
+ ASSERT_EQUAL(42, define_method_int_result);
185
+ }
186
+
187
+ TESTCASE(define_method_int_passed_two_args)
188
+ {
189
+ Class c =
190
+ define_class<IntHelper>("IntHelper")
191
+ .define_constructor(Constructor<IntHelper>())
192
+ .define_method("foo", &IntHelper::define_method_int_helper);
193
+
194
+ Object o = c.call("new");
195
+
196
+ ASSERT_EXCEPTION_CHECK(
197
+ Exception,
198
+ o.call("foo", 1, 2),
199
+ ASSERT_EQUAL(
200
+ Object(rb_eArgError),
201
+ Object(CLASS_OF(ex.value()))
202
+ )
203
+ );
204
+ }
205
+
206
+ TESTCASE(define_method_int_passed_no_args)
207
+ {
208
+ Class c =
209
+ define_class<IntHelper>("IntHelper")
210
+ .define_constructor(Constructor<IntHelper>())
211
+ .define_method("foo", &IntHelper::define_method_int_helper);
212
+
213
+ Object o = c.call("new");
214
+
215
+ ASSERT_EXCEPTION_CHECK(
216
+ Exception,
217
+ o.call("foo"),
218
+ ASSERT_EQUAL(
219
+ Object(rb_eArgError),
220
+ Object(CLASS_OF(ex.value()))
221
+ )
222
+ );
223
+ }
224
+
225
+
226
+ namespace
227
+ {
228
+
229
+ struct Foo
230
+ {
231
+ int x;
232
+ };
233
+
234
+ int define_method_int_foo_result_i;
235
+ Foo * define_method_int_foo_result_x;
236
+
237
+ void define_method_int_foo_helper(int i, Foo * x)
238
+ {
239
+ define_method_int_foo_result_i = i;
240
+ define_method_int_foo_result_x = x;
241
+ }
242
+
243
+ } // namespace
244
+
245
+ template<>
246
+ Foo * from_ruby<Foo *>(Object x)
247
+ {
248
+ Foo * retval;
249
+ Data_Get_Struct(x.value(), Foo, retval);
250
+ return retval;
251
+ }
252
+
253
+ TESTCASE(define_method_int_foo)
254
+ {
255
+ Class c(anonymous_class());
256
+ c.define_method("foo", &define_method_int_foo_helper);
257
+ Object o = c.call("new");
258
+ define_method_int_result = 0;
259
+ Foo * foo = new Foo;
260
+ foo->x = 1024;
261
+ VALUE f = Data_Wrap_Struct(rb_cObject, 0, Default_Allocation_Strategy<Foo>::free, foo);
262
+ o.call("foo", 42, Object(f));
263
+ ASSERT_EQUAL(42, define_method_int_foo_result_i);
264
+ ASSERT_EQUAL(foo, define_method_int_foo_result_x);
265
+ }
266
+
267
+ namespace
268
+ {
269
+
270
+ class Silly_Exception
271
+ : public std::exception
272
+ {
273
+ };
274
+
275
+ void handle_silly_exception(Silly_Exception const & ex)
276
+ {
277
+ throw Exception(rb_eRuntimeError, "SILLY");
278
+ }
279
+
280
+ void throw_silly_exception()
281
+ {
282
+ throw Silly_Exception();
283
+ }
284
+
285
+ }
286
+
287
+ TESTCASE(add_handler)
288
+ {
289
+ Class c(rb_cObject);
290
+ c.add_handler<Silly_Exception>(handle_silly_exception);
291
+ c.define_method("foo", throw_silly_exception);
292
+ Object exc = protect(rb_eval_string, "begin; foo; rescue Exception; $!; end");
293
+ ASSERT_EQUAL(rb_eRuntimeError, CLASS_OF(exc));
294
+ Exception ex(exc);
295
+ ASSERT_EQUAL(String("SILLY"), String(ex.message()));
296
+ }
297
+
298
+ namespace
299
+ {
300
+
301
+ class Container
302
+ {
303
+ public:
304
+ Container(int * array, size_t length)
305
+ : array_(array)
306
+ , length_(length)
307
+ {
308
+ }
309
+
310
+ int * begin() { return array_; }
311
+ int * end() { return array_ + length_; }
312
+
313
+ private:
314
+ int * array_;
315
+ size_t length_;
316
+ };
317
+
318
+ } // namespace
319
+
320
+ template<>
321
+ Container * from_ruby<Container *>(Object x)
322
+ {
323
+ Container * retval;
324
+ Data_Get_Struct(x.value(), Container, retval);
325
+ return retval;
326
+ }
327
+
328
+ TESTCASE(define_iterator)
329
+ {
330
+ int array[] = { 1, 2, 3 };
331
+ Class c(anonymous_class());
332
+ c.define_iterator(&Container::begin, &Container::end);
333
+ Container * container = new Container(array, 3);
334
+ Object wrapped_container = Data_Wrap_Struct(
335
+ c, 0, Default_Allocation_Strategy<Container>::free, container);
336
+ Array a = wrapped_container.instance_eval("a = []; each() { |x| a << x }; a");
337
+ ASSERT_EQUAL(3u, a.size());
338
+ ASSERT_EQUAL(to_ruby(1), Object(a[0]));
339
+ ASSERT_EQUAL(to_ruby(2), Object(a[1]));
340
+ ASSERT_EQUAL(to_ruby(3), Object(a[2]));
341
+ }
342
+
343
+ TESTCASE(define_class)
344
+ {
345
+ Class object(rb_cObject);
346
+ if(object.const_defined("Foo"))
347
+ {
348
+ object.remove_const("Foo");
349
+ }
350
+
351
+ Class c = define_class("Foo");
352
+
353
+ ASSERT(c.is_a(rb_cClass));
354
+ ASSERT_EQUAL(c, object.const_get("Foo"));
355
+ }
356
+
357
+ TESTCASE(define_class_under)
358
+ {
359
+ Class object(rb_cObject);
360
+ if(object.const_defined("Foo"))
361
+ {
362
+ object.remove_const("Foo");
363
+ }
364
+
365
+ Module math(rb_mMath);
366
+ if(math.const_defined("Foo"))
367
+ {
368
+ math.remove_const("Foo");
369
+ }
370
+
371
+ Class c = define_class_under(math, "Foo");
372
+
373
+ ASSERT(c.is_a(rb_cClass));
374
+ ASSERT_EQUAL(c, math.const_get("Foo"));
375
+ ASSERT(!object.const_defined("Foo"));
376
+ }
377
+
378
+ TESTCASE(module_define_class)
379
+ {
380
+ Class object(rb_cObject);
381
+ if(object.const_defined("Foo"))
382
+ {
383
+ object.remove_const("Foo");
384
+ }
385
+
386
+ Module math(rb_mMath);
387
+ if(math.const_defined("Foo"))
388
+ {
389
+ math.remove_const("Foo");
390
+ }
391
+
392
+ Class c = math.define_class("Foo");
393
+
394
+ ASSERT(c.is_a(rb_cClass));
395
+ ASSERT_EQUAL(c, math.const_get("Foo"));
396
+ ASSERT(!object.const_defined("Foo"));
397
+ }
398
+
399
+ namespace {
400
+ class BaseClass {
401
+ public:
402
+ BaseClass() { }
403
+ };
404
+ }
405
+
406
+ TESTCASE(subclassing)
407
+ {
408
+ Module m = define_module("Testing");
409
+ define_class_under<BaseClass>(m, "BaseClass").
410
+ define_constructor(Constructor<BaseClass>());
411
+
412
+ // Not sure how to make this a true failure case. If the subclassing
413
+ // doesn't work, Ruby will throw an error:
414
+ //
415
+ // in `new': wrong instance allocation
416
+ //
417
+ m.instance_eval("class NewClass < Testing::BaseClass; end;");
418
+ m.instance_eval("n = NewClass.new");
419
+ }
420
+
421
+ namespace
422
+ {
423
+ int defaults_method_one_arg1;
424
+ int defaults_method_one_arg2;
425
+ bool defaults_method_one_arg3 = false;
426
+
427
+ class DefaultArgs
428
+ {
429
+ public:
430
+ void defaults_method_one(int arg1, int arg2 = 3, bool arg3 = true)
431
+ {
432
+ defaults_method_one_arg1 = arg1;
433
+ defaults_method_one_arg2 = arg2;
434
+ defaults_method_one_arg3 = arg3;
435
+ }
436
+ };
437
+ }
438
+
439
+ TESTCASE(define_method_default_arguments)
440
+ {
441
+ Class c = define_class<DefaultArgs>("DefaultArgs")
442
+ .define_constructor(Constructor<DefaultArgs>())
443
+ .define_method("with_defaults",
444
+ &DefaultArgs::defaults_method_one,
445
+ (Arg("arg1"), Arg("arg2") = 3, Arg("arg3") = true));
446
+
447
+ Object o = c.call("new");
448
+ o.call("with_defaults", 2);
449
+
450
+ ASSERT_EQUAL(2, defaults_method_one_arg1);
451
+ ASSERT_EQUAL(3, defaults_method_one_arg2);
452
+ ASSERT(defaults_method_one_arg3);
453
+
454
+ o.call("with_defaults", 11, 10);
455
+
456
+ ASSERT_EQUAL(11, defaults_method_one_arg1);
457
+ ASSERT_EQUAL(10, defaults_method_one_arg2);
458
+ ASSERT(defaults_method_one_arg3);
459
+
460
+ o.call("with_defaults", 22, 33, false);
461
+
462
+ ASSERT_EQUAL(22, defaults_method_one_arg1);
463
+ ASSERT_EQUAL(33, defaults_method_one_arg2);
464
+ ASSERT(!defaults_method_one_arg3);
465
+ }
466
+
467
+ /*
468
+ namespace {
469
+ float with_reference_defaults_x;
470
+ std::string with_reference_defaults_str;
471
+
472
+ class DefaultArgsRefs
473
+ {
474
+ public:
475
+ void with_reference_defaults(float x, std::string const& str = std::string("testing"))
476
+ {
477
+ with_reference_defaults_x = x;
478
+ with_reference_defaults_str = str;
479
+ }
480
+ };
481
+
482
+ }
483
+
484
+ TESTCASE(define_method_works_with_reference_const_default_values)
485
+ {
486
+ Class c = define_class<DefaultArgsRefs>("DefaultArgsRefs")
487
+ .define_constructor(Constructor<DefaultArgsRefs>())
488
+ .define_method("bar",
489
+ &DefaultArgsRefs::with_reference_defaults,
490
+ (Arg("x"), Arg("str") = std::string("testing")));
491
+
492
+ Object o = c.call("new");
493
+ o.call("bar", 3);
494
+
495
+ ASSERT_EQUAL(3, with_reference_defaults_x);
496
+ ASSERT_EQUAL("testing", with_reference_defaults_str);
497
+ }
498
+ */
@@ -0,0 +1,128 @@
1
+ #include "unittest.hpp"
2
+ #include "rice/Constructor.hpp"
3
+ #include "rice/Data_Type.hpp"
4
+
5
+ #include "rice/detail/env.hpp"
6
+
7
+ #include <iostream>
8
+ using namespace std;
9
+
10
+ using namespace Rice;
11
+
12
+ TESTSUITE(Constructor);
13
+
14
+ namespace
15
+ {
16
+ class Default_Constructible
17
+ {
18
+ public:
19
+ Default_Constructible()
20
+ {
21
+ }
22
+ };
23
+ }
24
+
25
+ TESTCASE(default_constructor)
26
+ {
27
+ Data_Type<Default_Constructible> rb_cDefault_Constructible(
28
+ anonymous_class());
29
+ rb_cDefault_Constructible
30
+ .define_constructor(Constructor<Default_Constructible>());
31
+ Object o = rb_cDefault_Constructible.call("new");
32
+ ASSERT_EQUAL(rb_cDefault_Constructible, o.class_of());
33
+ }
34
+
35
+
36
+ namespace
37
+ {
38
+ class Non_Default_Constructible
39
+ {
40
+ public:
41
+ Non_Default_Constructible(int i)
42
+ : i_(i)
43
+ {
44
+ }
45
+
46
+ int i() const
47
+ {
48
+ return i_;
49
+ }
50
+
51
+ private:
52
+ int i_;
53
+ };
54
+ }
55
+
56
+ TESTCASE(non_default_constructor)
57
+ {
58
+ Data_Type<Non_Default_Constructible> rb_cNon_Default_Constructible(
59
+ anonymous_class());
60
+ rb_cNon_Default_Constructible
61
+ .define_constructor(Constructor<Non_Default_Constructible, int>());
62
+ Data_Object<Non_Default_Constructible> o =
63
+ rb_cNon_Default_Constructible.call("new", 42);
64
+ ASSERT_EQUAL(rb_cNon_Default_Constructible, o.class_of());
65
+ ASSERT_EQUAL(42, o->i());
66
+ }
67
+
68
+ namespace
69
+ {
70
+ int withArgsX;
71
+ float withArgsY;
72
+ bool withArgsYes;
73
+
74
+ class WithDefaultArgs
75
+ {
76
+ public:
77
+ WithDefaultArgs(int x, float y = 2.0, bool yes = false)
78
+ {
79
+ withArgsX = x;
80
+ withArgsY = y;
81
+ withArgsYes = yes;
82
+ }
83
+ };
84
+
85
+ int withArgX;
86
+ class WithOneArg
87
+ {
88
+ public:
89
+ WithOneArg(int x = 14) {
90
+ withArgX = x;
91
+ }
92
+ };
93
+ }
94
+
95
+ TESTCASE(constructor_supports_default_arguments)
96
+ {
97
+ Class klass = define_class<WithDefaultArgs>("WithDefaultArgs").
98
+ define_constructor(Constructor<WithDefaultArgs, int, float, bool>(),
99
+ ( Arg("x"), Arg("y") = (float)2.0, Arg("yes") = (bool)false ));
100
+
101
+ klass.call("new", 4);
102
+ ASSERT_EQUAL(4, withArgsX);
103
+ ASSERT_EQUAL(2.0, withArgsY);
104
+ ASSERT_EQUAL(false, withArgsYes);
105
+
106
+ klass.call("new", 5, 3.0);
107
+ ASSERT_EQUAL(5, withArgsX);
108
+ ASSERT_EQUAL(3.0, withArgsY);
109
+ ASSERT_EQUAL(false, withArgsYes);
110
+
111
+ klass.call("new", 7, 12.0, true);
112
+ ASSERT_EQUAL(7, withArgsX);
113
+ ASSERT_EQUAL(12.0, withArgsY);
114
+ ASSERT_EQUAL(true, withArgsYes);
115
+ }
116
+
117
+ TESTCASE(constructor_supports_single_default_argument)
118
+ {
119
+ Class klass = define_class<WithOneArg>("WithOneArg").
120
+ define_constructor(Constructor<WithOneArg, int>(),
121
+ ( Arg("x") = 14 ));
122
+
123
+ klass.call("new");
124
+ ASSERT_EQUAL(14, withArgX);
125
+
126
+ klass.call("new", 6);
127
+ ASSERT_EQUAL(6, withArgX);
128
+ }
@@ -0,0 +1,51 @@
1
+ #include "unittest.hpp"
2
+ #include "rice/Critical_Guard.hpp"
3
+ #include <stdexcept>
4
+
5
+ #if RICE__RUBY_VERSION_CODE < 190
6
+
7
+ using namespace Rice;
8
+
9
+ TESTSUITE(Critical_Guard);
10
+
11
+ SETUP(Critical_Guard)
12
+ {
13
+ ruby_init();
14
+ }
15
+
16
+ TESTCASE(normal_path)
17
+ {
18
+ rb_thread_critical = 1;
19
+ {
20
+ Critical_Guard g;
21
+ ASSERT(rb_thread_critical);
22
+ }
23
+ ASSERT(!rb_thread_critical);
24
+ }
25
+
26
+ TESTCASE(unset_thread_critical_on_destruction)
27
+ {
28
+ rb_thread_critical = 1;
29
+ {
30
+ Critical_Guard g;
31
+ ASSERT(rb_thread_critical);
32
+ }
33
+ ASSERT(!rb_thread_critical);
34
+ }
35
+
36
+ TESTCASE(exception)
37
+ {
38
+ rb_thread_critical = 1;
39
+ try
40
+ {
41
+ Critical_Guard g;
42
+ throw std::runtime_error("testing");
43
+ }
44
+ catch(...)
45
+ {
46
+ }
47
+ ASSERT(!rb_thread_critical);
48
+ }
49
+
50
+ #endif
51
+