RuCodeGen 0.1.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.
- data/LICENSE +25 -0
- data/README +34 -0
- data/Rakefile +32 -0
- data/docs/Principle +152 -0
- data/docs/ValueIncapsulator +195 -0
- data/docs/examples/cpp/custom/submit_deliver +909 -0
- data/docs/examples/cpp/value_incapsulator/cfg +127 -0
- data/docs/examples/cpp/value_incapsulator/host_config +51 -0
- data/examples/cpp/custom/submit_deliver/submit_deliver.rb +43 -0
- data/examples/cpp/custom/submit_deliver/submit_deliver_gen.rb +326 -0
- data/examples/cpp/value_incapsulator/cfg.decl.hpp +51 -0
- data/examples/cpp/value_incapsulator/cfg.impl.cpp +63 -0
- data/examples/cpp/value_incapsulator/cg-cfg.rb +19 -0
- data/examples/cpp/value_incapsulator/cg-cfg.test.cpp +15 -0
- data/examples/cpp/value_incapsulator/cg-host_config.rb +12 -0
- data/examples/cpp/value_incapsulator/cout.log +4 -0
- data/examples/cpp/value_incapsulator/host_config.impl.cpp +21 -0
- data/examples/cpp/value_incapsulator/host_config.impl.hpp +21 -0
- data/lib/rucodegen/begin_end.rb +26 -0
- data/lib/rucodegen/codegen.rb +4 -0
- data/lib/rucodegen/filename_producer.rb +57 -0
- data/lib/rucodegen/generation_initiator.rb +92 -0
- data/lib/rucodegen/generators.rb +36 -0
- data/lib/rucodegen/run_mode.rb +89 -0
- data/lib/rucodegen/value_incapsulator.rb +331 -0
- data/lib/rucodegen.rb +4 -0
- data/tests/generators_test_extension.rb +23 -0
- data/tests/tc_class_attribute.rb +87 -0
- data/tests/tc_codegen_initiator.rb +164 -0
- data/tests/tc_filename_producer.rb +54 -0
- data/tests/tc_generators.rb +55 -0
- data/tests/tc_run_mode.rb +72 -0
- data/tests/tc_value_incapsulator.rb +193 -0
- data/tests/tc_value_incapsulator_gen.rb +479 -0
- data/tests/ts_rucodegen.rb +11 -0
- metadata +98 -0
@@ -0,0 +1,331 @@
|
|
1
|
+
require 'set'
|
2
|
+
|
3
|
+
require 'rucodegen/generators'
|
4
|
+
require 'rucodegen/filename_producer'
|
5
|
+
|
6
|
+
module RuCodeGen
|
7
|
+
|
8
|
+
# �����, ������� ��������� ���� ������� ������ (��������, ���
|
9
|
+
# ������� ValueIncapsulator).
|
10
|
+
class ClassAttribute
|
11
|
+
# ������ �����, ������� ����� ��������� ������������.
|
12
|
+
PRIMITIVE_TYPES = Set.new [
|
13
|
+
"bool",
|
14
|
+
"char", "signed char", "unsigned char",
|
15
|
+
"wchar_t",
|
16
|
+
"short", "short int", "unsigned short", "unsigned short int",
|
17
|
+
"int", "unsigned int", "unsigned",
|
18
|
+
"long", "long int", "unsigned long", "unsigned long int", "long long",
|
19
|
+
"float", "double" ];
|
20
|
+
|
21
|
+
# ��� ��������.
|
22
|
+
attr :name
|
23
|
+
|
24
|
+
# ��� ���� ��������.
|
25
|
+
attr :type
|
26
|
+
|
27
|
+
# ��������� �������� ��������.
|
28
|
+
# ����� ���� nil, ���� ��������� �������� �� ������.
|
29
|
+
attr_writer :default
|
30
|
+
|
31
|
+
def initialize( a_name, a_type )
|
32
|
+
@name = a_name
|
33
|
+
@type = a_type
|
34
|
+
@default = nil
|
35
|
+
@getter_returns_value = false
|
36
|
+
end
|
37
|
+
|
38
|
+
# ���������� ������� ����, ��� getter ������ ����������
|
39
|
+
# �������� ��������, � �� ������ �� ����.
|
40
|
+
def getter_returns_value
|
41
|
+
@getter_returns_value = true
|
42
|
+
end
|
43
|
+
|
44
|
+
#FIXME: ����� once, ����� ������������ ��
|
45
|
+
#�������� ����������� ����� ���� ���, ����� ����������� ������
|
46
|
+
#getter_return_type, has_default?, default.
|
47
|
+
|
48
|
+
# ��������� ����� ����, ������� ����� ������������ � ��������
|
49
|
+
# ������������� �������� getter-� � � �������� ���� ���������
|
50
|
+
# setter-�.
|
51
|
+
def getter_return_type
|
52
|
+
# ���� getter ���������� ��������, � �� ������, �� ��� ������.
|
53
|
+
return type if @getter_returns_value
|
54
|
+
|
55
|
+
# � ��������� ������ �������� ����������, �������� �� ���
|
56
|
+
# ������������ �����.
|
57
|
+
return type if ClassAttribute::is_primitive_type( type )
|
58
|
+
|
59
|
+
# ����� ���������� ����������� ������.
|
60
|
+
return "const #{type} &"
|
61
|
+
end
|
62
|
+
|
63
|
+
# ����������, ���� �� ��� �������� ������������� ��-���������.
|
64
|
+
# ������������� �� ��������� ����, ����:
|
65
|
+
# * ������ �������� ��� �������� default;
|
66
|
+
# * �������� �������� default �� ������, �� ��� ����������� � �����
|
67
|
+
# ����������� �����.
|
68
|
+
def has_default?
|
69
|
+
return true if nil != @default
|
70
|
+
return ClassAttribute.is_primitive_type( type )
|
71
|
+
end
|
72
|
+
|
73
|
+
# �������� �������� �� ��������� ��� ��������.
|
74
|
+
def default
|
75
|
+
return @default if nil != @default
|
76
|
+
return 0 if ClassAttribute.is_primitive_type( type )
|
77
|
+
|
78
|
+
fail "Attribute #{name} has no default value"
|
79
|
+
end
|
80
|
+
|
81
|
+
# �������� ����, �������� �� ��� �������� ����������� �����.
|
82
|
+
# ����������� ��� ������������ getter-�� � ���������� setter-� �� ��������.
|
83
|
+
# ��� ������������ ���� ��������������� �� ��������� �������� 0.
|
84
|
+
def ClassAttribute.is_primitive_type( a_type )
|
85
|
+
return PRIMITIVE_TYPES.include?( a_type )
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
# �����, ������� �������� �������� C++ ������ ��� ������� ValueIncapsulator.
|
90
|
+
class ValueIncapsulator
|
91
|
+
# ����, ������� ���������� ��������� �������� ��������.
|
92
|
+
ATTR_DEFAULT_VALUE = :default
|
93
|
+
# ����, ������� ���������� getter ���������� �������� ��������,
|
94
|
+
# � �� ������ �� �������.
|
95
|
+
GETTER_RETURNS_VALUE = :getter_returns_value
|
96
|
+
|
97
|
+
#FIXME: ����� �������� ��� �� ���������:
|
98
|
+
# :protected, :private -- ��� ���������� �������� � ��������.
|
99
|
+
|
100
|
+
#FIXME: ��� ���������� ���������� ����� ��� �� ���������, ������ ��
|
101
|
+
#������������ ������ ���� inline ��� ���.
|
102
|
+
|
103
|
+
# ����������, ������� �������� �������� getter-�� � setter-��
|
104
|
+
# ��� ��������� ValueIncapsulator.
|
105
|
+
def self.getter_setter_for( a_attribute )
|
106
|
+
class_eval %Q{
|
107
|
+
def get_#{a_attribute}
|
108
|
+
@#{a_attribute}
|
109
|
+
end
|
110
|
+
def #{a_attribute}( a_value )
|
111
|
+
@#{a_attribute} = a_value
|
112
|
+
end
|
113
|
+
}
|
114
|
+
end
|
115
|
+
|
116
|
+
# ��� ������ � C++.
|
117
|
+
attr_reader :class_name
|
118
|
+
|
119
|
+
# �����������.
|
120
|
+
#
|
121
|
+
# [_a_class_name_] ��� ������ � C++.
|
122
|
+
def initialize( a_class_name )
|
123
|
+
@class_name = a_class_name
|
124
|
+
@attr_prefix = ""
|
125
|
+
@attr_suffix = ""
|
126
|
+
@getter_prefix = ""
|
127
|
+
@setter_prefix = ""
|
128
|
+
@attributes = []
|
129
|
+
@decl_file = nil
|
130
|
+
@impl_file = nil
|
131
|
+
end
|
132
|
+
|
133
|
+
getter_setter_for :attr_prefix
|
134
|
+
getter_setter_for :attr_suffix
|
135
|
+
getter_setter_for :getter_prefix
|
136
|
+
getter_setter_for :setter_prefix
|
137
|
+
getter_setter_for :decl_file
|
138
|
+
getter_setter_for :impl_file
|
139
|
+
|
140
|
+
# �������� ������ ���������.
|
141
|
+
def get_attributes
|
142
|
+
@attributes
|
143
|
+
end
|
144
|
+
|
145
|
+
# �������� ��� ���� �������.
|
146
|
+
#
|
147
|
+
# [_a_name_] ��� ��������.
|
148
|
+
# [_a_type_] ��� ���� ��������.
|
149
|
+
# [_a_props_] Hash � ��������������� ����������� ��������.
|
150
|
+
def attr( a_name, a_type, a_props = {} )
|
151
|
+
a = ClassAttribute.new( a_name, a_type )
|
152
|
+
a.default = a_props[ ATTR_DEFAULT_VALUE ] if
|
153
|
+
a_props.has_key? ATTR_DEFAULT_VALUE
|
154
|
+
|
155
|
+
a.getter_returns_value if a_props.has_key?( GETTER_RETURNS_VALUE ) and
|
156
|
+
true == a_props[ GETTER_RETURNS_VALUE ]
|
157
|
+
|
158
|
+
@attributes << a
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
class ValueIncapsulator
|
163
|
+
|
164
|
+
module Generation
|
165
|
+
|
166
|
+
# �������� ��� �������� ������ ��� ��� �������� (� ������
|
167
|
+
# ��������� � ���������).
|
168
|
+
def Generation.attribute_name( a_vi, a_attr )
|
169
|
+
"#{a_vi.get_attr_prefix}#{a_attr.name}#{a_vi.get_attr_suffix}"
|
170
|
+
end
|
171
|
+
|
172
|
+
# �������� ��� getter-� ��� �������� � ������ ��������.
|
173
|
+
def Generation.getter_name( a_vi, a_attr )
|
174
|
+
"#{a_vi.get_getter_prefix}#{a_attr.name}"
|
175
|
+
end
|
176
|
+
|
177
|
+
# �������� ��� setter-� ��� �������� � ������ ��������.
|
178
|
+
def Generation.setter_name( a_vi, a_attr )
|
179
|
+
"#{a_vi.get_setter_prefix}#{a_attr.name}"
|
180
|
+
end
|
181
|
+
|
182
|
+
# ������������� ���������� ���� ���������.
|
183
|
+
def Generation.attr_decl( a_vi )
|
184
|
+
a_vi.get_attributes.inject( "" ) do |decl, a|
|
185
|
+
decl += "\t\t#{a.type} #{attribute_name(a_vi,a)};\n"
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
# ������������� ���������� ���� getter-��/setter-��.
|
190
|
+
def Generation.getter_setter_decl( a_vi )
|
191
|
+
a_vi.get_attributes.inject( "" ) do |decl, a|
|
192
|
+
decl += "\t\t#{a.getter_return_type}\n" +
|
193
|
+
"\t\t#{getter_name(a_vi, a)}() const;\n" +
|
194
|
+
"\t\tvoid\n" +
|
195
|
+
"\t\t#{setter_name(a_vi, a)}( #{a.getter_return_type} v__ );\n"
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
# ������� ��������� ������������� �������������� ��������.
|
200
|
+
def Generation.make_initializer_representation( a_initializer )
|
201
|
+
if a_initializer.kind_of?( Integer ) or
|
202
|
+
a_initializer.kind_of?( Symbol )
|
203
|
+
return "#{a_initializer}"
|
204
|
+
elsif a_initializer.kind_of? String
|
205
|
+
return "\"#{a_initializer}\""
|
206
|
+
end
|
207
|
+
end
|
208
|
+
|
209
|
+
# ������������� ������ ��������������� ���� ��������� � ������������.
|
210
|
+
def Generation.attr_initializers( a_vi )
|
211
|
+
return "" if a_vi.get_attributes.empty?
|
212
|
+
|
213
|
+
r = a_vi.get_attributes.inject( [ "", "\t:\t" ] ) do |v, a|
|
214
|
+
initializer = a.has_default? ? a.default : nil
|
215
|
+
initializer_representation = initializer ?
|
216
|
+
make_initializer_representation( initializer ) : ""
|
217
|
+
|
218
|
+
v[ 0 ] += v[ 1 ] + "#{attribute_name(a_vi, a)}" +
|
219
|
+
"(#{initializer_representation})\n"
|
220
|
+
v[ 1 ] = "\t,\t"
|
221
|
+
v
|
222
|
+
end
|
223
|
+
|
224
|
+
r[ 0 ]
|
225
|
+
end
|
226
|
+
|
227
|
+
# ������������� ���������� ������� getter-��/setter-��.
|
228
|
+
def Generation.getter_setter_impl( a_vi )
|
229
|
+
a_vi.get_attributes.inject( "" ) do |v, a|
|
230
|
+
v += "#{a.getter_return_type}\n" +
|
231
|
+
"#{a_vi.class_name}::#{getter_name(a_vi, a)}() const\n" +
|
232
|
+
"\t{ return #{attribute_name(a_vi, a)}; }\n" +
|
233
|
+
"void\n" +
|
234
|
+
"#{a_vi.class_name}::#{setter_name(a_vi, a)}" +
|
235
|
+
"( #{a.getter_return_type} v__ )\n" +
|
236
|
+
"\t{ #{attribute_name(a_vi, a)} = v__; }\n"
|
237
|
+
end
|
238
|
+
end
|
239
|
+
|
240
|
+
# ������������� ���������� ������, ���������� � ValueIncapsulator.
|
241
|
+
def Generation.generate_decl( a_vi )
|
242
|
+
%Q{
|
243
|
+
class #{a_vi.class_name}
|
244
|
+
{
|
245
|
+
private :
|
246
|
+
#{attr_decl(a_vi)}
|
247
|
+
public :
|
248
|
+
#{a_vi.class_name}();
|
249
|
+
|
250
|
+
#{getter_setter_decl(a_vi)}
|
251
|
+
};
|
252
|
+
}
|
253
|
+
end
|
254
|
+
|
255
|
+
# ������������� ���������� ������, ���������� � ValueIncapsulator.
|
256
|
+
def Generation.generate_impl( a_vi )
|
257
|
+
%Q{
|
258
|
+
#{a_vi.class_name}::#{a_vi.class_name}()
|
259
|
+
#{attr_initializers(a_vi)}
|
260
|
+
{}
|
261
|
+
|
262
|
+
#{getter_setter_impl(a_vi)}
|
263
|
+
}
|
264
|
+
end
|
265
|
+
|
266
|
+
# ����� ��� ��������� ����� ���������� ������.
|
267
|
+
class DeclGenerator
|
268
|
+
# �����������.
|
269
|
+
#
|
270
|
+
# � �������� ��������� �������� ������ �� ������ ValueIncapsulator.
|
271
|
+
def initialize( value_incapsulator )
|
272
|
+
@vi = value_incapsulator
|
273
|
+
end
|
274
|
+
|
275
|
+
# ��������� ���������� ������.
|
276
|
+
def generate( to )
|
277
|
+
to << Generation.generate_decl( @vi )
|
278
|
+
end
|
279
|
+
end
|
280
|
+
|
281
|
+
# ����� ��� ��������� ����� ���������� ������.
|
282
|
+
class ImplGenerator
|
283
|
+
# �����������.
|
284
|
+
#
|
285
|
+
# � �������� ��������� �������� ������ �� ������ ValueIncapsulator.
|
286
|
+
def initialize( value_incapsulator )
|
287
|
+
@vi = value_incapsulator
|
288
|
+
end
|
289
|
+
|
290
|
+
# ��������� ���������� ������.
|
291
|
+
def generate( to )
|
292
|
+
to << Generation.generate_impl( @vi )
|
293
|
+
end
|
294
|
+
end
|
295
|
+
|
296
|
+
end # module Generation
|
297
|
+
|
298
|
+
end # class ValueIncapsulator
|
299
|
+
|
300
|
+
end # module RuCodeGen
|
301
|
+
|
302
|
+
# ������� ��� ����������� ���������� ValueIncapsulator.
|
303
|
+
#
|
304
|
+
# �������� ���������� ������� ���� ���� � ����� ���������� - ��������
|
305
|
+
# RuCodeGen::ValueIncapsulator.
|
306
|
+
#
|
307
|
+
# ���������:
|
308
|
+
# [_a_name_] ��� ������������� ������;
|
309
|
+
def cpp_value_incapsulator(
|
310
|
+
a_name,
|
311
|
+
&blk ) # :yields: c
|
312
|
+
vi = RuCodeGen::ValueIncapsulator.new( a_name )
|
313
|
+
blk.call( vi )
|
314
|
+
|
315
|
+
fail "decl_file not specified" if nil == vi.get_decl_file
|
316
|
+
fail "impl_file not specified" if nil == vi.get_impl_file
|
317
|
+
|
318
|
+
RuCodeGen::Generators.add(
|
319
|
+
RuCodeGen::FilenameProducer.produce(
|
320
|
+
$0, vi.get_decl_file ),
|
321
|
+
RuCodeGen::ValueIncapsulator::Generation::
|
322
|
+
DeclGenerator.new( vi ) )
|
323
|
+
RuCodeGen::Generators.add(
|
324
|
+
RuCodeGen::FilenameProducer.produce(
|
325
|
+
$0, vi.get_impl_file ),
|
326
|
+
RuCodeGen::ValueIncapsulator::Generation::
|
327
|
+
ImplGenerator.new( vi ) )
|
328
|
+
|
329
|
+
vi
|
330
|
+
end
|
331
|
+
|
data/lib/rucodegen.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
#
|
2
|
+
# ��� ���������� ������ RuCodeGen::Generators ��� ����������
|
3
|
+
# ���������� ����� ������ � unit-������.
|
4
|
+
#
|
5
|
+
|
6
|
+
require 'rucodegen/generators'
|
7
|
+
|
8
|
+
module RuCodeGen
|
9
|
+
|
10
|
+
class Generators
|
11
|
+
def Generators.test_setup
|
12
|
+
@@generators = {}
|
13
|
+
end
|
14
|
+
def Generators.test_teardown
|
15
|
+
test_setup
|
16
|
+
end
|
17
|
+
def Generators.test_all_generators
|
18
|
+
@@generators
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
end # module RuCodeGen
|
23
|
+
|
@@ -0,0 +1,87 @@
|
|
1
|
+
$:.unshift( File.dirname( __FILE__ ) + "/../lib" )
|
2
|
+
|
3
|
+
require 'test/unit'
|
4
|
+
|
5
|
+
require 'rucodegen/value_incapsulator'
|
6
|
+
|
7
|
+
class TC_ClassAttribute < Test::Unit::TestCase
|
8
|
+
|
9
|
+
def test_primitive_type
|
10
|
+
primitive_types = [
|
11
|
+
"char", "signed char", "unsigned char",
|
12
|
+
"short", "unsigned short",
|
13
|
+
"int", "unsigned int",
|
14
|
+
"long", "unsigned long", "long long",
|
15
|
+
"float", "double" ]
|
16
|
+
|
17
|
+
primitive_types.each do |t|
|
18
|
+
assert_equal true,
|
19
|
+
RuCodeGen::ClassAttribute::is_primitive_type( t )
|
20
|
+
end
|
21
|
+
|
22
|
+
non_primitive_types = [
|
23
|
+
"std::string", "string",
|
24
|
+
"std::vector", "vector",
|
25
|
+
"std::map", "map",
|
26
|
+
"shared_ptr", "ACE_Time_Value" ]
|
27
|
+
non_primitive_types.each do |t|
|
28
|
+
assert_equal false,
|
29
|
+
RuCodeGen::ClassAttribute::is_primitive_type( t )
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_getter_return_value_for_primitive_type
|
34
|
+
[ "char", "int", "float", "unsigned long" ].each do |t|
|
35
|
+
a = RuCodeGen::ClassAttribute.new( "a", t )
|
36
|
+
assert_equal t, a.getter_return_type
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_getter_return_ref_for_non_primitive_type
|
41
|
+
[ "std::string", "ACE_Date_Time",
|
42
|
+
"std::map< std::string, int >" ].each do |t|
|
43
|
+
a = RuCodeGen::ClassAttribute.new( "a", t )
|
44
|
+
assert_equal "const #{t} &", a.getter_return_type
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_getter_return_value_explicitly
|
49
|
+
[ "std::string", "ACE_Date_Time",
|
50
|
+
"std::map< std::string, int >" ].each do |t|
|
51
|
+
a = RuCodeGen::ClassAttribute.new( "a", t )
|
52
|
+
a.getter_returns_value
|
53
|
+
assert_equal t, a.getter_return_type
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_default_value_primitive_type
|
58
|
+
a = RuCodeGen::ClassAttribute.new( "a", "char" )
|
59
|
+
assert_equal true, a.has_default?
|
60
|
+
assert_equal 0, a.default
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_default_value_non_primitive_type
|
64
|
+
a = RuCodeGen::ClassAttribute.new( "a", "std::string" )
|
65
|
+
assert_equal false, a.has_default?
|
66
|
+
assert_raise RuntimeError do
|
67
|
+
a.default
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_explicit_default_value
|
72
|
+
[
|
73
|
+
[ "char", 'c' ],
|
74
|
+
[ "int", 48 ],
|
75
|
+
[ "std::string", "hello" ],
|
76
|
+
[ "ACE_Date_Time", :"ACE_Date_Time::now()" ]
|
77
|
+
].each do |v|
|
78
|
+
|
79
|
+
a = RuCodeGen::ClassAttribute.new( "a", v[ 0 ] )
|
80
|
+
a.default = v[ 1 ]
|
81
|
+
|
82
|
+
assert_equal true, a.has_default?
|
83
|
+
assert_equal v[ 1 ], a.default
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
@@ -0,0 +1,164 @@
|
|
1
|
+
$:.unshift( File.dirname( __FILE__ ) + "/../lib" )
|
2
|
+
|
3
|
+
require 'digest/md5'
|
4
|
+
require 'test/unit'
|
5
|
+
|
6
|
+
require 'rucodegen/generation_initiator'
|
7
|
+
|
8
|
+
require 'generators_test_extension'
|
9
|
+
|
10
|
+
class TC_CodegenInitiator < Test::Unit::TestCase
|
11
|
+
GENERATORS = RuCodeGen::Generators
|
12
|
+
INITIATOR = RuCodeGen::CodegenInitiator
|
13
|
+
|
14
|
+
class Generator
|
15
|
+
def initialize( content )
|
16
|
+
@content = content
|
17
|
+
end
|
18
|
+
|
19
|
+
def generate( to )
|
20
|
+
to << @content
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
CODEGEN_CONTENT = "This is contents of codegen file\n"
|
25
|
+
CODEGEN_CONTENT_HASH = Digest::MD5.new( CODEGEN_CONTENT ).to_s + "\n"
|
26
|
+
CODEGEN_FILE_NAME = "cg-tc_codegen_initiator.rb"
|
27
|
+
CODEGEN_HASH_FILE_NAME = ".#{CODEGEN_FILE_NAME}.md5"
|
28
|
+
OUT_DECL_FILE = "#{CODEGEN_FILE_NAME}.decl"
|
29
|
+
OUT_IMPL_FILE = "#{CODEGEN_FILE_NAME}.impl"
|
30
|
+
OUT_DECL_FILE_CONTENT = "declarations\n"
|
31
|
+
OUT_IMPL_FILE_CONTENT = "implementations\n"
|
32
|
+
|
33
|
+
OUT_DECL_FILE_GENERATOR = Generator.new( OUT_DECL_FILE_CONTENT )
|
34
|
+
OUT_IMPL_FILE_GENERATOR = Generator.new( OUT_IMPL_FILE_CONTENT )
|
35
|
+
|
36
|
+
def setup
|
37
|
+
GENERATORS.test_setup
|
38
|
+
|
39
|
+
GENERATORS.add OUT_DECL_FILE, OUT_DECL_FILE_GENERATOR
|
40
|
+
GENERATORS.add OUT_IMPL_FILE, OUT_IMPL_FILE_GENERATOR
|
41
|
+
|
42
|
+
File.open( CODEGEN_FILE_NAME, "w" ) do |f|
|
43
|
+
f << CODEGEN_CONTENT
|
44
|
+
end
|
45
|
+
|
46
|
+
@initiator = INITIATOR.new( CODEGEN_FILE_NAME )
|
47
|
+
end
|
48
|
+
|
49
|
+
def teardown
|
50
|
+
[
|
51
|
+
CODEGEN_FILE_NAME,
|
52
|
+
CODEGEN_HASH_FILE_NAME,
|
53
|
+
OUT_DECL_FILE,
|
54
|
+
OUT_IMPL_FILE
|
55
|
+
].each do |f|
|
56
|
+
File.delete f if FileTest.exists? f
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_regenerate_all
|
61
|
+
@initiator.initiate
|
62
|
+
|
63
|
+
# ������ ������������ ��� �����, ������� ���������� � ����������
|
64
|
+
# �������������.
|
65
|
+
[
|
66
|
+
CODEGEN_HASH_FILE_NAME,
|
67
|
+
OUT_DECL_FILE,
|
68
|
+
OUT_IMPL_FILE
|
69
|
+
].each do |f|
|
70
|
+
assert FileTest.exists?( f ), "#{f} not exists!"
|
71
|
+
end
|
72
|
+
|
73
|
+
# ���������� ������ �� ������ ����������.
|
74
|
+
assert_equal CODEGEN_CONTENT_HASH,
|
75
|
+
File.read( CODEGEN_HASH_FILE_NAME )
|
76
|
+
assert_equal OUT_DECL_FILE_CONTENT, File.read( OUT_DECL_FILE )
|
77
|
+
assert_equal OUT_IMPL_FILE_CONTENT, File.read( OUT_IMPL_FILE )
|
78
|
+
end
|
79
|
+
|
80
|
+
def test_no_regenerate_at_all
|
81
|
+
# ������� hash-���� � ������ �������������� �����.
|
82
|
+
# � ���������� �������������� ������������� ����������
|
83
|
+
# �������������� ������ �� ������ ����������.
|
84
|
+
File.open( CODEGEN_HASH_FILE_NAME, "w" ) do |f|
|
85
|
+
f << CODEGEN_CONTENT_HASH
|
86
|
+
end
|
87
|
+
|
88
|
+
[ OUT_DECL_FILE, OUT_IMPL_FILE ].each do |file_name|
|
89
|
+
File.open( file_name, "w" ) do |f| end
|
90
|
+
end
|
91
|
+
|
92
|
+
@initiator.initiate
|
93
|
+
|
94
|
+
# ���������� ������ �� ������ ����������.
|
95
|
+
assert_equal CODEGEN_CONTENT_HASH,
|
96
|
+
File.read( CODEGEN_HASH_FILE_NAME )
|
97
|
+
assert_equal "", File.read( OUT_DECL_FILE )
|
98
|
+
assert_equal "", File.read( OUT_IMPL_FILE )
|
99
|
+
end
|
100
|
+
|
101
|
+
def test_force_regeneration
|
102
|
+
# ������� hash-���� � ������ �������������� �����.
|
103
|
+
# � ���������� �������������� ������������� ����������
|
104
|
+
# �������������� ������ ������ ����������.
|
105
|
+
File.open( CODEGEN_HASH_FILE_NAME, "w" ) do |f|
|
106
|
+
f << CODEGEN_CONTENT_HASH
|
107
|
+
end
|
108
|
+
|
109
|
+
[ OUT_DECL_FILE, OUT_IMPL_FILE ].each do |file_name|
|
110
|
+
File.open( file_name, "w" ) do |f| end
|
111
|
+
end
|
112
|
+
|
113
|
+
@initiator.initiate( true )
|
114
|
+
|
115
|
+
# ���������� ������ �� ������ ����������.
|
116
|
+
assert_equal CODEGEN_CONTENT_HASH,
|
117
|
+
File.read( CODEGEN_HASH_FILE_NAME )
|
118
|
+
assert_equal OUT_DECL_FILE_CONTENT, File.read( OUT_DECL_FILE )
|
119
|
+
assert_equal OUT_IMPL_FILE_CONTENT, File.read( OUT_IMPL_FILE )
|
120
|
+
end
|
121
|
+
|
122
|
+
def test_regenerate_partial
|
123
|
+
# ������� hash-���� � ������ ���� �� �������������� ������.
|
124
|
+
# � ���������� ������������� ������ ���� ������ �������������
|
125
|
+
# �������������� ����, � �������������� ���� ������ ��������
|
126
|
+
# ����������.
|
127
|
+
|
128
|
+
File.open( CODEGEN_HASH_FILE_NAME, "w" ) do |f|
|
129
|
+
f << CODEGEN_CONTENT_HASH
|
130
|
+
end
|
131
|
+
|
132
|
+
[ OUT_DECL_FILE ].each do |file_name|
|
133
|
+
File.open( file_name, "w" ) do |f| end
|
134
|
+
end
|
135
|
+
|
136
|
+
@initiator.initiate
|
137
|
+
|
138
|
+
# ���������� ������ �� ������ ����������.
|
139
|
+
assert_equal CODEGEN_CONTENT_HASH,
|
140
|
+
File.read( CODEGEN_HASH_FILE_NAME )
|
141
|
+
assert_equal "", File.read( OUT_DECL_FILE )
|
142
|
+
assert_equal OUT_IMPL_FILE_CONTENT, File.read( OUT_IMPL_FILE )
|
143
|
+
end
|
144
|
+
|
145
|
+
def test_clean
|
146
|
+
# ������� hash-���� � ������ �������������� �����.
|
147
|
+
# � ���������� �������������� �������� clean ����� ������ ���� �������.
|
148
|
+
File.open( CODEGEN_HASH_FILE_NAME, "w" ) do |f|
|
149
|
+
f << CODEGEN_CONTENT_HASH
|
150
|
+
end
|
151
|
+
|
152
|
+
[ OUT_DECL_FILE, OUT_IMPL_FILE ].each do |file_name|
|
153
|
+
File.open( file_name, "w" ) do |f| end
|
154
|
+
end
|
155
|
+
|
156
|
+
@initiator.clean
|
157
|
+
|
158
|
+
# ���������� ������ �� ������ ����������.
|
159
|
+
assert !File.exists?( CODEGEN_HASH_FILE_NAME )
|
160
|
+
assert !File.exists?( OUT_DECL_FILE )
|
161
|
+
assert !File.exists?( OUT_IMPL_FILE )
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
@@ -0,0 +1,54 @@
|
|
1
|
+
$:.unshift( File.dirname( __FILE__ ) + "/../lib" )
|
2
|
+
|
3
|
+
require 'test/unit'
|
4
|
+
|
5
|
+
require 'rucodegen/filename_producer'
|
6
|
+
|
7
|
+
class TC_FilenameProducer < Test::Unit::TestCase
|
8
|
+
PRODUCER = RuCodeGen::FilenameProducer
|
9
|
+
|
10
|
+
def test_no_name_given
|
11
|
+
assert_raise( RuntimeError ) do
|
12
|
+
PRODUCER.produce( "dummy", {} )
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_too_much_names_given
|
17
|
+
assert_raise( RuntimeError ) do
|
18
|
+
PRODUCER.produce( "dummy",
|
19
|
+
{ :absolute => "/usr/local/include/a",
|
20
|
+
:script_relative => "/include/a" } )
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_invalid_key_given
|
25
|
+
assert_raise( RuntimeError ) do
|
26
|
+
PRODUCER.produce( "dummy",
|
27
|
+
{ :abs => "/usr/local/include/a" } )
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_absolute_path
|
32
|
+
path = "/usr/local/include/a"
|
33
|
+
|
34
|
+
assert_equal( path,
|
35
|
+
PRODUCER.produce( "dummy", :absolute => path ) )
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_script_relative
|
39
|
+
path = "/home/eao197/prj/cg"
|
40
|
+
|
41
|
+
assert_equal( path + "/include/a",
|
42
|
+
PRODUCER.produce( path + "/generator.rb",
|
43
|
+
:script_relative => "include/a" ) )
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_cwd_relative
|
47
|
+
path = Dir.pwd
|
48
|
+
|
49
|
+
assert_equal( path + "/include/a",
|
50
|
+
PRODUCER.produce( path + "/cg/generator.rb",
|
51
|
+
:cwd_relative => "include/a" ) )
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|