rice 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (166) hide show
  1. data/COPYING +23 -0
  2. data/Doxyfile +1253 -0
  3. data/Makefile.am +26 -0
  4. data/Makefile.in +736 -0
  5. data/README +881 -0
  6. data/README.mingw +8 -0
  7. data/bootstrap +8 -0
  8. data/config.guess +1535 -0
  9. data/config.sub +1644 -0
  10. data/configure +7310 -0
  11. data/configure.ac +48 -0
  12. data/depcomp +584 -0
  13. data/doxygen.ac +314 -0
  14. data/doxygen.am +186 -0
  15. data/install-sh +507 -0
  16. data/missing +367 -0
  17. data/post-autoconf.rb +22 -0
  18. data/post-automake.rb +28 -0
  19. data/rice/Address_Registration_Guard.hpp +7 -0
  20. data/rice/Address_Registration_Guard.ipp +34 -0
  21. data/rice/Address_Registration_Guard_defn.hpp +65 -0
  22. data/rice/Allocation_Strategies.hpp +37 -0
  23. data/rice/Array.hpp +220 -0
  24. data/rice/Array.ipp +262 -0
  25. data/rice/Builtin_Object.hpp +8 -0
  26. data/rice/Builtin_Object.ipp +50 -0
  27. data/rice/Builtin_Object_defn.hpp +51 -0
  28. data/rice/Class.cpp +57 -0
  29. data/rice/Class.hpp +8 -0
  30. data/rice/Class.ipp +4 -0
  31. data/rice/Class_defn.hpp +83 -0
  32. data/rice/Constructor.hpp +189 -0
  33. data/rice/Critical_Guard.hpp +34 -0
  34. data/rice/Critical_Guard.ipp +20 -0
  35. data/rice/Data_Object.hpp +127 -0
  36. data/rice/Data_Object.ipp +129 -0
  37. data/rice/Data_Type.cpp +21 -0
  38. data/rice/Data_Type.hpp +8 -0
  39. data/rice/Data_Type.ipp +227 -0
  40. data/rice/Data_Type_defn.hpp +219 -0
  41. data/rice/Data_Type_fwd.hpp +12 -0
  42. data/rice/Enum.hpp +118 -0
  43. data/rice/Enum.ipp +246 -0
  44. data/rice/Exception.cpp +59 -0
  45. data/rice/Exception.hpp +69 -0
  46. data/rice/Exception_Base.hpp +30 -0
  47. data/rice/Exception_Base.ipp +11 -0
  48. data/rice/Hash.hpp +206 -0
  49. data/rice/Hash.ipp +336 -0
  50. data/rice/Identifier.cpp +8 -0
  51. data/rice/Identifier.hpp +50 -0
  52. data/rice/Identifier.ipp +33 -0
  53. data/rice/Jump_Tag.hpp +24 -0
  54. data/rice/Makefile.am +112 -0
  55. data/rice/Makefile.in +675 -0
  56. data/rice/Module.cpp +75 -0
  57. data/rice/Module.hpp +8 -0
  58. data/rice/Module.ipp +6 -0
  59. data/rice/Module_defn.hpp +87 -0
  60. data/rice/Module_impl.hpp +237 -0
  61. data/rice/Module_impl.ipp +302 -0
  62. data/rice/Object.cpp +153 -0
  63. data/rice/Object.hpp +8 -0
  64. data/rice/Object.ipp +19 -0
  65. data/rice/Object_defn.hpp +183 -0
  66. data/rice/Require_Guard.hpp +21 -0
  67. data/rice/String.cpp +93 -0
  68. data/rice/String.hpp +88 -0
  69. data/rice/Struct.cpp +117 -0
  70. data/rice/Struct.hpp +162 -0
  71. data/rice/Struct.ipp +26 -0
  72. data/rice/Symbol.cpp +25 -0
  73. data/rice/Symbol.hpp +66 -0
  74. data/rice/Symbol.ipp +44 -0
  75. data/rice/VM.cpp +79 -0
  76. data/rice/VM.hpp +27 -0
  77. data/rice/config.hpp +23 -0
  78. data/rice/config.hpp.in +22 -0
  79. data/rice/detail/Auto_Function_Wrapper.hpp +719 -0
  80. data/rice/detail/Auto_Function_Wrapper.ipp +1354 -0
  81. data/rice/detail/Auto_Member_Function_Wrapper.hpp +685 -0
  82. data/rice/detail/Auto_Member_Function_Wrapper.ipp +1435 -0
  83. data/rice/detail/Caster.hpp +61 -0
  84. data/rice/detail/Exception_Handler.hpp +118 -0
  85. data/rice/detail/Iterator_Definer.hpp +98 -0
  86. data/rice/detail/Not_Copyable.hpp +25 -0
  87. data/rice/detail/Wrapped_Function.hpp +33 -0
  88. data/rice/detail/check_ruby_type.cpp +21 -0
  89. data/rice/detail/check_ruby_type.hpp +23 -0
  90. data/rice/detail/creation_funcs.hpp +45 -0
  91. data/rice/detail/creation_funcs.ipp +62 -0
  92. data/rice/detail/default_allocation_func.hpp +23 -0
  93. data/rice/detail/default_allocation_func.ipp +11 -0
  94. data/rice/detail/define_method_and_auto_wrap.hpp +27 -0
  95. data/rice/detail/define_method_and_auto_wrap.ipp +20 -0
  96. data/rice/detail/env.hpp +13 -0
  97. data/rice/detail/from_ruby.hpp +43 -0
  98. data/rice/detail/from_ruby.ipp +74 -0
  99. data/rice/detail/method_data.cpp +105 -0
  100. data/rice/detail/method_data.hpp +33 -0
  101. data/rice/detail/node.hpp +13 -0
  102. data/rice/detail/object_call.hpp +85 -0
  103. data/rice/detail/object_call.ipp +147 -0
  104. data/rice/detail/protect.cpp +27 -0
  105. data/rice/detail/protect.hpp +34 -0
  106. data/rice/detail/remove_const.hpp +21 -0
  107. data/rice/detail/ruby.hpp +85 -0
  108. data/rice/detail/rubysig.hpp +13 -0
  109. data/rice/detail/st.hpp +56 -0
  110. data/rice/detail/to_ruby.hpp +16 -0
  111. data/rice/detail/to_ruby.ipp +10 -0
  112. data/rice/detail/win32.hpp +16 -0
  113. data/rice/detail/wrap_function.hpp +288 -0
  114. data/rice/detail/wrap_function.ipp +473 -0
  115. data/rice/generate_code.rb +1092 -0
  116. data/rice/global_function.hpp +16 -0
  117. data/rice/global_function.ipp +11 -0
  118. data/rice/protect.hpp +91 -0
  119. data/rice/protect.ipp +803 -0
  120. data/rice/ruby_try_catch.hpp +86 -0
  121. data/rice/to_from_ruby.hpp +8 -0
  122. data/rice/to_from_ruby.ipp +299 -0
  123. data/rice/to_from_ruby_defn.hpp +71 -0
  124. data/ruby.ac +105 -0
  125. data/ruby/Makefile.am +1 -0
  126. data/ruby/Makefile.in +493 -0
  127. data/ruby/lib/Makefile.am +3 -0
  128. data/ruby/lib/Makefile.in +369 -0
  129. data/ruby/lib/mkmf-rice.rb.in +199 -0
  130. data/sample/Makefile.am +47 -0
  131. data/sample/Makefile.in +375 -0
  132. data/sample/enum/extconf.rb +3 -0
  133. data/sample/enum/sample_enum.cpp +54 -0
  134. data/sample/enum/test.rb +8 -0
  135. data/sample/inheritance/animals.cpp +98 -0
  136. data/sample/inheritance/extconf.rb +3 -0
  137. data/sample/inheritance/test.rb +7 -0
  138. data/sample/map/extconf.rb +3 -0
  139. data/sample/map/map.cpp +81 -0
  140. data/sample/map/test.rb +7 -0
  141. data/test/Makefile.am +44 -0
  142. data/test/Makefile.in +575 -0
  143. data/test/test_Address_Registration_Guard.cpp +43 -0
  144. data/test/test_Allocation_Strategies.cpp +77 -0
  145. data/test/test_Array.cpp +241 -0
  146. data/test/test_Builtin_Object.cpp +72 -0
  147. data/test/test_Class.cpp +350 -0
  148. data/test/test_Constructor.cpp +30 -0
  149. data/test/test_Critical_Guard.cpp +47 -0
  150. data/test/test_Data_Object.cpp +235 -0
  151. data/test/test_Enum.cpp +162 -0
  152. data/test/test_Exception.cpp +46 -0
  153. data/test/test_Hash.cpp +195 -0
  154. data/test/test_Identifier.cpp +70 -0
  155. data/test/test_Jump_Tag.cpp +17 -0
  156. data/test/test_Module.cpp +253 -0
  157. data/test/test_Object.cpp +148 -0
  158. data/test/test_String.cpp +94 -0
  159. data/test/test_Struct.cpp +192 -0
  160. data/test/test_Symbol.cpp +63 -0
  161. data/test/test_To_From_Ruby.cpp +281 -0
  162. data/test/test_VM.cpp +26 -0
  163. data/test/test_rice.rb +30 -0
  164. data/test/unittest.cpp +136 -0
  165. data/test/unittest.hpp +292 -0
  166. metadata +209 -0
