rice 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (166) hide show
  1. data/COPYING +23 -0
  2. data/Doxyfile +1253 -0
  3. data/Makefile.am +26 -0
  4. data/Makefile.in +736 -0
  5. data/README +881 -0
  6. data/README.mingw +8 -0
  7. data/bootstrap +8 -0
  8. data/config.guess +1535 -0
  9. data/config.sub +1644 -0
  10. data/configure +7310 -0
  11. data/configure.ac +48 -0
  12. data/depcomp +584 -0
  13. data/doxygen.ac +314 -0
  14. data/doxygen.am +186 -0
  15. data/install-sh +507 -0
  16. data/missing +367 -0
  17. data/post-autoconf.rb +22 -0
  18. data/post-automake.rb +28 -0
  19. data/rice/Address_Registration_Guard.hpp +7 -0
  20. data/rice/Address_Registration_Guard.ipp +34 -0
  21. data/rice/Address_Registration_Guard_defn.hpp +65 -0
  22. data/rice/Allocation_Strategies.hpp +37 -0
  23. data/rice/Array.hpp +220 -0
  24. data/rice/Array.ipp +262 -0
  25. data/rice/Builtin_Object.hpp +8 -0
  26. data/rice/Builtin_Object.ipp +50 -0
  27. data/rice/Builtin_Object_defn.hpp +51 -0
  28. data/rice/Class.cpp +57 -0
  29. data/rice/Class.hpp +8 -0
  30. data/rice/Class.ipp +4 -0
  31. data/rice/Class_defn.hpp +83 -0
  32. data/rice/Constructor.hpp +189 -0
  33. data/rice/Critical_Guard.hpp +34 -0
  34. data/rice/Critical_Guard.ipp +20 -0
  35. data/rice/Data_Object.hpp +127 -0
  36. data/rice/Data_Object.ipp +129 -0
  37. data/rice/Data_Type.cpp +21 -0
  38. data/rice/Data_Type.hpp +8 -0
  39. data/rice/Data_Type.ipp +227 -0
  40. data/rice/Data_Type_defn.hpp +219 -0
  41. data/rice/Data_Type_fwd.hpp +12 -0
  42. data/rice/Enum.hpp +118 -0
  43. data/rice/Enum.ipp +246 -0
  44. data/rice/Exception.cpp +59 -0
  45. data/rice/Exception.hpp +69 -0
  46. data/rice/Exception_Base.hpp +30 -0
  47. data/rice/Exception_Base.ipp +11 -0
  48. data/rice/Hash.hpp +206 -0
  49. data/rice/Hash.ipp +336 -0
  50. data/rice/Identifier.cpp +8 -0
  51. data/rice/Identifier.hpp +50 -0
  52. data/rice/Identifier.ipp +33 -0
  53. data/rice/Jump_Tag.hpp +24 -0
  54. data/rice/Makefile.am +112 -0
  55. data/rice/Makefile.in +675 -0
  56. data/rice/Module.cpp +75 -0
  57. data/rice/Module.hpp +8 -0
  58. data/rice/Module.ipp +6 -0
  59. data/rice/Module_defn.hpp +87 -0
  60. data/rice/Module_impl.hpp +237 -0
  61. data/rice/Module_impl.ipp +302 -0
  62. data/rice/Object.cpp +153 -0
  63. data/rice/Object.hpp +8 -0
  64. data/rice/Object.ipp +19 -0
  65. data/rice/Object_defn.hpp +183 -0
  66. data/rice/Require_Guard.hpp +21 -0
  67. data/rice/String.cpp +93 -0
  68. data/rice/String.hpp +88 -0
  69. data/rice/Struct.cpp +117 -0
  70. data/rice/Struct.hpp +162 -0
  71. data/rice/Struct.ipp +26 -0
  72. data/rice/Symbol.cpp +25 -0
  73. data/rice/Symbol.hpp +66 -0
  74. data/rice/Symbol.ipp +44 -0
  75. data/rice/VM.cpp +79 -0
  76. data/rice/VM.hpp +27 -0
  77. data/rice/config.hpp +23 -0
  78. data/rice/config.hpp.in +22 -0
  79. data/rice/detail/Auto_Function_Wrapper.hpp +719 -0
  80. data/rice/detail/Auto_Function_Wrapper.ipp +1354 -0
  81. data/rice/detail/Auto_Member_Function_Wrapper.hpp +685 -0
  82. data/rice/detail/Auto_Member_Function_Wrapper.ipp +1435 -0
  83. data/rice/detail/Caster.hpp +61 -0
  84. data/rice/detail/Exception_Handler.hpp +118 -0
  85. data/rice/detail/Iterator_Definer.hpp +98 -0
  86. data/rice/detail/Not_Copyable.hpp +25 -0
  87. data/rice/detail/Wrapped_Function.hpp +33 -0
  88. data/rice/detail/check_ruby_type.cpp +21 -0
  89. data/rice/detail/check_ruby_type.hpp +23 -0
  90. data/rice/detail/creation_funcs.hpp +45 -0
  91. data/rice/detail/creation_funcs.ipp +62 -0
  92. data/rice/detail/default_allocation_func.hpp +23 -0
  93. data/rice/detail/default_allocation_func.ipp +11 -0
  94. data/rice/detail/define_method_and_auto_wrap.hpp +27 -0
  95. data/rice/detail/define_method_and_auto_wrap.ipp +20 -0
  96. data/rice/detail/env.hpp +13 -0
  97. data/rice/detail/from_ruby.hpp +43 -0
  98. data/rice/detail/from_ruby.ipp +74 -0
  99. data/rice/detail/method_data.cpp +105 -0
  100. data/rice/detail/method_data.hpp +33 -0
  101. data/rice/detail/node.hpp +13 -0
  102. data/rice/detail/object_call.hpp +85 -0
  103. data/rice/detail/object_call.ipp +147 -0
  104. data/rice/detail/protect.cpp +27 -0
  105. data/rice/detail/protect.hpp +34 -0
  106. data/rice/detail/remove_const.hpp +21 -0
  107. data/rice/detail/ruby.hpp +85 -0
  108. data/rice/detail/rubysig.hpp +13 -0
  109. data/rice/detail/st.hpp +56 -0
  110. data/rice/detail/to_ruby.hpp +16 -0
  111. data/rice/detail/to_ruby.ipp +10 -0
  112. data/rice/detail/win32.hpp +16 -0
  113. data/rice/detail/wrap_function.hpp +288 -0
  114. data/rice/detail/wrap_function.ipp +473 -0
  115. data/rice/generate_code.rb +1092 -0
  116. data/rice/global_function.hpp +16 -0
  117. data/rice/global_function.ipp +11 -0
  118. data/rice/protect.hpp +91 -0
  119. data/rice/protect.ipp +803 -0
  120. data/rice/ruby_try_catch.hpp +86 -0
  121. data/rice/to_from_ruby.hpp +8 -0
  122. data/rice/to_from_ruby.ipp +299 -0
  123. data/rice/to_from_ruby_defn.hpp +71 -0
  124. data/ruby.ac +105 -0
  125. data/ruby/Makefile.am +1 -0
  126. data/ruby/Makefile.in +493 -0
  127. data/ruby/lib/Makefile.am +3 -0
  128. data/ruby/lib/Makefile.in +369 -0
  129. data/ruby/lib/mkmf-rice.rb.in +199 -0
  130. data/sample/Makefile.am +47 -0
  131. data/sample/Makefile.in +375 -0
  132. data/sample/enum/extconf.rb +3 -0
  133. data/sample/enum/sample_enum.cpp +54 -0
  134. data/sample/enum/test.rb +8 -0
  135. data/sample/inheritance/animals.cpp +98 -0
  136. data/sample/inheritance/extconf.rb +3 -0
  137. data/sample/inheritance/test.rb +7 -0
  138. data/sample/map/extconf.rb +3 -0
  139. data/sample/map/map.cpp +81 -0
  140. data/sample/map/test.rb +7 -0
  141. data/test/Makefile.am +44 -0
  142. data/test/Makefile.in +575 -0
  143. data/test/test_Address_Registration_Guard.cpp +43 -0
  144. data/test/test_Allocation_Strategies.cpp +77 -0
  145. data/test/test_Array.cpp +241 -0
  146. data/test/test_Builtin_Object.cpp +72 -0
  147. data/test/test_Class.cpp +350 -0
  148. data/test/test_Constructor.cpp +30 -0
  149. data/test/test_Critical_Guard.cpp +47 -0
  150. data/test/test_Data_Object.cpp +235 -0
  151. data/test/test_Enum.cpp +162 -0
  152. data/test/test_Exception.cpp +46 -0
  153. data/test/test_Hash.cpp +195 -0
  154. data/test/test_Identifier.cpp +70 -0
  155. data/test/test_Jump_Tag.cpp +17 -0
  156. data/test/test_Module.cpp +253 -0
  157. data/test/test_Object.cpp +148 -0
  158. data/test/test_String.cpp +94 -0
  159. data/test/test_Struct.cpp +192 -0
  160. data/test/test_Symbol.cpp +63 -0
  161. data/test/test_To_From_Ruby.cpp +281 -0
  162. data/test/test_VM.cpp +26 -0
  163. data/test/test_rice.rb +30 -0
  164. data/test/unittest.cpp +136 -0
  165. data/test/unittest.hpp +292 -0
  166. metadata +209 -0
