rtext 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+
3
+ require 'serializer_test'
4
+ require 'instantiator_test'
5
+
@@ -0,0 +1,418 @@
1
+ $:.unshift File.join(File.dirname(__FILE__),"..","lib")
2
+
3
+ require 'test/unit'
4
+ require 'rgen/environment'
5
+ require 'rgen/metamodel_builder'
6
+ require 'rtext/serializer'
7
+ require 'rtext/language'
8
+
9
+ class RTextSerializerTest < Test::Unit::TestCase
10
+
11
+ class StringWriter < String
12
+ alias write concat
13
+ end
14
+
15
+ module TestMM
16
+ extend RGen::MetamodelBuilder::ModuleExtension
17
+ SomeEnum = RGen::MetamodelBuilder::DataTypes::Enum.new(
18
+ :name => "SomeEnum", :literals => [:A, :B, :'non-word*chars'])
19
+ class TestNode < RGen::MetamodelBuilder::MMBase
20
+ has_attr 'text', String
21
+ has_attr 'integer', Integer
22
+ has_attr 'float', Float
23
+ has_attr 'enum', SomeEnum
24
+ contains_many 'childs', TestNode, 'parent'
25
+ end
26
+ end
27
+
28
+ def test_simple
29
+ testModel = TestMM::TestNode.new(:text => "some text", :childs => [
30
+ TestMM::TestNode.new(:text => "child")])
31
+
32
+ output = StringWriter.new
33
+ serialize(testModel, TestMM, output)
34
+
35
+ assert_equal %Q(\
36
+ TestNode text: "some text" {
37
+ TestNode text: "child"
38
+ }
39
+ ), output
40
+ end
41
+
42
+ module TestMMFeatureProvider
43
+ extend RGen::MetamodelBuilder::ModuleExtension
44
+ class TestNode < RGen::MetamodelBuilder::MMBase
45
+ has_attr 'attr1', String
46
+ has_attr 'attr2', String
47
+ has_attr 'attr3', String
48
+ contains_many 'childs1', TestNode, 'parent1'
49
+ contains_many 'childs2', TestNode, 'parent2'
50
+ contains_many 'childs3', TestNode, 'parent3'
51
+ end
52
+ end
53
+
54
+ def test_feature_provider
55
+ testModel = TestMMFeatureProvider::TestNode.new(
56
+ :attr1 => "attr1",
57
+ :attr2 => "attr2",
58
+ :attr3 => "attr3",
59
+ :childs1 => [TestMMFeatureProvider::TestNode.new(:attr1 => "child1")],
60
+ :childs2 => [TestMMFeatureProvider::TestNode.new(:attr1 => "child2")],
61
+ :childs3 => [TestMMFeatureProvider::TestNode.new(:attr1 => "child3")])
62
+
63
+ output = StringWriter.new
64
+ serialize(testModel, TestMMFeatureProvider, output,
65
+ :feature_provider => proc {|clazz|
66
+ clazz.eAllStructuralFeatures.reject{|f| f.name =~ /parent|2$/}.reverse})
67
+
68
+ assert_equal %Q(\
69
+ TestNode attr3: "attr3", attr1: "attr1" {
70
+ childs3:
71
+ TestNode attr1: "child3"
72
+ childs1:
73
+ TestNode attr1: "child1"
74
+ }
75
+ ), output
76
+ end
77
+
78
+ module TestMMUnlabledUnquoted
79
+ extend RGen::MetamodelBuilder::ModuleExtension
80
+ class TestNode < RGen::MetamodelBuilder::MMBase
81
+ has_attr 'unlabled', String
82
+ has_attr 'unquoted', String
83
+ has_attr 'both', String
84
+ has_attr 'none', String
85
+ end
86
+ end
87
+
88
+ def test_unlabled_unquoted
89
+ testModel = [
90
+ TestMMUnlabledUnquoted::TestNode.new(:unlabled => "unlabled", :unquoted => "unquoted", :both => "both", :none => "none"),
91
+ TestMMUnlabledUnquoted::TestNode.new(:unquoted => "no identifier"),
92
+ TestMMUnlabledUnquoted::TestNode.new(:unquoted => "true"),
93
+ TestMMUnlabledUnquoted::TestNode.new(:unquoted => "333"),
94
+ TestMMUnlabledUnquoted::TestNode.new(:unquoted => "33.3"),
95
+ TestMMUnlabledUnquoted::TestNode.new(:unquoted => "5x"),
96
+ TestMMUnlabledUnquoted::TestNode.new(:unquoted => "//")
97
+ ]
98
+
99
+ output = StringWriter.new
100
+ serialize(testModel, TestMMUnlabledUnquoted, output,
101
+ :unlabled_arguments => proc {|clazz| ["unlabled", "both"]},
102
+ :unquoted_arguments => proc {|clazz| ["unquoted", "both"]}
103
+ )
104
+
105
+ assert_equal %Q(\
106
+ TestNode "unlabled", both, unquoted: unquoted, none: "none"
107
+ TestNode unquoted: "no identifier"
108
+ TestNode unquoted: "true"
109
+ TestNode unquoted: "333"
110
+ TestNode unquoted: "33.3"
111
+ TestNode unquoted: "5x"
112
+ TestNode unquoted: "//"
113
+ ), output
114
+ end
115
+
116
+ module TestMMComment
117
+ extend RGen::MetamodelBuilder::ModuleExtension
118
+ class TestNode < RGen::MetamodelBuilder::MMBase
119
+ has_attr 'comment', String
120
+ contains_many 'childs', TestNode, 'parent'
121
+ end
122
+ end
123
+
124
+ def test_comment_provider
125
+ testModel = TestMMComment::TestNode.new(
126
+ :comment => "this is a comment",
127
+ :childs => [
128
+ TestMMComment::TestNode.new(:comment => "comment of a child node\n multiline"),
129
+ TestMMComment::TestNode.new(:comment => "don't show")])
130
+
131
+ output = StringWriter.new
132
+ serialize(testModel, TestMMComment, output,
133
+ :comment_provider => proc { |e|
134
+ if e.comment != "don't show"
135
+ c = e.comment
136
+ e.comment = nil
137
+ c
138
+ else
139
+ nil
140
+ end
141
+ })
142
+
143
+ assert_equal %Q(\
144
+ #this is a comment
145
+ TestNode {
146
+ #comment of a child node
147
+ # multiline
148
+ TestNode
149
+ TestNode comment: "don't show"
150
+ }
151
+ ), output
152
+ end
153
+
154
+ def test_indent_string
155
+ testModel = TestMM::TestNode.new(:childs => [
156
+ TestMM::TestNode.new(:text => "child")])
157
+
158
+ output = StringWriter.new
159
+ serialize(testModel, TestMM, output, :indent_string => "____")
160
+
161
+ assert_equal %Q(\
162
+ TestNode {
163
+ ____TestNode text: "child"
164
+ }
165
+ ), output
166
+ end
167
+
168
+ module TestMMRef
169
+ extend RGen::MetamodelBuilder::ModuleExtension
170
+ class TestNode < RGen::MetamodelBuilder::MMBase
171
+ has_attr 'name', String
172
+ contains_many 'childs', TestNode, 'parent'
173
+ has_many 'refMany', TestNode
174
+ has_one 'refOne', TestNode
175
+ one_to_many 'refManyBi', TestNode, 'refManyBack'
176
+ one_to_one 'refOneBi', TestNode, 'refOneBack'
177
+ many_to_many 'refManyMany', TestNode, 'refManyManyBack'
178
+ end
179
+ end
180
+
181
+ def test_identifier_provider
182
+ testModel = [
183
+ TestMMRef::TestNode.new(:name => "Source"),
184
+ TestMMRef::TestNode.new(:name => "Target")]
185
+ testModel[0].refOne = testModel[1]
186
+
187
+ output = StringWriter.new
188
+ serialize(testModel, TestMMRef, output,
189
+ :identifier_provider => proc{|e, context|
190
+ assert_equal testModel[0], context
191
+ "/target/ref"
192
+ }
193
+ )
194
+
195
+ assert_equal %Q(\
196
+ TestNode name: "Source", refOne: /target/ref
197
+ TestNode name: "Target"
198
+ ),output
199
+ end
200
+
201
+ def test_references
202
+ testModel = [
203
+ TestMMRef::TestNode.new(:name => "Source"),
204
+ TestMMRef::TestNode.new(:name => "Target",
205
+ :childs => [
206
+ TestMMRef::TestNode.new(:name => "A",
207
+ :childs => [
208
+ TestMMRef::TestNode.new(:name => "A1")
209
+ ]),
210
+ TestMMRef::TestNode.new(:name => "B"),
211
+ ])
212
+ ]
213
+ testModel[0].refOne = testModel[1].childs[0].childs[0]
214
+ testModel[0].refOneBi = testModel[1].childs[0].childs[0]
215
+ testModel[0].refMany = [testModel[1].childs[0], testModel[1].childs[1]]
216
+ testModel[0].refManyBi = [testModel[1].childs[0], testModel[1].childs[1]]
217
+ testModel[0].refManyMany = [testModel[1].childs[0], testModel[1].childs[1]]
218
+ testModel[0].addRefMany(RGen::MetamodelBuilder::MMProxy.new("/some/ref"))
219
+
220
+ output = StringWriter.new
221
+ serialize(testModel, TestMMRef, output)
222
+
223
+ assert_equal %Q(\
224
+ TestNode name: "Source", refMany: [/Target/A, /Target/B, /some/ref], refOne: /Target/A/A1, refOneBi: /Target/A/A1
225
+ TestNode name: "Target" {
226
+ TestNode name: "A", refManyBack: /Source, refManyManyBack: /Source {
227
+ TestNode name: "A1"
228
+ }
229
+ TestNode name: "B", refManyBack: /Source, refManyManyBack: /Source
230
+ }
231
+ ), output
232
+ end
233
+
234
+ module TestMMChildRole
235
+ extend RGen::MetamodelBuilder::ModuleExtension
236
+ class TestNodeA < RGen::MetamodelBuilder::MMBase
237
+ has_attr 'text', String
238
+ end
239
+ class TestNodeB < RGen::MetamodelBuilder::MMBase
240
+ has_attr 'text', String
241
+ end
242
+ class TestNodeC < RGen::MetamodelBuilder::MMBase
243
+ has_attr 'text', String
244
+ end
245
+ class TestNodeD < RGen::MetamodelBuilder::MMBase
246
+ has_attr 'text', String
247
+ end
248
+ class TestNodeE < RGen::MetamodelBuilder::MMMultiple(TestNodeC, TestNodeD)
249
+ has_attr 'text2', String
250
+ end
251
+ class TestNode < RGen::MetamodelBuilder::MMBase
252
+ has_attr 'text', String
253
+ contains_one 'child1', TestNode, 'parent1'
254
+ contains_many 'childs2', TestNode, 'parent2'
255
+ contains_one 'child3', TestNodeA, 'parent3'
256
+ contains_many 'childs4', TestNodeB, 'parent4'
257
+ contains_one 'child5', TestNodeC, 'parent5'
258
+ contains_many 'childs6', TestNodeD, 'parent6'
259
+ contains_one 'child7', TestNodeE, 'parent7'
260
+ end
261
+ end
262
+
263
+ def test_child_role
264
+ testModel = TestMMChildRole::TestNode.new(
265
+ :child1 => TestMMChildRole::TestNode.new(:text => "child1"),
266
+ :childs2 => [
267
+ TestMMChildRole::TestNode.new(:text => "child2a"),
268
+ TestMMChildRole::TestNode.new(:text => "child2b")
269
+ ],
270
+ :child3 => TestMMChildRole::TestNodeA.new(:text => "child3"),
271
+ :childs4 => [TestMMChildRole::TestNodeB.new(:text => "child4")],
272
+ :child5 => TestMMChildRole::TestNodeC.new(:text => "child5"),
273
+ :childs6 => [TestMMChildRole::TestNodeD.new(:text => "child6")],
274
+ :child7 => TestMMChildRole::TestNodeE.new(:text2 => "child7")
275
+ )
276
+
277
+ output = StringWriter.new
278
+ serialize(testModel, TestMMChildRole, output)
279
+
280
+ assert_equal %Q(\
281
+ TestNode {
282
+ child1:
283
+ TestNode text: "child1"
284
+ childs2: [
285
+ TestNode text: "child2a"
286
+ TestNode text: "child2b"
287
+ ]
288
+ TestNodeA text: "child3"
289
+ TestNodeB text: "child4"
290
+ TestNodeC text: "child5"
291
+ TestNodeD text: "child6"
292
+ child7:
293
+ TestNodeE text2: "child7"
294
+ }
295
+ ), output
296
+ end
297
+
298
+ def test_escapes
299
+ testModel = TestMM::TestNode.new(:text => %Q(some " \\ \\" text \r xx \n xx \r\n xx \t xx \b xx \f))
300
+ output = StringWriter.new
301
+ serialize(testModel, TestMM, output)
302
+
303
+ assert_equal %q(TestNode text: "some \" \\\\ \\\\\" text \r xx \n xx \r\n xx \t xx \b xx \f")+"\n", output
304
+ end
305
+
306
+ def test_integer
307
+ testModel = TestMM::TestNode.new(:integer => 7)
308
+ output = StringWriter.new
309
+ serialize(testModel, TestMM, output)
310
+ assert_equal %q(TestNode integer: 7)+"\n", output
311
+ end
312
+
313
+ def test_integer_format_spec
314
+ testModel = TestMM::TestNode.new(:integer => 10)
315
+ output = StringWriter.new
316
+ serialize(testModel, TestMM, output, :argument_format_provider => proc {|a|
317
+ if a.name == "integer"
318
+ "0x%02X"
319
+ else
320
+ nil
321
+ end})
322
+ assert_equal %q(TestNode integer: 0x0A)+"\n", output
323
+ end
324
+
325
+ def test_float
326
+ testModel = TestMM::TestNode.new(:float => 1.23)
327
+ output = StringWriter.new
328
+ serialize(testModel, TestMM, output)
329
+ assert_equal %q(TestNode float: 1.23)+"\n", output
330
+ end
331
+
332
+ def test_float2
333
+ testModel = TestMM::TestNode.new(:float => 1.23e-08)
334
+ output = StringWriter.new
335
+ serialize(testModel, TestMM, output)
336
+ assert output =~ /TestNode float: 1.23e-0?08\n/
337
+ end
338
+
339
+ def test_float_format_spec
340
+ testModel = TestMM::TestNode.new(:float => 1.23)
341
+ output = StringWriter.new
342
+ serialize(testModel, TestMM, output, :argument_format_provider => proc {|a|
343
+ if a.name == "float"
344
+ "%1.1f"
345
+ else
346
+ nil
347
+ end})
348
+ assert_equal %q(TestNode float: 1.2)+"\n", output
349
+ end
350
+
351
+ def test_enum
352
+ testModel = [
353
+ TestMM::TestNode.new(:enum => :A),
354
+ TestMM::TestNode.new(:enum => :B),
355
+ TestMM::TestNode.new(:enum => :'non-word*chars')
356
+ ]
357
+ output = StringWriter.new
358
+ serialize(testModel, TestMM, output)
359
+ assert_equal %Q(\
360
+ TestNode enum: A
361
+ TestNode enum: B
362
+ TestNode enum: "non-word*chars"
363
+ ), output
364
+ end
365
+
366
+ module TestMMData
367
+ extend RGen::MetamodelBuilder::ModuleExtension
368
+ # class "Data" exists in the standard Ruby namespace
369
+ class Data < RGen::MetamodelBuilder::MMBase
370
+ has_attr 'notTheBuiltin', String
371
+ end
372
+ end
373
+
374
+ module TestMMSubpackage
375
+ extend RGen::MetamodelBuilder::ModuleExtension
376
+ module SubPackage
377
+ extend RGen::MetamodelBuilder::ModuleExtension
378
+ class Data < RGen::MetamodelBuilder::MMBase
379
+ has_attr 'notTheBuiltin', String
380
+ end
381
+ class Data2 < RGen::MetamodelBuilder::MMBase
382
+ has_attr 'data2', String
383
+ end
384
+ end
385
+ end
386
+
387
+ def test_subpackage
388
+ testModel = TestMMSubpackage::SubPackage::Data2.new(:data2 => "xxx")
389
+ output = StringWriter.new
390
+ serialize(testModel, TestMMSubpackage, output)
391
+ assert_equal %q(Data2 data2: "xxx")+"\n", output
392
+ end
393
+
394
+ def test_command_name_provider
395
+ testModel = TestMM::TestNode.new(:text => "some text", :childs => [
396
+ TestMM::TestNode.new(:text => "child")])
397
+
398
+ output = StringWriter.new
399
+ serialize(testModel, TestMM, output, :command_name_provider => proc do |c|
400
+ c.name + "X"
401
+ end)
402
+
403
+ assert_equal %Q(\
404
+ TestNodeX text: "some text" {
405
+ TestNodeX text: "child"
406
+ }
407
+ ), output
408
+ end
409
+
410
+ def serialize(model, mm, output, options={})
411
+ lang = RText::Language.new(mm.ecore, options)
412
+ ser = RText::Serializer.new(lang)
413
+ ser.serialize(model, output)
414
+ end
415
+
416
+
417
+ end
418
+
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rtext
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Martin Thiede
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-30 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rgen
16
+ requirement: &24349776 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.6.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *24349776
25
+ description: RText can be used to derive textual languages from an RGen metamodel
26
+ with very little effort.
27
+ email: martin dot thiede at gmx de
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files:
31
+ - README
32
+ - CHANGELOG
33
+ - MIT-LICENSE
34
+ - RText_Users_Guide
35
+ - RText_Plugin_Implementation_Guide
36
+ files:
37
+ - lib/rtext/completer.rb
38
+ - lib/rtext/context_element_builder.rb
39
+ - lib/rtext/default_loader.rb
40
+ - lib/rtext/default_service_provider.rb
41
+ - lib/rtext/instantiator.rb
42
+ - lib/rtext/language.rb
43
+ - lib/rtext/parser.rb
44
+ - lib/rtext/serializer.rb
45
+ - lib/rtext/service.rb
46
+ - lib/rtext_plugin/connection_manager.rb
47
+ - test/instantiator_test.rb
48
+ - test/rtext_test.rb
49
+ - test/serializer_test.rb
50
+ - README
51
+ - CHANGELOG
52
+ - MIT-LICENSE
53
+ - RText_Users_Guide
54
+ - RText_Plugin_Implementation_Guide
55
+ - Rakefile
56
+ homepage: http://ruby-gen.org
57
+ licenses: []
58
+ post_install_message:
59
+ rdoc_options:
60
+ - --main
61
+ - README
62
+ - -x
63
+ - test
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 1.7.2
81
+ signing_key:
82
+ specification_version: 3
83
+ summary: Ruby Textual Modelling
84
+ test_files: []