@@ -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,77 @@
1
+ #include "unittest.hpp"
2
+ #include "rice/Allocation_Strategies.hpp"
3
+
4
+ using namespace Rice;
5
+
6
+ namespace
7
+ {
8
+
9
+ bool constructor_called = false;
10
+ bool destructor_called = false;
11
+
12
+ class Foo
13
+ {
14
+ public:
15
+ Foo()
16
+ {
17
+ constructor_called = true;
18
+ }
19
+
20
+ ~Foo()
21
+ {
22
+ destructor_called = true;
23
+ }
24
+ };
25
+
26
+ } // namespace
27
+
28
+ TESTSUITE(Allocation_Strategies);
29
+
30
+ SETUP(Allocation_Strategies)
31
+ {
32
+ ruby_init();
33
+ constructor_called = false;
34
+ destructor_called = false;
35
+ }
36
+
37
+ TESTCASE(default_allocation_strategy_allocate)
38
+ {
39
+ Foo * t = Default_Allocation_Strategy<Foo>::allocate();
40
+ ASSERT(constructor_called);
41
+ ASSERT(!destructor_called);
42
+ delete t;
43
+ ASSERT(constructor_called);
44
+ ASSERT(destructor_called);
45
+ }
46
+
47
+ TESTCASE(default_allocation_strategy_free)
48
+ {
49
+ Foo * t = new Foo;
50
+ ASSERT(constructor_called);
51
+ ASSERT(!destructor_called);
52
+ Default_Allocation_Strategy<Foo>::free(t);
53
+ ASSERT(constructor_called);
54
+ ASSERT(destructor_called);
55
+ }
56
+
57
+ TESTCASE(xmalloc_allocation_strategy_allocate)
58
+ {
59
+ Foo * t = Xmalloc_Allocation_Strategy<Foo>::allocate();
60
+ ASSERT(constructor_called);
61
+ ASSERT(!destructor_called);
62
+ t->~Foo();
63
+ xfree(t);
64
+ ASSERT(constructor_called);
65
+ ASSERT(destructor_called);
66
+ }
67
+
68
+ TESTCASE(xmalloc_allocation_strategy_free)
69
+ {
70
+ Foo * t = new Foo;
71
+ ASSERT(constructor_called);
72
+ ASSERT(!destructor_called);
73
+ Xmalloc_Allocation_Strategy<Foo>::free(t);
74
+ ASSERT(constructor_called);
75
+ ASSERT(destructor_called);
76
+ }
77
+
@@ -0,0 +1,241 @@
1
+ #include "unittest.hpp"
2
+ #include "rice/Array.hpp"
3
+ #include "rice/String.hpp"
4
+
5
+ using namespace Rice;
6
+
7
+ TESTSUITE(Array);
8
+
9
+ SETUP(Array)
10
+ {
11
+ ruby_init();
12
+ }
13
+
14
+ TESTCASE(default_construct)
15
+ {
16
+ Array a;
17
+ ASSERT_EQUAL(T_ARRAY, rb_type(a));
18
+ ASSERT_EQUAL(0, RARRAY(a.value())->len);
19
+ }
20
+
21
+ TESTCASE(construct_from_vector_of_int)
22
+ {
23
+ std::vector<int> v;
24
+ v.push_back(10);
25
+ v.push_back(6);
26
+ v.push_back(42);
27
+ Array a(v.begin(), v.end());
28
+ ASSERT_EQUAL(3u, a.size());
29
+ ASSERT_EQUAL(to_ruby(10), a[0]);
30
+ ASSERT_EQUAL(to_ruby(6), a[1]);
31
+ ASSERT_EQUAL(to_ruby(42), a[2]);
32
+ }
33
+
34
+ TESTCASE(construct_from_c_array)
35
+ {
36
+ int arr[] = { 10, 6, 42 };
37
+ Array a(arr);
38
+ ASSERT_EQUAL(3u, a.size());
39
+ ASSERT_EQUAL(to_ruby(10), a[0]);
40
+ ASSERT_EQUAL(to_ruby(6), a[1]);
41
+ ASSERT_EQUAL(to_ruby(42), a[2]);
42
+ }
43
+
44
+
45
+ TESTCASE(push_no_items)
46
+ {
47
+ Array a;
48
+ ASSERT_EQUAL(0u, a.size());
49
+ }
50
+
51
+ TESTCASE(push_one_item)
52
+ {
53
+ Array a;
54
+ a.push(Rice::True);
55
+ ASSERT_EQUAL(1u, a.size());
56
+ ASSERT_EQUAL(Qtrue, a[0]);
57
+ }
58
+
59
+ TESTCASE(push_two_items)
60
+ {
61
+ Array a;
62
+ a.push(to_ruby(42));
63
+ a.push(to_ruby(43));
64
+ ASSERT_EQUAL(2u, a.size());
65
+ ASSERT_EQUAL(42, from_ruby<int>(a[0]));
66
+ ASSERT_EQUAL(43, from_ruby<int>(a[1]));
67
+ }
68
+
69
+ TESTCASE(push_three_items)
70
+ {
71
+ Array a;
72
+ a.push(to_ruby(42));
73
+ a.push(to_ruby(43));
74
+ a.push(to_ruby(44));
75
+ ASSERT_EQUAL(3u, a.size());
76
+ ASSERT_EQUAL(42, from_ruby<int>(a[0]));
77
+ ASSERT_EQUAL(43, from_ruby<int>(a[1]));
78
+ ASSERT_EQUAL(44, from_ruby<int>(a[2]));
79
+ }
80
+
81
+ TESTCASE(push_int)
82
+ {
83
+ Array a;
84
+ a.push(42);
85
+ ASSERT_EQUAL(1u, a.size());
86
+ ASSERT_EQUAL(to_ruby(42), a[0]);
87
+ }
88
+
89
+ TESTCASE(bracket_equals)
90
+ {
91
+ Array a;
92
+ a.push(to_ruby(42));
93
+ a.push(to_ruby(43));
94
+ a.push(to_ruby(44));
95
+ a[1] = to_ruby(10);
96
+ ASSERT_EQUAL(10, from_ruby<int>(a[1]));
97
+ }
98
+
99
+ TESTCASE(to_s)
100
+ {
101
+ Array a;
102
+ a.push(to_ruby(42));
103
+ a.push(to_ruby(43));
104
+ a.push(to_ruby(44));
105
+ String s1(a.call("to_s"));
106
+ String s2(a.to_s());
107
+ ASSERT_EQUAL(s1.str(), s2.str());
108
+ }
109
+
110
+ TESTCASE(pop)
111
+ {
112
+ Array a;
113
+ a.push(to_ruby(42));
114
+ a.push(to_ruby(43));
115
+ a.push(to_ruby(44));
116
+ VALUE result = a.pop();
117
+ ASSERT_EQUAL(2u, a.size());
118
+ ASSERT_EQUAL(42, from_ruby<int>(a[0]));
119
+ ASSERT_EQUAL(43, from_ruby<int>(a[1]));
120
+ ASSERT_EQUAL(44, from_ruby<int>(result));
121
+ }
122
+
123
+ TESTCASE(unshift)
124
+ {
125
+ Array a;
126
+ a.push(to_ruby(42));
127
+ a.push(to_ruby(43));
128
+ a.push(to_ruby(44));
129
+ a.unshift(to_ruby(10));
130
+ ASSERT_EQUAL(4u, a.size());
131
+ ASSERT_EQUAL(10, from_ruby<int>(a[0]));
132
+ ASSERT_EQUAL(42, from_ruby<int>(a[1]));
133
+ ASSERT_EQUAL(43, from_ruby<int>(a[2]));
134
+ ASSERT_EQUAL(44, from_ruby<int>(a[3]));
135
+ }
136
+
137
+ TESTCASE(unshift_int)
138
+ {
139
+ Array a;
140
+ a.unshift(42);
141
+ ASSERT_EQUAL(1u, a.size());
142
+ ASSERT_EQUAL(to_ruby(42), a[0]);
143
+ }
144
+
145
+ TESTCASE(shift)
146
+ {
147
+ Array a;
148
+ a.push(to_ruby(42));
149
+ a.push(to_ruby(43));
150
+ a.push(to_ruby(44));
151
+ VALUE result = a.shift();
152
+ ASSERT_EQUAL(2u, a.size());
153
+ ASSERT_EQUAL(42, from_ruby<int>(result));
154
+ ASSERT_EQUAL(43, from_ruby<int>(a[0]));
155
+ ASSERT_EQUAL(44, from_ruby<int>(a[1]));
156
+ }
157
+
158
+ TESTCASE(to_c_array)
159
+ {
160
+ Array a;
161
+ ASSERT_EQUAL(RARRAY(a.value())->ptr, a.to_c_array());
162
+ }
163
+
164
+ TESTCASE(iterate)
165
+ {
166
+ Array a;
167
+ a.push(to_ruby(42));
168
+ a.push(to_ruby(43));
169
+ a.push(to_ruby(44));
170
+ int ca[] = { 42, 43, 44 };
171
+ Array::iterator it = a.begin();
172
+ Array::iterator end = a.end();
173
+ for(int j = 0; it != end; ++j, ++it)
174
+ {
175
+ ASSERT_EQUAL(ca[j], from_ruby<int>(*it));
176
+ }
177
+ }
178
+
179
+ TESTCASE(const_iterate)
180
+ {
181
+ Array a;
182
+ a.push(to_ruby(42));
183
+ a.push(to_ruby(43));
184
+ a.push(to_ruby(44));
185
+ int ca[] = { 42, 43, 44 };
186
+ Array::const_iterator it = a.begin();
187
+ Array::const_iterator end = a.end();
188
+ for(int j = 0; it != end; ++j, ++it)
189
+ {
190
+ ASSERT_EQUAL(ca[j], from_ruby<int>(*it));
191
+ }
192
+ }
193
+
194
+ TESTCASE(iterate_and_change)
195
+ {
196
+ Array a;
197
+ a.push(to_ruby(42));
198
+ a.push(to_ruby(43));
199
+ a.push(to_ruby(44));
200
+ Array::iterator it = a.begin();
201
+ Array::iterator end = a.end();
202
+ for(int j = 0; it != end; ++j, ++it)
203
+ {
204
+ int value = from_ruby<int>(*it);
205
+ *it = to_ruby(value + j);
206
+ }
207
+ ASSERT_EQUAL(42, from_ruby<int>(a[0]));
208
+ ASSERT_EQUAL(44, from_ruby<int>(a[1]));
209
+ ASSERT_EQUAL(46, from_ruby<int>(a[2]));
210
+ }
211
+
212
+ TESTCASE(iterate_and_call_member)
213
+ {
214
+ Array a;
215
+ a.push(to_ruby(42));
216
+ a.push(to_ruby(43));
217
+ a.push(to_ruby(44));
218
+ Array::iterator it = a.begin();
219
+ Array::iterator end = a.end();
220
+ std::vector<Object> v;
221
+ for(int j = 0; it != end; ++j, ++it)
222
+ {
223
+ v.push_back(it->to_s());
224
+ }
225
+ ASSERT_EQUAL(42, from_ruby<int>(a[0]));
226
+ ASSERT_EQUAL(43, from_ruby<int>(a[1]));
227
+ ASSERT_EQUAL(44, from_ruby<int>(a[2]));
228
+ ASSERT_EQUAL(3u, v.size());
229
+ ASSERT_EQUAL(Object(a[0]).to_s(), v[0]);
230
+ ASSERT_EQUAL(Object(a[1]).to_s(), v[1]);
231
+ ASSERT_EQUAL(Object(a[2]).to_s(), v[2]);
232
+ }
233
+
234
+ TESTCASE(assign_int)
235
+ {
236
+ Array a;
237
+ a.push(42);
238
+ a[0] = 10;
239
+ ASSERT_EQUAL(10, from_ruby<int>(a[0]));
240
+ }
241
+
@@ -0,0 +1,72 @@
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<RObject, 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<RObject, T_OBJECT> b(o);
30
+ Builtin_Object<RObject, 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<RObject, 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<RObject, 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<RObject, 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<RObject, T_OBJECT> b1(o1);
66
+ Object o2(c.call("new"));
67
+ Builtin_Object<RObject, T_OBJECT> b2(o2);
68
+ b1.swap(b2);
69
+ ASSERT_EQUAL(b1.value(), o2.value());
70
+ ASSERT_EQUAL(b2.value(), o1.value());
71
+ }
72
+
@@ -0,0 +1,350 @@
1
+ #include "unittest.hpp"
2
+ #include "rice/Class.hpp"
3
+ #include "rice/protect.hpp"
4
+ #include "rice/Exception.hpp"
5
+ #include "rice/Array.hpp"
6
+ #include "rice/String.hpp"
7
+ #include "rice/Symbol.hpp"
8
+ #include <iostream>
9
+
10
+ using namespace Rice;
11
+
12
+ TESTSUITE(Class);
13
+
14
+ SETUP(Class)
15
+ {
16
+ ruby_init();
17
+ }
18
+
19
+ TESTCASE(construct)
20
+ {
21
+ Class c(rb_cObject);
22
+ ASSERT_EQUAL(rb_cObject, c.value());
23
+ }
24
+
25
+ TESTCASE(undef_creation_funcs)
26
+ {
27
+ Class c(anonymous_class());
28
+ Class & c2(c.undef_creation_funcs());
29
+ ASSERT_EQUAL(&c, &c2);
30
+ ASSERT_EXCEPTION_CHECK(
31
+ Exception,
32
+ c.call("new"),
33
+ ASSERT_EQUAL(
34
+ Object(rb_eTypeError), // TODO: 1.6.x?
35
+ Object(CLASS_OF(ex.value()))
36
+ )
37
+ );
38
+ }
39
+
40
+ TESTCASE(include_module)
41
+ {
42
+ Class c(anonymous_class());
43
+ Class & c2(c.include_module(rb_mEnumerable));
44
+ ASSERT_EQUAL(&c, &c2);
45
+ Array ancestors(c.ancestors());
46
+ Array expected_ancestors;
47
+ expected_ancestors.push(c);
48
+ expected_ancestors.push(Module(rb_mEnumerable));
49
+ expected_ancestors.push(Module(rb_cObject));
50
+ expected_ancestors.push(Module(rb_mKernel));
51
+ ASSERT_EQUAL(expected_ancestors, ancestors);
52
+ }
53
+
54
+ TESTCASE(const_set_get_by_id)
55
+ {
56
+ Class c(anonymous_class());
57
+ Object v = to_ruby(42);
58
+ Class & c2(c.const_set(rb_intern("FOO"), v));
59
+ ASSERT_EQUAL(&c, &c2);
60
+ ASSERT_EQUAL(v, c.const_get(rb_intern("FOO")));
61
+ }
62
+
63
+ TESTCASE(const_set_get_by_identifier)
64
+ {
65
+ Class c(anonymous_class());
66
+ Object v = to_ruby(42);
67
+ Class & c2(c.const_set(Identifier("FOO"), v));
68
+ ASSERT_EQUAL(&c, &c2);
69
+ ASSERT_EQUAL(v, c.const_get(Identifier("FOO")));
70
+ }
71
+
72
+ TESTCASE(const_set_get_by_string)
73
+ {
74
+ Class c(anonymous_class());
75
+ Object v = to_ruby(42);
76
+ Class & c2(c.const_set("FOO", v));
77
+ ASSERT_EQUAL(&c, &c2);
78
+ ASSERT_EQUAL(v, c.const_get("FOO"));
79
+ }
80
+
81
+ namespace
82
+ {
83
+
84
+ bool define_method_simple_ok;
85
+
86
+ void define_method_simple_helper(Object o)
87
+ {
88
+ define_method_simple_ok = true;
89
+ }
90
+
91
+ } // namespace
92
+
93
+ TESTCASE(define_method_simple)
94
+ {
95
+ Class c(anonymous_class());
96
+ c.define_method("foo", &define_method_simple_helper);
97
+ Object o = c.call("new");
98
+ define_method_simple_ok = false;
99
+ o.call("foo");
100
+ ASSERT(define_method_simple_ok);
101
+ }
102
+
103
+ TESTCASE(define_singleton_method_simple)
104
+ {
105
+ Class c(anonymous_class());
106
+ c.define_singleton_method("foo", &define_method_simple_helper);
107
+ define_method_simple_ok = false;
108
+ Object o = c.call("foo");
109
+ ASSERT(define_method_simple_ok);
110
+ }
111
+
112
+ TESTCASE(define_module_function_simple)
113
+ {
114
+ // module_function only works with Module, not Class
115
+ Class c(anonymous_class());
116
+ ASSERT_EXCEPTION_CHECK(
117
+ Exception,
118
+ c.define_module_function("foo", &define_method_simple_helper),
119
+ ASSERT_EQUAL(
120
+ Object(rb_eNoMethodError), // TODO: 1.6.x?
121
+ Object(CLASS_OF(ex.value()))
122
+ )
123
+ );
124
+ }
125
+
126
+ namespace
127
+ {
128
+
129
+ int define_method_int_result;
130
+
131
+ void define_method_int_helper(Object o, int i)
132
+ {
133
+ define_method_int_result = i;
134
+ }
135
+
136
+ } // namespace
137
+
138
+ TESTCASE(define_method_int)
139
+ {
140
+ Class c(anonymous_class());
141
+ c.define_method("foo", &define_method_int_helper);
142
+ Object o = c.call("new");
143
+ define_method_int_result = 0;
144
+ o.call("foo", 42);
145
+ ASSERT_EQUAL(42, define_method_int_result);
146
+ }
147
+
148
+ TESTCASE(define_method_int_passed_two_args)
149
+ {
150
+ Class c(anonymous_class());
151
+ c.define_method("foo", &define_method_int_helper);
152
+ Object o = c.call("new");
153
+ ASSERT_EXCEPTION_CHECK(
154
+ Exception,
155
+ o.call("foo", 1, 2),
156
+ ASSERT_EQUAL(
157
+ Object(rb_eArgError),
158
+ Object(CLASS_OF(ex.value()))
159
+ )
160
+ );
161
+ }
162
+
163
+ TESTCASE(define_method_int_passed_no_args)
164
+ {
165
+ Class c(anonymous_class());
166
+ c.define_method("foo", &define_method_int_helper);
167
+ Object o = c.call("new");
168
+ ASSERT_EXCEPTION_CHECK(
169
+ Exception,
170
+ o.call("foo"),
171
+ ASSERT_EQUAL(
172
+ Object(rb_eArgError),
173
+ Object(CLASS_OF(ex.value()))
174
+ )
175
+ );
176
+ }
177
+
178
+
179
+ namespace
180
+ {
181
+
182
+ struct Foo
183
+ {
184
+ int x;
185
+ };
186
+
187
+ int define_method_int_foo_result_i;
188
+ Foo * define_method_int_foo_result_x;
189
+
190
+ void define_method_int_foo_helper(Object o, int i, Foo * x)
191
+ {
192
+ define_method_int_foo_result_i = i;
193
+ define_method_int_foo_result_x = x;
194
+ }
195
+
196
+ } // namespace
197
+
198
+ template<>
199
+ Foo * from_ruby<Foo *>(Object x)
200
+ {
201
+ Foo * retval;
202
+ Data_Get_Struct(x.value(), Foo, retval);
203
+ return retval;
204
+ }
205
+
206
+ TESTCASE(define_method_int_foo)
207
+ {
208
+ Class c(anonymous_class());
209
+ c.define_method("foo", &define_method_int_foo_helper);
210
+ Object o = c.call("new");
211
+ define_method_int_result = 0;
212
+ Foo * foo = new Foo;
213
+ foo->x = 1024;
214
+ VALUE f = Data_Wrap_Struct(rb_cObject, 0, Default_Allocation_Strategy<Foo>::free, foo);
215
+ o.call("foo", 42, Object(f));
216
+ ASSERT_EQUAL(42, define_method_int_foo_result_i);
217
+ ASSERT_EQUAL(foo, define_method_int_foo_result_x);
218
+ }
219
+
220
+ namespace
221
+ {
222
+
223
+ class Silly_Exception
224
+ : public std::exception
225
+ {
226
+ };
227
+
228
+ void handle_silly_exception(Silly_Exception const & ex)
229
+ {
230
+ throw Exception(rb_eRuntimeError, "SILLY");
231
+ }
232
+
233
+ void throw_silly_exception(Object self)
234
+ {
235
+ throw Silly_Exception();
236
+ }
237
+
238
+ }
239
+
240
+ TESTCASE(add_handler)
241
+ {
242
+ Class c(rb_cObject);
243
+ c.add_handler<Silly_Exception>(handle_silly_exception);
244
+ c.define_method("foo", throw_silly_exception);
245
+ Object exc = protect(rb_eval_string, "begin; foo; rescue Exception; $!; end");
246
+ ASSERT_EQUAL(rb_eRuntimeError, CLASS_OF(exc));
247
+ Exception ex(exc);
248
+ ASSERT_EQUAL(String("SILLY"), String(ex.message()));
249
+ }
250
+
251
+ namespace
252
+ {
253
+
254
+ class Container
255
+ {
256
+ public:
257
+ Container(int * array, size_t length)
258
+ : array_(array)
259
+ , length_(length)
260
+ {
261
+ }
262
+
263
+ int * begin() { return array_; }
264
+ int * end() { return array_ + length_; }
265
+
266
+ private:
267
+ int * array_;
268
+ size_t length_;
269
+ };
270
+
271
+ } // namespace
272
+
273
+ template<>
274
+ Container * from_ruby<Container *>(Object x)
275
+ {
276
+ Container * retval;
277
+ Data_Get_Struct(x.value(), Container, retval);
278
+ return retval;
279
+ }
280
+
281
+ TESTCASE(define_iterator)
282
+ {
283
+ int array[] = { 1, 2, 3 };
284
+ Class c(anonymous_class());
285
+ c.define_iterator(&Container::begin, &Container::end);
286
+ Container * container = new Container(array, 3);
287
+ Object wrapped_container = Data_Wrap_Struct(
288
+ c, 0, Default_Allocation_Strategy<Container>::free, container);
289
+ Array a = wrapped_container.instance_eval("a = []; each() { |x| a << x }; a");
290
+ ASSERT_EQUAL(3u, a.size());
291
+ ASSERT_EQUAL(to_ruby(1), Object(a[0]));
292
+ ASSERT_EQUAL(to_ruby(2), Object(a[1]));
293
+ ASSERT_EQUAL(to_ruby(3), Object(a[2]));
294
+ }
295
+
296
+ TESTCASE(define_class)
297
+ {
298
+ Class object(rb_cObject);
299
+ if(object.const_defined("Foo"))
300
+ {
301
+ object.remove_const("Foo");
302
+ }
303
+
304
+ Class c = define_class("Foo");
305
+
306
+ ASSERT(c.is_a(rb_cClass));
307
+ ASSERT_EQUAL(c, object.const_get("Foo"));
308
+ }
309
+
310
+ TESTCASE(define_class_under)
311
+ {
312
+ Class object(rb_cObject);
313
+ if(object.const_defined("Foo"))
314
+ {
315
+ object.remove_const("Foo");
316
+ }
317
+
318
+ Module math(rb_mMath);
319
+ if(math.const_defined("Foo"))
320
+ {
321
+ math.remove_const("Foo");
322
+ }
323
+
324
+ Class c = define_class_under(math, "Foo");
325
+
326
+ ASSERT(c.is_a(rb_cClass));
327
+ ASSERT_EQUAL(c, math.const_get("Foo"));
328
+ ASSERT(!object.const_defined("Foo"));
329
+ }
330
+
331
+ TESTCASE(module_define_class)
332
+ {
333
+ Class object(rb_cObject);
334
+ if(object.const_defined("Foo"))
335
+ {
336
+ object.remove_const("Foo");
337
+ }
338
+
339
+ Module math(rb_mMath);
340
+ if(math.const_defined("Foo"))
341
+ {
342
+ math.remove_const("Foo");
343
+ }
344
+
345
+ Class c = math.define_class("Foo");
346
+
347
+ ASSERT(c.is_a(rb_cClass));
348
+ ASSERT_EQUAL(c, math.const_get("Foo"));
349
+ ASSERT(!object.const_defined("Foo"));
350
+ }