rice2 2.2.0

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