rice 2.1.1 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/CHANGELOG.md +121 -0
- data/CONTRIBUTORS.md +19 -0
- data/COPYING +2 -2
- data/Gemfile +3 -0
- data/README.md +69 -0
- data/Rakefile +95 -12
- data/include/rice/rice.hpp +7766 -0
- data/lib/mkmf-rice.rb +127 -0
- data/lib/version.rb +3 -0
- data/rice/Address_Registration_Guard.ipp +75 -32
- data/rice/Address_Registration_Guard_defn.hpp +60 -56
- data/rice/Arg.hpp +80 -4
- data/rice/Arg.ipp +51 -0
- data/rice/Constructor.hpp +30 -376
- data/rice/Data_Object.ipp +234 -107
- data/rice/Data_Object_defn.hpp +77 -117
- data/rice/Data_Type.hpp +1 -2
- data/rice/Data_Type.ipp +251 -295
- data/rice/Data_Type_defn.hpp +175 -243
- data/rice/Director.hpp +14 -9
- data/rice/Enum.hpp +54 -104
- data/rice/Enum.ipp +104 -230
- data/rice/Exception.hpp +2 -8
- data/rice/Exception.ipp +65 -0
- data/rice/Exception_defn.hpp +46 -47
- data/rice/Identifier.hpp +28 -28
- data/rice/Identifier.ipp +23 -27
- data/rice/Return.hpp +39 -0
- data/rice/Return.ipp +33 -0
- data/rice/detail/Exception_Handler.ipp +22 -62
- data/rice/detail/Exception_Handler_defn.hpp +76 -91
- data/rice/detail/Iterator.hpp +18 -88
- data/rice/detail/Iterator.ipp +47 -0
- data/rice/detail/Jump_Tag.hpp +21 -0
- data/rice/detail/MethodInfo.hpp +44 -0
- data/rice/detail/MethodInfo.ipp +78 -0
- data/rice/detail/NativeAttribute.hpp +53 -0
- data/rice/detail/NativeAttribute.ipp +83 -0
- data/rice/detail/NativeFunction.hpp +69 -0
- data/rice/detail/NativeFunction.ipp +248 -0
- data/rice/detail/RubyFunction.hpp +39 -0
- data/rice/detail/RubyFunction.ipp +92 -0
- data/rice/detail/Type.hpp +29 -0
- data/rice/detail/Type.ipp +138 -0
- data/rice/detail/TypeRegistry.hpp +50 -0
- data/rice/detail/TypeRegistry.ipp +106 -0
- data/rice/detail/Wrapper.hpp +51 -0
- data/rice/detail/Wrapper.ipp +151 -0
- data/rice/detail/default_allocation_func.hpp +8 -19
- data/rice/detail/default_allocation_func.ipp +9 -8
- data/rice/detail/from_ruby.hpp +2 -37
- data/rice/detail/from_ruby.ipp +1020 -46
- data/rice/detail/from_ruby_defn.hpp +38 -0
- data/rice/detail/function_traits.hpp +124 -0
- data/rice/detail/method_data.hpp +23 -15
- data/rice/detail/method_data.ipp +53 -0
- data/rice/detail/rice_traits.hpp +116 -0
- data/rice/detail/ruby.hpp +9 -49
- data/rice/detail/to_ruby.hpp +3 -17
- data/rice/detail/to_ruby.ipp +409 -31
- data/rice/detail/to_ruby_defn.hpp +48 -0
- data/rice/forward_declares.ipp +82 -0
- data/rice/global_function.hpp +16 -20
- data/rice/global_function.ipp +8 -17
- data/rice/rice.hpp +59 -0
- data/rice/ruby_mark.hpp +5 -3
- data/rice/ruby_try_catch.hpp +4 -4
- data/rice/stl.hpp +11 -0
- data/sample/callbacks/extconf.rb +6 -0
- data/sample/callbacks/sample_callbacks.cpp +35 -0
- data/sample/callbacks/test.rb +28 -0
- data/sample/enum/extconf.rb +3 -0
- data/sample/enum/sample_enum.cpp +3 -17
- data/sample/enum/test.rb +2 -2
- data/sample/inheritance/animals.cpp +8 -24
- data/sample/inheritance/extconf.rb +3 -0
- data/sample/inheritance/test.rb +1 -1
- data/sample/map/extconf.rb +3 -0
- data/sample/map/map.cpp +10 -18
- data/sample/map/test.rb +1 -1
- data/test/embed_ruby.cpp +34 -0
- data/test/embed_ruby.hpp +4 -0
- data/test/ext/t1/extconf.rb +3 -0
- data/test/ext/t1/t1.cpp +1 -3
- data/test/ext/t2/extconf.rb +3 -0
- data/test/ext/t2/t2.cpp +1 -1
- data/test/extconf.rb +23 -0
- data/test/ruby/test_callbacks_sample.rb +28 -0
- data/test/ruby/test_multiple_extensions.rb +18 -0
- data/test/ruby/test_multiple_extensions_same_class.rb +14 -0
- data/test/ruby/test_multiple_extensions_with_inheritance.rb +20 -0
- data/test/test_Address_Registration_Guard.cpp +25 -11
- data/test/test_Array.cpp +131 -74
- data/test/test_Attribute.cpp +147 -0
- data/test/test_Builtin_Object.cpp +36 -15
- data/test/test_Class.cpp +151 -274
- data/test/test_Constructor.cpp +10 -9
- data/test/test_Data_Object.cpp +135 -193
- data/test/test_Data_Type.cpp +323 -252
- data/test/test_Director.cpp +56 -42
- data/test/test_Enum.cpp +230 -104
- data/test/test_Exception.cpp +7 -7
- data/test/test_Hash.cpp +33 -31
- data/test/test_Identifier.cpp +6 -6
- data/test/test_Inheritance.cpp +221 -0
- data/test/test_Iterator.cpp +161 -0
- data/test/test_Jump_Tag.cpp +1 -1
- data/test/test_Keep_Alive.cpp +161 -0
- data/test/test_Memory_Management.cpp +4 -5
- data/test/test_Module.cpp +169 -111
- data/test/test_Object.cpp +51 -19
- data/test/test_Ownership.cpp +275 -0
- data/test/test_Self.cpp +205 -0
- data/test/test_Stl_Optional.cpp +90 -0
- data/test/test_Stl_Pair.cpp +144 -0
- data/test/test_Stl_SmartPointer.cpp +200 -0
- data/test/test_Stl_String.cpp +74 -0
- data/test/test_Stl_Vector.cpp +652 -0
- data/test/test_String.cpp +3 -3
- data/test/test_Struct.cpp +31 -40
- data/test/test_Symbol.cpp +3 -3
- data/test/test_To_From_Ruby.cpp +283 -218
- data/test/test_global_functions.cpp +41 -20
- data/test/unittest.cpp +34 -8
- data/test/unittest.hpp +0 -4
- metadata +117 -137
- data/Doxyfile +0 -2280
- data/Makefile.am +0 -26
- data/Makefile.in +0 -920
- data/README +0 -1055
- data/README.mingw +0 -8
- data/aclocal.m4 +0 -1088
- data/bootstrap +0 -8
- data/check_stdcxx_11.ac +0 -142
- data/config.guess +0 -1421
- data/config.sub +0 -1807
- data/configure +0 -7481
- data/configure.ac +0 -55
- data/depcomp +0 -791
- data/doxygen.ac +0 -314
- data/doxygen.am +0 -186
- data/extconf.rb +0 -69
- data/install-sh +0 -501
- data/missing +0 -215
- data/post-autoconf.rb +0 -22
- data/post-automake.rb +0 -28
- data/rice/Address_Registration_Guard.cpp +0 -22
- data/rice/Arg_impl.hpp +0 -129
- data/rice/Arg_operators.cpp +0 -21
- data/rice/Arg_operators.hpp +0 -19
- data/rice/Array.hpp +0 -214
- data/rice/Array.ipp +0 -256
- data/rice/Builtin_Object.hpp +0 -8
- data/rice/Builtin_Object.ipp +0 -50
- data/rice/Builtin_Object_defn.hpp +0 -50
- data/rice/Class.cpp +0 -57
- data/rice/Class.hpp +0 -8
- data/rice/Class.ipp +0 -6
- data/rice/Class_defn.hpp +0 -83
- data/rice/Data_Type.cpp +0 -54
- data/rice/Data_Type_fwd.hpp +0 -12
- data/rice/Director.cpp +0 -13
- data/rice/Exception.cpp +0 -59
- data/rice/Exception_Base.hpp +0 -8
- data/rice/Exception_Base.ipp +0 -13
- data/rice/Exception_Base_defn.hpp +0 -27
- data/rice/Hash.hpp +0 -227
- data/rice/Hash.ipp +0 -329
- data/rice/Identifier.cpp +0 -8
- data/rice/Jump_Tag.hpp +0 -24
- data/rice/Makefile.am +0 -124
- data/rice/Makefile.in +0 -839
- data/rice/Module.cpp +0 -84
- data/rice/Module.hpp +0 -8
- data/rice/Module.ipp +0 -6
- data/rice/Module_defn.hpp +0 -88
- data/rice/Module_impl.hpp +0 -281
- data/rice/Module_impl.ipp +0 -345
- data/rice/Object.cpp +0 -169
- data/rice/Object.hpp +0 -8
- data/rice/Object.ipp +0 -19
- data/rice/Object_defn.hpp +0 -191
- data/rice/Require_Guard.hpp +0 -21
- data/rice/String.cpp +0 -94
- data/rice/String.hpp +0 -91
- data/rice/Struct.cpp +0 -117
- data/rice/Struct.hpp +0 -162
- data/rice/Struct.ipp +0 -26
- data/rice/Symbol.cpp +0 -25
- data/rice/Symbol.hpp +0 -66
- data/rice/Symbol.ipp +0 -44
- data/rice/config.hpp +0 -47
- data/rice/config.hpp.in +0 -46
- data/rice/detail/Arguments.hpp +0 -118
- data/rice/detail/Auto_Function_Wrapper.hpp +0 -898
- data/rice/detail/Auto_Function_Wrapper.ipp +0 -3694
- data/rice/detail/Auto_Member_Function_Wrapper.hpp +0 -897
- data/rice/detail/Auto_Member_Function_Wrapper.ipp +0 -2774
- data/rice/detail/Caster.hpp +0 -103
- data/rice/detail/Not_Copyable.hpp +0 -25
- data/rice/detail/Wrapped_Function.hpp +0 -33
- data/rice/detail/cfp.hpp +0 -24
- data/rice/detail/cfp.ipp +0 -51
- data/rice/detail/check_ruby_type.cpp +0 -27
- data/rice/detail/check_ruby_type.hpp +0 -23
- data/rice/detail/creation_funcs.hpp +0 -37
- data/rice/detail/creation_funcs.ipp +0 -36
- data/rice/detail/define_method_and_auto_wrap.hpp +0 -31
- data/rice/detail/define_method_and_auto_wrap.ipp +0 -30
- data/rice/detail/demangle.cpp +0 -56
- data/rice/detail/demangle.hpp +0 -19
- data/rice/detail/env.hpp +0 -11
- data/rice/detail/method_data.cpp +0 -86
- data/rice/detail/node.hpp +0 -13
- data/rice/detail/object_call.hpp +0 -69
- data/rice/detail/object_call.ipp +0 -131
- data/rice/detail/protect.cpp +0 -29
- data/rice/detail/protect.hpp +0 -34
- data/rice/detail/ruby_version_code.hpp +0 -6
- data/rice/detail/ruby_version_code.hpp.in +0 -6
- data/rice/detail/st.hpp +0 -22
- data/rice/detail/traits.hpp +0 -43
- data/rice/detail/win32.hpp +0 -16
- data/rice/detail/wrap_function.hpp +0 -341
- data/rice/detail/wrap_function.ipp +0 -514
- data/rice/protect.hpp +0 -92
- data/rice/protect.ipp +0 -1134
- data/rice/rubypp.rb +0 -97
- data/rice/to_from_ruby.hpp +0 -8
- data/rice/to_from_ruby.ipp +0 -294
- data/rice/to_from_ruby_defn.hpp +0 -70
- data/ruby.ac +0 -135
- data/ruby/Makefile.am +0 -1
- data/ruby/Makefile.in +0 -625
- data/ruby/lib/Makefile.am +0 -3
- data/ruby/lib/Makefile.in +0 -503
- data/ruby/lib/mkmf-rice.rb.in +0 -217
- data/ruby/lib/version.rb +0 -3
- data/sample/Makefile.am +0 -47
- data/sample/Makefile.in +0 -486
- data/test/Makefile.am +0 -72
- data/test/Makefile.in +0 -1150
- data/test/ext/Makefile.am +0 -41
- data/test/ext/Makefile.in +0 -480
- data/test/test_rice.rb +0 -41
data/test/test_Director.cpp
CHANGED
@@ -1,13 +1,10 @@
|
|
1
1
|
#include "unittest.hpp"
|
2
|
-
#include "
|
3
|
-
#include
|
4
|
-
#include "rice/Constructor.hpp"
|
5
|
-
#include "rice/Data_Type.hpp"
|
2
|
+
#include "embed_ruby.hpp"
|
3
|
+
#include <rice/rice.hpp>
|
6
4
|
|
7
5
|
#include <vector>
|
8
6
|
#include <iostream>
|
9
7
|
|
10
|
-
using namespace std;
|
11
8
|
using namespace Rice;
|
12
9
|
|
13
10
|
TESTSUITE(Director);
|
@@ -16,9 +13,10 @@ namespace {
|
|
16
13
|
/**
|
17
14
|
* Abstract base class
|
18
15
|
*/
|
19
|
-
class Worker
|
16
|
+
class Worker
|
17
|
+
{
|
20
18
|
public:
|
21
|
-
virtual ~Worker()
|
19
|
+
virtual ~Worker() = default;
|
22
20
|
|
23
21
|
int getNumber() { return 12; }
|
24
22
|
|
@@ -40,18 +38,24 @@ namespace {
|
|
40
38
|
/**
|
41
39
|
* Class to handle workers
|
42
40
|
*/
|
43
|
-
class Handler
|
41
|
+
class Handler
|
42
|
+
{
|
44
43
|
std::vector<Worker*> mWorkers;
|
45
44
|
|
46
45
|
public:
|
47
46
|
|
48
|
-
void addWorker(Worker* worker)
|
47
|
+
void addWorker(Worker* worker)
|
48
|
+
{
|
49
|
+
mWorkers.push_back(worker);
|
50
|
+
}
|
49
51
|
|
50
|
-
int processWorkers(int start)
|
52
|
+
int processWorkers(int start)
|
53
|
+
{
|
51
54
|
std::vector<Worker*>::iterator i = mWorkers.begin();
|
52
55
|
int results = start;
|
53
56
|
|
54
|
-
for(; i != mWorkers.end(); i++)
|
57
|
+
for(; i != mWorkers.end(); i++)
|
58
|
+
{
|
55
59
|
results = (*i)->process(results);
|
56
60
|
}
|
57
61
|
|
@@ -62,24 +66,29 @@ namespace {
|
|
62
66
|
/**
|
63
67
|
* Our Director wrapper of Worker
|
64
68
|
*/
|
65
|
-
class WorkerDirector : public Worker, public Rice::Director
|
69
|
+
class WorkerDirector : public Worker, public Rice::Director
|
70
|
+
{
|
66
71
|
public:
|
67
72
|
WorkerDirector(Object self) : Director(self) { }
|
68
73
|
|
69
|
-
virtual int process(int num)
|
70
|
-
|
74
|
+
virtual int process(int num)
|
75
|
+
{
|
76
|
+
return detail::From_Ruby<int>().convert( getSelf().call("process", num) );
|
71
77
|
}
|
72
78
|
|
73
|
-
int default_process(int num)
|
79
|
+
int default_process(int num)
|
80
|
+
{
|
74
81
|
raisePureVirtual();
|
75
82
|
return 0;
|
76
83
|
}
|
77
84
|
|
78
|
-
virtual int doSomething(int num)
|
79
|
-
|
85
|
+
virtual int doSomething(int num)
|
86
|
+
{
|
87
|
+
return detail::From_Ruby<int>().convert( getSelf().call("do_something", num) );
|
80
88
|
}
|
81
89
|
|
82
|
-
int default_doSomething(int num)
|
90
|
+
int default_doSomething(int num)
|
91
|
+
{
|
83
92
|
return Worker::doSomething(num);
|
84
93
|
}
|
85
94
|
};
|
@@ -87,7 +96,7 @@ namespace {
|
|
87
96
|
|
88
97
|
SETUP(Director)
|
89
98
|
{
|
90
|
-
|
99
|
+
embed_ruby();
|
91
100
|
}
|
92
101
|
|
93
102
|
TESTCASE(exposes_worker_as_instantiatable_class)
|
@@ -100,7 +109,7 @@ TESTCASE(exposes_worker_as_instantiatable_class)
|
|
100
109
|
Module m = define_module("Testing");
|
101
110
|
Object result = m.instance_eval("worker = Worker.new; worker.get_number");
|
102
111
|
|
103
|
-
ASSERT_EQUAL(12,
|
112
|
+
ASSERT_EQUAL(12, detail::From_Ruby<int>().convert(result.value()));
|
104
113
|
}
|
105
114
|
|
106
115
|
TESTCASE(can_call_virtual_methods_on_base_class)
|
@@ -115,7 +124,7 @@ TESTCASE(can_call_virtual_methods_on_base_class)
|
|
115
124
|
|
116
125
|
Object result = m.instance_eval("worker = Worker.new; worker.do_something(4)");
|
117
126
|
|
118
|
-
ASSERT_EQUAL(16,
|
127
|
+
ASSERT_EQUAL(16, detail::From_Ruby<int>().convert(result.value()));
|
119
128
|
}
|
120
129
|
|
121
130
|
TESTCASE(super_calls_pass_execution_up_the_inheritance_chain)
|
@@ -130,7 +139,7 @@ TESTCASE(super_calls_pass_execution_up_the_inheritance_chain)
|
|
130
139
|
|
131
140
|
Object result = m.instance_eval("worker = RubyWorker.new; worker.do_something(10)");
|
132
141
|
|
133
|
-
ASSERT_EQUAL(400,
|
142
|
+
ASSERT_EQUAL(400, detail::From_Ruby<int>().convert(result.value()));
|
134
143
|
}
|
135
144
|
|
136
145
|
TESTCASE(super_calls_on_pure_virtual_raise_error)
|
@@ -157,7 +166,7 @@ TESTCASE(polymorphic_calls_head_down_the_call_chain)
|
|
157
166
|
{
|
158
167
|
define_class<Handler>("Handler")
|
159
168
|
.define_constructor(Constructor<Handler>())
|
160
|
-
.define_method("add_worker", &Handler::addWorker)
|
169
|
+
.define_method("add_worker", &Handler::addWorker, Arg("worker").keepAlive())
|
161
170
|
.define_method("process_workers", &Handler::processWorkers);
|
162
171
|
|
163
172
|
define_class<Worker>("Worker")
|
@@ -178,7 +187,7 @@ TESTCASE(polymorphic_calls_head_down_the_call_chain)
|
|
178
187
|
Object result = m.instance_eval("$handler.process_workers(5)");
|
179
188
|
|
180
189
|
// Hit's EchoWorker, so 5 + 2, then passes that to DoubleWorker, so 7 * 2 = 14
|
181
|
-
ASSERT_EQUAL(14,
|
190
|
+
ASSERT_EQUAL(14, detail::From_Ruby<int>().convert(result.value()));
|
182
191
|
}
|
183
192
|
|
184
193
|
namespace {
|
@@ -194,23 +203,26 @@ namespace {
|
|
194
203
|
virtual int doItImpl(int in) = 0;
|
195
204
|
};
|
196
205
|
|
197
|
-
class CallsSelfDirector : public CallsSelf, public Director
|
198
|
-
|
206
|
+
class CallsSelfDirector : public CallsSelf, public Director
|
207
|
+
{
|
199
208
|
public:
|
200
209
|
CallsSelfDirector(Object self) : Director(self) { }
|
201
210
|
virtual ~CallsSelfDirector() { }
|
202
211
|
|
203
|
-
virtual int doItImpl(int in)
|
204
|
-
|
212
|
+
virtual int doItImpl(int in)
|
213
|
+
{
|
214
|
+
return detail::From_Ruby<int>().convert( getSelf().call("do_it_impl", in) );
|
205
215
|
}
|
206
216
|
|
207
|
-
int default_doItImpl(int in)
|
217
|
+
int default_doItImpl(int in)
|
218
|
+
{
|
208
219
|
raisePureVirtual();
|
209
220
|
return 0;
|
210
221
|
}
|
211
222
|
};
|
212
223
|
|
213
|
-
struct MyCallsSelf : CallsSelf
|
224
|
+
struct MyCallsSelf : CallsSelf
|
225
|
+
{
|
214
226
|
MyCallsSelf() { }
|
215
227
|
virtual ~MyCallsSelf() { }
|
216
228
|
|
@@ -218,19 +230,21 @@ namespace {
|
|
218
230
|
};
|
219
231
|
|
220
232
|
// Abstract type return types handled properly
|
221
|
-
CallsSelf* getCallsSelf()
|
233
|
+
CallsSelf* getCallsSelf()
|
234
|
+
{
|
222
235
|
return new MyCallsSelf();
|
223
236
|
}
|
224
237
|
|
225
238
|
// Abstract type Argument types handled properly
|
226
|
-
int doItOnPointer(CallsSelf* obj, int in)
|
239
|
+
int doItOnPointer(CallsSelf* obj, int in)
|
240
|
+
{
|
227
241
|
return obj->doIt(in);
|
228
242
|
}
|
229
243
|
|
230
|
-
int doItOnReference(CallsSelf& obj, int in)
|
244
|
+
int doItOnReference(CallsSelf& obj, int in)
|
245
|
+
{
|
231
246
|
return obj.doIt(in);
|
232
247
|
}
|
233
|
-
|
234
248
|
}
|
235
249
|
|
236
250
|
TESTCASE(mix_of_polymorphic_calls_and_inheritance_dont_cause_infinite_loops)
|
@@ -248,7 +262,7 @@ TESTCASE(mix_of_polymorphic_calls_and_inheritance_dont_cause_infinite_loops)
|
|
248
262
|
"c = MySelf.new; c.do_it(10)"
|
249
263
|
);
|
250
264
|
|
251
|
-
ASSERT_EQUAL(100,
|
265
|
+
ASSERT_EQUAL(100, detail::From_Ruby<int>().convert(result.value()));
|
252
266
|
}
|
253
267
|
|
254
268
|
TESTCASE(director_class_super_classes_get_type_bound)
|
@@ -262,8 +276,9 @@ TESTCASE(director_class_super_classes_get_type_bound)
|
|
262
276
|
.define_method("do_it_impl", &CallsSelfDirector::default_doItImpl)
|
263
277
|
.define_method("do_it", &CallsSelf::doIt);
|
264
278
|
|
265
|
-
Object result = m.instance_eval("cs = Testing::get_calls_self
|
266
|
-
|
279
|
+
Object result = m.instance_eval(R"(cs = Testing::get_calls_self
|
280
|
+
cs.do_it(3))");
|
281
|
+
ASSERT_EQUAL(36, detail::From_Ruby<int>().convert(result.value()));
|
267
282
|
}
|
268
283
|
|
269
284
|
TESTCASE(director_allows_abstract_types_used_as_parameters_pointers)
|
@@ -283,9 +298,9 @@ TESTCASE(director_allows_abstract_types_used_as_parameters_pointers)
|
|
283
298
|
"Testing::do_it_on_pointer(c, 5)"
|
284
299
|
);
|
285
300
|
|
286
|
-
ASSERT_EQUAL(50,
|
301
|
+
ASSERT_EQUAL(50, detail::From_Ruby<int>().convert(result.value()));
|
287
302
|
}
|
288
|
-
|
303
|
+
|
289
304
|
TESTCASE(director_allows_abstract_types_used_as_parameters_reference)
|
290
305
|
{
|
291
306
|
Module m = define_module("Testing");
|
@@ -303,6 +318,5 @@ TESTCASE(director_allows_abstract_types_used_as_parameters_reference)
|
|
303
318
|
"Testing::do_it_on_ref(c, 3)"
|
304
319
|
);
|
305
320
|
|
306
|
-
ASSERT_EQUAL(30,
|
307
|
-
}
|
308
|
-
*/
|
321
|
+
ASSERT_EQUAL(30, detail::From_Ruby<int>().convert(result.value()));
|
322
|
+
}
|
data/test/test_Enum.cpp
CHANGED
@@ -1,11 +1,10 @@
|
|
1
|
-
#include "unittest.hpp"
|
2
|
-
#include "rice/Enum.hpp"
|
3
|
-
#include "rice/Array.hpp"
|
4
|
-
#include "rice/String.hpp"
|
5
|
-
#include "rice/Constructor.hpp"
|
6
|
-
#include "rice/global_function.hpp"
|
7
1
|
#include <iostream>
|
8
2
|
|
3
|
+
#include "unittest.hpp"
|
4
|
+
#include "embed_ruby.hpp"
|
5
|
+
|
6
|
+
#include <rice/rice.hpp>
|
7
|
+
|
9
8
|
using namespace Rice;
|
10
9
|
|
11
10
|
TESTSUITE(Enum);
|
@@ -22,144 +21,202 @@ namespace
|
|
22
21
|
.define_value("GREEN", GREEN);
|
23
22
|
return colors;
|
24
23
|
}
|
24
|
+
|
25
|
+
enum class Season { Spring, Summer, Fall, Winter };
|
26
|
+
|
27
|
+
// This is needed to make unittest compile (it uses ostream to report errors)
|
28
|
+
inline std::ostream& operator<<(std::ostream& os, const Season& season)
|
29
|
+
{
|
30
|
+
os << static_cast<std::underlying_type_t<Season>>(season);
|
31
|
+
return os;
|
32
|
+
}
|
33
|
+
|
34
|
+
Enum<Season> define_season_enum()
|
35
|
+
{
|
36
|
+
static Enum<Season> seasons = define_enum<Season>("Season")
|
37
|
+
.define_value("Spring", Season::Spring)
|
38
|
+
.define_value("Summer", Season::Summer)
|
39
|
+
.define_value("Fall", Season::Fall)
|
40
|
+
.define_value("Winter", Season::Winter);
|
41
|
+
|
42
|
+
return seasons;
|
43
|
+
}
|
25
44
|
}
|
26
45
|
|
27
46
|
SETUP(Enum)
|
28
47
|
{
|
29
|
-
|
48
|
+
embed_ruby();
|
30
49
|
}
|
31
50
|
|
32
51
|
TESTCASE(copy_construct)
|
33
52
|
{
|
34
|
-
Enum<Color>
|
35
|
-
ASSERT_EQUAL(define_color_enum().value(),
|
53
|
+
Enum<Color> colorEnum(define_color_enum());
|
54
|
+
ASSERT_EQUAL(define_color_enum().value(), colorEnum.value());
|
36
55
|
}
|
37
56
|
|
38
57
|
TESTCASE(assignment)
|
39
58
|
{
|
40
|
-
Enum<Color>
|
41
|
-
|
42
|
-
ASSERT_EQUAL(define_color_enum().value(),
|
59
|
+
Enum<Color> colorEnum;
|
60
|
+
colorEnum = define_color_enum();
|
61
|
+
ASSERT_EQUAL(define_color_enum().value(), colorEnum.value());
|
43
62
|
}
|
44
63
|
|
45
64
|
TESTCASE(each)
|
46
65
|
{
|
47
|
-
|
48
|
-
|
66
|
+
Module m = define_module("Testing");
|
67
|
+
|
68
|
+
Enum<Color> colorEnum = define_color_enum();
|
69
|
+
|
70
|
+
std::string code = R"(a = []
|
71
|
+
Color.each { |x| a << x }
|
72
|
+
a)";
|
73
|
+
Array a = m.instance_eval(code);
|
49
74
|
ASSERT_EQUAL(3u, a.size());
|
50
|
-
|
51
|
-
|
52
|
-
ASSERT_EQUAL(
|
75
|
+
|
76
|
+
Data_Object<Color> enum_0(a[0]);
|
77
|
+
ASSERT_EQUAL(RED, *enum_0);
|
78
|
+
|
79
|
+
Data_Object<Color> enum_1(a[1]);
|
80
|
+
ASSERT_EQUAL(BLACK, *enum_1);
|
81
|
+
|
82
|
+
Data_Object<Color> enum_2(a[2]);
|
83
|
+
ASSERT_EQUAL(GREEN, *enum_2);
|
84
|
+
}
|
85
|
+
|
86
|
+
TESTCASE(each_seasons)
|
87
|
+
{
|
88
|
+
Module m = define_module("Testing");
|
89
|
+
|
90
|
+
Enum<Season> rb_cSeason = define_season_enum();
|
91
|
+
std::string code = R"(a = []
|
92
|
+
Season.each { |x| a << x }
|
93
|
+
a)";
|
94
|
+
|
95
|
+
Array a = m.instance_eval(code);
|
96
|
+
ASSERT_EQUAL(4u, a.size());
|
97
|
+
|
98
|
+
Data_Object<Season> enum_0(a[0]);
|
99
|
+
ASSERT_EQUAL(Season::Spring, *enum_0);
|
100
|
+
|
101
|
+
Data_Object<Season> enum_1(a[1]);
|
102
|
+
ASSERT_EQUAL(Season::Summer, *enum_1);
|
103
|
+
|
104
|
+
Data_Object<Season> enum_2(a[2]);
|
105
|
+
ASSERT_EQUAL(Season::Fall, *enum_2);
|
106
|
+
|
107
|
+
Data_Object<Season> enum_3(a[3]);
|
108
|
+
ASSERT_EQUAL(Season::Winter, *enum_3);
|
53
109
|
}
|
54
110
|
|
55
111
|
TESTCASE(to_s)
|
56
112
|
{
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
ASSERT_EQUAL(String("
|
113
|
+
Module m = define_module("Testing");
|
114
|
+
|
115
|
+
Enum<Color> colorEnum = define_color_enum();
|
116
|
+
ASSERT_EQUAL(String("RED"), String(m.instance_eval("Color::RED.to_s")));
|
117
|
+
ASSERT_EQUAL(String("BLACK"), String(m.instance_eval("Color::BLACK.to_s")));
|
118
|
+
ASSERT_EQUAL(String("GREEN"), String(m.instance_eval("Color::GREEN.to_s")));
|
61
119
|
}
|
62
120
|
|
63
121
|
TESTCASE(to_i)
|
64
122
|
{
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
ASSERT_EQUAL(to_ruby(int(
|
123
|
+
Module m = define_module("Testing");
|
124
|
+
|
125
|
+
Enum<Color> colorEnum = define_color_enum();
|
126
|
+
ASSERT_EQUAL(detail::to_ruby(int(RED)), m.instance_eval("Color::RED.to_i").value());
|
127
|
+
ASSERT_EQUAL(detail::to_ruby(int(BLACK)), m.instance_eval("Color::BLACK.to_i").value());
|
128
|
+
ASSERT_EQUAL(detail::to_ruby(int(GREEN)), m.instance_eval("Color::GREEN.to_i").value());
|
69
129
|
}
|
70
130
|
|
71
131
|
TESTCASE(inspect)
|
72
132
|
{
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
ASSERT_EQUAL(String("#<Color::
|
133
|
+
Module m = define_module("Testing");
|
134
|
+
|
135
|
+
Enum<Color> colorEnum = define_color_enum();
|
136
|
+
ASSERT_EQUAL(String("#<Color::RED>"), String(m.instance_eval("Color::RED.inspect")));
|
137
|
+
ASSERT_EQUAL(String("#<Color::BLACK>"), String(m.instance_eval("Color::BLACK.inspect")));
|
138
|
+
ASSERT_EQUAL(String("#<Color::GREEN>"), String(m.instance_eval("Color::GREEN.inspect")));
|
77
139
|
}
|
78
140
|
|
79
141
|
TESTCASE(compare)
|
80
142
|
{
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
ASSERT_EQUAL(to_ruby(
|
143
|
+
Module m = define_module("Testing");
|
144
|
+
|
145
|
+
Enum<Color> colorEnum = define_color_enum();
|
146
|
+
ASSERT_EQUAL(detail::to_ruby(-1), m.instance_eval("Color::RED <=> Color::BLACK").value());
|
147
|
+
ASSERT_EQUAL(detail::to_ruby(1), m.instance_eval("Color::GREEN <=> Color::RED").value());
|
148
|
+
ASSERT_EQUAL(detail::to_ruby(0), m.instance_eval("Color::BLACK <=> Color::BLACK").value());
|
85
149
|
}
|
86
150
|
|
87
151
|
TESTCASE(eql)
|
88
152
|
{
|
89
|
-
|
90
|
-
ASSERT_EQUAL(to_ruby(false), Object(protect(rb_eval_string, "Color::RED == Color::BLACK")));
|
91
|
-
ASSERT_EQUAL(to_ruby(true), Object(protect(rb_eval_string, "Color::GREEN == Color::GREEN")));
|
92
|
-
}
|
153
|
+
Module m = define_module("Testing");
|
93
154
|
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
Data_Object<Color> invalid(new Color(static_cast<Color>(42)));
|
98
|
-
ASSERT_EQUAL(String("INVALID(42)"), invalid.to_s());
|
155
|
+
Enum<Color> colorEnum = define_color_enum();
|
156
|
+
ASSERT_EQUAL(detail::to_ruby(false), m.instance_eval("Color::RED == Color::BLACK").value());
|
157
|
+
ASSERT_EQUAL(detail::to_ruby(true), m.instance_eval("Color::GREEN == Color::GREEN").value());
|
99
158
|
}
|
100
159
|
|
101
|
-
TESTCASE(
|
160
|
+
TESTCASE(compare_equal)
|
102
161
|
{
|
103
|
-
Enum<Color>
|
104
|
-
|
105
|
-
|
162
|
+
Enum<Color> colorEnum = define_color_enum();
|
163
|
+
Object color1 = colorEnum.const_get("RED");
|
164
|
+
Object color2 = colorEnum.const_get("RED");
|
165
|
+
Object result = color1.call("<=>", color2);
|
166
|
+
ASSERT_EQUAL(0, detail::From_Ruby<int>().convert(result));
|
106
167
|
}
|
107
168
|
|
108
|
-
TESTCASE(
|
169
|
+
TESTCASE(compare_less)
|
109
170
|
{
|
110
|
-
Enum<Color>
|
111
|
-
|
112
|
-
|
171
|
+
Enum<Color> colorEnum = define_color_enum();
|
172
|
+
Object color1 = colorEnum.const_get("RED");
|
173
|
+
Object color2 = colorEnum.const_get("BLACK");
|
174
|
+
Object result = color1.call("<=>", color2);
|
175
|
+
ASSERT_EQUAL(-1, detail::From_Ruby<int>().convert(result));
|
113
176
|
}
|
114
177
|
|
115
|
-
TESTCASE(
|
178
|
+
TESTCASE(compare_more)
|
116
179
|
{
|
117
|
-
Enum<Color>
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
ASSERT_EQUAL(
|
122
|
-
ASSERT_EQUAL(0, invalid.compare(invalid));
|
180
|
+
Enum<Color> colorEnum = define_color_enum();
|
181
|
+
Object color1 = colorEnum.const_get("GREEN");
|
182
|
+
Object color2 = colorEnum.const_get("BLACK");
|
183
|
+
Object result = color1.call("<=>", color2);
|
184
|
+
ASSERT_EQUAL(1, detail::From_Ruby<int>().convert(result));
|
123
185
|
}
|
124
186
|
|
125
|
-
TESTCASE(
|
187
|
+
TESTCASE(different_objects_eql)
|
126
188
|
{
|
127
|
-
Enum<Color>
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
ASSERT_EQUAL(
|
132
|
-
ASSERT_EQUAL(true, invalid == invalid);
|
189
|
+
Enum<Color> colorEnum = define_color_enum();
|
190
|
+
Object black1 = colorEnum.const_get("BLACK");
|
191
|
+
Object black2 = colorEnum.const_get("BLACK");
|
192
|
+
Object result = black1.call("==", black2);
|
193
|
+
ASSERT_EQUAL(Qtrue, result.value());
|
133
194
|
}
|
134
195
|
|
135
|
-
TESTCASE(
|
196
|
+
TESTCASE(hash)
|
136
197
|
{
|
137
|
-
Enum<Color>
|
138
|
-
|
139
|
-
|
140
|
-
ASSERT_EQUAL(
|
198
|
+
Enum<Color> colorEnum = define_color_enum();
|
199
|
+
Object color = colorEnum.const_get("GREEN");
|
200
|
+
Object result = color.call("hash");
|
201
|
+
ASSERT_EQUAL((int)Color::GREEN, detail::From_Ruby<int>().convert(result));
|
141
202
|
}
|
142
203
|
|
143
|
-
TESTCASE(
|
204
|
+
TESTCASE(from_int)
|
144
205
|
{
|
145
|
-
Enum<Color>
|
146
|
-
|
147
|
-
|
148
|
-
ASSERT_EQUAL(true, red1 == red2);
|
206
|
+
Enum<Color> colorEnum = define_color_enum();
|
207
|
+
Object result = colorEnum.call("from_int", 2);
|
208
|
+
ASSERT_EQUAL(result, colorEnum.const_get("GREEN"));
|
149
209
|
}
|
150
210
|
|
151
|
-
TESTCASE(
|
211
|
+
TESTCASE(from_int_invalid)
|
152
212
|
{
|
153
|
-
Enum<Color>
|
154
|
-
Data_Object<Color> red(new Color(RED));
|
155
|
-
ASSERT_EQUAL(to_ruby(int(RED)), red.call("hash"));
|
156
|
-
}
|
213
|
+
Enum<Color> colorEnum = define_color_enum();
|
157
214
|
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
215
|
+
ASSERT_EXCEPTION_CHECK(
|
216
|
+
Exception,
|
217
|
+
Object result = colorEnum.call("from_int", 22),
|
218
|
+
ASSERT_EQUAL("Unknown enum value: 22", ex.what())
|
219
|
+
);
|
163
220
|
}
|
164
221
|
|
165
222
|
namespace
|
@@ -178,38 +235,107 @@ namespace
|
|
178
235
|
|
179
236
|
TESTCASE(nested_enums)
|
180
237
|
{
|
238
|
+
Data_Type<Inner> inner = define_class<Inner>("Inner");
|
239
|
+
define_enum<Inner::Props>("Props", inner)
|
240
|
+
.define_value("VALUE1", Inner::VALUE1)
|
241
|
+
.define_value("VALUE2", Inner::VALUE2)
|
242
|
+
.define_value("VALUE3", Inner::VALUE3);
|
243
|
+
inner.define_constructor(Constructor<Inner>());
|
244
|
+
|
245
|
+
Module m = define_module("Testing");
|
246
|
+
|
247
|
+
ASSERT_EQUAL(detail::to_ruby(int(0)), m.instance_eval("Inner::Props::VALUE1.to_i").value());
|
248
|
+
ASSERT_EQUAL(detail::to_ruby(int(1)), m.instance_eval("Inner::Props::VALUE2.to_i").value());
|
249
|
+
ASSERT_EQUAL(detail::to_ruby(int(2)), m.instance_eval("Inner::Props::VALUE3.to_i").value());
|
250
|
+
}
|
251
|
+
|
252
|
+
namespace
|
253
|
+
{
|
254
|
+
Color myFavoriteColor()
|
181
255
|
{
|
182
|
-
|
183
|
-
define_enum<Inner::Props>("Props", inner)
|
184
|
-
.define_value("VALUE1", Inner::VALUE1)
|
185
|
-
.define_value("VALUE2", Inner::VALUE2)
|
186
|
-
.define_value("VALUE3", Inner::VALUE3);
|
187
|
-
inner.define_constructor(Constructor<Inner>());
|
256
|
+
return RED;
|
188
257
|
}
|
189
258
|
|
190
|
-
|
191
|
-
|
192
|
-
|
259
|
+
bool isMyFavoriteColor(Color aColor)
|
260
|
+
{
|
261
|
+
return aColor == RED;
|
262
|
+
}
|
263
|
+
}
|
264
|
+
|
265
|
+
TESTCASE(using_enums)
|
266
|
+
{
|
267
|
+
Enum<Color> colorEnum = define_color_enum();
|
268
|
+
colorEnum.define_singleton_function("my_favorite_color", &myFavoriteColor)
|
269
|
+
.define_singleton_function("is_my_favorite_color", &isMyFavoriteColor)
|
270
|
+
.define_singleton_function("is_my_favorite_color", &isMyFavoriteColor)
|
271
|
+
.define_method("is_my_favorite_color", &isMyFavoriteColor);
|
272
|
+
|
273
|
+
Module m = define_module("Testing");
|
274
|
+
|
275
|
+
Object result = m.instance_eval("Color.my_favorite_color");
|
276
|
+
ASSERT_EQUAL(RED, detail::From_Ruby<Color>().convert(result.value()));
|
277
|
+
|
278
|
+
result = m.instance_eval("Color.is_my_favorite_color(Color::RED)");
|
279
|
+
ASSERT_EQUAL(Qtrue, result.value());
|
280
|
+
|
281
|
+
result = m.instance_eval("Color.is_my_favorite_color(Color::BLACK)");
|
282
|
+
ASSERT_EQUAL(Qfalse, result.value());
|
283
|
+
|
284
|
+
result = m.instance_eval("Color::RED.is_my_favorite_color");
|
285
|
+
ASSERT_EQUAL(Qtrue, result.value());
|
286
|
+
|
287
|
+
result = m.instance_eval("Color::BLACK.is_my_favorite_color");
|
288
|
+
ASSERT_EQUAL(Qfalse, result.value());
|
193
289
|
}
|
194
290
|
|
195
291
|
namespace
|
196
292
|
{
|
197
|
-
Color
|
293
|
+
Color defaultColor(Color aColor = BLACK)
|
198
294
|
{
|
199
|
-
return
|
295
|
+
return aColor;
|
200
296
|
}
|
201
297
|
}
|
202
298
|
|
203
|
-
TESTCASE(
|
299
|
+
TESTCASE(default_argument)
|
204
300
|
{
|
205
|
-
|
206
|
-
define_global_function("get_enum", &getEnum);
|
301
|
+
define_global_function("default_color", &defaultColor, Arg("aColor") = Color::BLACK);
|
207
302
|
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
303
|
+
Module m = define_module("Testing");
|
304
|
+
Object result = m.instance_eval("default_color");
|
305
|
+
ASSERT_EQUAL(Color::BLACK, detail::From_Ruby<Color>().convert(result.value()));
|
306
|
+
}
|
307
|
+
|
308
|
+
namespace
|
309
|
+
{
|
310
|
+
enum class Undefined { A, B, C };
|
311
|
+
|
312
|
+
void undefinedArg(Undefined undefined)
|
313
|
+
{
|
314
|
+
}
|
212
315
|
|
213
|
-
|
214
|
-
|
316
|
+
Undefined undefinedReturn()
|
317
|
+
{
|
318
|
+
return Undefined::B;
|
319
|
+
}
|
215
320
|
}
|
321
|
+
|
322
|
+
TESTCASE(not_defined)
|
323
|
+
{
|
324
|
+
#ifdef _MSC_VER
|
325
|
+
const char* message = "Type is not defined with Rice: enum `anonymous namespace'::Undefined";
|
326
|
+
#else
|
327
|
+
const char* message = "Type is not defined with Rice: (anonymous namespace)::Undefined";
|
328
|
+
#endif
|
329
|
+
|
330
|
+
ASSERT_EXCEPTION_CHECK(
|
331
|
+
std::invalid_argument,
|
332
|
+
define_global_function("undefined_arg", &undefinedArg),
|
333
|
+
ASSERT_EQUAL(message, ex.what())
|
334
|
+
);
|
335
|
+
|
336
|
+
ASSERT_EXCEPTION_CHECK(
|
337
|
+
std::invalid_argument,
|
338
|
+
define_global_function("undefined_return", &undefinedReturn),
|
339
|
+
ASSERT_EQUAL(message, ex.what())
|
340
|
+
);
|
341
|
+
}
|