rgen 0.4.2 → 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (33) hide show
  1. data/CHANGELOG +7 -0
  2. data/lib/rgen/metamodel_builder.rb +5 -4
  3. data/lib/rgen/metamodel_builder.rb.bak +196 -0
  4. data/lib/rgen/metamodel_builder/builder_extensions.rb +51 -38
  5. data/lib/rgen/metamodel_builder/builder_extensions.rb.bak +437 -0
  6. data/lib/rgen/metamodel_builder/builder_runtime.rb +2 -20
  7. data/lib/rgen/metamodel_builder/builder_runtime.rb.bak +73 -0
  8. data/lib/rgen/name_helper.rb.bak +37 -0
  9. data/lib/rgen/template_language.rb +8 -0
  10. data/lib/rgen/template_language.rb.bak +289 -0
  11. data/lib/rgen/template_language/directory_template_container.rb +11 -0
  12. data/lib/rgen/template_language/directory_template_container.rb.bak +69 -0
  13. data/lib/rgen/template_language/output_handler.rb +3 -2
  14. data/lib/rgen/template_language/output_handler.rb.bak +88 -0
  15. data/lib/rgen/template_language/template_container.rb +5 -4
  16. data/lib/rgen/template_language/template_container.rb.bak +196 -0
  17. data/lib/rgen/transformer.rb.bak +381 -0
  18. data/test/environment_test.rb.bak +52 -0
  19. data/test/metamodel_builder_test.rb +6 -0
  20. data/test/metamodel_builder_test.rb.bak +443 -0
  21. data/test/metamodel_roundtrip_test/TestModel_Regenerated.rb +34 -32
  22. data/test/metamodel_roundtrip_test/houseMetamodel_Regenerated.ecore +58 -58
  23. data/test/output_handler_test.rb +8 -0
  24. data/test/output_handler_test.rb.bak +50 -0
  25. data/test/template_language_test.rb +23 -0
  26. data/test/template_language_test.rb.bak +72 -0
  27. data/test/template_language_test/indentStringTestDefaultIndent.out +1 -0
  28. data/test/template_language_test/indentStringTestTabIndent.out +1 -0
  29. data/test/template_language_test/templates/indent_string_test.tpl +12 -0
  30. data/test/template_language_test/templates/null_context_test.tpl +12 -0
  31. data/test/transformer_test.rb.bak +223 -0
  32. metadata +65 -48
  33. data/lib/rgen/environment.rb.bak +0 -42
@@ -0,0 +1,52 @@
1
+ $:.unshift File.join(File.dirname(__FILE__),"..","lib")
2
+
3
+ require 'test/unit'
4
+ require 'rgen/environment'
5
+
6
+ class EnvironmentTest < Test::Unit::TestCase
7
+
8
+ class Model
9
+ attr_accessor :name
10
+ end
11
+
12
+ class ModelSub < Model
13
+ end
14
+
15
+ def test_find
16
+ m1 = Model.new
17
+ m1.name = "M1"
18
+ m2 = ModelSub.new
19
+ m2.name = "M2"
20
+ m3 = "justAString"
21
+ env = RGen::Environment.new << m1 << m2 << m3
22
+ assertFind(env, m1, m2, m3)
23
+ idx = env.findIndex(:name)
24
+ assertFind(idx, m1, m2, m3)
25
+ end
26
+
27
+ def assertFind(context, m1, m2, m3)
28
+ result = context.find(:class => Model, :name => "M1")
29
+ assert result.is_a?(Array)
30
+ assert_equal 1, result.size
31
+ assert_equal m1, result.first
32
+
33
+ result = context.find(:class => Model)
34
+ assert result.is_a?(Array)
35
+ assert_equal 2, result.size
36
+ assert_equal m1, result[0]
37
+ assert_equal m2, result[1]
38
+
39
+ result = context.find(:name => "M2")
40
+ assert result.is_a?(Array)
41
+ assert_equal 1, result.size
42
+ assert_equal m2, result[0]
43
+
44
+ result = context.find(:class => [Model, String])
45
+ assert result.is_a?(Array)
46
+ assert_equal 3, result.size
47
+ assert_equal m1, result[0]
48
+ assert_equal m2, result[1]
49
+ assert_equal m3, result[2]
50
+ end
51
+
52
+ end
@@ -74,6 +74,12 @@ class MetamodelBuilderTest < Test::Unit::TestCase
74
74
  end