@@ -0,0 +1,46 @@
1
+ #include "unittest.hpp"
2
+ #include "rice/Exception.hpp"
3
+ #include "rice/String.hpp"
4
+
5
+ using namespace Rice;
6
+
7
+ TESTSUITE(Exception);
8
+
9
+ SETUP(Exception)
10
+ {
11
+ ruby_init();
12
+ }
13
+
14
+ TESTCASE(construct_from_exception_object)
15
+ {
16
+ VALUE v = protect(rb_exc_new2, rb_eRuntimeError, "foo");
17
+ Exception ex(v);
18
+ ASSERT_EQUAL(ex.value(), v);
19
+ }
20
+
21
+ TESTCASE(copy_construct)
22
+ {
23
+ VALUE v = protect(rb_exc_new2, rb_eRuntimeError, "foo");
24
+ Exception ex1(v);
25
+ Exception ex2(v);
26
+ ASSERT_EQUAL(ex2.value(), v);
27
+ }
28
+
29
+ TESTCASE(construct_from_format_string)
30
+ {
31
+ Exception ex(rb_eRuntimeError, "%s", "foo");
32
+ ASSERT_EQUAL(ex.class_of(), Object(rb_eRuntimeError));
33
+ }
34
+
35
+ TESTCASE(message)
36
+ {
37
+ Exception ex(rb_eRuntimeError, "%s", "foo");
38
+ ASSERT_EQUAL(String("foo"), ex.message());
39
+ }
40
+
41
+ TESTCASE(what)
42
+ {
43
+ Exception ex(rb_eRuntimeError, "%s", "foo");
44
+ ASSERT_EQUAL("foo", ex.what());
45
+ }
46
+
@@ -0,0 +1,195 @@
1
+ #include "unittest.hpp"
2
+ #include "rice/Hash.hpp"
3
+ #include <vector>
4
+ #include <map>
5
+ #include <algorithm>
6
+
7
+ using namespace Rice;
8
+
9
+ TESTSUITE(Hash);
10
+
11
+ SETUP(Hash)
12
+ {
13
+ ruby_init();
14
+ }
15
+
16
+ TESTCASE(default_construct)
17
+ {
18
+ Hash h;
19
+ ASSERT_EQUAL(T_HASH, rb_type(h));
20
+ ASSERT_EQUAL(0, RHASH(h.value())->tbl->num_entries);
21
+ }
22
+
23
+ TESTCASE(construct_from_object)
24
+ {
25
+ Object o(rb_hash_new());
26
+ Hash h(o);
27
+ ASSERT_EQUAL(T_HASH, rb_type(h));
28
+ ASSERT_EQUAL(0, RHASH(h.value())->tbl->num_entries);
29
+ }
30
+
31
+ TESTCASE(construct_from_value)
32
+ {
33
+ VALUE v(rb_hash_new());
34
+ Hash h(v);
35
+ ASSERT_EQUAL(T_HASH, rb_type(h));
36
+ ASSERT_EQUAL(0, RHASH(h.value())->tbl->num_entries);
37
+ }
38
+
39
+ TESTCASE(copy_construct)
40
+ {
41
+ Hash h;
42
+ Hash h2(h);
43
+ ASSERT_EQUAL(h.value(), h2.value());
44
+ }
45
+
46
+ TESTCASE(assignment)
47
+ {
48
+ Hash h;
49
+ Hash h2;
50
+ h = h2;
51
+ }
52
+
53
+ TESTCASE(size)
54
+ {
55
+ Hash h;
56
+ ASSERT_EQUAL(0u, h.size());
57
+ h[1] = 5;
58
+ ASSERT_EQUAL(1u, h.size());
59
+ h[6] = 9;
60
+ ASSERT_EQUAL(2u, h.size());
61
+ h[42] = 42;
62
+ ASSERT_EQUAL(3u, h.size());
63
+ h[6] = 1;
64
+ ASSERT_EQUAL(3u, h.size());
65
+ }
66
+
67
+ TESTCASE(bracket)
68
+ {
69
+ Hash h;
70
+ h[1] = 5;
71
+ h[6] = 9;
72
+ h[42] = 42;
73
+ h[6] = 1;
74
+ ASSERT_EQUAL(to_ruby(5), Object(h[1]));
75
+ ASSERT_EQUAL(to_ruby(42), Object(h[42]));
76
+ ASSERT_EQUAL(to_ruby(1), Object(h[6]));
77
+ }
78
+
79
+ TESTCASE(get)
80
+ {
81
+ Hash h;
82
+ h[1] = 5;
83
+ h[6] = 9;
84
+ h[42] = 42;
85
+ h[6] = 1;
86
+ ASSERT_EQUAL(5, h.get<int>(1));
87
+ ASSERT_EQUAL(42, h.get<int>(42));
88
+ ASSERT_EQUAL(1, h.get<int>(6));
89
+ }
90
+
91
+ TESTCASE(construct_vector_from_hash_iterators)
92
+ {
93
+ Hash h;
94
+ h[1] = 5;
95
+ h[6] = 9;
96
+ h[42] = 42;
97
+ h[6] = 1;
98
+ std::vector<Hash::Entry> v(h.begin(), h.end());
99
+ std::sort(v.begin(), v.end());
100
+ ASSERT_EQUAL(3u, v.size());
101
+ ASSERT_EQUAL(v[0].key, to_ruby(1));
102
+ ASSERT_EQUAL(v[1].key, to_ruby(6));
103
+ ASSERT_EQUAL(v[2].key, to_ruby(42));
104
+ ASSERT_EQUAL(&v[0].key, &v[0].first);
105
+ ASSERT_EQUAL(&v[1].key, &v[1].first);
106
+ ASSERT_EQUAL(&v[2].key, &v[2].first);
107
+ ASSERT_EQUAL(v[0].value, to_ruby(5));
108
+ ASSERT_EQUAL(v[1].value, to_ruby(1));
109
+ ASSERT_EQUAL(v[2].value, to_ruby(42));
110
+ ASSERT_EQUAL(&v[0].value, &v[0].second);
111
+ ASSERT_EQUAL(&v[1].value, &v[1].second);
112
+ ASSERT_EQUAL(&v[2].value, &v[2].second);
113
+ }
114
+
115
+ TESTCASE(iterate)
116
+ {
117
+ Hash h;
118
+ h[1] = 5;
119
+ h[6] = 9;
120
+ h[42] = 42;
121
+ h[6] = 1;
122
+ std::vector<Hash::Entry> v;
123
+ Hash::iterator it = h.begin();
124
+ Hash::iterator end = h.end();
125
+ for(; it != end; ++it)
126
+ {
127
+ v.push_back(*it);
128
+ }
129
+ std::sort(v.begin(), v.end());
130
+ ASSERT_EQUAL(3u, v.size());
131
+ ASSERT_EQUAL(v[0].key, to_ruby(1));
132
+ ASSERT_EQUAL(v[1].key, to_ruby(6));
133
+ ASSERT_EQUAL(v[2].key, to_ruby(42));
134
+ ASSERT_EQUAL(&v[0].key, &v[0].first);
135
+ ASSERT_EQUAL(&v[1].key, &v[1].first);
136
+ ASSERT_EQUAL(&v[2].key, &v[2].first);
137
+ ASSERT_EQUAL(v[0].value, to_ruby(5));
138
+ ASSERT_EQUAL(v[1].value, to_ruby(1));
139
+ ASSERT_EQUAL(v[2].value, to_ruby(42));
140
+ ASSERT_EQUAL(&v[0].value, &v[0].second);
141
+ ASSERT_EQUAL(&v[1].value, &v[1].second);
142
+ ASSERT_EQUAL(&v[2].value, &v[2].second);
143
+ }
144
+
145
+ TESTCASE(const_iterate)
146
+ {
147
+ Hash h;
148
+ h[1] = 5;
149
+ h[6] = 9;
150
+ h[42] = 42;
151
+ h[6] = 1;
152
+ std::vector<Hash::Entry> v;
153
+ Hash::const_iterator it = h.begin();
154
+ Hash::const_iterator end = h.end();
155
+ for(; it != end; ++it)
156
+ {
157
+ v.push_back(*it);
158
+ }
159
+ std::sort(v.begin(), v.end());
160
+ ASSERT_EQUAL(3u, v.size());
161
+ ASSERT_EQUAL(v[0].key, to_ruby(1));
162
+ ASSERT_EQUAL(v[1].key, to_ruby(6));
163
+ ASSERT_EQUAL(v[2].key, to_ruby(42));
164
+ ASSERT_EQUAL(&v[0].key, &v[0].first);
165
+ ASSERT_EQUAL(&v[1].key, &v[1].first);
166
+ ASSERT_EQUAL(&v[2].key, &v[2].first);
167
+ ASSERT_EQUAL(v[0].value, to_ruby(5));
168
+ ASSERT_EQUAL(v[1].value, to_ruby(1));
169
+ ASSERT_EQUAL(v[2].value, to_ruby(42));
170
+ ASSERT_EQUAL(&v[0].value, &v[0].value);
171
+ ASSERT_EQUAL(&v[1].value, &v[1].value);
172
+ ASSERT_EQUAL(&v[2].value, &v[2].value);
173
+ }
174
+
175
+ TESTCASE(iterate_and_change)
176
+ {
177
+ Hash h;
178
+ h[1] = 5;
179
+ h[6] = 9;
180
+ h[42] = 42;
181
+ h[6] = 1;
182
+ Hash::const_iterator it = h.begin();
183
+ Hash::const_iterator end = h.end();
184
+ std::map<int, int> m;
185
+ for(int j = 0; it != end; ++j, ++it)
186
+ {
187
+ it->second = j;
188
+ m[from_ruby<int>(it->first)] = j;
189
+ }
190
+ ASSERT_EQUAL(3u, m.size());
191
+ ASSERT_EQUAL(to_ruby(m[1]), h[1]);
192
+ ASSERT_EQUAL(to_ruby(m[6]), h[6]);
193
+ ASSERT_EQUAL(to_ruby(m[42]), h[42]);
194
+ }
195
+
@@ -0,0 +1,70 @@
1
+ #include "unittest.hpp"
2
+ #include "rice/Identifier.hpp"
3
+ #include "rice/Symbol.hpp"
4
+
5
+ using namespace Rice;
6
+
7
+ TESTSUITE(Identifier);
8
+
9
+ SETUP(Identifier)
10
+ {
11
+ ruby_init();
12
+ }
13
+
14
+ TESTCASE(construct_from_id)
15
+ {
16
+ ID id = rb_intern("foo");
17
+ Identifier identifier(id);
18
+ ASSERT_EQUAL(id, identifier.id());
19
+ }
20
+
21
+ TESTCASE(construct_from_symbol)
22
+ {
23
+ Symbol symbol("FOO");
24
+ Identifier identifier(symbol);
25
+ ASSERT_EQUAL(rb_intern("FOO"), identifier.id());
26
+ }
27
+
28
+ TESTCASE(construct_from_c_string)
29
+ {
30
+ Identifier identifier("Foo");
31
+ ASSERT_EQUAL(rb_intern("Foo"), identifier.id());
32
+ }
33
+
34
+ TESTCASE(default_construct)
35
+ {
36
+ Identifier identifier;
37
+ ASSERT_EQUAL(rb_intern(""), identifier.id());
38
+ }
39
+
40
+ TESTCASE(copy_construct)
41
+ {
42
+ Identifier identifier1("Foo");
43
+ Identifier identifier2(identifier1);
44
+ ASSERT_EQUAL(rb_intern("Foo"), identifier2.id());
45
+ }
46
+
47
+ TESTCASE(c_str)
48
+ {
49
+ Identifier identifier("Foo");
50
+ ASSERT_EQUAL("Foo", identifier.c_str());
51
+ }
52
+
53
+ TESTCASE(str)
54
+ {
55
+ Identifier identifier("Foo");
56
+ ASSERT_EQUAL(std::string("Foo"), identifier.str());
57
+ }
58
+
59
+ TESTCASE(implicit_conversion_to_id)
60
+ {
61
+ Identifier identifier("Foo");
62
+ ASSERT_EQUAL(rb_intern("Foo"), static_cast<ID>(identifier));
63
+ }
64
+
65
+ TESTCASE(to_sym)
66
+ {
67
+ Identifier identifier("Foo");
68
+ ASSERT_EQUAL(Symbol("Foo"), Symbol(identifier.to_sym()));
69
+ }
70
+
@@ -0,0 +1,17 @@
1
+ #include "unittest.hpp"
2
+ #include "rice/Jump_Tag.hpp"
3
+
4
+ using namespace Rice;
5
+
6
+ TESTSUITE(Jump_Tag);
7
+
8
+ SETUP(Jump_Tag)
9
+ {
10
+ }
11
+
12
+ TESTCASE(construct)
13
+ {
14
+ Jump_Tag jump_tag(42);
15
+ ASSERT_EQUAL(42, jump_tag.tag);
16
+ }
17
+
@@ -0,0 +1,253 @@
1
+ #include "unittest.hpp"
2
+ #include "rice/Module.hpp"
3
+ #include "rice/Exception.hpp"
4
+ #include "rice/Array.hpp"
5
+
6
+ using namespace Rice;
7
+
8
+ TESTSUITE(Module);
9
+
10
+ SETUP(Object)
11
+ {
12
+ ruby_init();
13
+ }
14
+
15
+ TESTCASE(construct_from_value)
16
+ {
17
+ Module m(rb_mEnumerable);
18
+ ASSERT_EQUAL(rb_mEnumerable, m.value());
19
+ }
20
+
21
+ namespace
22
+ {
23
+
24
+ class Silly_Exception
25
+ : public std::exception
26
+ {
27
+ };
28
+
29
+ void handle_silly_exception(Silly_Exception const & ex)
30
+ {
31
+ throw Exception(rb_eRuntimeError, "SILLY");
32
+ }
33
+
34
+ void throw_silly_exception(Object self)
35
+ {
36
+ throw Silly_Exception();
37
+ }
38
+
39
+ } // namespace
40
+
41
+ TESTCASE(add_handler)
42
+ {
43
+ Module m(anonymous_module());
44
+ m.add_handler<Silly_Exception>(handle_silly_exception);
45
+ m.define_singleton_method("foo", throw_silly_exception);
46
+ Object exc = m.instance_eval("begin; foo; rescue Exception; $!; end");
47
+ ASSERT_EQUAL(rb_eRuntimeError, CLASS_OF(exc));
48
+ Exception ex(exc);
49
+ ASSERT_EQUAL(String("SILLY"), String(ex.message()));
50
+ }
51
+
52
+ namespace
53
+ {
54
+
55
+ bool define_method_simple_ok;
56
+
57
+ void define_method_simple_helper(Object o)
58
+ {
59
+ define_method_simple_ok = true;
60
+ }
61
+
62
+ } // namespace
63
+
64
+ TESTCASE(define_method_simple)
65
+ {
66
+ Module m(anonymous_module());
67
+ m.define_method("foo", define_method_simple_helper);
68
+ define_method_simple_ok = false;
69
+ Object o = m.instance_eval("o = Object.new; o.extend(self); o.foo");
70
+ ASSERT(define_method_simple_ok);
71
+ }
72
+
73
+ TESTCASE(define_singleton_method_simple)
74
+ {
75
+ Module m(anonymous_module());
76
+ m.define_singleton_method("foo", define_method_simple_helper);
77
+ define_method_simple_ok = false;
78
+ Object o = m.call("foo");
79
+ ASSERT(define_method_simple_ok);
80
+ }
81
+
82
+ TESTCASE(define_module_function_simple)
83
+ {
84
+ Module m(anonymous_module());
85
+ m.define_module_function("foo", define_method_simple_helper);
86
+ define_method_simple_ok = false;
87
+ m.instance_eval("o = Object.new; o.extend(self); o.instance_eval { foo }");
88
+ ASSERT(define_method_simple_ok);
89
+ define_method_simple_ok = false;
90
+ m.call("foo");
91
+ ASSERT(define_method_simple_ok);
92
+ }
93
+
94
+ namespace
95
+ {
96
+
97
+ int define_method_int_result;
98
+
99
+ void define_method_int_helper(Object o, int i)
100
+ {
101
+ define_method_int_result = i;
102
+ }
103
+
104
+ } // namespace
105
+
106
+ TESTCASE(define_method_int)
107
+ {
108
+ Module m(anonymous_module());
109
+ m.define_method("foo", define_method_int_helper);
110
+ define_method_int_result = 0;
111
+ Object o = m.instance_eval("o = Object.new; o.extend(self); o.foo(42)");
112
+ ASSERT_EQUAL(42, define_method_int_result);
113
+ }
114
+
115
+ TESTCASE(define_singleton_method_int)
116
+ {
117
+ Module m(anonymous_module());
118
+ m.define_singleton_method("foo", define_method_int_helper);
119
+ define_method_int_result = 0;
120
+ Object o = m.call("foo", 42);
121
+ ASSERT_EQUAL(42, define_method_int_result);
122
+ }
123
+
124
+ TESTCASE(define_module_function_int)
125
+ {
126
+ Module m(anonymous_module());
127
+ m.define_module_function("foo", define_method_int_helper);
128
+ define_method_int_result = 0;
129
+ m.instance_eval("o = Object.new; o.extend(self); o.instance_eval { foo(42) }");
130
+ ASSERT_EQUAL(42, define_method_int_result);
131
+ define_method_int_result = 0;
132
+ m.call("foo", 42);
133
+ ASSERT_EQUAL(42, define_method_int_result);
134
+ }
135
+
136
+ TESTCASE(define_method_int_passed_no_args)
137
+ {
138
+ Module m(anonymous_module());
139
+ m.define_method("foo", define_method_int_helper);
140
+ ASSERT_EXCEPTION_CHECK(
141
+ Exception,
142
+ m.instance_eval("o = Object.new; o.extend(self); o.foo"),
143
+ ASSERT_EQUAL(
144
+ Object(rb_eArgError),
145
+ Object(CLASS_OF(ex.value()))
146
+ )
147
+ );
148
+ }
149
+
150
+ namespace
151
+ {
152
+
153
+ struct Foo
154
+ {
155
+ int x;
156
+ };
157
+
158
+ int define_method_int_foo_result_i;
159
+ Foo * define_method_int_foo_result_x;
160
+
161
+ void define_method_int_foo_helper(Object o, int i, Foo * x)
162
+ {
163
+ define_method_int_foo_result_i = i;
164
+ define_method_int_foo_result_x = x;
165
+ }
166
+
167
+ } // namespace
168
+
169
+ template<>
170
+ Foo * from_ruby<Foo *>(Object x)
171
+ {
172
+ Foo * retval;
173
+ Data_Get_Struct(x.value(), Foo, retval);
174
+ return retval;
175
+ }
176
+
177
+ TESTCASE(define_singleton_method_int_foo)
178
+ {
179
+ Module m(anonymous_module());
180
+ m.define_singleton_method("foo", define_method_int_foo_helper);
181
+ define_method_int_result = 0;
182
+ Foo * foo = new Foo;
183
+ foo->x = 1024;
184
+ VALUE f = Data_Wrap_Struct(rb_cObject, 0, Default_Allocation_Strategy<Foo>::free, foo);
185
+ m.call("foo", 42, Object(f));
186
+ ASSERT_EQUAL(42, define_method_int_foo_result_i);
187
+ ASSERT_EQUAL(foo, define_method_int_foo_result_x);
188
+ }
189
+
190
+ // TODO: how to test define_iterator?
191
+
192
+ TESTCASE(include_module)
193
+ {
194
+ Module m(anonymous_module());
195
+ Module & m2(m.include_module(rb_mEnumerable));
196
+ ASSERT_EQUAL(&m, &m2);
197
+ Array ancestors(m.ancestors());
198
+ Array expected_ancestors;
199
+ expected_ancestors.push(m);
200
+ expected_ancestors.push(Module(rb_mEnumerable));
201
+ ASSERT_EQUAL(expected_ancestors, ancestors);
202
+ }
203
+
204
+ TESTCASE(const_set_get_by_id)
205
+ {
206
+ Module m(anonymous_module());
207
+ Object v = to_ruby(42);
208
+ Module & m2(m.const_set(rb_intern("FOO"), v));
209
+ ASSERT_EQUAL(&m, &m2);
210
+ ASSERT_EQUAL(v, m.const_get(rb_intern("FOO")));
211
+ }
212
+
213
+ TESTCASE(const_set_get_by_identifier)
214
+ {
215
+ Module m(anonymous_module());
216
+ Object v = to_ruby(42);
217
+ Module & m2(m.const_set(Identifier("FOO"), v));
218
+ ASSERT_EQUAL(&m, &m2);
219
+ ASSERT_EQUAL(v, m.const_get(Identifier("FOO")));
220
+ }
221
+
222
+ TESTCASE(const_set_get_by_string)
223
+ {
224
+ Module m(anonymous_module());
225
+ Object v = to_ruby(42);
226
+ Module & m2(m.const_set("FOO", v));
227
+ ASSERT_EQUAL(&m, &m2);
228
+ ASSERT_EQUAL(v, m.const_get("FOO"));
229
+ }
230
+
231
+ TESTCASE(remove_const)
232
+ {
233
+ Module m(anonymous_module());
234
+ Object v = to_ruby(42);
235
+ m.const_set("FOO", v);
236
+ ASSERT_EQUAL(v, m.const_get("FOO"));
237
+ m.remove_const("FOO");
238
+ ASSERT_EXCEPTION_CHECK(
239
+ Exception,
240
+ m.const_get("FOO"),
241
+ ASSERT_EQUAL(
242
+ Object(rb_eNameError),
243
+ Object(CLASS_OF(ex.value()))
244
+ )
245
+ );
246
+ }
247
+
248
+ TESTCASE(mod_name_anonymous)
249
+ {
250
+ Module m(anonymous_module());
251
+ ASSERT_EQUAL(String(""), m.mod_name());
252
+ }
253
+