rice2 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (190) hide show
  1. checksums.yaml +7 -0
  2. data/COPYING +23 -0
  3. data/Doxyfile +2268 -0
  4. data/Makefile.am +26 -0
  5. data/Makefile.in +929 -0
  6. data/README.md +1054 -0
  7. data/README.mingw +8 -0
  8. data/Rakefile +24 -0
  9. data/aclocal.m4 +1090 -0
  10. data/bootstrap +8 -0
  11. data/config.guess +1667 -0
  12. data/config.sub +1793 -0
  13. data/configure +8209 -0
  14. data/configure.ac +55 -0
  15. data/depcomp +791 -0
  16. data/doxygen.ac +314 -0
  17. data/doxygen.am +186 -0
  18. data/extconf.rb +66 -0
  19. data/install-sh +529 -0
  20. data/missing +215 -0
  21. data/post-autoconf.rb +22 -0
  22. data/post-automake.rb +28 -0
  23. data/rice/Address_Registration_Guard.cpp +22 -0
  24. data/rice/Address_Registration_Guard.hpp +7 -0
  25. data/rice/Address_Registration_Guard.ipp +37 -0
  26. data/rice/Address_Registration_Guard_defn.hpp +75 -0
  27. data/rice/Arg.hpp +8 -0
  28. data/rice/Arg_impl.hpp +129 -0
  29. data/rice/Arg_operators.cpp +21 -0
  30. data/rice/Arg_operators.hpp +19 -0
  31. data/rice/Array.hpp +214 -0
  32. data/rice/Array.ipp +256 -0
  33. data/rice/Builtin_Object.hpp +8 -0
  34. data/rice/Builtin_Object.ipp +50 -0
  35. data/rice/Builtin_Object_defn.hpp +50 -0
  36. data/rice/Class.cpp +57 -0
  37. data/rice/Class.hpp +8 -0
  38. data/rice/Class.ipp +6 -0
  39. data/rice/Class_defn.hpp +83 -0
  40. data/rice/Constructor.hpp +47 -0
  41. data/rice/Data_Object.hpp +8 -0
  42. data/rice/Data_Object.ipp +133 -0
  43. data/rice/Data_Object_defn.hpp +138 -0
  44. data/rice/Data_Type.cpp +54 -0
  45. data/rice/Data_Type.hpp +8 -0
  46. data/rice/Data_Type.ipp +365 -0
  47. data/rice/Data_Type_defn.hpp +261 -0
  48. data/rice/Data_Type_fwd.hpp +12 -0
  49. data/rice/Director.cpp +13 -0
  50. data/rice/Director.hpp +39 -0
  51. data/rice/Enum.hpp +117 -0
  52. data/rice/Enum.ipp +246 -0
  53. data/rice/Exception.cpp +59 -0
  54. data/rice/Exception.hpp +13 -0
  55. data/rice/Exception_Base.hpp +8 -0
  56. data/rice/Exception_Base.ipp +13 -0
  57. data/rice/Exception_Base_defn.hpp +27 -0
  58. data/rice/Exception_defn.hpp +69 -0
  59. data/rice/Hash.hpp +227 -0
  60. data/rice/Hash.ipp +329 -0
  61. data/rice/Identifier.cpp +8 -0
  62. data/rice/Identifier.hpp +50 -0
  63. data/rice/Identifier.ipp +33 -0
  64. data/rice/Jump_Tag.hpp +24 -0
  65. data/rice/Makefile.am +122 -0
  66. data/rice/Makefile.in +885 -0
  67. data/rice/Module.cpp +84 -0
  68. data/rice/Module.hpp +8 -0
  69. data/rice/Module.ipp +6 -0
  70. data/rice/Module_defn.hpp +88 -0
  71. data/rice/Module_impl.hpp +281 -0
  72. data/rice/Module_impl.ipp +345 -0
  73. data/rice/Object.cpp +169 -0
  74. data/rice/Object.hpp +8 -0
  75. data/rice/Object.ipp +33 -0
  76. data/rice/Object_defn.hpp +214 -0
  77. data/rice/Require_Guard.hpp +21 -0
  78. data/rice/String.cpp +94 -0
  79. data/rice/String.hpp +91 -0
  80. data/rice/Struct.cpp +117 -0
  81. data/rice/Struct.hpp +162 -0
  82. data/rice/Struct.ipp +26 -0
  83. data/rice/Symbol.cpp +25 -0
  84. data/rice/Symbol.hpp +66 -0
  85. data/rice/Symbol.ipp +44 -0
  86. data/rice/config.hpp +47 -0
  87. data/rice/config.hpp.in +46 -0
  88. data/rice/detail/Arguments.hpp +118 -0
  89. data/rice/detail/Auto_Function_Wrapper.hpp +898 -0
  90. data/rice/detail/Auto_Function_Wrapper.ipp +3694 -0
  91. data/rice/detail/Auto_Member_Function_Wrapper.hpp +897 -0
  92. data/rice/detail/Auto_Member_Function_Wrapper.ipp +2774 -0
  93. data/rice/detail/Caster.hpp +103 -0
  94. data/rice/detail/Exception_Handler.hpp +8 -0
  95. data/rice/detail/Exception_Handler.ipp +68 -0
  96. data/rice/detail/Exception_Handler_defn.hpp +96 -0
  97. data/rice/detail/Iterator.hpp +93 -0
  98. data/rice/detail/Not_Copyable.hpp +25 -0
  99. data/rice/detail/Wrapped_Function.hpp +33 -0
  100. data/rice/detail/cfp.hpp +24 -0
  101. data/rice/detail/cfp.ipp +51 -0
  102. data/rice/detail/check_ruby_type.cpp +27 -0
  103. data/rice/detail/check_ruby_type.hpp +23 -0
  104. data/rice/detail/creation_funcs.hpp +37 -0
  105. data/rice/detail/creation_funcs.ipp +36 -0
  106. data/rice/detail/default_allocation_func.hpp +23 -0
  107. data/rice/detail/default_allocation_func.ipp +11 -0
  108. data/rice/detail/define_method_and_auto_wrap.hpp +31 -0
  109. data/rice/detail/define_method_and_auto_wrap.ipp +30 -0
  110. data/rice/detail/demangle.cpp +56 -0
  111. data/rice/detail/demangle.hpp +19 -0
  112. data/rice/detail/env.hpp +11 -0
  113. data/rice/detail/from_ruby.hpp +43 -0
  114. data/rice/detail/from_ruby.ipp +60 -0
  115. data/rice/detail/method_data.cpp +92 -0
  116. data/rice/detail/method_data.hpp +21 -0
  117. data/rice/detail/node.hpp +13 -0
  118. data/rice/detail/protect.cpp +29 -0
  119. data/rice/detail/protect.hpp +34 -0
  120. data/rice/detail/ruby.hpp +74 -0
  121. data/rice/detail/ruby_version_code.hpp +6 -0
  122. data/rice/detail/ruby_version_code.hpp.in +6 -0
  123. data/rice/detail/st.hpp +22 -0
  124. data/rice/detail/to_ruby.hpp +22 -0
  125. data/rice/detail/to_ruby.ipp +36 -0
  126. data/rice/detail/traits.hpp +43 -0
  127. data/rice/detail/win32.hpp +16 -0
  128. data/rice/detail/wrap_function.hpp +66 -0
  129. data/rice/global_function.hpp +33 -0
  130. data/rice/global_function.ipp +22 -0
  131. data/rice/protect.hpp +38 -0
  132. data/rice/protect.ipp +1134 -0
  133. data/rice/ruby_mark.hpp +13 -0
  134. data/rice/ruby_try_catch.hpp +86 -0
  135. data/rice/rubypp.rb +97 -0
  136. data/rice/to_from_ruby.hpp +8 -0
  137. data/rice/to_from_ruby.ipp +418 -0
  138. data/rice/to_from_ruby_defn.hpp +70 -0
  139. data/ruby.ac +135 -0
  140. data/ruby/Makefile.am +1 -0
  141. data/ruby/Makefile.in +628 -0
  142. data/ruby/lib/Makefile.am +3 -0
  143. data/ruby/lib/Makefile.in +506 -0
  144. data/ruby/lib/mkmf-rice.rb.in +217 -0
  145. data/ruby/lib/version.rb +3 -0
  146. data/sample/Makefile.am +47 -0
  147. data/sample/Makefile.in +489 -0
  148. data/sample/enum/extconf.rb +3 -0
  149. data/sample/enum/sample_enum.cpp +54 -0
  150. data/sample/enum/test.rb +8 -0
  151. data/sample/inheritance/animals.cpp +98 -0
  152. data/sample/inheritance/extconf.rb +3 -0
  153. data/sample/inheritance/test.rb +7 -0
  154. data/sample/map/extconf.rb +3 -0
  155. data/sample/map/map.cpp +81 -0
  156. data/sample/map/test.rb +7 -0
  157. data/test/Makefile.am +72 -0
  158. data/test/Makefile.in +1213 -0
  159. data/test/ext/Makefile.am +41 -0
  160. data/test/ext/Makefile.in +483 -0
  161. data/test/ext/t1/Foo.hpp +10 -0
  162. data/test/ext/t1/extconf.rb +2 -0
  163. data/test/ext/t1/t1.cpp +15 -0
  164. data/test/ext/t2/extconf.rb +2 -0
  165. data/test/ext/t2/t2.cpp +11 -0
  166. data/test/test_Address_Registration_Guard.cpp +43 -0
  167. data/test/test_Array.cpp +248 -0
  168. data/test/test_Builtin_Object.cpp +71 -0
  169. data/test/test_Class.cpp +496 -0
  170. data/test/test_Constructor.cpp +128 -0
  171. data/test/test_Data_Object.cpp +275 -0
  172. data/test/test_Data_Type.cpp +348 -0
  173. data/test/test_Director.cpp +308 -0
  174. data/test/test_Enum.cpp +215 -0
  175. data/test/test_Exception.cpp +47 -0
  176. data/test/test_Hash.cpp +212 -0
  177. data/test/test_Identifier.cpp +70 -0
  178. data/test/test_Jump_Tag.cpp +17 -0
  179. data/test/test_Memory_Management.cpp +50 -0
  180. data/test/test_Module.cpp +497 -0
  181. data/test/test_Object.cpp +159 -0
  182. data/test/test_String.cpp +107 -0
  183. data/test/test_Struct.cpp +205 -0
  184. data/test/test_Symbol.cpp +63 -0
  185. data/test/test_To_From_Ruby.cpp +428 -0
  186. data/test/test_global_functions.cpp +114 -0
  187. data/test/test_rice.rb +41 -0
  188. data/test/unittest.cpp +136 -0
  189. data/test/unittest.hpp +294 -0
  190. metadata +297 -0
