dub 0.5.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.

Potentially problematic release.


This version of dub might be problematic. Click here for more details.

Files changed (142) hide show
  1. data/.gitignore +8 -0
  2. data/History.txt +5 -0
  3. data/LICENSE +20 -0
  4. data/README.rdoc +53 -0
  5. data/Rakefile +58 -0
  6. data/dub.gemspec +194 -0
  7. data/lib/dub/argument.rb +261 -0
  8. data/lib/dub/entities_unescape.rb +16 -0
  9. data/lib/dub/function.rb +111 -0
  10. data/lib/dub/function_group.rb +74 -0
  11. data/lib/dub/generator.rb +15 -0
  12. data/lib/dub/group.rb +10 -0
  13. data/lib/dub/klass.rb +231 -0
  14. data/lib/dub/lua/class.cpp.erb +75 -0
  15. data/lib/dub/lua/class_gen.rb +78 -0
  16. data/lib/dub/lua/function.cpp.erb +4 -0
  17. data/lib/dub/lua/function_gen.rb +223 -0
  18. data/lib/dub/lua/group.cpp.erb +10 -0
  19. data/lib/dub/lua/lua_cpp_helper.h +141 -0
  20. data/lib/dub/lua/namespace.cpp.erb +35 -0
  21. data/lib/dub/lua/namespace_gen.rb +86 -0
  22. data/lib/dub/lua.rb +24 -0
  23. data/lib/dub/member_extraction.rb +88 -0
  24. data/lib/dub/namespace.rb +276 -0
  25. data/lib/dub/parser.rb +46 -0
  26. data/lib/dub/templates/lua_template.erb +21 -0
  27. data/lib/dub/version.rb +3 -0
  28. data/lib/dub.rb +26 -0
  29. data/test/argument_test.rb +423 -0
  30. data/test/fixtures/app/CMakeLists.txt +54 -0
  31. data/test/fixtures/app/Doxyfile +1600 -0
  32. data/test/fixtures/app/bindings/all_lua.cpp +299 -0
  33. data/test/fixtures/app/include/matrix.h +123 -0
  34. data/test/fixtures/app/make_lua_bindings.rb +13 -0
  35. data/test/fixtures/app/vendor/lua/CMakeLists.txt +25 -0
  36. data/test/fixtures/app/vendor/lua/COPYRIGHT +34 -0
  37. data/test/fixtures/app/vendor/lua/HISTORY +183 -0
  38. data/test/fixtures/app/vendor/lua/INSTALL +99 -0
  39. data/test/fixtures/app/vendor/lua/Makefile +183 -0
  40. data/test/fixtures/app/vendor/lua/README +37 -0
  41. data/test/fixtures/app/vendor/lua/lapi.c +1080 -0
  42. data/test/fixtures/app/vendor/lua/lapi.h +16 -0
  43. data/test/fixtures/app/vendor/lua/lauxlib.c +653 -0
  44. data/test/fixtures/app/vendor/lua/lauxlib.h +174 -0
  45. data/test/fixtures/app/vendor/lua/lbaselib.c +643 -0
  46. data/test/fixtures/app/vendor/lua/lcode.c +839 -0
  47. data/test/fixtures/app/vendor/lua/lcode.h +76 -0
  48. data/test/fixtures/app/vendor/lua/ldblib.c +397 -0
  49. data/test/fixtures/app/vendor/lua/ldebug.c +622 -0
  50. data/test/fixtures/app/vendor/lua/ldebug.h +33 -0
  51. data/test/fixtures/app/vendor/lua/ldo.c +516 -0
  52. data/test/fixtures/app/vendor/lua/ldo.h +57 -0
  53. data/test/fixtures/app/vendor/lua/ldump.c +164 -0
  54. data/test/fixtures/app/vendor/lua/lfunc.c +174 -0
  55. data/test/fixtures/app/vendor/lua/lfunc.h +34 -0
  56. data/test/fixtures/app/vendor/lua/lgc.c +711 -0
  57. data/test/fixtures/app/vendor/lua/lgc.h +110 -0
  58. data/test/fixtures/app/vendor/lua/liblua.a +0 -0
  59. data/test/fixtures/app/vendor/lua/linit.c +38 -0
  60. data/test/fixtures/app/vendor/lua/liolib.c +532 -0
  61. data/test/fixtures/app/vendor/lua/llex.c +461 -0
  62. data/test/fixtures/app/vendor/lua/llex.h +81 -0
  63. data/test/fixtures/app/vendor/lua/llimits.h +128 -0
  64. data/test/fixtures/app/vendor/lua/lmathlib.c +263 -0
  65. data/test/fixtures/app/vendor/lua/lmem.c +86 -0
  66. data/test/fixtures/app/vendor/lua/lmem.h +49 -0
  67. data/test/fixtures/app/vendor/lua/loadlib.c +664 -0
  68. data/test/fixtures/app/vendor/lua/lobject.c +214 -0
  69. data/test/fixtures/app/vendor/lua/lobject.h +381 -0
  70. data/test/fixtures/app/vendor/lua/lopcodes.c +102 -0
  71. data/test/fixtures/app/vendor/lua/lopcodes.h +268 -0
  72. data/test/fixtures/app/vendor/lua/loslib.c +244 -0
  73. data/test/fixtures/app/vendor/lua/lparser.c +1337 -0
  74. data/test/fixtures/app/vendor/lua/lparser.h +82 -0
  75. data/test/fixtures/app/vendor/lua/lstate.c +214 -0
  76. data/test/fixtures/app/vendor/lua/lstate.h +168 -0
  77. data/test/fixtures/app/vendor/lua/lstring.c +111 -0
  78. data/test/fixtures/app/vendor/lua/lstring.h +31 -0
  79. data/test/fixtures/app/vendor/lua/lstrlib.c +868 -0
  80. data/test/fixtures/app/vendor/lua/ltable.c +588 -0
  81. data/test/fixtures/app/vendor/lua/ltable.h +40 -0
  82. data/test/fixtures/app/vendor/lua/ltablib.c +278 -0
  83. data/test/fixtures/app/vendor/lua/ltm.c +75 -0
  84. data/test/fixtures/app/vendor/lua/ltm.h +54 -0
  85. data/test/fixtures/app/vendor/lua/lua.c +695 -0
  86. data/test/fixtures/app/vendor/lua/lua.h +385 -0
  87. data/test/fixtures/app/vendor/lua/lua_dub_helper.h +77 -0
  88. data/test/fixtures/app/vendor/lua/luac +0 -0
  89. data/test/fixtures/app/vendor/lua/luac.c +200 -0
  90. data/test/fixtures/app/vendor/lua/luaconf.h +762 -0
  91. data/test/fixtures/app/vendor/lua/lualib.h +53 -0
  92. data/test/fixtures/app/vendor/lua/lundump.c +223 -0
  93. data/test/fixtures/app/vendor/lua/lundump.h +36 -0
  94. data/test/fixtures/app/vendor/lua/lvm.c +765 -0
  95. data/test/fixtures/app/vendor/lua/lvm.h +36 -0
  96. data/test/fixtures/app/vendor/lua/lzio.c +82 -0
  97. data/test/fixtures/app/vendor/lua/lzio.h +67 -0
  98. data/test/fixtures/app/vendor/lua/matrix.h +102 -0
  99. data/test/fixtures/app/vendor/lua/print.c +227 -0
  100. data/test/fixtures/app/vendor/lua/test/README +26 -0
  101. data/test/fixtures/app/vendor/lua/test/bisect.lua +27 -0
  102. data/test/fixtures/app/vendor/lua/test/cf.lua +16 -0
  103. data/test/fixtures/app/vendor/lua/test/echo.lua +5 -0
  104. data/test/fixtures/app/vendor/lua/test/env.lua +7 -0
  105. data/test/fixtures/app/vendor/lua/test/factorial.lua +32 -0
  106. data/test/fixtures/app/vendor/lua/test/fib.lua +40 -0
  107. data/test/fixtures/app/vendor/lua/test/fibfor.lua +13 -0
  108. data/test/fixtures/app/vendor/lua/test/globals.lua +13 -0
  109. data/test/fixtures/app/vendor/lua/test/hello.lua +3 -0
  110. data/test/fixtures/app/vendor/lua/test/life.lua +111 -0
  111. data/test/fixtures/app/vendor/lua/test/luac.lua +7 -0
  112. data/test/fixtures/app/vendor/lua/test/printf.lua +7 -0
  113. data/test/fixtures/app/vendor/lua/test/readonly.lua +12 -0
  114. data/test/fixtures/app/vendor/lua/test/sieve.lua +29 -0
  115. data/test/fixtures/app/vendor/lua/test/sort.lua +66 -0
  116. data/test/fixtures/app/vendor/lua/test/table.lua +12 -0
  117. data/test/fixtures/app/vendor/lua/test/trace-calls.lua +32 -0
  118. data/test/fixtures/app/vendor/lua/test/trace-globals.lua +38 -0
  119. data/test/fixtures/app/vendor/lua/test/xd.lua +14 -0
  120. data/test/fixtures/app/xml/classdub_1_1_matrix.xml +239 -0
  121. data/test/fixtures/app/xml/classdub_1_1_t_mat.xml +233 -0
  122. data/test/fixtures/app/xml/combine.xslt +15 -0
  123. data/test/fixtures/app/xml/compound.xsd +814 -0
  124. data/test/fixtures/app/xml/dir_53661a2bdeb1d55e60581a7e15deb763.xml +12 -0
  125. data/test/fixtures/app/xml/index.xml +42 -0
  126. data/test/fixtures/app/xml/index.xsd +66 -0
  127. data/test/fixtures/app/xml/matrix_8h.xml +149 -0
  128. data/test/fixtures/app/xml/namespacedub.xml +41 -0
  129. data/test/fixtures/classcv_1_1_mat.xml +1996 -0
  130. data/test/fixtures/classcv_1_1_point__.xml +341 -0
  131. data/test/fixtures/classcv_1_1_size__.xml +270 -0
  132. data/test/fixtures/group___magic_type.xml +406 -0
  133. data/test/fixtures/namespacecv.xml +12659 -0
  134. data/test/function_group_test.rb +15 -0
  135. data/test/function_test.rb +252 -0
  136. data/test/group_test.rb +155 -0
  137. data/test/helper.rb +34 -0
  138. data/test/klass_test.rb +297 -0
  139. data/test/lua_function_gen_test.rb +179 -0
  140. data/test/namespace_test.rb +220 -0
  141. data/test/parser_test.rb +36 -0
  142. metadata +216 -0
