jameskilton-rice 1.2.0
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/Makefile.in +749 -0
- data/README +879 -0
- data/README.mingw +8 -0
- data/Rakefile +72 -0
- data/aclocal.m4 +891 -0
- data/bootstrap +8 -0
- data/config.guess +1526 -0
- data/config.sub +1658 -0
- data/configure +7668 -0
- data/configure.ac +52 -0
- data/depcomp +589 -0
- data/doxygen.ac +314 -0
- data/doxygen.am +186 -0
- data/extconf.rb +22 -0
- data/install-sh +519 -0
- data/missing +367 -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 +116 -0
- data/rice/Arg_operators.cpp +21 -0
- data/rice/Arg_operators.hpp +18 -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 +339 -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 +21 -0
- data/rice/Data_Type.hpp +8 -0
- data/rice/Data_Type.ipp +306 -0
- data/rice/Data_Type_defn.hpp +219 -0
- data/rice/Data_Type_fwd.hpp +12 -0
- data/rice/Director.cpp +19 -0
- data/rice/Director.hpp +47 -0
- data/rice/Enum.hpp +118 -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 +140 -0
- data/rice/Makefile.in +740 -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 +153 -0
- data/rice/Object.hpp +8 -0
- data/rice/Object.ipp +19 -0
- data/rice/Object_defn.hpp +183 -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 +44 -0
- data/rice/config.hpp.in +43 -0
- data/rice/detail/Arguments.hpp +126 -0
- data/rice/detail/Auto_Function_Wrapper.hpp +861 -0
- data/rice/detail/Auto_Function_Wrapper.ipp +2929 -0
- data/rice/detail/Auto_Member_Function_Wrapper.hpp +828 -0
- data/rice/detail/Auto_Member_Function_Wrapper.ipp +2326 -0
- data/rice/detail/Caster.hpp +63 -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/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 +392 -0
- data/rice/detail/method_data.cpp.rpp +301 -0
- data/rice/detail/method_data.hpp +21 -0
- data/rice/detail/mininode.cpp +1220 -0
- data/rice/detail/mininode.cpp.rpp +62 -0
- data/rice/detail/mininode.hpp +320 -0
- data/rice/detail/mininode.hpp.rpp +119 -0
- data/rice/detail/node.hpp +13 -0
- data/rice/detail/object_call.hpp +85 -0
- data/rice/detail/object_call.ipp +147 -0
- data/rice/detail/protect.cpp +29 -0
- data/rice/detail/protect.hpp +34 -0
- data/rice/detail/remove_const.hpp +21 -0
- data/rice/detail/ruby.hpp +89 -0
- data/rice/detail/ruby_version_code.hpp +6 -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 +37 -0
- data/rice/detail/win32.hpp +16 -0
- data/rice/detail/wrap_function.hpp +345 -0
- data/rice/detail/wrap_function.ipp +531 -0
- data/rice/generate_code.rb +1311 -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 +803 -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 +299 -0
- data/rice/to_from_ruby_defn.hpp +71 -0
- data/ruby.ac +150 -0
- data/ruby/Makefile.am +1 -0
- data/ruby/Makefile.in +497 -0
- data/ruby/lib/Makefile.am +3 -0
- data/ruby/lib/Makefile.in +374 -0
- data/ruby/lib/mkmf-rice.rb.in +209 -0
- data/ruby/lib/version.rb +3 -0
- data/sample/Makefile.am +47 -0
- data/sample/Makefile.in +380 -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 +49 -0
- data/test/Makefile.in +603 -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 +398 -0
- data/test/test_Constructor.cpp +238 -0
- data/test/test_Critical_Guard.cpp +51 -0
- data/test/test_Data_Object.cpp +275 -0
- data/test/test_Data_Type.cpp +121 -0
- data/test/test_Director.cpp +225 -0
- data/test/test_Enum.cpp +162 -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_Module.cpp +428 -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 +97 -0
- data/test/test_rice.rb +35 -0
- data/test/unittest.cpp +136 -0
- data/test/unittest.hpp +292 -0
- metadata +247 -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
|
+
|
data/test/test_Hash.cpp
ADDED
@@ -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_TBL(h.value())->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_TBL(h.value())->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_TBL(h.value())->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,428 @@
|
|
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
|
+
|
8
|
+
using namespace Rice;
|
9
|
+
|
10
|
+
TESTSUITE(Module);
|
11
|
+
|
12
|
+
SETUP(Object)
|
13
|
+
{
|
14
|
+
ruby_init();
|
15
|
+
}
|
16
|
+
|
17
|
+
TESTCASE(construct_from_value)
|
18
|
+
{
|
19
|
+
Module m(rb_mEnumerable);
|
20
|
+
ASSERT_EQUAL(rb_mEnumerable, m.value());
|
21
|
+
}
|
22
|
+
|
23
|
+
namespace
|
24
|
+
{
|
25
|
+
|
26
|
+
class Silly_Exception
|
27
|
+
: public std::exception
|
28
|
+
{
|
29
|
+
};
|
30
|
+
|
31
|
+
void handle_silly_exception(Silly_Exception const & ex)
|
32
|
+
{
|
33
|
+
throw Exception(rb_eRuntimeError, "SILLY");
|
34
|
+
}
|
35
|
+
|
36
|
+
void throw_silly_exception(Object self)
|
37
|
+
{
|
38
|
+
throw Silly_Exception();
|
39
|
+
}
|
40
|
+
|
41
|
+
} // namespace
|
42
|
+
|
43
|
+
TESTCASE(add_handler)
|
44
|
+
{
|
45
|
+
Module m(anonymous_module());
|
46
|
+
m.add_handler<Silly_Exception>(handle_silly_exception);
|
47
|
+
m.define_singleton_method("foo", throw_silly_exception);
|
48
|
+
Object exc = m.instance_eval("begin; foo; rescue Exception; $!; end");
|
49
|
+
ASSERT_EQUAL(rb_eRuntimeError, CLASS_OF(exc));
|
50
|
+
Exception ex(exc);
|
51
|
+
ASSERT_EQUAL(String("SILLY"), String(ex.message()));
|
52
|
+
}
|
53
|
+
|
54
|
+
namespace
|
55
|
+
{
|
56
|
+
|
57
|
+
bool define_method_simple_ok;
|
58
|
+
|
59
|
+
void define_method_simple_helper(Object o)
|
60
|
+
{
|
61
|
+
define_method_simple_ok = true;
|
62
|
+
}
|
63
|
+
|
64
|
+
} // namespace
|
65
|
+
|
66
|
+
TESTCASE(define_method_simple)
|
67
|
+
{
|
68
|
+
Module m(anonymous_module());
|
69
|
+
m.define_method("foo", define_method_simple_helper);
|
70
|
+
define_method_simple_ok = false;
|
71
|
+
Object o = m.instance_eval("o = Object.new; o.extend(self); o.foo");
|
72
|
+
ASSERT(define_method_simple_ok);
|
73
|
+
}
|
74
|
+
|
75
|
+
TESTCASE(define_singleton_method_simple)
|
76
|
+
{
|
77
|
+
Module m(anonymous_module());
|
78
|
+
m.define_singleton_method("foo", define_method_simple_helper);
|
79
|
+
define_method_simple_ok = false;
|
80
|
+
Object o = m.call("foo");
|
81
|
+
ASSERT(define_method_simple_ok);
|
82
|
+
}
|
83
|
+
|
84
|
+
TESTCASE(define_module_function_simple)
|
85
|
+
{
|
86
|
+
Module m(anonymous_module());
|
87
|
+
m.define_module_function("foo", define_method_simple_helper);
|
88
|
+
define_method_simple_ok = false;
|
89
|
+
m.instance_eval("o = Object.new; o.extend(self); o.instance_eval { foo }");
|
90
|
+
ASSERT(define_method_simple_ok);
|
91
|
+
define_method_simple_ok = false;
|
92
|
+
m.call("foo");
|
93
|
+
ASSERT(define_method_simple_ok);
|
94
|
+
}
|
95
|
+
|
96
|
+
namespace
|
97
|
+
{
|
98
|
+
|
99
|
+
int define_method_int_result;
|
100
|
+
|
101
|
+
void define_method_int_helper(Object o, int i)
|
102
|
+
{
|
103
|
+
define_method_int_result = i;
|
104
|
+
}
|
105
|
+
|
106
|
+
} // namespace
|
107
|
+
|
108
|
+
TESTCASE(define_method_int)
|
109
|
+
{
|
110
|
+
Module m(anonymous_module());
|
111
|
+
m.define_method("foo", define_method_int_helper);
|
112
|
+
define_method_int_result = 0;
|
113
|
+
Object o = m.instance_eval("o = Object.new; o.extend(self); o.foo(42)");
|
114
|
+
ASSERT_EQUAL(42, define_method_int_result);
|
115
|
+
}
|
116
|
+
|
117
|
+
TESTCASE(define_singleton_method_int)
|
118
|
+
{
|
119
|
+
Module m(anonymous_module());
|
120
|
+
m.define_singleton_method("foo", define_method_int_helper);
|
121
|
+
define_method_int_result = 0;
|
122
|
+
Object o = m.call("foo", 42);
|
123
|
+
ASSERT_EQUAL(42, define_method_int_result);
|
124
|
+
}
|
125
|
+
|
126
|
+
TESTCASE(define_module_function_int)
|
127
|
+
{
|
128
|
+
Module m(anonymous_module());
|
129
|
+
m.define_module_function("foo", define_method_int_helper);
|
130
|
+
define_method_int_result = 0;
|
131
|
+
m.instance_eval("o = Object.new; o.extend(self); o.instance_eval { foo(42) }");
|
132
|
+
ASSERT_EQUAL(42, define_method_int_result);
|
133
|
+
define_method_int_result = 0;
|
134
|
+
m.call("foo", 42);
|
135
|
+
ASSERT_EQUAL(42, define_method_int_result);
|
136
|
+
}
|
137
|
+
|
138
|
+
TESTCASE(define_method_int_passed_no_args)
|
139
|
+
{
|
140
|
+
Module m(anonymous_module());
|
141
|
+
m.define_method("foo", define_method_int_helper);
|
142
|
+
ASSERT_EXCEPTION_CHECK(
|
143
|
+
Exception,
|
144
|
+
m.instance_eval("o = Object.new; o.extend(self); o.foo"),
|
145
|
+
ASSERT_EQUAL(
|
146
|
+
Object(rb_eArgError),
|
147
|
+
Object(CLASS_OF(ex.value()))
|
148
|
+
)
|
149
|
+
);
|
150
|
+
}
|
151
|
+
|
152
|
+
namespace
|
153
|
+
{
|
154
|
+
|
155
|
+
struct Foo
|
156
|
+
{
|
157
|
+
int x;
|
158
|
+
};
|
159
|
+
|
160
|
+
int define_method_int_foo_result_i;
|
161
|
+
Foo * define_method_int_foo_result_x;
|
162
|
+
|
163
|
+
void define_method_int_foo_helper(Object o, int i, Foo * x)
|
164
|
+
{
|
165
|
+
define_method_int_foo_result_i = i;
|
166
|
+
define_method_int_foo_result_x = x;
|
167
|
+
}
|
168
|
+
|
169
|
+
} // namespace
|
170
|
+
|
171
|
+
template<>
|
172
|
+
Foo * from_ruby<Foo *>(Object x)
|
173
|
+
{
|
174
|
+
Foo * retval;
|
175
|
+
Data_Get_Struct(x.value(), Foo, retval);
|
176
|
+
return retval;
|
177
|
+
}
|
178
|
+
|
179
|
+
TESTCASE(define_singleton_method_int_foo)
|
180
|
+
{
|
181
|
+
Module m(anonymous_module());
|
182
|
+
m.define_singleton_method("foo", define_method_int_foo_helper);
|
183
|
+
define_method_int_result = 0;
|
184
|
+
Foo * foo = new Foo;
|
185
|
+
foo->x = 1024;
|
186
|
+
VALUE f = Data_Wrap_Struct(rb_cObject, 0, Default_Allocation_Strategy<Foo>::free, foo);
|
187
|
+
m.call("foo", 42, Object(f));
|
188
|
+
ASSERT_EQUAL(42, define_method_int_foo_result_i);
|
189
|
+
ASSERT_EQUAL(foo, define_method_int_foo_result_x);
|
190
|
+
}
|
191
|
+
|
192
|
+
// TODO: how to test define_iterator?
|
193
|
+
|
194
|
+
TESTCASE(include_module)
|
195
|
+
{
|
196
|
+
Module m(anonymous_module());
|
197
|
+
Module & m2(m.include_module(rb_mEnumerable));
|
198
|
+
ASSERT_EQUAL(&m, &m2);
|
199
|
+
Array ancestors(m.ancestors());
|
200
|
+
Array expected_ancestors;
|
201
|
+
expected_ancestors.push(m);
|
202
|
+
expected_ancestors.push(Module(rb_mEnumerable));
|
203
|
+
ASSERT_EQUAL(expected_ancestors, ancestors);
|
204
|
+
}
|
205
|
+
|
206
|
+
TESTCASE(const_set_get_by_id)
|
207
|
+
{
|
208
|
+
Module m(anonymous_module());
|
209
|
+
Object v = to_ruby(42);
|
210
|
+
Module & m2(m.const_set(rb_intern("FOO"), v));
|
211
|
+
ASSERT_EQUAL(&m, &m2);
|
212
|
+
ASSERT_EQUAL(v, m.const_get(rb_intern("FOO")));
|
213
|
+
}
|
214
|
+
|
215
|
+
TESTCASE(const_set_get_by_identifier)
|
216
|
+
{
|
217
|
+
Module m(anonymous_module());
|
218
|
+
Object v = to_ruby(42);
|
219
|
+
Module & m2(m.const_set(Identifier("FOO"), v));
|
220
|
+
ASSERT_EQUAL(&m, &m2);
|
221
|
+
ASSERT_EQUAL(v, m.const_get(Identifier("FOO")));
|
222
|
+
}
|
223
|
+
|
224
|
+
TESTCASE(const_set_get_by_string)
|
225
|
+
{
|
226
|
+
Module m(anonymous_module());
|
227
|
+
Object v = to_ruby(42);
|
228
|
+
Module & m2(m.const_set("FOO", v));
|
229
|
+
ASSERT_EQUAL(&m, &m2);
|
230
|
+
ASSERT_EQUAL(v, m.const_get("FOO"));
|
231
|
+
}
|
232
|
+
|
233
|
+
TESTCASE(remove_const)
|
234
|
+
{
|
235
|
+
Module m(anonymous_module());
|
236
|
+
Object v = to_ruby(42);
|
237
|
+
m.const_set("FOO", v);
|
238
|
+
ASSERT_EQUAL(v, m.const_get("FOO"));
|
239
|
+
m.remove_const("FOO");
|
240
|
+
ASSERT_EXCEPTION_CHECK(
|
241
|
+
Exception,
|
242
|
+
m.const_get("FOO"),
|
243
|
+
ASSERT_EQUAL(
|
244
|
+
Object(rb_eNameError),
|
245
|
+
Object(CLASS_OF(ex.value()))
|
246
|
+
)
|
247
|
+
);
|
248
|
+
}
|
249
|
+
|
250
|
+
TESTCASE(mod_name_anonymous)
|
251
|
+
{
|
252
|
+
Module m(anonymous_module());
|
253
|
+
ASSERT_EQUAL(String(""), m.name());
|
254
|
+
}
|
255
|
+
|
256
|
+
/**
|
257
|
+
* Tests for default arguments
|
258
|
+
*/
|
259
|
+
namespace
|
260
|
+
{
|
261
|
+
int defaults_method_one_arg1;
|
262
|
+
int defaults_method_one_arg2;
|
263
|
+
bool defaults_method_one_arg3 = false;
|
264
|
+
|
265
|
+
void defaults_method_one(int arg1, int arg2 = 3, bool arg3 = true)
|
266
|
+
{
|
267
|
+
defaults_method_one_arg1 = arg1;
|
268
|
+
defaults_method_one_arg2 = arg2;
|
269
|
+
defaults_method_one_arg3 = arg3;
|
270
|
+
}
|
271
|
+
}
|
272
|
+
|
273
|
+
TESTCASE(define_method_default_arguments)
|
274
|
+
{
|
275
|
+
Module m(anonymous_module());
|
276
|
+
m.define_method("foo", &defaults_method_one, (Arg("arg1"), Arg("arg2") = 3, Arg("arg3") = true));
|
277
|
+
|
278
|
+
Object o = m.instance_eval("o = Object.new; o.extend(self); o");
|
279
|
+
o.call("foo", 2);
|
280
|
+
|
281
|
+
ASSERT_EQUAL(2, defaults_method_one_arg1);
|
282
|
+
ASSERT_EQUAL(3, defaults_method_one_arg2);
|
283
|
+
ASSERT(defaults_method_one_arg3);
|
284
|
+
|
285
|
+
o.call("foo", 11, 10);
|
286
|
+
|
287
|
+
ASSERT_EQUAL(11, defaults_method_one_arg1);
|
288
|
+
ASSERT_EQUAL(10, defaults_method_one_arg2);
|
289
|
+
ASSERT(defaults_method_one_arg3);
|
290
|
+
|
291
|
+
o.call("foo", 22, 33, false);
|
292
|
+
|
293
|
+
ASSERT_EQUAL(22, defaults_method_one_arg1);
|
294
|
+
ASSERT_EQUAL(33, defaults_method_one_arg2);
|
295
|
+
ASSERT(!defaults_method_one_arg3);
|
296
|
+
}
|
297
|
+
|
298
|
+
TESTCASE(default_arguments_still_throws_argument_error)
|
299
|
+
{
|
300
|
+
Module m(anonymous_module());
|
301
|
+
m.define_method("foo", &defaults_method_one, (Arg("arg1"), Arg("arg2") = 3, Arg("arg3") = true));
|
302
|
+
|
303
|
+
ASSERT_EXCEPTION_CHECK(
|
304
|
+
Exception,
|
305
|
+
m.instance_eval("o = Object.new; o.extend(self); o.foo()"),
|
306
|
+
ASSERT_EQUAL(
|
307
|
+
Object(rb_eArgError),
|
308
|
+
Object(CLASS_OF(ex.value()))
|
309
|
+
)
|
310
|
+
);
|
311
|
+
|
312
|
+
ASSERT_EXCEPTION_CHECK(
|
313
|
+
Exception,
|
314
|
+
m.instance_eval("o = Object.new; o.extend(self); o.foo(3, 4, false, 17)"),
|
315
|
+
ASSERT_EQUAL(
|
316
|
+
Object(rb_eArgError),
|
317
|
+
Object(CLASS_OF(ex.value()))
|
318
|
+
)
|
319
|
+
);
|
320
|
+
}
|
321
|
+
|
322
|
+
namespace {
|
323
|
+
int the_one_default_arg = 0;
|
324
|
+
void method_with_one_default_arg(int num = 4) {
|
325
|
+
the_one_default_arg = num;
|
326
|
+
}
|
327
|
+
}
|
328
|
+
|
329
|
+
TESTCASE(defining_methods_with_single_default_argument)
|
330
|
+
{
|
331
|
+
// define_method
|
332
|
+
Module m(anonymous_module());
|
333
|
+
m.define_method("foo", &method_with_one_default_arg, (Arg("num") = 4));
|
334
|
+
m.instance_eval("o = Object.new; o.extend(self); o.foo()");
|
335
|
+
ASSERT_EQUAL(4, the_one_default_arg);
|
336
|
+
|
337
|
+
the_one_default_arg = 0;
|
338
|
+
|
339
|
+
// define_singleton_method
|
340
|
+
Class c(anonymous_class());
|
341
|
+
c.define_singleton_method("foo", &method_with_one_default_arg, (Arg("num") = 4));
|
342
|
+
c.call("foo");
|
343
|
+
ASSERT_EQUAL(4, the_one_default_arg);
|
344
|
+
|
345
|
+
the_one_default_arg = 0;
|
346
|
+
|
347
|
+
// define_module_function
|
348
|
+
m.define_module_function("foo2", &method_with_one_default_arg, (Arg("num") = 4));
|
349
|
+
|
350
|
+
m.call("foo2");
|
351
|
+
ASSERT_EQUAL(4, the_one_default_arg);
|
352
|
+
}
|
353
|
+
|
354
|
+
TESTCASE(default_arguments_for_define_singleton_method)
|
355
|
+
{
|
356
|
+
Class c(anonymous_class());
|
357
|
+
c.define_singleton_method("foo", &defaults_method_one, (Arg("arg1"), Arg("arg2") = 3, Arg("arg3") = true));
|
358
|
+
|
359
|
+
c.call("foo", 2);
|
360
|
+
|
361
|
+
ASSERT_EQUAL(2, defaults_method_one_arg1);
|
362
|
+
ASSERT_EQUAL(3, defaults_method_one_arg2);
|
363
|
+
ASSERT(defaults_method_one_arg3);
|
364
|
+
|
365
|
+
c.call("foo", 11, 10);
|
366
|
+
|
367
|
+
ASSERT_EQUAL(11, defaults_method_one_arg1);
|
368
|
+
ASSERT_EQUAL(10, defaults_method_one_arg2);
|
369
|
+
ASSERT(defaults_method_one_arg3);
|
370
|
+
|
371
|
+
c.call("foo", 22, 33, false);
|
372
|
+
|
373
|
+
ASSERT_EQUAL(22, defaults_method_one_arg1);
|
374
|
+
ASSERT_EQUAL(33, defaults_method_one_arg2);
|
375
|
+
ASSERT(!defaults_method_one_arg3);
|
376
|
+
}
|
377
|
+
|
378
|
+
TESTCASE(default_arguments_for_define_module_function)
|
379
|
+
{
|
380
|
+
Module m(anonymous_module());
|
381
|
+
m.define_module_function("foo", &defaults_method_one, (Arg("arg1"), Arg("arg2") = 3, Arg("arg3") = true));
|
382
|
+
|
383
|
+
m.call("foo", 2);
|
384
|
+
|
385
|
+
ASSERT_EQUAL(2, defaults_method_one_arg1);
|
386
|
+
ASSERT_EQUAL(3, defaults_method_one_arg2);
|
387
|
+
ASSERT(defaults_method_one_arg3);
|
388
|
+
|
389
|
+
m.call("foo", 11, 10);
|
390
|
+
|
391
|
+
ASSERT_EQUAL(11, defaults_method_one_arg1);
|
392
|
+
ASSERT_EQUAL(10, defaults_method_one_arg2);
|
393
|
+
ASSERT(defaults_method_one_arg3);
|
394
|
+
|
395
|
+
m.call("foo", 22, 33, false);
|
396
|
+
|
397
|
+
ASSERT_EQUAL(22, defaults_method_one_arg1);
|
398
|
+
ASSERT_EQUAL(33, defaults_method_one_arg2);
|
399
|
+
ASSERT(!defaults_method_one_arg3);
|
400
|
+
}
|
401
|
+
|
402
|
+
namespace {
|
403
|
+
std::string with_defaults_and_references_x;
|
404
|
+
bool with_defaults_and_references_doIt;
|
405
|
+
|
406
|
+
void with_defaults_and_references(std::string const& x, bool doIt = false)
|
407
|
+
{
|
408
|
+
with_defaults_and_references_x = x;
|
409
|
+
with_defaults_and_references_doIt = doIt;
|
410
|
+
}
|
411
|
+
}
|
412
|
+
|
413
|
+
template<>
|
414
|
+
std::string* from_ruby<std::string*>(Rice::Object x) {
|
415
|
+
return new std::string(from_ruby<char const*>(x));
|
416
|
+
}
|
417
|
+
|
418
|
+
TESTCASE(define_method_works_with_reference_arguments)
|
419
|
+
{
|
420
|
+
Module m(anonymous_module());
|
421
|
+
m.define_module_function("foo", &with_defaults_and_references,
|
422
|
+
(Arg("x"), Arg("doIt") = false));
|
423
|
+
|
424
|
+
m.call("foo", "test");
|
425
|
+
|
426
|
+
ASSERT_EQUAL("test", with_defaults_and_references_x);
|
427
|
+
ASSERT(!with_defaults_and_references_doIt);
|
428
|
+
}
|