rice2 2.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/COPYING +23 -0
- data/Doxyfile +2268 -0
- data/Makefile.am +26 -0
- data/Makefile.in +929 -0
- data/README.md +1054 -0
- data/README.mingw +8 -0
- data/Rakefile +24 -0
- data/aclocal.m4 +1090 -0
- data/bootstrap +8 -0
- data/config.guess +1667 -0
- data/config.sub +1793 -0
- data/configure +8209 -0
- data/configure.ac +55 -0
- data/depcomp +791 -0
- data/doxygen.ac +314 -0
- data/doxygen.am +186 -0
- data/extconf.rb +66 -0
- data/install-sh +529 -0
- data/missing +215 -0
- data/post-autoconf.rb +22 -0
- data/post-automake.rb +28 -0
- data/rice/Address_Registration_Guard.cpp +22 -0
- data/rice/Address_Registration_Guard.hpp +7 -0
- data/rice/Address_Registration_Guard.ipp +37 -0
- data/rice/Address_Registration_Guard_defn.hpp +75 -0
- data/rice/Arg.hpp +8 -0
- data/rice/Arg_impl.hpp +129 -0
- data/rice/Arg_operators.cpp +21 -0
- data/rice/Arg_operators.hpp +19 -0
- data/rice/Array.hpp +214 -0
- data/rice/Array.ipp +256 -0
- data/rice/Builtin_Object.hpp +8 -0
- data/rice/Builtin_Object.ipp +50 -0
- data/rice/Builtin_Object_defn.hpp +50 -0
- data/rice/Class.cpp +57 -0
- data/rice/Class.hpp +8 -0
- data/rice/Class.ipp +6 -0
- data/rice/Class_defn.hpp +83 -0
- data/rice/Constructor.hpp +47 -0
- data/rice/Data_Object.hpp +8 -0
- data/rice/Data_Object.ipp +133 -0
- data/rice/Data_Object_defn.hpp +138 -0
- data/rice/Data_Type.cpp +54 -0
- data/rice/Data_Type.hpp +8 -0
- data/rice/Data_Type.ipp +365 -0
- data/rice/Data_Type_defn.hpp +261 -0
- data/rice/Data_Type_fwd.hpp +12 -0
- data/rice/Director.cpp +13 -0
- data/rice/Director.hpp +39 -0
- data/rice/Enum.hpp +117 -0
- data/rice/Enum.ipp +246 -0
- data/rice/Exception.cpp +59 -0
- data/rice/Exception.hpp +13 -0
- data/rice/Exception_Base.hpp +8 -0
- data/rice/Exception_Base.ipp +13 -0
- data/rice/Exception_Base_defn.hpp +27 -0
- data/rice/Exception_defn.hpp +69 -0
- data/rice/Hash.hpp +227 -0
- data/rice/Hash.ipp +329 -0
- data/rice/Identifier.cpp +8 -0
- data/rice/Identifier.hpp +50 -0
- data/rice/Identifier.ipp +33 -0
- data/rice/Jump_Tag.hpp +24 -0
- data/rice/Makefile.am +122 -0
- data/rice/Makefile.in +885 -0
- data/rice/Module.cpp +84 -0
- data/rice/Module.hpp +8 -0
- data/rice/Module.ipp +6 -0
- data/rice/Module_defn.hpp +88 -0
- data/rice/Module_impl.hpp +281 -0
- data/rice/Module_impl.ipp +345 -0
- data/rice/Object.cpp +169 -0
- data/rice/Object.hpp +8 -0
- data/rice/Object.ipp +33 -0
- data/rice/Object_defn.hpp +214 -0
- data/rice/Require_Guard.hpp +21 -0
- data/rice/String.cpp +94 -0
- data/rice/String.hpp +91 -0
- data/rice/Struct.cpp +117 -0
- data/rice/Struct.hpp +162 -0
- data/rice/Struct.ipp +26 -0
- data/rice/Symbol.cpp +25 -0
- data/rice/Symbol.hpp +66 -0
- data/rice/Symbol.ipp +44 -0
- data/rice/config.hpp +47 -0
- data/rice/config.hpp.in +46 -0
- data/rice/detail/Arguments.hpp +118 -0
- data/rice/detail/Auto_Function_Wrapper.hpp +898 -0
- data/rice/detail/Auto_Function_Wrapper.ipp +3694 -0
- data/rice/detail/Auto_Member_Function_Wrapper.hpp +897 -0
- data/rice/detail/Auto_Member_Function_Wrapper.ipp +2774 -0
- data/rice/detail/Caster.hpp +103 -0
- data/rice/detail/Exception_Handler.hpp +8 -0
- data/rice/detail/Exception_Handler.ipp +68 -0
- data/rice/detail/Exception_Handler_defn.hpp +96 -0
- data/rice/detail/Iterator.hpp +93 -0
- data/rice/detail/Not_Copyable.hpp +25 -0
- data/rice/detail/Wrapped_Function.hpp +33 -0
- data/rice/detail/cfp.hpp +24 -0
- data/rice/detail/cfp.ipp +51 -0
- data/rice/detail/check_ruby_type.cpp +27 -0
- data/rice/detail/check_ruby_type.hpp +23 -0
- data/rice/detail/creation_funcs.hpp +37 -0
- data/rice/detail/creation_funcs.ipp +36 -0
- data/rice/detail/default_allocation_func.hpp +23 -0
- data/rice/detail/default_allocation_func.ipp +11 -0
- data/rice/detail/define_method_and_auto_wrap.hpp +31 -0
- data/rice/detail/define_method_and_auto_wrap.ipp +30 -0
- data/rice/detail/demangle.cpp +56 -0
- data/rice/detail/demangle.hpp +19 -0
- data/rice/detail/env.hpp +11 -0
- data/rice/detail/from_ruby.hpp +43 -0
- data/rice/detail/from_ruby.ipp +60 -0
- data/rice/detail/method_data.cpp +92 -0
- data/rice/detail/method_data.hpp +21 -0
- data/rice/detail/node.hpp +13 -0
- data/rice/detail/protect.cpp +29 -0
- data/rice/detail/protect.hpp +34 -0
- data/rice/detail/ruby.hpp +74 -0
- data/rice/detail/ruby_version_code.hpp +6 -0
- data/rice/detail/ruby_version_code.hpp.in +6 -0
- data/rice/detail/st.hpp +22 -0
- data/rice/detail/to_ruby.hpp +22 -0
- data/rice/detail/to_ruby.ipp +36 -0
- data/rice/detail/traits.hpp +43 -0
- data/rice/detail/win32.hpp +16 -0
- data/rice/detail/wrap_function.hpp +66 -0
- data/rice/global_function.hpp +33 -0
- data/rice/global_function.ipp +22 -0
- data/rice/protect.hpp +38 -0
- data/rice/protect.ipp +1134 -0
- data/rice/ruby_mark.hpp +13 -0
- data/rice/ruby_try_catch.hpp +86 -0
- data/rice/rubypp.rb +97 -0
- data/rice/to_from_ruby.hpp +8 -0
- data/rice/to_from_ruby.ipp +418 -0
- data/rice/to_from_ruby_defn.hpp +70 -0
- data/ruby.ac +135 -0
- data/ruby/Makefile.am +1 -0
- data/ruby/Makefile.in +628 -0
- data/ruby/lib/Makefile.am +3 -0
- data/ruby/lib/Makefile.in +506 -0
- data/ruby/lib/mkmf-rice.rb.in +217 -0
- data/ruby/lib/version.rb +3 -0
- data/sample/Makefile.am +47 -0
- data/sample/Makefile.in +489 -0
- data/sample/enum/extconf.rb +3 -0
- data/sample/enum/sample_enum.cpp +54 -0
- data/sample/enum/test.rb +8 -0
- data/sample/inheritance/animals.cpp +98 -0
- data/sample/inheritance/extconf.rb +3 -0
- data/sample/inheritance/test.rb +7 -0
- data/sample/map/extconf.rb +3 -0
- data/sample/map/map.cpp +81 -0
- data/sample/map/test.rb +7 -0
- data/test/Makefile.am +72 -0
- data/test/Makefile.in +1213 -0
- data/test/ext/Makefile.am +41 -0
- data/test/ext/Makefile.in +483 -0
- data/test/ext/t1/Foo.hpp +10 -0
- data/test/ext/t1/extconf.rb +2 -0
- data/test/ext/t1/t1.cpp +15 -0
- data/test/ext/t2/extconf.rb +2 -0
- data/test/ext/t2/t2.cpp +11 -0
- data/test/test_Address_Registration_Guard.cpp +43 -0
- data/test/test_Array.cpp +248 -0
- data/test/test_Builtin_Object.cpp +71 -0
- data/test/test_Class.cpp +496 -0
- data/test/test_Constructor.cpp +128 -0
- data/test/test_Data_Object.cpp +275 -0
- data/test/test_Data_Type.cpp +348 -0
- data/test/test_Director.cpp +308 -0
- data/test/test_Enum.cpp +215 -0
- data/test/test_Exception.cpp +47 -0
- data/test/test_Hash.cpp +212 -0
- data/test/test_Identifier.cpp +70 -0
- data/test/test_Jump_Tag.cpp +17 -0
- data/test/test_Memory_Management.cpp +50 -0
- data/test/test_Module.cpp +497 -0
- data/test/test_Object.cpp +159 -0
- data/test/test_String.cpp +107 -0
- data/test/test_Struct.cpp +205 -0
- data/test/test_Symbol.cpp +63 -0
- data/test/test_To_From_Ruby.cpp +428 -0
- data/test/test_global_functions.cpp +114 -0
- data/test/test_rice.rb +41 -0
- data/test/unittest.cpp +136 -0
- data/test/unittest.hpp +294 -0
- metadata +297 -0
data/test/ext/t1/Foo.hpp
ADDED
data/test/ext/t1/t1.cpp
ADDED
@@ -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
|
+
|
data/test/ext/t2/t2.cpp
ADDED
@@ -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
|
+
|
data/test/test_Array.cpp
ADDED
@@ -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
|
+
}
|
data/test/test_Class.cpp
ADDED
@@ -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
|
+
*/
|