@@ -0,0 +1,297 @@
1
+ require 'helper'
2
+ require 'dub/lua'
3
+
4
+ class KlassTest < Test::Unit::TestCase
5
+
6
+ context 'A Klass' do
7
+ setup do
8
+ # namespacecv_xml = Dub.parse(fixture('app/xml/namespacedub.xml'))
9
+ @class = namespacedub_xml[:dub][:Matrix]
10
+ end
11
+
12
+ should 'return a list of Functions with members' do
13
+ assert_kind_of Array, @class.members
14
+ assert_kind_of Dub::Function, @class.members.first
15
+ end
16
+
17
+ should 'return name with name' do
18
+ assert_equal 'Matrix', @class.name
19
+ end
20
+
21
+ should 'have namespace prefix' do
22
+ assert_equal 'dub', @class.prefix
23
+ end
24
+
25
+ should 'combine prefix and name in lib_name' do
26
+ assert_equal 'dub_Matrix', @class.lib_name
27
+ end
28
+
29
+ should 'combine prefix and name in id_name' do
30
+ assert_equal 'dub.Matrix', @class.id_name
31
+ end
32
+
33
+ should 'combine prefix and provided name in id_name' do
34
+ assert_equal 'dub.Foobar', @class.id_name('Foobar')
35
+ end
36
+
37
+ should 'use parent namespace in full_type' do
38
+ assert_equal 'dub::Matrix', @class.full_type
39
+ end
40
+
41
+ should 'return file and line on source' do
42
+ assert_match %r{app/include/matrix\.h:\d+}, @class.source
43
+ end
44
+
45
+ should 'return a list of class methods' do
46
+ assert_kind_of Array, @class.class_methods
47
+ end
48
+
49
+ context 'bound member list' do
50
+ setup do
51
+ Dub::Lua.bind(@class)
52
+ @list = @class.members.map {|m| m.name}
53
+ end
54
+
55
+ should 'remove destructor from member list' do
56
+ assert !@list.include?("~Matrix")
57
+ end
58
+
59
+ should 'remove constructor from member list' do
60
+ assert !@list.include?("Matrix")
61
+ end
62
+
63
+ should 'ignore template methods in member list' do
64
+ assert !@list.include?("give_me_tea")
65
+ end
66
+
67
+ should 'ignore members with templated arguments' do
68
+ # at least for now
69
+ assert !@list.include?("mul")
70
+ assert_no_match %r{Matrix_mul}, @class.to_s
71
+ end
72
+
73
+ should 'ignore operator methods in member list' do
74
+ assert !@list.include?("operator size_t")
75
+ end
76
+
77
+ should 'ignore members returning native pointers' do
78
+ assert !@list.include?("ptr")
79
+ end
80
+ end
81
+
82
+ should 'return constructor with constructor' do
83
+ const = @class.constructor
84
+ assert_kind_of Dub::Function, const.first
85
+ end
86
+
87
+ should 'respond to destructor_name' do
88
+ assert_equal 'Matrix_destructor', @class.destructor_name
89
+ end
90
+
91
+ should 'respond to tostring_name' do
92
+ assert_equal 'Matrix__tostring', @class.tostring_name
93
+ end
94
+
95
+ should 'respond to constructor.method_name' do
96
+ assert_equal 'Matrix_Matrix', @class.constructor.method_name(0)
97
+ end
98
+
99
+ should 'find method with array index' do
100
+ assert_kind_of Dub::Function, @class[:rows]
101
+ end
102
+
103
+ should 'return header name on header' do
104
+ assert_equal 'matrix.h', @class.header
105
+ end
106
+
107
+ should 'return defined header if changed' do
108
+ klass = Dub.parse(fixture('app/xml/namespacedub.xml'))[:dub][:Matrix]
109
+ klass.header = 'opencv/cv.h'
110
+ assert_equal 'opencv/cv.h', klass.header
111
+ end
112
+
113
+ should 'know that it is not a template' do
114
+ assert !@class.template?
115
+ end
116
+
117
+ context 'bound to a generator' do
118
+ setup do
119
+ Dub::Lua.bind(@class)
120
+ end
121
+
122
+ should 'bind each member' do
123
+ assert_equal Dub::Lua.function_generator, @class.members.first.gen
124
+ end
125
+
126
+ should 'register constructor' do
127
+ assert_match %r{\{\s*\"Matrix\"\s*,\s*Matrix_Matrix\s*\}}, @class.to_s
128
+ end
129
+
130
+ should 'build constructor' do
131
+ result = @class.to_s
132
+ assert_match %r{static int Matrix_Matrix1\s*\(}, result
133
+ assert_match %r{static int Matrix_Matrix2\s*\(}, result
134
+ assert_match %r{static int Matrix_Matrix\s*\(}, result
135
+ end
136
+
137
+ should 'return new objects in constructors' do
138
+ @class = namespacecv_xml[:cv][:Mat]
139
+ Dub::Lua.bind(@class)
140
+ assert_match %r{lua_pushclass<Mat>.*"cv.Mat"}, @class.constructor.first.to_s
141
+ end
142
+
143
+ should 'include class header' do
144
+ assert_match %r{#include\s+"matrix.h"}, @class.to_s
145
+ end
146
+
147
+ should 'include helper header' do
148
+ assert_match %r{#include\s+"lua_cpp_helper.h"}, @class.to_s
149
+ end
150
+
151
+ should 'create Lua metatable with class name' do
152
+ assert_match %r{luaL_newmetatable\(L,\s*"dub.Matrix"\)}, @class.to_s
153
+ end
154
+
155
+ should 'not build template methods' do
156
+ assert_no_match %r{give_me_tea}, @class.to_s
157
+ end
158
+
159
+ should 'declare tostring' do
160
+ assert_match %r{__tostring}, @class.to_s
161
+ end
162
+
163
+ should 'implement tostring' do
164
+ assert_match %r{Matrix__tostring\(lua_State}, @class.to_s
165
+ end
166
+ end
167
+ end
168
+
169
+ context 'A template class' do
170
+ setup do
171
+ # namespacecv_xml = Dub.parse(fixture('app/xml/namespacedub.xml'))
172
+ @class = namespacedub_xml[:dub].template_class(:TMat)
173
+ end
174
+
175
+ should 'know that it is a template' do
176
+ assert @class.template?
177
+ end
178
+
179
+ should 'return template parameters' do
180
+ assert_equal ['T'], @class.template_params
181
+ end
182
+ end
183
+
184
+ context 'A class defined from a template' do
185
+ setup do
186
+ @class = namespacecv_xml[:cv][:Size]
187
+ end
188
+
189
+ should 'replace template parameter in method arguments' do
190
+ Dub::Lua.bind(@class)
191
+ assert_match %r{int *_width}, @class.to_s
192
+ end
193
+
194
+ should 'register in the template for these types' do
195
+ @tclass = namespacecv_xml[:cv].template_class(:Size_)
196
+ assert_equal @class, @tclass.instanciations[['int']]
197
+ end
198
+ end
199
+
200
+ context 'A class with alias names' do
201
+ setup do
202
+ # namespacecv_xml = Dub.parse(fixture('app/xml/namespacedub.xml'))
203
+ @class = namespacedub_xml[:dub][:FloatMat]
204
+ end
205
+
206
+ should 'return a list of these alias on alias_names' do
207
+ assert_equal ['FloatMat'], @class.alias_names
208
+ assert_equal 'FMatrix', @class.name
209
+ end
210
+
211
+ should 'find class from alias in namespace' do
212
+ assert_equal @class, namespacedub_xml[:dub][:FMatrix]
213
+ end
214
+
215
+ should 'use the shortest alias as main name' do
216
+ assert_equal 'Size', namespacecv_xml[:cv][:Size2i].name
217
+ end
218
+
219
+ should 'rename constructors to shortest name' do
220
+ assert_equal 'Size', namespacecv_xml[:cv][:Size].constructor.name
221
+ end
222
+
223
+ should 'parse arguments and evaluate types by resolving template params' do
224
+ size_class = namespacecv_xml[:cv][:Size]
225
+ Dub::Lua.bind(size_class)
226
+ assert_match %r{const Point \*pt}, size_class.constructor[5].to_s
227
+ end
228
+
229
+ context 'bound to a generator' do
230
+ setup do
231
+ Dub::Lua.bind(@class)
232
+ end
233
+
234
+ should 'register all alias_names' do
235
+ result = @class.to_s
236
+ assert_match %r{"FloatMat"\s*,\s*FMatrix_FMatrix}, result
237
+ assert_match %r{"FMatrix"\s*,\s*FMatrix_FMatrix}, result
238
+ assert_match %r{luaL_register\(L,\s*"dub".*FMatrix_namespace_methods}, result
239
+ end
240
+
241
+ should 'use the smallest name in method definition' do
242
+ assert_match %r{int FMatrix_FMatrix}, @class.to_s
243
+ end
244
+ end
245
+ end
246
+
247
+ context 'A class with overloaded methods' do
248
+ setup do
249
+ # namespacecv_xml = Dub.parse(fixture('app/xml/namespacedub.xml'))
250
+ @class = namespacecv_xml[:cv][:Mat]
251
+ Dub::Lua.bind(@class)
252
+ end
253
+
254
+ should 'declare chooser' do
255
+ result = @class.gen.method_registration(@class)
256
+ assert_match %r{"zeros"\s*,\s*Mat_zeros\}}, result
257
+ assert_no_match %r{zeros1}, result
258
+ end
259
+ end
260
+
261
+ context 'A complex class' do
262
+ setup do
263
+ # namespacecv_xml = Dub.parse(fixture('app/xml/namespacedub.xml'))
264
+ @class = namespacecv_xml[:cv][:Mat]
265
+ end
266
+
267
+ context 'bound to a generator' do
268
+ setup do
269
+ Dub::Lua.bind(@class)
270
+ end
271
+
272
+ should 'list all members on members' do
273
+ assert_equal %w{addref adjustROI assignTo channels clone col colRange convertTo copyTo create cross depth diag dot elemSize elemSize1 empty eye isContinuous locateROI ones release reshape row rowRange setTo size step1 type zeros}, @class.members.map {|m| m.name}
274
+ end
275
+ end
276
+ end
277
+
278
+ context 'A class with enums' do
279
+ setup do
280
+ # namespacecv_xml = Dub.parse(fixture('app/xml/namespacedub.xml'))
281
+ @class = namespacecv_xml[:cv][:Mat]
282
+ end
283
+
284
+ should 'respond true to has_enums' do
285
+ assert @class.has_constants?
286
+ end
287
+
288
+ should 'produce namespaced declarations' do
289
+ assert_match %r{\{"AUTO_STEP"\s*,\s*cv::Mat::AUTO_STEP\}}, Dub::Lua.class_generator.constants_registration(@class)
290
+ end
291
+
292
+ should 'find a list of enums' do
293
+ assert_equal %w{MAGIC_VAL AUTO_STEP CONTINUOUS_FLAG}, @class.enums
294
+ end
295
+ end
296
+
297
+ end
@@ -0,0 +1,179 @@
1
+ require 'helper'
2
+ require 'dub/lua'
3
+
4
+ class LuaFunctionGenTest < Test::Unit::TestCase
5
+ context 'A Lua generator' do
6
+ setup do
7
+ @generator = Dub::Lua
8
+ end
9
+
10
+ context 'with a function' do
11
+ setup do
12
+ # namespacecv_xml = Dub.parse(fixture('namespacecv.xml'))
13
+ @function = namespacecv_xml[:cv][:resize]
14
+ end
15
+
16
+ should 'return Function on bind' do
17
+ assert_kind_of Dub::Function, @generator.bind(@function)
18
+ end
19
+ end
20
+ end
21
+
22
+ context 'A Function' do
23
+ setup do
24
+ @function = namespacecv_xml[:cv][:resize]
25
+ end
26
+
27
+ context 'bound to a Lua generator' do
28
+ setup do
29
+ Dub::Lua.bind(@function)
30
+ @generator = Dub::Lua.function_generator
31
+ end
32
+
33
+
34
+ should 'return string on to_s' do
35
+ assert_kind_of String, @function.to_s
36
+ end
37
+
38
+ should 'generate a static function returning an int' do
39
+ assert_match %r{static int cv_resize}, @function.to_s
40
+ end
41
+
42
+ should 'insert code to check for an arguments on get_arg' do
43
+ assert_match /Mat.*\*\s*src\s*=\s*\*\(\(const\s+Mat\s+\*\*\)\s*luaL_checkudata\s*\(L,\s*1,\s*\"cv\.Mat\"\s*\)\)\s*;/,
44
+ @generator.get_arg(@function.arguments.first, 1) # 1 = first argument in Lua stack
45
+ end
46
+
47
+ context 'with default values' do
48
+ should 'verify stack size' do
49
+ assert_match /top__\s*<\s*6/, @generator.function(@function)
50
+ end
51
+ end
52
+
53
+ context 'in a class with a class as parameter' do
54
+ setup do
55
+ @function = namespacecv_xml[:cv][:Mat][:locateROI]
56
+ end
57
+
58
+ should 'use parameter class identifier' do
59
+ assert_match /Size\s*\*\*\)luaL_checkudata\s*\(L,\s*1,\s*\"cv\.Size\"\s*\)\)\s*;/,
60
+ @generator.get_arg(@function.arguments.first, 1) # 1 = first argument in Lua stack
61
+ end
62
+ end
63
+ end
64
+ end
65
+
66
+ context 'A group of overloaded functions' do
67
+ setup do
68
+ # namespacecv_xml = Dub.parse(fixture('namespacecv.xml'))
69
+ @function_group = namespacecv_xml[:cv][:divide]
70
+ end
71
+
72
+ context 'bound to a Lua generator' do
73
+ setup do
74
+ Dub::Lua.bind(@function_group)
75
+ end
76
+
77
+ should 'return string content on to_s' do
78
+ assert_kind_of String, @function_group.to_s
79
+ end
80
+
81
+ should 'generate a static function returning an int' do
82
+ assert_match %r{static int cv_divide}, @function_group.to_s
83
+ end
84
+
85
+ should 'generate a static function returning an int for each overloaded function' do
86
+ bindings = @function_group.to_s
87
+ assert_match %r{static int cv_divide1}, bindings
88
+ assert_match %r{static int cv_divide2}, bindings
89
+ assert_match %r{static int cv_divide3}, bindings
90
+ assert_match %r{static int cv_divide4}, bindings
91
+ end
92
+
93
+ # should 'declare chooser' do
94
+ # assert_match %r{"divide",\s*cv_divide\}}, @group.to_s
95
+ # end
96
+ end
97
+ end
98
+
99
+ context 'A function with a custom class return value' do
100
+ setup do
101
+ # namespacecv_xml = Dub.parse(fixture('namespacecv.xml'))
102
+ @function = namespacecv_xml[:cv][:getRotationMatrix2D]
103
+ end
104
+
105
+ context 'bound to a Lua generator' do
106
+ setup do
107
+ Dub::Lua.bind(@function)
108
+ end
109
+
110
+ should 'call template push method' do
111
+ assert_match %r{lua_pushclass<Mat>\(\s*L\s*,\s*retval__\s*,\s*"cv.Mat"\s*\)}, @function.to_s
112
+ end
113
+ end
114
+ end
115
+
116
+ context 'A function with pointer parameters' do
117
+ setup do
118
+ # namespacecv_xml = Dub.parse(fixture('namespacecv.xml'))
119
+ @function = namespacecv_xml[:cv][:calcHist][0]
120
+ end
121
+
122
+ context 'bound to a Lua generator' do
123
+ setup do
124
+ Dub::Lua.bind(@function)
125
+ end
126
+
127
+ should 'use a DubArgPointer with the given type' do
128
+ assert_match %r{DubArgPointer<int>}, @function.to_s
129
+ end
130
+ end
131
+ end
132
+
133
+ context 'A member function bound to a Lua generator' do
134
+ setup do
135
+ @member = namespacedub_xml[:dub][:Matrix][:rows]
136
+ Dub::Lua.bind(@member)
137
+ end
138
+
139
+ should 'start by getting self' do
140
+ assert_match %r{self__\s*=\s*\*\(\(Matrix\*\*\)luaL_checkudata}, @member.to_s
141
+ end
142
+
143
+ should 'prefix call with self' do
144
+ assert_match %r{self__->rows\(}, @member.to_s
145
+ end
146
+ end
147
+
148
+ context 'A constructor bound to a Lua generator' do
149
+ setup do
150
+ @constructor = namespacedub_xml[:dub][:Matrix][:Matrix].first
151
+ Dub::Lua.bind(@constructor)
152
+ end
153
+
154
+ should 'use pushclass in constructor' do
155
+ result = @constructor.to_s
156
+ assert_match %r{lua_pushclass<Matrix>\s*\(L, retval__, \"dub.Matrix\"\s*\)}, result
157
+ end
158
+ end
159
+
160
+ context 'A function without return value' do
161
+ setup do
162
+ @function = namespacecv_xml[:cv][:blur]
163
+ end
164
+
165
+ context 'bound to a Lua generator' do
166
+ setup do
167
+ Dub::Lua.bind(@function)
168
+ end
169
+
170
+ should 'return 0' do
171
+ assert_match %r{return\s+0}, @function.to_s
172
+ end
173
+ end
174
+ end
175
+ end
176
+
177
+
178
+
179
+
@@ -0,0 +1,220 @@
1
+ require 'helper'
2
+ require 'dub/lua'
3
+
4
+ class NamespaceTest < Test::Unit::TestCase
5
+ context 'A Namespace' do
6
+ setup do
7
+ # namespacecv_xml = Dub.parse(fixture('namespacecv.xml'))
8
+ @namespace = namespacecv_xml[:cv]
9
+ end
10
+
11
+ should 'find a function with functions method' do
12
+ assert_kind_of Dub::Function, @namespace.function(:resize)
13
+ end
14
+
15
+ should 'find a function with array index' do
16
+ assert_kind_of Dub::Function, @namespace[:resize]
17
+ end
18
+
19
+ should 'respond to name' do
20
+ assert_equal 'cv', @namespace.name
21
+ end
22
+
23
+ should 'respond to lib_name' do
24
+ # nested namespace could be cv_more
25
+ assert_equal 'cv', @namespace.lib_name
26
+ end
27
+
28
+ should 'respond to id_name' do
29
+ # nested namespace could be cv.more
30
+ assert_equal 'cv', @namespace.id_name
31
+ end
32
+
33
+ should 'return header name on header' do
34
+ assert_equal 'cv.hpp', @namespace.header
35
+ end
36
+
37
+ should 'return defined header if changed' do
38
+ namespace = Dub.parse(fixture('namespacecv.xml'))[:cv]
39
+ namespace.header = 'opencv/cv.h'
40
+ assert_equal 'opencv/cv.h', namespace.header
41
+ end
42
+
43
+ should 'remove members on minus equal operator when bound' do
44
+ namespace = Dub.parse(fixture('app/xml/namespacedub.xml'))[:dub]
45
+ namespace.ignore 'FMatrix'
46
+ assert_equal %w{Matrix}, namespace.classes.map{|k| k.name}
47
+ end
48
+
49
+ context 'when bound' do
50
+ setup do
51
+ @generator = Dub::Lua.namespace_generator
52
+ end
53
+
54
+ should 'contain generator' do
55
+ res = Dub::Lua.bind(@namespace)
56
+ assert_equal res, @namespace
57
+ assert_equal @generator, @namespace.gen
58
+ end
59
+
60
+ end
61
+
62
+ context 'with overloaded functions' do
63
+ setup do
64
+ @function = namespacecv_xml[:cv][:divide]
65
+ end
66
+
67
+ should 'find a Dub::FunctionGroup' do
68
+ assert_kind_of Dub::FunctionGroup, @function
69
+ end
70
+
71
+ should 'find a group of functions' do
72
+ assert_kind_of Dub::Function, @function[0]
73
+ assert_kind_of Dub::Function, @function[1]
74
+ end
75
+
76
+ should 'group functions by name' do
77
+ assert_equal 'divide', @function[0].name
78
+ assert_equal 'divide', @function[1].name
79
+ end
80
+
81
+ should 'assign an overloaded_index to grouped functions' do
82
+ assert_equal 1, @function[0].overloaded_index
83
+ assert_equal 2, @function[1].overloaded_index
84
+ end
85
+ end
86
+ end
87
+
88
+ context 'A namespace with class definitions' do
89
+ setup do
90
+ @namespace = namespacedub_xml[:dub]
91
+ end
92
+
93
+ should 'find classes by array index' do
94
+ assert_kind_of Dub::Klass, @namespace[:Matrix]
95
+ end
96
+
97
+ should 'find classes with klass' do
98
+ assert_kind_of Dub::Klass, @namespace.klass('Matrix')
99
+ end
100
+
101
+ should 'return a list of classes with classes' do
102
+ assert_kind_of Array, @namespace.classes
103
+ assert_kind_of Dub::Klass, @namespace.classes.first
104
+ end
105
+ end
106
+
107
+ context 'A namespace with template class definitions' do
108
+ setup do
109
+ @namespace = namespacedub_xml[:dub]
110
+ end
111
+
112
+ should 'ignore template classes in class list' do
113
+ assert !@namespace.classes.map{|m| m.name}.include?("TMat")
114
+ end
115
+
116
+ should 'return template class with template_class' do
117
+ assert_kind_of Dub::Klass, @namespace.template_class('TMat')
118
+ end
119
+
120
+ should 'build a full classes for template typedefs' do
121
+ assert_kind_of Dub::Klass, @namespace.klass(:FloatMat)
122
+ end
123
+
124
+ context 'bound to a generator' do
125
+ setup do
126
+ Dub::Lua.bind(@namespace)
127
+ end
128
+
129
+ should 'generate a valid class' do
130
+ # TODO: rerun all tests for lua class generation
131
+ assert_match %r{luaL_register\(L,\s*"dub".*FMatrix}, @namespace[:FloatMat].to_s
132
+ end
133
+ end
134
+ end
135
+
136
+ context 'A namespace with enums' do
137
+ setup do
138
+ @namespace = namespacecv_xml[:cv]
139
+ end
140
+
141
+ should 'respond true to has_enums' do
142
+ assert @namespace.has_constants?
143
+ end
144
+
145
+ should 'produce namespaced declarations' do
146
+ assert_match %r{\{"INTER_LINEAR"\s*,\s*cv::INTER_LINEAR\}}, Dub::Lua.namespace_generator.constants_registration(@namespace)
147
+ end
148
+
149
+ context 'bound to a generator' do
150
+ setup do
151
+ Dub::Lua.bind(@namespace)
152
+ end
153
+
154
+ should 'produce enums registration' do
155
+ result = @namespace.to_s
156
+ assert_match %r{\{"INTER_LINEAR"\s*,\s*cv::INTER_LINEAR\}}, result
157
+ assert_match %r{register_constants\(L,\s*"cv",\s*cv_namespace_constants\)}, result
158
+ end
159
+ end
160
+
161
+ context 'On merge with a group' do
162
+ setup do
163
+ @namespace = Dub.parse(fixture('namespacecv.xml'))[:cv]
164
+ group = Dub.parse(fixture('group___magic_type.xml'))[:MagicType]
165
+ @namespace.merge!(group)
166
+ Dub::Lua.bind(@namespace)
167
+ end
168
+
169
+ should 'produce enums and defines registration' do
170
+ result = @namespace.to_s
171
+ assert_match %r{\{"INTER_LINEAR"\s*,\s*cv::INTER_LINEAR\}}, result
172
+ assert_match %r{register_constants\(L,\s*"cv",\s*cv_namespace_constants\)}, result
173
+ assert_match %r{\{"CV_32FC1"\s*,\s*CV_32FC1\}}, result
174
+ assert_match %r{register_constants\(L,\s*"cv",\s*cv_namespace_constants\)}, result
175
+ end
176
+ end
177
+ end
178
+
179
+ context 'A Group with defines' do
180
+ setup do
181
+ # groupmagic_xml = Dub.parse(fixture('group___magic_type.xml'))
182
+ @group = groupmagic_xml[:MagicType]
183
+ end
184
+
185
+ should 'parse defines' do
186
+ assert @group.defines.include?("CV_32FC1")
187
+ end
188
+
189
+ should 'ignore defines with parameters' do
190
+ assert !@group.defines.include?("CV_8UC")
191
+ end
192
+
193
+ context 'bound to a generator' do
194
+ setup do
195
+ Dub::Lua.bind(@group)
196
+ @group.name = 'cv'
197
+ end
198
+
199
+ should 'produce defines registration' do
200
+ result = @group.to_s
201
+ assert_match %r{\{"CV_32FC1"\s*,\s*CV_32FC1\}}, result
202
+ assert_match %r{register_constants\(L,\s*"cv",\s*cv_namespace_constants\)}, result
203
+ end
204
+ end
205
+ end
206
+
207
+ context 'A namespace bound to a generator' do
208
+ setup do
209
+ @namespace = namespacecv_xml[:cv]
210
+ Dub::Lua.bind(@namespace)
211
+ end
212
+
213
+ should 'produce methods registration' do
214
+ result = @namespace.to_s
215
+ assert_match %r{\{"resize"\s*,\s*cv_resize\},}, result
216
+ assert_match %r{luaL_register\(L,\s*"cv".*cv_functions}, result
217
+ end
218
+ end
219
+
220
+ end