wurlinc-rice 1.4.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/COPYING +23 -0
- data/Doxyfile +1253 -0
- data/Makefile.am +26 -0
- data/README +1119 -0
- data/README.mingw +8 -0
- data/Rakefile +33 -0
- data/bootstrap +9 -0
- data/configure.ac +52 -0
- data/doxygen.ac +314 -0
- data/doxygen.am +186 -0
- data/extconf.rb +41 -0
- data/post-autoconf.rb +22 -0
- data/post-automake.rb +28 -0
- data/rice/Address_Registration_Guard.hpp +7 -0
- data/rice/Address_Registration_Guard.ipp +34 -0
- data/rice/Address_Registration_Guard_defn.hpp +65 -0
- data/rice/Allocation_Strategies.hpp +37 -0
- data/rice/Arg.hpp +8 -0
- data/rice/Arg_impl.hpp +127 -0
- data/rice/Arg_operators.cpp +21 -0
- data/rice/Arg_operators.hpp +19 -0
- data/rice/Array.hpp +220 -0
- data/rice/Array.ipp +263 -0
- data/rice/Builtin_Object.hpp +8 -0
- data/rice/Builtin_Object.ipp +50 -0
- data/rice/Builtin_Object_defn.hpp +51 -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 +367 -0
- data/rice/Critical_Guard.hpp +40 -0
- data/rice/Critical_Guard.ipp +26 -0
- data/rice/Data_Object.hpp +8 -0
- data/rice/Data_Object.ipp +133 -0
- data/rice/Data_Object_defn.hpp +132 -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 +9 -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 +210 -0
- data/rice/Hash.ipp +338 -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 +129 -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 +348 -0
- data/rice/Object.cpp +160 -0
- data/rice/Object.hpp +8 -0
- data/rice/Object.ipp +19 -0
- data/rice/Object_defn.hpp +191 -0
- data/rice/Require_Guard.hpp +21 -0
- data/rice/String.cpp +94 -0
- data/rice/String.hpp +89 -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/VM.cpp +92 -0
- data/rice/VM.hpp +32 -0
- data/rice/config.hpp.in +40 -0
- data/rice/detail/Arguments.hpp +118 -0
- data/rice/detail/Auto_Function_Wrapper.hpp +829 -0
- data/rice/detail/Auto_Function_Wrapper.ipp +3391 -0
- data/rice/detail/Auto_Member_Function_Wrapper.hpp +828 -0
- data/rice/detail/Auto_Member_Function_Wrapper.ipp +2503 -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 +19 -0
- data/rice/detail/from_ruby.hpp +43 -0
- data/rice/detail/from_ruby.ipp +60 -0
- data/rice/detail/method_data.cpp +159 -0
- data/rice/detail/method_data.hpp +21 -0
- data/rice/detail/mininode.cpp +1220 -0
- data/rice/detail/mininode.hpp +320 -0
- data/rice/detail/node.hpp +13 -0
- data/rice/detail/object_call.hpp +68 -0
- data/rice/detail/object_call.ipp +131 -0
- data/rice/detail/protect.cpp +29 -0
- data/rice/detail/protect.hpp +34 -0
- data/rice/detail/ruby.hpp +93 -0
- data/rice/detail/ruby_version_code.hpp.in +6 -0
- data/rice/detail/rubysig.hpp +19 -0
- data/rice/detail/st.hpp +60 -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 +341 -0
- data/rice/detail/wrap_function.ipp +514 -0
- data/rice/global_function.hpp +33 -0
- data/rice/global_function.ipp +22 -0
- data/rice/protect.hpp +91 -0
- data/rice/protect.ipp +1133 -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 +297 -0
- data/rice/to_from_ruby_defn.hpp +71 -0
- data/ruby/Makefile.am +1 -0
- data/ruby/lib/Makefile.am +3 -0
- data/ruby/lib/mkmf-rice.rb.in +216 -0
- data/ruby/lib/version.rb +3 -0
- data/ruby.ac +136 -0
- data/sample/Makefile.am +47 -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 +78 -0
- data/test/ext/Makefile.am +43 -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_Allocation_Strategies.cpp +77 -0
- data/test/test_Array.cpp +241 -0
- data/test/test_Builtin_Object.cpp +72 -0
- data/test/test_Class.cpp +498 -0
- data/test/test_Constructor.cpp +128 -0
- data/test/test_Critical_Guard.cpp +51 -0
- data/test/test_Data_Object.cpp +275 -0
- data/test/test_Data_Type.cpp +348 -0
- data/test/test_Director.cpp +301 -0
- data/test/test_Enum.cpp +195 -0
- data/test/test_Exception.cpp +46 -0
- data/test/test_Hash.cpp +195 -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 +481 -0
- data/test/test_Object.cpp +148 -0
- data/test/test_String.cpp +94 -0
- data/test/test_Struct.cpp +192 -0
- data/test/test_Symbol.cpp +63 -0
- data/test/test_To_From_Ruby.cpp +263 -0
- data/test/test_VM.cpp +26 -0
- data/test/test_global_functions.cpp +114 -0
- data/test/test_rice.rb +43 -0
- data/test/unittest.cpp +136 -0
- data/test/unittest.hpp +292 -0
- metadata +276 -0
@@ -0,0 +1,481 @@
|
|
1
|
+
#include "unittest.hpp"
|
2
|
+
#include "rice/Module.hpp"
|
3
|
+
#include "rice/Exception.hpp"
|
4
|
+
#include "rice/Array.hpp"
|
5
|
+
#include "rice/Arg.hpp"
|
6
|
+
#include "rice/global_function.hpp"
|
7
|
+
#include "rice/Constructor.hpp"
|
8
|
+
|
9
|
+
using namespace Rice;
|
10
|
+
|
11
|
+
TESTSUITE(Module);
|
12
|
+
|
13
|
+
SETUP(Object)
|
14
|
+
{
|
15
|
+
ruby_init();
|
16
|
+
}
|
17
|
+
|
18
|
+
TESTCASE(construct_from_value)
|
19
|
+
{
|
20
|
+
Module m(rb_mEnumerable);
|
21
|
+
ASSERT_EQUAL(rb_mEnumerable, m.value());
|
22
|
+
}
|
23
|
+
|
24
|
+
namespace
|
25
|
+
{
|
26
|
+
|
27
|
+
class Quite_Silly_Exception
|
28
|
+
: public std::exception
|
29
|
+
{
|
30
|
+
};
|
31
|
+
|
32
|
+
void handle_silly_exception(Quite_Silly_Exception const & ex)
|
33
|
+
{
|
34
|
+
throw Exception(rb_eRuntimeError, "SILLY");
|
35
|
+
}
|
36
|
+
|
37
|
+
void throw_silly_exception(Object self)
|
38
|
+
{
|
39
|
+
throw Quite_Silly_Exception();
|
40
|
+
}
|
41
|
+
|
42
|
+
} // namespace
|
43
|
+
|
44
|
+
TESTCASE(add_handler)
|
45
|
+
{
|
46
|
+
Module m(anonymous_module());
|
47
|
+
m.add_handler<Quite_Silly_Exception>(handle_silly_exception);
|
48
|
+
m.define_singleton_method("foo", throw_silly_exception);
|
49
|
+
Object exc = m.instance_eval("begin; foo; rescue Exception; $!; end");
|
50
|
+
ASSERT_EQUAL(rb_eRuntimeError, CLASS_OF(exc));
|
51
|
+
Exception ex(exc);
|
52
|
+
ASSERT_EQUAL(String("SILLY"), String(ex.message()));
|
53
|
+
}
|
54
|
+
|
55
|
+
namespace
|
56
|
+
{
|
57
|
+
|
58
|
+
bool define_method_simple_ok;
|
59
|
+
|
60
|
+
void define_method_simple_helper(Object o)
|
61
|
+
{
|
62
|
+
define_method_simple_ok = true;
|
63
|
+
}
|
64
|
+
|
65
|
+
} // namespace
|
66
|
+
|
67
|
+
TESTCASE(define_method_simple)
|
68
|
+
{
|
69
|
+
Module m(anonymous_module());
|
70
|
+
m.define_method("foo", define_method_simple_helper);
|
71
|
+
define_method_simple_ok = false;
|
72
|
+
Object o = m.instance_eval("o = Object.new; o.extend(self); o.foo");
|
73
|
+
ASSERT(define_method_simple_ok);
|
74
|
+
}
|
75
|
+
|
76
|
+
TESTCASE(define_singleton_method_simple)
|
77
|
+
{
|
78
|
+
Module m(anonymous_module());
|
79
|
+
m.define_singleton_method("foo", define_method_simple_helper);
|
80
|
+
define_method_simple_ok = false;
|
81
|
+
Object o = m.call("foo");
|
82
|
+
ASSERT(define_method_simple_ok);
|
83
|
+
}
|
84
|
+
|
85
|
+
TESTCASE(define_module_function_simple)
|
86
|
+
{
|
87
|
+
Module m(anonymous_module());
|
88
|
+
m.define_module_function("foo", define_method_simple_helper);
|
89
|
+
define_method_simple_ok = false;
|
90
|
+
m.instance_eval("o = Object.new; o.extend(self); o.instance_eval { foo }");
|
91
|
+
ASSERT(define_method_simple_ok);
|
92
|
+
define_method_simple_ok = false;
|
93
|
+
m.call("foo");
|
94
|
+
ASSERT(define_method_simple_ok);
|
95
|
+
}
|
96
|
+
|
97
|
+
namespace
|
98
|
+
{
|
99
|
+
|
100
|
+
int define_method_int_result;
|
101
|
+
|
102
|
+
void define_method_int_helper(Object o, int i)
|
103
|
+
{
|
104
|
+
define_method_int_result = i;
|
105
|
+
}
|
106
|
+
|
107
|
+
} // namespace
|
108
|
+
|
109
|
+
TESTCASE(define_method_int)
|
110
|
+
{
|
111
|
+
Module m(anonymous_module());
|
112
|
+
m.define_method("foo", define_method_int_helper);
|
113
|
+
define_method_int_result = 0;
|
114
|
+
Object o = m.instance_eval("o = Object.new; o.extend(self); o.foo(42)");
|
115
|
+
ASSERT_EQUAL(42, define_method_int_result);
|
116
|
+
}
|
117
|
+
|
118
|
+
TESTCASE(define_singleton_method_int)
|
119
|
+
{
|
120
|
+
Module m(anonymous_module());
|
121
|
+
m.define_singleton_method("foo", define_method_int_helper);
|
122
|
+
define_method_int_result = 0;
|
123
|
+
Object o = m.call("foo", 42);
|
124
|
+
ASSERT_EQUAL(42, define_method_int_result);
|
125
|
+
}
|
126
|
+
|
127
|
+
TESTCASE(define_module_function_int)
|
128
|
+
{
|
129
|
+
Module m(anonymous_module());
|
130
|
+
m.define_module_function("foo", define_method_int_helper);
|
131
|
+
define_method_int_result = 0;
|
132
|
+
m.instance_eval("o = Object.new; o.extend(self); o.instance_eval { foo(42) }");
|
133
|
+
ASSERT_EQUAL(42, define_method_int_result);
|
134
|
+
define_method_int_result = 0;
|
135
|
+
m.call("foo", 42);
|
136
|
+
ASSERT_EQUAL(42, define_method_int_result);
|
137
|
+
}
|
138
|
+
|
139
|
+
TESTCASE(define_method_int_passed_no_args)
|
140
|
+
{
|
141
|
+
Module m(anonymous_module());
|
142
|
+
m.define_method("foo", define_method_int_helper);
|
143
|
+
ASSERT_EXCEPTION_CHECK(
|
144
|
+
Exception,
|
145
|
+
m.instance_eval("o = Object.new; o.extend(self); o.foo"),
|
146
|
+
ASSERT_EQUAL(
|
147
|
+
Object(rb_eArgError),
|
148
|
+
Object(CLASS_OF(ex.value()))
|
149
|
+
)
|
150
|
+
);
|
151
|
+
}
|
152
|
+
|
153
|
+
namespace
|
154
|
+
{
|
155
|
+
|
156
|
+
struct Foo
|
157
|
+
{
|
158
|
+
int x;
|
159
|
+
};
|
160
|
+
|
161
|
+
int define_method_int_foo_result_i;
|
162
|
+
Foo * define_method_int_foo_result_x;
|
163
|
+
|
164
|
+
void define_method_int_foo_helper(Object o, int i, Foo * x)
|
165
|
+
{
|
166
|
+
define_method_int_foo_result_i = i;
|
167
|
+
define_method_int_foo_result_x = x;
|
168
|
+
}
|
169
|
+
|
170
|
+
} // namespace
|
171
|
+
|
172
|
+
template<>
|
173
|
+
Foo * from_ruby<Foo *>(Object x)
|
174
|
+
{
|
175
|
+
Foo * retval;
|
176
|
+
Data_Get_Struct(x.value(), Foo, retval);
|
177
|
+
return retval;
|
178
|
+
}
|
179
|
+
|
180
|
+
TESTCASE(define_singleton_method_int_foo)
|
181
|
+
{
|
182
|
+
Module m(anonymous_module());
|
183
|
+
m.define_singleton_method("int_and_foo", define_method_int_foo_helper);
|
184
|
+
define_method_int_result = 0;
|
185
|
+
Foo * foo = new Foo;
|
186
|
+
foo->x = 1024;
|
187
|
+
VALUE f = Data_Wrap_Struct(rb_cObject, 0, Default_Allocation_Strategy<Foo>::free, foo);
|
188
|
+
m.call("int_and_foo", 42, Object(f));
|
189
|
+
ASSERT_EQUAL(42, define_method_int_foo_result_i);
|
190
|
+
ASSERT_EQUAL(foo, define_method_int_foo_result_x);
|
191
|
+
}
|
192
|
+
|
193
|
+
// TODO: how to test define_iterator?
|
194
|
+
|
195
|
+
TESTCASE(include_module)
|
196
|
+
{
|
197
|
+
Module m(anonymous_module());
|
198
|
+
Module & m2(m.include_module(rb_mEnumerable));
|
199
|
+
ASSERT_EQUAL(&m, &m2);
|
200
|
+
Array ancestors(m.ancestors());
|
201
|
+
Array expected_ancestors;
|
202
|
+
expected_ancestors.push(m);
|
203
|
+
expected_ancestors.push(Module(rb_mEnumerable));
|
204
|
+
ASSERT_EQUAL(expected_ancestors, ancestors);
|
205
|
+
}
|
206
|
+
|
207
|
+
TESTCASE(const_set_get_by_id)
|
208
|
+
{
|
209
|
+
Module m(anonymous_module());
|
210
|
+
Object v = to_ruby(42);
|
211
|
+
Module & m2(m.const_set(rb_intern("FOO"), v));
|
212
|
+
ASSERT_EQUAL(&m, &m2);
|
213
|
+
ASSERT_EQUAL(v, m.const_get(rb_intern("FOO")));
|
214
|
+
}
|
215
|
+
|
216
|
+
TESTCASE(const_set_get_by_identifier)
|
217
|
+
{
|
218
|
+
Module m(anonymous_module());
|
219
|
+
Object v = to_ruby(42);
|
220
|
+
Module & m2(m.const_set(Identifier("FOO"), v));
|
221
|
+
ASSERT_EQUAL(&m, &m2);
|
222
|
+
ASSERT_EQUAL(v, m.const_get(Identifier("FOO")));
|
223
|
+
}
|
224
|
+
|
225
|
+
TESTCASE(const_set_get_by_string)
|
226
|
+
{
|
227
|
+
Module m(anonymous_module());
|
228
|
+
Object v = to_ruby(42);
|
229
|
+
Module & m2(m.const_set("FOO", v));
|
230
|
+
ASSERT_EQUAL(&m, &m2);
|
231
|
+
ASSERT_EQUAL(v, m.const_get("FOO"));
|
232
|
+
}
|
233
|
+
|
234
|
+
TESTCASE(remove_const)
|
235
|
+
{
|
236
|
+
Module m(anonymous_module());
|
237
|
+
Object v = to_ruby(42);
|
238
|
+
m.const_set("FOO", v);
|
239
|
+
ASSERT_EQUAL(v, m.const_get("FOO"));
|
240
|
+
m.remove_const("FOO");
|
241
|
+
ASSERT_EXCEPTION_CHECK(
|
242
|
+
Exception,
|
243
|
+
m.const_get("FOO"),
|
244
|
+
ASSERT_EQUAL(
|
245
|
+
Object(rb_eNameError),
|
246
|
+
Object(CLASS_OF(ex.value()))
|
247
|
+
)
|
248
|
+
);
|
249
|
+
}
|
250
|
+
|
251
|
+
TESTCASE(mod_name_anonymous)
|
252
|
+
{
|
253
|
+
Module m(anonymous_module());
|
254
|
+
ASSERT_EQUAL(String(""), m.name());
|
255
|
+
}
|
256
|
+
|
257
|
+
/**
|
258
|
+
* Tests for default arguments
|
259
|
+
*/
|
260
|
+
namespace
|
261
|
+
{
|
262
|
+
int defaults_method_one_arg1;
|
263
|
+
int defaults_method_one_arg2;
|
264
|
+
bool defaults_method_one_arg3 = false;
|
265
|
+
|
266
|
+
void defaults_method_one(int arg1, int arg2 = 3, bool arg3 = true)
|
267
|
+
{
|
268
|
+
defaults_method_one_arg1 = arg1;
|
269
|
+
defaults_method_one_arg2 = arg2;
|
270
|
+
defaults_method_one_arg3 = arg3;
|
271
|
+
}
|
272
|
+
}
|
273
|
+
|
274
|
+
TESTCASE(define_method_default_arguments)
|
275
|
+
{
|
276
|
+
Module m(anonymous_module());
|
277
|
+
m.define_method("foo", &defaults_method_one, (Arg("arg1"), Arg("arg2") = 3, Arg("arg3") = true));
|
278
|
+
|
279
|
+
Object o = m.instance_eval("o = Object.new; o.extend(self); o");
|
280
|
+
o.call("foo", 2);
|
281
|
+
|
282
|
+
ASSERT_EQUAL(2, defaults_method_one_arg1);
|
283
|
+
ASSERT_EQUAL(3, defaults_method_one_arg2);
|
284
|
+
ASSERT(defaults_method_one_arg3);
|
285
|
+
|
286
|
+
o.call("foo", 11, 10);
|
287
|
+
|
288
|
+
ASSERT_EQUAL(11, defaults_method_one_arg1);
|
289
|
+
ASSERT_EQUAL(10, defaults_method_one_arg2);
|
290
|
+
ASSERT(defaults_method_one_arg3);
|
291
|
+
|
292
|
+
o.call("foo", 22, 33, false);
|
293
|
+
|
294
|
+
ASSERT_EQUAL(22, defaults_method_one_arg1);
|
295
|
+
ASSERT_EQUAL(33, defaults_method_one_arg2);
|
296
|
+
ASSERT(!defaults_method_one_arg3);
|
297
|
+
}
|
298
|
+
|
299
|
+
TESTCASE(default_arguments_still_throws_argument_error)
|
300
|
+
{
|
301
|
+
Module m(anonymous_module());
|
302
|
+
m.define_method("foo", &defaults_method_one, (Arg("arg1"), Arg("arg2") = 3, Arg("arg3") = true));
|
303
|
+
|
304
|
+
ASSERT_EXCEPTION_CHECK(
|
305
|
+
Exception,
|
306
|
+
m.instance_eval("o = Object.new; o.extend(self); o.foo()"),
|
307
|
+
ASSERT_EQUAL(
|
308
|
+
Object(rb_eArgError),
|
309
|
+
Object(CLASS_OF(ex.value()))
|
310
|
+
)
|
311
|
+
);
|
312
|
+
|
313
|
+
ASSERT_EXCEPTION_CHECK(
|
314
|
+
Exception,
|
315
|
+
m.instance_eval("o = Object.new; o.extend(self); o.foo(3, 4, false, 17)"),
|
316
|
+
ASSERT_EQUAL(
|
317
|
+
Object(rb_eArgError),
|
318
|
+
Object(CLASS_OF(ex.value()))
|
319
|
+
)
|
320
|
+
);
|
321
|
+
}
|
322
|
+
|
323
|
+
namespace {
|
324
|
+
int the_one_default_arg = 0;
|
325
|
+
void method_with_one_default_arg(int num = 4) {
|
326
|
+
the_one_default_arg = num;
|
327
|
+
}
|
328
|
+
}
|
329
|
+
|
330
|
+
TESTCASE(defining_methods_with_single_default_argument)
|
331
|
+
{
|
332
|
+
// define_method
|
333
|
+
Module m(anonymous_module());
|
334
|
+
m.define_method("foo", &method_with_one_default_arg, (Arg("num") = 4));
|
335
|
+
m.instance_eval("o = Object.new; o.extend(self); o.foo()");
|
336
|
+
ASSERT_EQUAL(4, the_one_default_arg);
|
337
|
+
|
338
|
+
the_one_default_arg = 0;
|
339
|
+
|
340
|
+
// define_singleton_method
|
341
|
+
Class c(anonymous_class());
|
342
|
+
c.define_singleton_method("foo", &method_with_one_default_arg, (Arg("num") = 4));
|
343
|
+
c.call("foo");
|
344
|
+
ASSERT_EQUAL(4, the_one_default_arg);
|
345
|
+
|
346
|
+
the_one_default_arg = 0;
|
347
|
+
|
348
|
+
// define_module_function
|
349
|
+
m.define_module_function("foo2", &method_with_one_default_arg, (Arg("num") = 4));
|
350
|
+
|
351
|
+
m.call("foo2");
|
352
|
+
ASSERT_EQUAL(4, the_one_default_arg);
|
353
|
+
}
|
354
|
+
|
355
|
+
TESTCASE(default_arguments_for_define_singleton_method)
|
356
|
+
{
|
357
|
+
Class c(anonymous_class());
|
358
|
+
c.define_singleton_method("foo", &defaults_method_one, (Arg("arg1"), Arg("arg2") = 3, Arg("arg3") = true));
|
359
|
+
|
360
|
+
c.call("foo", 2);
|
361
|
+
|
362
|
+
ASSERT_EQUAL(2, defaults_method_one_arg1);
|
363
|
+
ASSERT_EQUAL(3, defaults_method_one_arg2);
|
364
|
+
ASSERT(defaults_method_one_arg3);
|
365
|
+
|
366
|
+
c.call("foo", 11, 10);
|
367
|
+
|
368
|
+
ASSERT_EQUAL(11, defaults_method_one_arg1);
|
369
|
+
ASSERT_EQUAL(10, defaults_method_one_arg2);
|
370
|
+
ASSERT(defaults_method_one_arg3);
|
371
|
+
|
372
|
+
c.call("foo", 22, 33, false);
|
373
|
+
|
374
|
+
ASSERT_EQUAL(22, defaults_method_one_arg1);
|
375
|
+
ASSERT_EQUAL(33, defaults_method_one_arg2);
|
376
|
+
ASSERT(!defaults_method_one_arg3);
|
377
|
+
}
|
378
|
+
|
379
|
+
TESTCASE(default_arguments_for_define_module_function)
|
380
|
+
{
|
381
|
+
Module m(anonymous_module());
|
382
|
+
m.define_module_function("foo", &defaults_method_one, (Arg("arg1"), Arg("arg2") = 3, Arg("arg3") = true));
|
383
|
+
|
384
|
+
m.call("foo", 2);
|
385
|
+
|
386
|
+
ASSERT_EQUAL(2, defaults_method_one_arg1);
|
387
|
+
ASSERT_EQUAL(3, defaults_method_one_arg2);
|
388
|
+
ASSERT(defaults_method_one_arg3);
|
389
|
+
|
390
|
+
m.call("foo", 11, 10);
|
391
|
+
|
392
|
+
ASSERT_EQUAL(11, defaults_method_one_arg1);
|
393
|
+
ASSERT_EQUAL(10, defaults_method_one_arg2);
|
394
|
+
ASSERT(defaults_method_one_arg3);
|
395
|
+
|
396
|
+
m.call("foo", 22, 33, false);
|
397
|
+
|
398
|
+
ASSERT_EQUAL(22, defaults_method_one_arg1);
|
399
|
+
ASSERT_EQUAL(33, defaults_method_one_arg2);
|
400
|
+
ASSERT(!defaults_method_one_arg3);
|
401
|
+
}
|
402
|
+
|
403
|
+
namespace {
|
404
|
+
std::string with_defaults_and_references_x;
|
405
|
+
bool with_defaults_and_references_doIt;
|
406
|
+
|
407
|
+
void with_defaults_and_references(std::string const& x, bool doIt = false)
|
408
|
+
{
|
409
|
+
with_defaults_and_references_x = x;
|
410
|
+
with_defaults_and_references_doIt = doIt;
|
411
|
+
}
|
412
|
+
}
|
413
|
+
|
414
|
+
TESTCASE(define_method_works_with_reference_arguments)
|
415
|
+
{
|
416
|
+
Module m(anonymous_module());
|
417
|
+
m.define_module_function("foo", &with_defaults_and_references,
|
418
|
+
(Arg("x"), Arg("doIt") = false));
|
419
|
+
|
420
|
+
m.call("foo", "test");
|
421
|
+
|
422
|
+
ASSERT_EQUAL("test", with_defaults_and_references_x);
|
423
|
+
ASSERT(!with_defaults_and_references_doIt);
|
424
|
+
}
|
425
|
+
|
426
|
+
namespace {
|
427
|
+
class ReturnTest { };
|
428
|
+
|
429
|
+
class Factory {
|
430
|
+
public:
|
431
|
+
Factory() { returnTest_ = new ReturnTest(); }
|
432
|
+
|
433
|
+
const ReturnTest& getReturnTest() const {
|
434
|
+
return *returnTest_;
|
435
|
+
}
|
436
|
+
|
437
|
+
private:
|
438
|
+
const ReturnTest* returnTest_;
|
439
|
+
};
|
440
|
+
}
|
441
|
+
|
442
|
+
TESTCASE(define_method_works_with_const_reference_return)
|
443
|
+
{
|
444
|
+
define_class<ReturnTest>("ReturnTest")
|
445
|
+
.define_constructor(Constructor<ReturnTest>());
|
446
|
+
|
447
|
+
define_class<Factory>("Factory")
|
448
|
+
.define_constructor(Constructor<Factory>())
|
449
|
+
.define_method("get_return_test", &Factory::getReturnTest);
|
450
|
+
|
451
|
+
Module m(anonymous_module());
|
452
|
+
|
453
|
+
Object result = m.instance_eval("Factory.new.get_return_test");
|
454
|
+
|
455
|
+
ASSERT_EQUAL("ReturnTest", result.class_of().name().c_str());
|
456
|
+
}
|
457
|
+
|
458
|
+
/*
|
459
|
+
namespace {
|
460
|
+
float with_reference_defaults_x;
|
461
|
+
std::string with_reference_defaults_str;
|
462
|
+
|
463
|
+
void with_reference_defaults(float x, std::string const& str = std::string("testing"))
|
464
|
+
{
|
465
|
+
with_reference_defaults_x = x;
|
466
|
+
with_reference_defaults_str = str;
|
467
|
+
}
|
468
|
+
}
|
469
|
+
|
470
|
+
TESTCASE(define_method_works_with_reference_const_default_values)
|
471
|
+
{
|
472
|
+
Module m(anonymous_module());
|
473
|
+
m.define_module_function("bar", &with_reference_defaults,
|
474
|
+
(Arg("x"), Arg("str") = std::string("testing")));
|
475
|
+
|
476
|
+
m.call("bar", 3);
|
477
|
+
|
478
|
+
ASSERT_EQUAL(3, with_reference_defaults_x);
|
479
|
+
ASSERT_EQUAL("testing", with_reference_defaults_str);
|
480
|
+
}
|
481
|
+
*/
|
@@ -0,0 +1,148 @@
|
|
1
|
+
#include "unittest.hpp"
|
2
|
+
#include "rice/Object.hpp"
|
3
|
+
#include "rice/Class.hpp"
|
4
|
+
#include "rice/String.hpp"
|
5
|
+
#include "rice/Array.hpp"
|
6
|
+
|
7
|
+
using namespace Rice;
|
8
|
+
|
9
|
+
TESTSUITE(Object);
|
10
|
+
|
11
|
+
SETUP(Object)
|
12
|
+
{
|
13
|
+
ruby_init();
|
14
|
+
}
|
15
|
+
|
16
|
+
TESTCASE(default_construct)
|
17
|
+
{
|
18
|
+
Object o;
|
19
|
+
ASSERT_EQUAL(Qnil, o.value());
|
20
|
+
}
|
21
|
+
|
22
|
+
TESTCASE(construct_with_value)
|
23
|
+
{
|
24
|
+
Object o(INT2NUM(42));
|
25
|
+
ASSERT_EQUAL(INT2NUM(42), o.value());
|
26
|
+
}
|
27
|
+
|
28
|
+
TESTCASE(copy_construct)
|
29
|
+
{
|
30
|
+
Object o(INT2NUM(42));
|
31
|
+
Object o2(o);
|
32
|
+
ASSERT_EQUAL(INT2NUM(42), o2.value());
|
33
|
+
}
|
34
|
+
|
35
|
+
TESTCASE(test)
|
36
|
+
{
|
37
|
+
ASSERT_EQUAL(true, Object(Qtrue).test());
|
38
|
+
ASSERT_EQUAL(true, Object(INT2NUM(42)).test());
|
39
|
+
ASSERT_EQUAL(false, Object(Qfalse).test());
|
40
|
+
ASSERT_EQUAL(false, Object(Qnil).test());
|
41
|
+
ASSERT_EQUAL(true, Object(Qundef).test());
|
42
|
+
}
|
43
|
+
|
44
|
+
TESTCASE(explicit_conversion_to_bool)
|
45
|
+
{
|
46
|
+
// g++ 3.3.3 can't handle constructor-style inside the assert, which
|
47
|
+
// is why we use cast-style here.
|
48
|
+
ASSERT_EQUAL(true, (bool)Object(Qtrue));
|
49
|
+
ASSERT_EQUAL(true, (bool)Object(INT2NUM(42)));
|
50
|
+
ASSERT_EQUAL(false, (bool)Object(Qfalse));
|
51
|
+
ASSERT_EQUAL(false, (bool)Object(Qnil));
|
52
|
+
ASSERT_EQUAL(true, (bool)Object(Qundef));
|
53
|
+
}
|
54
|
+
|
55
|
+
TESTCASE(is_nil)
|
56
|
+
{
|
57
|
+
ASSERT_EQUAL(false, Object(Qtrue).is_nil());
|
58
|
+
ASSERT_EQUAL(false, Object(INT2NUM(42)).is_nil());
|
59
|
+
ASSERT_EQUAL(false, Object(Qfalse).is_nil());
|
60
|
+
ASSERT_EQUAL(true, Object(Qnil).is_nil());
|
61
|
+
ASSERT_EQUAL(false, Object(Qundef).is_nil());
|
62
|
+
}
|
63
|
+
|
64
|
+
TESTCASE(implicit_conversion_to_value)
|
65
|
+
{
|
66
|
+
// g++ 3.3.3 can't handle constructor-style inside the assert, which
|
67
|
+
// is why we use cast-style here.
|
68
|
+
ASSERT_EQUAL(Qtrue, (VALUE)Object(Qtrue));
|
69
|
+
ASSERT_EQUAL(INT2NUM(42), (VALUE)Object(INT2NUM(42)));
|
70
|
+
ASSERT_EQUAL(Qfalse, (VALUE)Object(Qfalse));
|
71
|
+
ASSERT_EQUAL(Qnil, (VALUE)Object(Qnil));
|
72
|
+
ASSERT_EQUAL(Qundef, (VALUE)Object(Qundef));
|
73
|
+
}
|
74
|
+
|
75
|
+
TESTCASE(explicit_conversion_to_value)
|
76
|
+
{
|
77
|
+
ASSERT_EQUAL(Qtrue, Object(Qtrue).value());
|
78
|
+
ASSERT_EQUAL(INT2NUM(42), Object(INT2NUM(42)).value());
|
79
|
+
ASSERT_EQUAL(Qfalse, Object(Qfalse).value());
|
80
|
+
ASSERT_EQUAL(Qnil, Object(Qnil).value());
|
81
|
+
ASSERT_EQUAL(Qundef, Object(Qundef).value());
|
82
|
+
}
|
83
|
+
|
84
|
+
TESTCASE(class_of)
|
85
|
+
{
|
86
|
+
ASSERT_EQUAL(Class(rb_cObject), Class(rb_cObject).call("new").class_of());
|
87
|
+
ASSERT_EQUAL(Class(rb_cFloat), Object(rb_float_new(42.0)).class_of());
|
88
|
+
}
|
89
|
+
|
90
|
+
TESTCASE(compare)
|
91
|
+
{
|
92
|
+
ASSERT_EQUAL(0, Object(INT2NUM(42)).compare(Object(INT2NUM(42))));
|
93
|
+
ASSERT_EQUAL(-1, Object(INT2NUM(42)).compare(Object(INT2NUM(43))));
|
94
|
+
ASSERT_EQUAL(1, Object(INT2NUM(42)).compare(Object(INT2NUM(41))));
|
95
|
+
}
|
96
|
+
|
97
|
+
TESTCASE(to_s)
|
98
|
+
{
|
99
|
+
ASSERT_EQUAL(String("42"), Object(INT2NUM(42)).to_s());
|
100
|
+
}
|
101
|
+
|
102
|
+
TESTCASE(inspect)
|
103
|
+
{
|
104
|
+
ASSERT_EQUAL(String("42"), Object(INT2NUM(42)).inspect());
|
105
|
+
ASSERT_EQUAL(String("\"foo\""), Object(rb_str_new2("foo")).inspect());
|
106
|
+
}
|
107
|
+
|
108
|
+
TESTCASE(freeze)
|
109
|
+
{
|
110
|
+
Object o(Class(rb_cObject).call("new"));
|
111
|
+
ASSERT(!OBJ_FROZEN(o.value()));
|
112
|
+
o.freeze();
|
113
|
+
ASSERT(OBJ_FROZEN(o.value()));
|
114
|
+
}
|
115
|
+
|
116
|
+
TESTCASE(is_frozen)
|
117
|
+
{
|
118
|
+
Object o(Class(rb_cObject).call("new"));
|
119
|
+
ASSERT(!o.is_frozen());
|
120
|
+
rb_obj_freeze(o);
|
121
|
+
ASSERT(o.is_frozen());
|
122
|
+
}
|
123
|
+
|
124
|
+
TESTCASE(swap)
|
125
|
+
{
|
126
|
+
Object o1(INT2NUM(42));
|
127
|
+
Object o2(rb_ary_new());
|
128
|
+
o1.swap(o2);
|
129
|
+
ASSERT_EQUAL(to_ruby(42), o2);
|
130
|
+
ASSERT_EQUAL(Class(rb_cArray), o1.class_of());
|
131
|
+
}
|
132
|
+
|
133
|
+
TESTCASE(instance_eval)
|
134
|
+
{
|
135
|
+
Object o(Class(rb_cObject).call("new"));
|
136
|
+
o.iv_set("@foo", 42);
|
137
|
+
ASSERT_EQUAL(to_ruby(42), o.instance_eval("@foo"));
|
138
|
+
}
|
139
|
+
|
140
|
+
TESTCASE(rb_type)
|
141
|
+
{
|
142
|
+
ASSERT_EQUAL(T_TRUE, Object(Qtrue).rb_type());
|
143
|
+
ASSERT_EQUAL(T_FIXNUM, Object(INT2NUM(42)).rb_type());
|
144
|
+
ASSERT_EQUAL(T_FALSE, Object(Qfalse).rb_type());
|
145
|
+
ASSERT_EQUAL(T_NIL, Object(Qnil).rb_type());
|
146
|
+
ASSERT_EQUAL(T_UNDEF, Object(Qundef).rb_type());
|
147
|
+
}
|
148
|
+
|
@@ -0,0 +1,94 @@
|
|
1
|
+
#include "unittest.hpp"
|
2
|
+
#include "rice/String.hpp"
|
3
|
+
|
4
|
+
using namespace Rice;
|
5
|
+
|
6
|
+
TESTSUITE(String);
|
7
|
+
|
8
|
+
SETUP(String)
|
9
|
+
{
|
10
|
+
ruby_init();
|
11
|
+
}
|
12
|
+
|
13
|
+
TESTCASE(default_construct)
|
14
|
+
{
|
15
|
+
String s;
|
16
|
+
ASSERT_EQUAL(T_STRING, rb_type(s));
|
17
|
+
ASSERT_EQUAL("", RSTRING_PTR(s.value()));
|
18
|
+
}
|
19
|
+
|
20
|
+
TESTCASE(construct_from_value)
|
21
|
+
{
|
22
|
+
String s(rb_str_new2("foo"));
|
23
|
+
ASSERT_EQUAL(T_STRING, rb_type(s));
|
24
|
+
ASSERT_EQUAL("foo", RSTRING_PTR(s.value()));
|
25
|
+
}
|
26
|
+
|
27
|
+
TESTCASE(construct_from_object)
|
28
|
+
{
|
29
|
+
Object o(rb_str_new2("foo"));
|
30
|
+
String s(o);
|
31
|
+
ASSERT_EQUAL(T_STRING, rb_type(s));
|
32
|
+
ASSERT_EQUAL("foo", RSTRING_PTR(s.value()));
|
33
|
+
}
|
34
|
+
|
35
|
+
TESTCASE(construct_from_identifier)
|
36
|
+
{
|
37
|
+
String s(Identifier("foo"));
|
38
|
+
ASSERT_EQUAL(T_STRING, rb_type(s));
|
39
|
+
ASSERT_EQUAL("foo", RSTRING_PTR(s.value()));
|
40
|
+
}
|
41
|
+
|
42
|
+
TESTCASE(construct_from_c_string)
|
43
|
+
{
|
44
|
+
String s("foo");
|
45
|
+
ASSERT_EQUAL(T_STRING, rb_type(s));
|
46
|
+
ASSERT_EQUAL("foo", RSTRING_PTR(s.value()));
|
47
|
+
}
|
48
|
+
|
49
|
+
TESTCASE(construct_from_std_string)
|
50
|
+
{
|
51
|
+
String s(std::string("foo"));
|
52
|
+
ASSERT_EQUAL(T_STRING, rb_type(s));
|
53
|
+
ASSERT_EQUAL("foo", RSTRING_PTR(s.value()));
|
54
|
+
}
|
55
|
+
|
56
|
+
TESTCASE(format)
|
57
|
+
{
|
58
|
+
String s(String::format("%s %d", "foo", 42));
|
59
|
+
ASSERT_EQUAL(T_STRING, rb_type(s));
|
60
|
+
ASSERT_EQUAL("foo 42", RSTRING_PTR(s.value()));
|
61
|
+
}
|
62
|
+
|
63
|
+
TESTCASE(length)
|
64
|
+
{
|
65
|
+
String s("foo");
|
66
|
+
ASSERT_EQUAL(3u, s.length());
|
67
|
+
}
|
68
|
+
|
69
|
+
TESTCASE(bracket)
|
70
|
+
{
|
71
|
+
String s("foo");
|
72
|
+
ASSERT_EQUAL('f', s[0]);
|
73
|
+
ASSERT_EQUAL('o', s[1]);
|
74
|
+
ASSERT_EQUAL('o', s[2]);
|
75
|
+
}
|
76
|
+
|
77
|
+
TESTCASE(c_str)
|
78
|
+
{
|
79
|
+
String s("foo");
|
80
|
+
ASSERT_EQUAL(RSTRING_PTR(s.value()), s.c_str());
|
81
|
+
}
|
82
|
+
|
83
|
+
TESTCASE(str)
|
84
|
+
{
|
85
|
+
String s("foo");
|
86
|
+
ASSERT_EQUAL(std::string("foo"), s.str());
|
87
|
+
}
|
88
|
+
|
89
|
+
TESTCASE(intern)
|
90
|
+
{
|
91
|
+
String s("foo");
|
92
|
+
ASSERT_EQUAL(Identifier("foo"), s.intern());
|
93
|
+
}
|
94
|
+
|