75
75
 
76
76
  class ClassA < RGen::MetamodelBuilder::MMBase
77
+ # metamodel accessors must work independent of the ==() method
78
+ module ClassModule
79
+ def ==(o)
80
+ o.is_a?(ClassA)
81
+ end
82
+ end
77
83
  end
78
84
 
79
85
  class ClassB < RGen::MetamodelBuilder::MMBase
@@ -0,0 +1,443 @@
1
+ $:.unshift File.join(File.dirname(__FILE__),"..","lib")
2
+
3
+ require 'test/unit'
4
+ require 'rgen/metamodel_builder'
5
+ require 'rgen/array_extensions'
6
+
7
+ class MetamodelBuilderTest < Test::Unit::TestCase
8
+
9
+ class SimpleClass < RGen::MetamodelBuilder::MMBase
10
+ KindType = RGen::MetamodelBuilder::DataTypes::Enum.new([:simple, :extended])
11
+ has_attr 'name' # default is String
12
+ has_attr 'stringWithDefault', String, :defaultValueLiteral => "xtest"
13
+ has_attr 'anything', Object
14
+ has_attr 'allowed', RGen::MetamodelBuilder::DataTypes::Boolean
15
+ has_attr 'kind', KindType
16
+ has_attr 'kindWithDefault', KindType, :defaultValueLiteral => "extended"
17
+ end
18
+
19
+ def test_has_attr
20
+ sc = SimpleClass.new
21
+
22
+ assert_respond_to sc, :name
23
+ assert_respond_to sc, :name=
24
+ sc.name = "TestName"
25
+ assert_equal "TestName", sc.name
26
+ sc.name = nil
27
+ assert_equal nil, sc.name
28
+ assert_raise StandardError do
29
+ sc.name = 5
30
+ end
31
+ assert_equal "EString", SimpleClass.ecore.eAttributes.find{|a| a.name=="name"}.eType.name
32
+
33
+ assert_equal "xtest", sc.stringWithDefault
34
+ assert_equal :extended, sc.kindWithDefault
35
+
36
+ sc.anything = :asymbol
37
+ assert_equal :asymbol, sc.anything
38
+ sc.anything = self # a class
39
+ assert_equal self, sc.anything
40
+
41
+ assert_respond_to sc, :allowed
42
+ assert_respond_to sc, :allowed=
43
+ sc.allowed = true
44
+ assert_equal true, sc.allowed
45
+ sc.allowed = false
46
+ assert_equal false, sc.allowed
47
+ sc.allowed = nil
48
+ assert_equal nil, sc.allowed
49
+ assert_raise StandardError do
50
+ sc.allowed = :someSymbol
51
+ end
52
+ assert_raise StandardError do
53
+ sc.allowed = "a string"
54
+ end
55
+ assert_equal "EBoolean", SimpleClass.ecore.eAttributes.find{|a| a.name=="allowed"}.eType.name
56
+
57
+ assert_respond_to sc, :kind
58
+ assert_respond_to sc, :kind=
59
+ sc.kind = :simple
60
+ assert_equal :simple, sc.kind
61
+ sc.kind = :extended
62
+ assert_equal :extended, sc.kind
63
+ sc.kind = nil
64
+ assert_equal nil, sc.kind
65
+ assert_raise StandardError do
66
+ sc.kind = :false
67
+ end
68
+ assert_raise StandardError do
69
+ sc.kind = "a string"
70
+ end
71
+
72
+ enum = SimpleClass.ecore.eAttributes.find{|a| a.name=="kind"}.eType
73
+ assert_equal ["extended", "simple"], enum.eLiterals.name.sort
74
+ end
75
+
76
+ class ClassA < RGen::MetamodelBuilder::MMBase
77
+ end
78
+
79
+ class ClassB < RGen::MetamodelBuilder::MMBase
80
+ end
81
+
82
+ class ClassC < RGen::MetamodelBuilder::MMBase
83
+ end
84
+
85
+ class HasOneTestClass < RGen::MetamodelBuilder::MMBase
86
+ has_one 'classA', ClassA
87
+ has_one 'classB', ClassB
88
+ end
89
+
90
+ def test_has_one
91
+ sc = HasOneTestClass.new
92
+ assert_respond_to sc, :classA
93
+ assert_respond_to sc, :classA=
94
+ ca = ClassA.new
95
+ sc.classA = ca
96
+ assert_equal ca, sc.classA
97
+ sc.classA = nil
98
+ assert_equal nil, sc.classA
99
+
100
+ assert_respond_to sc, :classB
101
+ assert_respond_to sc, :classB=
102
+ cb = ClassB.new
103
+ sc.classB = cb
104
+ assert_equal cb, sc.classB
105
+
106
+ assert_raise StandardError do
107
+ sc.classB = ca
108
+ end
109
+
110
+ assert_equal [], ClassA.ecore.eReferences
111
+ assert_equal [], ClassB.ecore.eReferences
112
+ assert_equal ["classA", "classB"].sort, HasOneTestClass.ecore.eReferences.name.sort
113
+ assert_equal [], HasOneTestClass.ecore.eReferences.select { |a| a.many == true }
114
+ assert_equal [], HasOneTestClass.ecore.eAttributes
115
+ end
116
+
117
+ class HasManyTestClass < RGen::MetamodelBuilder::MMBase
118
+ has_many 'classA', ClassA
119
+ end
120
+
121
+ def test_has_many
122
+ o = HasManyTestClass.new
123
+ ca1 = ClassA.new
124
+ ca2 = ClassA.new
125
+ ca3 = ClassA.new
126
+ o.addClassA(ca1)
127
+ o.addClassA(ca2)
128
+ assert_equal [ca1, ca2], o.classA
129
+ # make sure we get a copy
130
+ o.classA.clear
131
+ assert_equal [ca1, ca2], o.classA
132
+ o.removeClassA(ca3)
133
+ assert_equal [ca1, ca2], o.classA
134
+ o.removeClassA(ca2)
135
+ assert_equal [ca1], o.classA
136
+ assert_raise StandardError do
137
+ o.addClassA(ClassB.new)
138
+ end
139
+ assert_equal [], HasManyTestClass.ecore.eReferences.select{|r| r.many == false}
140
+ assert_equal ["classA"], HasManyTestClass.ecore.eReferences.select{|r| r.many == true}.name
141
+ end
142
+
143
+ class OneClass < RGen::MetamodelBuilder::MMBase
144
+ end
145
+ class ManyClass < RGen::MetamodelBuilder::MMBase
146
+ end
147
+ OneClass.one_to_many 'manyClasses', ManyClass, 'oneClass', :upperBound => 5
148
+
149
+ def test_one_to_many
150
+ oc = OneClass.new
151
+ assert_respond_to oc, :manyClasses
152
+ assert oc.manyClasses.empty?
153
+
154
+ mc = ManyClass.new
155
+ assert_respond_to mc, :oneClass
156
+ assert_respond_to mc, :oneClass=
157
+ assert_nil mc.oneClass
158
+
159
+ # put the OneClass into the ManyClass
160
+ mc.oneClass = oc
161
+ assert_equal oc, mc.oneClass
162
+ assert oc.manyClasses.include?(mc)
163
+
164
+ # remove the OneClass from the ManyClass
165
+ mc.oneClass = nil
166
+ assert_equal nil, mc.oneClass
167
+ assert !oc.manyClasses.include?(mc)
168
+
169
+ # put the ManyClass into the OneClass
170
+ oc.addManyClasses mc
171
+ assert oc.manyClasses.include?(mc)
172
+ assert_equal oc, mc.oneClass
173
+
174
+ # remove the ManyClass from the OneClass
175
+ oc.removeManyClasses mc
176
+ assert !oc.manyClasses.include?(mc)
177
+ assert_equal nil, mc.oneClass
178
+
179
+ assert_equal [], OneClass.ecore.eReferences.select{|r| r.many == false}
180
+ assert_equal ["manyClasses"], OneClass.ecore.eReferences.select{|r| r.many == true}.name
181
+ assert_equal 5, OneClass.ecore.eReferences.find{|r| r.many == true}.upperBound
182
+ assert_equal ["oneClass"], ManyClass.ecore.eReferences.select{|r| r.many == false}.name
183
+ assert_equal [], ManyClass.ecore.eReferences.select{|r| r.many == true}
184
+ end
185
+
186
+ class OneClass2 < RGen::MetamodelBuilder::MMBase
187
+ end
188
+ class ManyClass2 < RGen::MetamodelBuilder::MMBase
189
+ end
190
+ ManyClass2.many_to_one 'oneClass', OneClass2, 'manyClasses'
191
+
192
+ def test_one_to_many2
193
+ oc = OneClass2.new
194
+ assert_respond_to oc, :manyClasses
195
+ assert oc.manyClasses.empty?
196
+
197
+ mc = ManyClass2.new
198
+ assert_respond_to mc, :oneClass
199
+ assert_respond_to mc, :oneClass=
200
+ assert_nil mc.oneClass
201
+
202
+ # put the OneClass into the ManyClass
203
+ mc.oneClass = oc
204
+ assert_equal oc, mc.oneClass
205
+ assert oc.manyClasses.include?(mc)
206
+
207
+ # remove the OneClass from the ManyClass
208
+ mc.oneClass = nil
209
+ assert_equal nil, mc.oneClass
210
+ assert !oc.manyClasses.include?(mc)
211
+
212
+ # put the ManyClass into the OneClass
213
+ oc.addManyClasses mc
214
+ assert oc.manyClasses.include?(mc)
215
+ assert_equal oc, mc.oneClass
216
+
217
+ # remove the ManyClass from the OneClass
218
+ oc.removeManyClasses mc
219
+ assert !oc.manyClasses.include?(mc)
220
+ assert_equal nil, mc.oneClass
221
+
222
+ assert_equal [], OneClass2.ecore.eReferences.select{|r| r.many == false}
223
+ assert_equal ["manyClasses"], OneClass2.ecore.eReferences.select{|r| r.many == true}.name
224
+ assert_equal ["oneClass"], ManyClass2.ecore.eReferences.select{|r| r.many == false}.name
225
+ assert_equal [], ManyClass2.ecore.eReferences.select{|r| r.many == true}
226
+ end
227
+
228
+ class AClassOO < RGen::MetamodelBuilder::MMBase
229
+ end
230
+ class BClassOO < RGen::MetamodelBuilder::MMBase
231
+ end
232
+ AClassOO.one_to_one 'bClass', BClassOO, 'aClass'
233
+
234
+ def test_one_to_one
235
+ ac = AClassOO.new
236
+ assert_respond_to ac, :bClass
237
+ assert_respond_to ac, :bClass=
238
+ assert_nil ac.bClass
239
+
240
+ bc = BClassOO.new
241
+ assert_respond_to bc, :aClass
242
+ assert_respond_to bc, :aClass=
243
+ assert_nil bc.aClass
244
+
245
+ # put the AClass into the BClass
246
+ bc.aClass = ac
247
+ assert_equal ac, bc.aClass
248
+ assert_equal bc, ac.bClass
249
+
250
+ # remove the AClass from the BClass
251
+ bc.aClass = nil
252
+ assert_equal nil, bc.aClass
253
+ assert_equal nil, ac.bClass
254
+
255
+ # put the BClass into the AClass
256
+ ac.bClass = bc
257
+ assert_equal bc, ac.bClass
258
+ assert_equal ac, bc.aClass
259
+
260
+ # remove the BClass from the AClass
261
+ ac.bClass = nil
262
+ assert_equal nil, ac.bClass
263
+ assert_equal nil, bc.aClass
264
+
265
+ assert_equal ["bClass"], AClassOO.ecore.eReferences.select{|r| r.many == false}.name
266
+ assert_equal [], AClassOO.ecore.eReferences.select{|r| r.many == true}
267
+ assert_equal ["aClass"], BClassOO.ecore.eReferences.select{|r| r.many == false}.name
268
+ assert_equal [], BClassOO.ecore.eReferences.select{|r| r.many == true}
269
+ end
270
+
271
+ class AClassMM < RGen::MetamodelBuilder::MMBase
272
+ end
273
+ class BClassMM < RGen::MetamodelBuilder::MMBase
274
+ end
275
+ AClassMM.many_to_many 'bClasses', BClassMM, 'aClasses'
276
+
277
+ def test_many_to_many
278
+
279
+ ac = AClassMM.new
280
+ assert_respond_to ac, :bClasses
281
+ assert ac.bClasses.empty?
282
+
283
+ bc = BClassMM.new
284
+ assert_respond_to bc, :aClasses
285
+ assert bc.aClasses.empty?
286
+
287
+ # put the AClass into the BClass
288
+ bc.addAClasses ac
289
+ assert bc.aClasses.include?(ac)
290
+ assert ac.bClasses.include?(bc)
291
+
292
+ # put something else into the BClass
293
+ assert_raise StandardError do
294
+ bc.addAClasses :notaaclass
295
+ end
296
+
297
+ # remove the AClass from the BClass
298
+ bc.removeAClasses ac
299
+ assert !bc.aClasses.include?(ac)
300
+ assert !ac.bClasses.include?(bc)
301
+
302
+ # put the BClass into the AClass
303
+ ac.addBClasses bc
304
+ assert ac.bClasses.include?(bc)
305
+ assert bc.aClasses.include?(ac)
306
+
307
+ # put something else into the AClass
308
+ assert_raise StandardError do
309
+ ac.addBClasses :notabclass
310
+ end
311
+
312
+ # remove the BClass from the AClass
313
+ ac.removeBClasses bc
314
+ assert !ac.bClasses.include?(bc)
315
+ assert !bc.aClasses.include?(ac)
316
+
317
+ assert_equal [], AClassMM.ecore.eReferences.select{|r| r.many == false}
318
+ assert_equal ["bClasses"], AClassMM.ecore.eReferences.select{|r| r.many == true}.name
319
+ assert_equal [], BClassMM.ecore.eReferences.select{|r| r.many == false}
320
+ assert_equal ["aClasses"], BClassMM.ecore.eReferences.select{|r| r.many == true}.name
321
+ end
322
+
323
+ class SomeSuperClass < RGen::MetamodelBuilder::MMBase
324
+ has_attr "name"
325
+ has_many "classAs", ClassA
326
+ end
327
+
328
+ class SomeSubClass < SomeSuperClass
329
+ has_attr "subname"
330
+ has_many "classBs", ClassB
331
+ end
332
+
333
+ class OtherSubClass < SomeSuperClass
334
+ has_attr "othersubname"
335
+ has_many "classCs", ClassC
336
+ end
337
+
338
+ def test_inheritance
339
+ assert_equal ["name"], SomeSuperClass.ecore.eAllAttributes.name
340
+ assert_equal ["classAs"], SomeSuperClass.ecore.eAllReferences.name
341
+ assert_equal ["name", "subname"], SomeSubClass.ecore.eAllAttributes.name.sort
342
+ assert_equal ["classAs", "classBs"], SomeSubClass.ecore.eAllReferences.name.sort
343
+ assert_equal ["name", "othersubname"], OtherSubClass.ecore.eAllAttributes.name.sort
344
+ assert_equal ["classAs", "classCs"], OtherSubClass.ecore.eAllReferences.name.sort
345
+ end
346
+
347
+ module AnnotatedModule
348
+ extend RGen::MetamodelBuilder::ModuleExtension
349
+
350
+ annotation "moduletag" => "modulevalue"
351
+
352
+ class AnnotatedClass < RGen::MetamodelBuilder::MMBase
353
+ annotation "sometag" => "somevalue", "othertag" => "othervalue"
354
+ annotation :source => "rgen/test", :details => {"thirdtag" => "thirdvalue"}
355
+
356
+ has_attr "boolAttr", Boolean do
357
+ annotation "attrtag" => "attrval"
358
+ annotation :source => "rgen/test2", :details => {"attrtag2" => "attrvalue2", "attrtag3" => "attrvalue3"}
359
+ end
360
+
361
+ has_many "others", AnnotatedClass do
362
+ annotation "reftag" => "refval"
363
+ annotation :source => "rgen/test3", :details => {"reftag2" => "refvalue2", "reftag3" => "refvalue3"}
364
+ end
365
+
366
+ many_to_many "m2m", AnnotatedClass, "m2mback" do
367
+ annotation "m2mtag" => "m2mval"
368
+ opposite_annotation "opposite_m2mtag" => "opposite_m2mval"
369
+ end
370
+ end
371
+
372
+ end
373
+
374
+ def test_annotations
375
+ assert_equal 1, AnnotatedModule.ecore.eAnnotations.size
376
+ anno = AnnotatedModule.ecore.eAnnotations.first
377
+ checkAnnotation(anno, nil, {"moduletag" => "modulevalue"})
378
+
379
+ eClass = AnnotatedModule::AnnotatedClass.ecore
380
+ assert_equal 2, eClass.eAnnotations.size
381
+ anno = eClass.eAnnotations.find{|a| a.source == "rgen/test"}
382
+ checkAnnotation(anno, "rgen/test", {"thirdtag" => "thirdvalue"})
383
+ anno = eClass.eAnnotations.find{|a| a.source == nil}
384
+ checkAnnotation(anno, nil, {"sometag" => "somevalue", "othertag" => "othervalue"})
385
+
386
+ eAttr = eClass.eAttributes.first
387
+ assert_equal 2, eAttr.eAnnotations.size
388
+ anno = eAttr.eAnnotations.find{|a| a.source == "rgen/test2"}
389
+ checkAnnotation(anno, "rgen/test2", {"attrtag2" => "attrvalue2", "attrtag3" => "attrvalue3"})
390
+ anno = eAttr.eAnnotations.find{|a| a.source == nil}
391
+ checkAnnotation(anno, nil, {"attrtag" => "attrval"})
392
+
393
+ eRef = eClass.eReferences.find{|r| !r.eOpposite}
394
+ assert_equal 2, eRef.eAnnotations.size
395
+ anno = eRef.eAnnotations.find{|a| a.source == "rgen/test3"}
396
+ checkAnnotation(anno, "rgen/test3", {"reftag2" => "refvalue2", "reftag3" => "refvalue3"})
397
+ anno = eRef.eAnnotations.find{|a| a.source == nil}
398
+ checkAnnotation(anno, nil, {"reftag" => "refval"})
399
+
400
+ eRef = eClass.eReferences.find{|r| r.eOpposite}
401
+ assert_equal 1, eRef.eAnnotations.size
402
+ anno = eRef.eAnnotations.first
403
+ checkAnnotation(anno, nil, {"m2mtag" => "m2mval"})
404
+ eRef = eRef.eOpposite
405
+ assert_equal 1, eRef.eAnnotations.size
406
+ anno = eRef.eAnnotations.first
407
+ checkAnnotation(anno, nil, {"opposite_m2mtag" => "opposite_m2mval"})
408
+ end
409
+
410
+ def checkAnnotation(anno, source, hash)
411
+ assert anno.is_a?(RGen::ECore::EAnnotation)
412
+ assert_equal source, anno.source
413
+ assert_equal hash.size, anno.details.size
414
+ hash.each_pair do |k, v|
415
+ detail = anno.details.find{|d| d.key == k}
416
+ assert detail.is_a?(RGen::ECore::EStringToStringMapEntry)
417
+ assert_equal v, detail.value
418
+ end
419
+ end
420
+
421
+ module SomePackage
422
+ extend RGen::MetamodelBuilder::ModuleExtension
423
+
424
+ class ClassA < RGen::MetamodelBuilder::MMBase
425
+ end
426
+
427
+ module SubPackage
428
+ extend RGen::MetamodelBuilder::ModuleExtension
429
+
430
+ class ClassB < RGen::MetamodelBuilder::MMBase
431
+ end
432
+ end
433
+ end
434
+
435
+ def test_ecore_identity
436
+ subPackage = SomePackage::SubPackage.ecore
437
+ assert_equal subPackage.eClassifiers.first.object_id, SomePackage::SubPackage::ClassB.ecore.object_id
438
+
439
+ somePackage = SomePackage.ecore
440
+ assert_equal somePackage.eSubpackages.first.object_id, subPackage.object_id
441
+ end
442
+
443
+ end