@@ -0,0 +1,308 @@
1
+ #include "unittest.hpp"
2
+ #include "rice/Exception.hpp"
3
+ #include "rice/Director.hpp"
4
+ #include "rice/Constructor.hpp"
5
+ #include "rice/Data_Type.hpp"
6
+
7
+ #include <vector>
8
+ #include <iostream>
9
+
10
+ using namespace std;
11
+ using namespace Rice;
12
+
13
+ TESTSUITE(Director);
14
+
15
+ namespace {
16
+ /**
17
+ * Abstract base class
18
+ */
19
+ class Worker {
20
+ public:
21
+ virtual ~Worker() { }
22
+
23
+ int getNumber() { return 12; }
24
+
25
+ virtual int doSomething(int num) { return num * 4; }
26
+
27
+ virtual int process(int num) = 0;
28
+ };
29
+
30
+ /**
31
+ * Subclass that implements pure virtual
32
+ */
33
+ class MultiplyWorker : public Worker {
34
+ public:
35
+ virtual ~MultiplyWorker() { }
36
+
37
+ virtual int process(int num) { return num * 2; }
38
+ };
39
+
40
+ /**
41
+ * Class to handle workers
42
+ */
43
+ class Handler {
44
+ std::vector<Worker*> mWorkers;
45
+
46
+ public:
47
+
48
+ void addWorker(Worker* worker) { mWorkers.push_back(worker); }
49
+
50
+ int processWorkers(int start) {
51
+ std::vector<Worker*>::iterator i = mWorkers.begin();
52
+ int results = start;
53
+
54
+ for(; i != mWorkers.end(); i++) {
55
+ results = (*i)->process(results);
56
+ }
57
+
58
+ return results;
59
+ }
60
+ };
61
+
62
+ /**
63
+ * Our Director wrapper of Worker
64
+ */
65
+ class WorkerDirector : public Worker, public Rice::Director {
66
+ public:
67
+ WorkerDirector(Object self) : Director(self) { }
68
+
69
+ virtual int process(int num) {
70
+ return from_ruby<int>( getSelf().call("process", num) );
71
+ }
72
+
73
+ int default_process(int num) {
74
+ raisePureVirtual();
75
+ return 0;
76
+ }
77
+
78
+ virtual int doSomething(int num) {
79
+ return from_ruby<int>( getSelf().call("do_something", num) );
80
+ }
81
+
82
+ int default_doSomething(int num) {
83
+ return Worker::doSomething(num);
84
+ }
85
+ };
86
+ };
87
+
88
+ SETUP(Director)
89
+ {
90
+ ruby_init();
91
+ }
92
+
93
+ TESTCASE(exposes_worker_as_instantiatable_class)
94
+ {
95
+ define_class<Worker>("Worker")
96
+ .define_director<WorkerDirector>()
97
+ .define_constructor(Constructor<WorkerDirector, Object>())
98
+ .define_method("get_number", &Worker::getNumber);
99
+
100
+ Module m = define_module("Testing");
101
+ Object result = m.instance_eval("worker = Worker.new; worker.get_number");
102
+
103
+ ASSERT_EQUAL(12, from_ruby<int>(result.value()));
104
+ }
105
+
106
+ TESTCASE(can_call_virtual_methods_on_base_class)
107
+ {
108
+ define_class<Worker>("Worker")
109
+ .define_director<WorkerDirector>()
110
+ .define_constructor(Constructor<WorkerDirector, Object>())
111
+ .define_method("get_number", &Worker::getNumber)
112
+ .define_method("do_something", &WorkerDirector::default_doSomething);
113
+
114
+ Module m = define_module("Testing");
115
+
116
+ Object result = m.instance_eval("worker = Worker.new; worker.do_something(4)");
117
+
118
+ ASSERT_EQUAL(16, from_ruby<int>(result.value()));
119
+ }
120
+
121
+ TESTCASE(super_calls_pass_execution_up_the_inheritance_chain)
122
+ {
123
+ define_class<Worker>("Worker")
124
+ .define_director<WorkerDirector>()
125
+ .define_constructor(Constructor<WorkerDirector, Object>())
126
+ .define_method("do_something", &WorkerDirector::default_doSomething);
127
+
128
+ Module m = define_module("Testing");
129
+ m.instance_eval("class RubyWorker < Worker; def do_something(num); super * num; end; end");
130
+
131
+ Object result = m.instance_eval("worker = RubyWorker.new; worker.do_something(10)");
132
+
133
+ ASSERT_EQUAL(400, from_ruby<int>(result.value()));
134
+ }
135
+
136
+ TESTCASE(super_calls_on_pure_virtual_raise_error)
137
+ {
138
+ define_class<Worker>("Worker")
139
+ .define_director<WorkerDirector>()
140
+ .define_constructor(Constructor<WorkerDirector, Object>())
141
+ .define_method("process", &WorkerDirector::default_process);
142
+
143
+ Module m = define_module("Testing");
144
+ m.instance_eval("class RubyWorker < Worker; def process(num); super; end; end");
145
+
146
+ ASSERT_EXCEPTION_CHECK(
147
+ Exception,
148
+ m.instance_eval("worker = RubyWorker.new; worker.process(10)"),
149
+ ASSERT_EQUAL(
150
+ Object(rb_eNotImpError),
151
+ Object(CLASS_OF(ex.value()))
152
+ )
153
+ );
154
+ }
155
+
156
+ TESTCASE(polymorphic_calls_head_down_the_call_chain)
157
+ {
158
+ define_class<Handler>("Handler")
159
+ .define_constructor(Constructor<Handler>())
160
+ .define_method("add_worker", &Handler::addWorker)
161
+ .define_method("process_workers", &Handler::processWorkers);
162
+
163
+ define_class<Worker>("Worker")
164
+ .define_director<WorkerDirector>()
165
+ .define_constructor(Constructor<WorkerDirector, Object>())
166
+ .define_method("process", &WorkerDirector::default_process);
167
+
168
+ Module m = define_module("Testing");
169
+
170
+ m.instance_eval(
171
+ "class EchoWorker < Worker; def process(num); num + 2; end; end;"
172
+ "class DoubleWorker < Worker; def process(num); num * 2; end; end;"
173
+ "$handler = Handler.new;"
174
+ "$handler.add_worker(EchoWorker.new);"
175
+ "$handler.add_worker(DoubleWorker.new);"
176
+ );
177
+
178
+ Object result = m.instance_eval("$handler.process_workers(5)");
179
+
180
+ // Hit's EchoWorker, so 5 + 2, then passes that to DoubleWorker, so 7 * 2 = 14
181
+ ASSERT_EQUAL(14, from_ruby<int>(result.value()));
182
+ }
183
+
184
+ namespace {
185
+
186
+ class CallsSelf {
187
+ public:
188
+ virtual ~CallsSelf() { }
189
+
190
+ int doIt(int in) {
191
+ return doItImpl(in);
192
+ }
193
+
194
+ virtual int doItImpl(int in) = 0;
195
+ };
196
+
197
+ class CallsSelfDirector : public CallsSelf, public Director {
198
+
199
+ public:
200
+ CallsSelfDirector(Object self) : Director(self) { }
201
+ virtual ~CallsSelfDirector() { }
202
+
203
+ virtual int doItImpl(int in) {
204
+ return from_ruby<int>( getSelf().call("do_it_impl", in) );
205
+ }
206
+
207
+ int default_doItImpl(int in) {
208
+ raisePureVirtual();
209
+ return 0;
210
+ }
211
+ };
212
+
213
+ struct MyCallsSelf : CallsSelf {
214
+ MyCallsSelf() { }
215
+ virtual ~MyCallsSelf() { }
216
+
217
+ int doItImpl(int in) { return in * 12; }
218
+ };
219
+
220
+ // Abstract type return types handled properly
221
+ CallsSelf* getCallsSelf() {
222
+ return new MyCallsSelf();
223
+ }
224
+
225
+ // Abstract type Argument types handled properly
226
+ int doItOnPointer(CallsSelf* obj, int in) {
227
+ return obj->doIt(in);
228
+ }
229
+
230
+ int doItOnReference(CallsSelf& obj, int in) {
231
+ return obj.doIt(in);
232
+ }
233
+
234
+ }
235
+
236
+ TESTCASE(mix_of_polymorphic_calls_and_inheritance_dont_cause_infinite_loops)
237
+ {
238
+ define_class<CallsSelf>("CallsSelf")
239
+ .define_director<CallsSelfDirector>()
240
+ .define_constructor(Constructor<CallsSelfDirector, Rice::Object>())
241
+ .define_method("do_it_impl", &CallsSelfDirector::default_doItImpl)
242
+ .define_method("do_it", &CallsSelf::doIt);
243
+
244
+ Module m = define_module("Testing");
245
+
246
+ Object result = m.instance_eval(
247
+ "class MySelf < CallsSelf; def do_it_impl(num); num * 10; end; end;"
248
+ "c = MySelf.new; c.do_it(10)"
249
+ );
250
+
251
+ ASSERT_EQUAL(100, from_ruby<int>(result.value()));
252
+ }
253
+
254
+ TESTCASE(director_class_super_classes_get_type_bound)
255
+ {
256
+ Module m = define_module("Testing");
257
+ m.define_module_function("get_calls_self", &getCallsSelf);
258
+
259
+ define_class<CallsSelf>("CallsSelf")
260
+ .define_director<CallsSelfDirector>()
261
+ .define_constructor(Constructor<CallsSelfDirector, Rice::Object>())
262
+ .define_method("do_it_impl", &CallsSelfDirector::default_doItImpl)
263
+ .define_method("do_it", &CallsSelf::doIt);
264
+
265
+ Object result = m.instance_eval("cs = Testing::get_calls_self; cs.do_it(3);");
266
+ ASSERT_EQUAL(36, from_ruby<int>(result.value()));
267
+ }
268
+
269
+ TESTCASE(director_allows_abstract_types_used_as_parameters_pointers)
270
+ {
271
+ Module m = define_module("Testing");
272
+ m.define_module_function("do_it_on_pointer", &doItOnPointer);
273
+
274
+ define_class<CallsSelf>("CallsSelf")
275
+ .define_director<CallsSelfDirector>()
276
+ .define_constructor(Constructor<CallsSelfDirector, Rice::Object>())
277
+ .define_method("do_it_impl", &CallsSelfDirector::default_doItImpl)
278
+ .define_method("do_it", &CallsSelf::doIt);
279
+
280
+ Object result = m.instance_eval(
281
+ "class MySelf < CallsSelf; def do_it_impl(num); num * 10; end; end;"
282
+ "c = MySelf.new;"
283
+ "Testing::do_it_on_pointer(c, 5)"
284
+ );
285
+
286
+ ASSERT_EQUAL(50, from_ruby<int>(result.value()));
287
+ }
288
+ /*
289
+ TESTCASE(director_allows_abstract_types_used_as_parameters_reference)
290
+ {
291
+ Module m = define_module("Testing");
292
+ m.define_module_function("do_it_on_ref", &doItOnReference);
293
+
294
+ define_class<CallsSelf>("CallsSelf")
295
+ .define_director<CallsSelfDirector>()
296
+ .define_constructor(Constructor<CallsSelfDirector, Rice::Object>())
297
+ .define_method("do_it_impl", &CallsSelfDirector::default_doItImpl)
298
+ .define_method("do_it", &CallsSelf::doIt);
299
+
300
+ Object result = m.instance_eval(
301
+ "class MySelf < CallsSelf; def do_it_impl(num); num * 10; end; end;"
302
+ "c = MySelf.new;"
303
+ "Testing::do_it_on_ref(c, 3)"
304
+ );
305
+
306
+ ASSERT_EQUAL(30, from_ruby<int>(result.value()));
307
+ }
308
+ */
@@ -0,0 +1,215 @@
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
+ #include <iostream>
8
+
9
+ using namespace Rice;
10
+
11
+ TESTSUITE(Enum);
12
+
13
+ namespace
14
+ {
15
+ enum Color { RED, BLACK, GREEN };
16
+
17
+ Enum<Color> define_color_enum()
18
+ {
19
+ static Enum<Color> colors = define_enum<Color>("Color")
20
+ .define_value("RED", RED)
21
+ .define_value("BLACK", BLACK)
22
+ .define_value("GREEN", GREEN);
23
+ return colors;
24
+ }
25
+ }
26
+
27
+ SETUP(Enum)
28
+ {
29
+ ruby_init();
30
+ }
31
+
32
+ TESTCASE(copy_construct)
33
+ {
34
+ Enum<Color> rb_cColor(define_color_enum());
35
+ ASSERT_EQUAL(define_color_enum().value(), rb_cColor.value());
36
+ }
37
+
38
+ TESTCASE(assignment)
39
+ {
40
+ Enum<Color> rb_cColor;
41
+ rb_cColor = define_color_enum();
42
+ ASSERT_EQUAL(define_color_enum().value(), rb_cColor.value());
43
+ }
44
+
45
+ TESTCASE(each)
46
+ {
47
+ Enum<Color> rb_cColor = define_color_enum();
48
+ Array a = protect(rb_eval_string, "a = []; Color.each { |x| a << x }; a");
49
+ ASSERT_EQUAL(3u, a.size());
50
+ ASSERT_EQUAL(RED, from_ruby<Color>(a[0]));
51
+ ASSERT_EQUAL(BLACK, from_ruby<Color>(a[1]));
52
+ ASSERT_EQUAL(GREEN, from_ruby<Color>(a[2]));
53
+ }
54
+
55
+ TESTCASE(to_s)
56
+ {
57
+ Enum<Color> rb_cColor = define_color_enum();
58
+ ASSERT_EQUAL(String("RED"), String(protect(rb_eval_string, "Color::RED.to_s")));
59
+ ASSERT_EQUAL(String("BLACK"), String(protect(rb_eval_string, "Color::BLACK.to_s")));
60
+ ASSERT_EQUAL(String("GREEN"), String(protect(rb_eval_string, "Color::GREEN.to_s")));
61
+ }
62
+
63
+ TESTCASE(to_i)
64
+ {
65
+ Enum<Color> rb_cColor = define_color_enum();
66
+ ASSERT_EQUAL(to_ruby(int(RED)), Object(protect(rb_eval_string, "Color::RED.to_i")));
67
+ ASSERT_EQUAL(to_ruby(int(BLACK)), Object(protect(rb_eval_string, "Color::BLACK.to_i")));
68
+ ASSERT_EQUAL(to_ruby(int(GREEN)), Object(protect(rb_eval_string, "Color::GREEN.to_i")));
69
+ }
70
+
71
+ TESTCASE(inspect)
72
+ {
73
+ Enum<Color> rb_cColor = define_color_enum();
74
+ ASSERT_EQUAL(String("#<Color::RED>"), String(protect(rb_eval_string, "Color::RED.inspect")));
75
+ ASSERT_EQUAL(String("#<Color::BLACK>"), String(protect(rb_eval_string, "Color::BLACK.inspect")));
76
+ ASSERT_EQUAL(String("#<Color::GREEN>"), String(protect(rb_eval_string, "Color::GREEN.inspect")));
77
+ }
78
+
79
+ TESTCASE(compare)
80
+ {
81
+ Enum<Color> rb_cColor = define_color_enum();
82
+ ASSERT_EQUAL(to_ruby(-1), Object(protect(rb_eval_string, "Color::RED <=> Color::BLACK")));
83
+ ASSERT_EQUAL(to_ruby(1), Object(protect(rb_eval_string, "Color::GREEN <=> Color::RED")));
84
+ ASSERT_EQUAL(to_ruby(0), Object(protect(rb_eval_string, "Color::BLACK <=> Color::BLACK")));
85
+ }
86
+
87
+ TESTCASE(eql)
88
+ {
89
+ Enum<Color> rb_cColor = define_color_enum();
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
+ }
93
+
94
+ TESTCASE(invalid_to_s)
95
+ {
96
+ Enum<Color> rb_cColor = define_color_enum();
97
+ Data_Object<Color> invalid(new Color(static_cast<Color>(42)));
98
+ ASSERT_EQUAL(String("INVALID(42)"), invalid.to_s());
99
+ }
100
+
101
+ TESTCASE(invalid_to_i)
102
+ {
103
+ Enum<Color> rb_cColor = define_color_enum();
104
+ Data_Object<Color> invalid(new Color(static_cast<Color>(42)));
105
+ ASSERT_EQUAL(to_ruby(42), invalid.call("to_i"));
106
+ }
107
+
108
+ TESTCASE(invalid_inspect)
109
+ {
110
+ Enum<Color> rb_cColor = define_color_enum();
111
+ Data_Object<Color> invalid(new Color(static_cast<Color>(42)));
112
+ ASSERT_EQUAL(String("#<Color::INVALID(42)>"), invalid.inspect());
113
+ }
114
+
115
+ TESTCASE(invalid_compare)
116
+ {
117
+ Enum<Color> rb_cColor = define_color_enum();
118
+ Data_Object<Color> invalid(new Color(static_cast<Color>(42)));
119
+ Data_Object<Color> red(new Color(RED));
120
+ ASSERT_EQUAL(1, invalid.compare(red));
121
+ ASSERT_EQUAL(-1, red.compare(invalid));
122
+ ASSERT_EQUAL(0, invalid.compare(invalid));
123
+ }
124
+
125
+ TESTCASE(invalid_eql)
126
+ {
127
+ Enum<Color> rb_cColor = define_color_enum();
128
+ Data_Object<Color> invalid(new Color(static_cast<Color>(42)));
129
+ Data_Object<Color> red(new Color(RED));
130
+ ASSERT_EQUAL(false, invalid == red);
131
+ ASSERT_EQUAL(false, red == invalid);
132
+ ASSERT_EQUAL(true, invalid == invalid);
133
+ }
134
+
135
+ TESTCASE(different_objects_compare)
136
+ {
137
+ Enum<Color> rb_cColor = define_color_enum();
138
+ Data_Object<Color> red1(new Color(RED));
139
+ Data_Object<Color> red2(new Color(RED));
140
+ ASSERT_EQUAL(0, red1.compare(red2));
141
+ }
142
+
143
+ TESTCASE(different_objects_eql)
144
+ {
145
+ Enum<Color> rb_cColor = define_color_enum();
146
+ Data_Object<Color> red1(new Color(RED));
147
+ Data_Object<Color> red2(new Color(RED));
148
+ ASSERT_EQUAL(true, red1 == red2);
149
+ }
150
+
151
+ TESTCASE(hash)
152
+ {
153
+ Enum<Color> rb_cColor = define_color_enum();
154
+ Data_Object<Color> red(new Color(RED));
155
+ ASSERT_EQUAL(to_ruby(int(RED)), red.call("hash"));
156
+ }
157
+
158
+ TESTCASE(from_int)
159
+ {
160
+ Enum<Color> rb_cColor = define_color_enum();
161
+ Data_Object<Color> color(rb_cColor.call("from_int", int(RED)));
162
+ ASSERT_EQUAL(RED, *color);
163
+ }
164
+
165
+ namespace
166
+ {
167
+ class Inner
168
+ {
169
+ public:
170
+ enum Props
171
+ {
172
+ VALUE1,
173
+ VALUE2,
174
+ VALUE3
175
+ };
176
+ };
177
+ }
178
+
179
+ TESTCASE(nested_enums)
180
+ {
181
+ {
182
+ Data_Type<Inner> inner = define_class<Inner>("Inner");
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>());
188
+ }
189
+
190
+ ASSERT_EQUAL(to_ruby(int(0)), Object(protect(rb_eval_string, "Inner::Props::VALUE1.to_i")));
191
+ ASSERT_EQUAL(to_ruby(int(1)), Object(protect(rb_eval_string, "Inner::Props::VALUE2.to_i")));
192
+ ASSERT_EQUAL(to_ruby(int(2)), Object(protect(rb_eval_string, "Inner::Props::VALUE3.to_i")));
193
+ }
194
+
195
+ namespace
196
+ {
197
+ Color getEnum()
198
+ {
199
+ return GREEN;
200
+ }
201
+ }
202
+
203
+ TESTCASE(return_enum_value_to_ruby)
204
+ {
205
+ Enum<Color> rb_cColor = define_color_enum();
206
+ define_global_function("get_enum", &getEnum);
207
+
208
+ ASSERT_EQUAL(
209
+ Data_Object<Color>(protect(rb_eval_string, "Color::GREEN")),
210
+ Data_Object<Color>(protect(rb_eval_string, "get_enum"))
211
+ );
212
+
213
+ ASSERT_EQUAL(String("GREEN"), String(protect(rb_eval_string, "get_enum.to_s")));
214
+ ASSERT_EQUAL(String("#<Color::GREEN>"), String(protect(rb_eval_string, "get_enum.inspect")));
215
+ }