rtext 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,360 @@
1
+ $:.unshift File.join(File.dirname(__FILE__),"..","lib")
2
+
3
+ require 'test/unit'
4
+ require 'rgen/metamodel_builder'
5
+ require 'rtext/language'
6
+ require 'rtext/context_builder'
7
+
8
+ class ContextBuilderTest < Test::Unit::TestCase
9
+
10
+ module TestMM
11
+ extend RGen::MetamodelBuilder::ModuleExtension
12
+ class TestNode < RGen::MetamodelBuilder::MMBase
13
+ has_attr 'text', String
14
+ has_many_attr 'nums', Integer
15
+ has_one 'related', TestNode
16
+ has_many 'others', TestNode
17
+ contains_many 'childs', TestNode, 'parent'
18
+ end
19
+ end
20
+
21
+ def test_in_cmd_after_cmd
22
+ c = build_context TestMM, <<-END
23
+ TestNode text: a {
24
+ TestNode nums: 3 {
25
+ TestNode |others: /dummy {
26
+ END
27
+ assert_equal("", c.prefix)
28
+ assert_nil(c.feature)
29
+ assert(!c.in_array)
30
+ assert(!c.in_block)
31
+ assert_simple_model(c.element)
32
+ assert_other_values(c.element, [])
33
+ end
34
+
35
+ def test_in_cmd_in_label
36
+ c = build_context TestMM, <<-END
37
+ TestNode text: a {
38
+ TestNode nums: 3 {
39
+ TestNode ot|hers: /dummy {
40
+ END
41
+ assert_equal("ot", c.prefix)
42
+ assert_nil(c.feature)
43
+ assert(!c.in_array)
44
+ assert(!c.in_block)
45
+ assert_simple_model(c.element)
46
+ assert_other_values(c.element, [])
47
+ end
48
+
49
+ def test_in_cmd_in_label2
50
+ c = build_context TestMM, <<-END
51
+ TestNode text: a {
52
+ TestNode nums: 3 {
53
+ TestNode others:| /dummy {
54
+ END
55
+ assert_equal("", c.prefix)
56
+ assert_equal("others", c.feature.name)
57
+ assert(!c.in_array)
58
+ assert(!c.in_block)
59
+ assert_simple_model(c.element)
60
+ assert_other_values(c.element, [])
61
+ end
62
+
63
+ def test_in_cmd_after_label
64
+ c = build_context TestMM, <<-END
65
+ TestNode text: a {
66
+ TestNode nums: 3 {
67
+ TestNode others: |/dummy {
68
+ END
69
+ assert_equal("", c.prefix)
70
+ assert_equal("others", c.feature.name)
71
+ assert(!c.in_array)
72
+ assert(!c.in_block)
73
+ assert_simple_model(c.element)
74
+ assert_other_values(c.element, [])
75
+ end
76
+
77
+ def test_in_cmd_in_value
78
+ c = build_context TestMM, <<-END
79
+ TestNode text: a {
80
+ TestNode nums: 3 {
81
+ TestNode others: /du|mmy {
82
+ END
83
+ assert_equal("/du", c.prefix)
84
+ assert_equal("others", c.feature.name)
85
+ assert(!c.in_array)
86
+ assert(!c.in_block)
87
+ assert_simple_model(c.element)
88
+ assert_other_values(c.element, [])
89
+ end
90
+
91
+ def test_in_cmd_in_value2
92
+ c = build_context TestMM, <<-END
93
+ TestNode text: a {
94
+ TestNode nums: 3 {
95
+ TestNode others: /dummy| {
96
+ END
97
+ assert_equal("/dummy", c.prefix)
98
+ assert_equal("others", c.feature.name)
99
+ assert(!c.in_array)
100
+ assert(!c.in_block)
101
+ assert_simple_model(c.element)
102
+ assert_other_values(c.element, [])
103
+ end
104
+
105
+ def test_in_cmd_after_value
106
+ c = build_context TestMM, <<-END
107
+ TestNode text: a {
108
+ TestNode nums: 3 {
109
+ TestNode others: /dummy, |text: x {
110
+ END
111
+ assert_equal("", c.prefix)
112
+ assert_nil(c.feature)
113
+ assert(!c.in_array)
114
+ assert(!c.in_block)
115
+ assert_simple_model(c.element)
116
+ assert_other_values(c.element, ["/dummy"])
117
+ end
118
+
119
+ def test_in_cmd_in_second_label
120
+ c = build_context TestMM, <<-END
121
+ TestNode text: a {
122
+ TestNode nums: 3 {
123
+ TestNode others: /dummy, te|xt: x {
124
+ END
125
+ assert_equal("te", c.prefix)
126
+ assert_nil(c.feature)
127
+ assert(!c.in_array)
128
+ assert(!c.in_block)
129
+ assert_simple_model(c.element)
130
+ assert_other_values(c.element, ["/dummy"])
131
+ end
132
+
133
+ def test_in_cmd_after_second_label
134
+ c = build_context TestMM, <<-END
135
+ TestNode text: a {
136
+ TestNode nums: 3 {
137
+ TestNode others: /dummy, text: |x {
138
+ END
139
+ assert_equal("", c.prefix)
140
+ assert_equal("text", c.feature.name)
141
+ assert(!c.in_array)
142
+ assert(!c.in_block)
143
+ assert_simple_model(c.element)
144
+ assert_other_values(c.element, ["/dummy"])
145
+ end
146
+
147
+ def test_in_cmd_in_second_value
148
+ c = build_context TestMM, <<-END
149
+ TestNode text: a {
150
+ TestNode nums: 3 {
151
+ TestNode others: /dummy, text: x| {
152
+ END
153
+ assert_equal("x", c.prefix)
154
+ assert_equal("text", c.feature.name)
155
+ assert(!c.in_array)
156
+ assert(!c.in_block)
157
+ assert_simple_model(c.element)
158
+ assert_other_values(c.element, ["/dummy"])
159
+ end
160
+
161
+ def test_in_cmd_in_array
162
+ c = build_context TestMM, <<-END
163
+ TestNode text: a {
164
+ TestNode nums: 3 {
165
+ TestNode others: [|/dummy, text: x
166
+ END
167
+ assert_equal("", c.prefix)
168
+ assert_equal("others", c.feature.name)
169
+ assert(c.in_array)
170
+ assert(!c.in_block)
171
+ assert_simple_model(c.element)
172
+ assert_other_values(c.element, [])
173
+ end
174
+
175
+ def test_in_cmd_in_array_value
176
+ c = build_context TestMM, <<-END
177
+ TestNode text: a {
178
+ TestNode nums: 3 {
179
+ TestNode others: [/d|ummy, text: x
180
+ END
181
+ assert_equal("/d", c.prefix)
182
+ assert_equal("others", c.feature.name)
183
+ assert(c.in_array)
184
+ assert(!c.in_block)
185
+ assert_simple_model(c.element)
186
+ assert_other_values(c.element, [])
187
+ end
188
+
189
+ def test_in_cmd_after_array_value
190
+ c = build_context TestMM, <<-END
191
+ TestNode text: a {
192
+ TestNode nums: 3 {
193
+ TestNode others: [/dummy,| text: x
194
+ END
195
+ assert_equal("", c.prefix)
196
+ assert_equal("others", c.feature.name)
197
+ assert(c.in_array)
198
+ assert(!c.in_block)
199
+ assert_simple_model(c.element)
200
+ assert_other_values(c.element, ["/dummy"])
201
+ end
202
+
203
+ def test_in_cmd_in_second_array_value
204
+ c = build_context TestMM, <<-END
205
+ TestNode text: a {
206
+ TestNode nums: 3 {
207
+ TestNode others: [/dummy, /dom|my
208
+ END
209
+ assert_equal("/dom", c.prefix)
210
+ assert_equal("others", c.feature.name)
211
+ assert(c.in_array)
212
+ assert(!c.in_block)
213
+ assert_simple_model(c.element)
214
+ assert_other_values(c.element, ["/dummy"])
215
+ end
216
+
217
+ def test_in_cmd_after_array
218
+ c = build_context TestMM, <<-END
219
+ TestNode text: a {
220
+ TestNode nums: 3 {
221
+ TestNode others: [/dummy, /dommy], |
222
+ END
223
+ assert_equal("", c.prefix)
224
+ assert_nil(c.feature)
225
+ assert(!c.in_array)
226
+ assert(!c.in_block)
227
+ assert_simple_model(c.element)
228
+ assert_other_values(c.element, ["/dummy", "/dommy"])
229
+ end
230
+
231
+ def test_in_cmd_after_array2
232
+ c = build_context TestMM, <<-END
233
+ TestNode text: a {
234
+ TestNode nums: 3 {
235
+ TestNode others: [/dummy, /dommy], nums: |
236
+ END
237
+ assert_equal("", c.prefix)
238
+ assert_equal("nums", c.feature.name)
239
+ assert(!c.in_array)
240
+ assert(!c.in_block)
241
+ assert_simple_model(c.element)
242
+ assert_other_values(c.element, ["/dummy", "/dommy"])
243
+ end
244
+
245
+ def test_below_single_label
246
+ c = build_context TestMM, <<-END
247
+ TestNode text: a {
248
+ TestNode nums: 3 {
249
+ TestNode others: /dummy {
250
+ childs:
251
+ |
252
+ END
253
+ assert_equal("", c.prefix)
254
+ assert_equal("childs", c.feature.name)
255
+ assert(!c.in_array)
256
+ assert(c.in_block)
257
+ assert_simple_model(c.element)
258
+ assert_other_values(c.element, ["/dummy"])
259
+ end
260
+
261
+ def test_below_single_label_started_command
262
+ c = build_context TestMM, <<-END
263
+ TestNode text: a {
264
+ TestNode nums: 3 {
265
+ TestNode others: /dummy {
266
+ childs:
267
+ Tes|
268
+ END
269
+ assert_equal("Tes", c.prefix)
270
+ assert_equal("childs", c.feature.name)
271
+ assert(!c.in_array)
272
+ assert(c.in_block)
273
+ assert_simple_model(c.element)
274
+ assert_other_values(c.element, ["/dummy"])
275
+ end
276
+
277
+ def test_below_multi_label
278
+ c = build_context TestMM, <<-END
279
+ TestNode text: a {
280
+ TestNode nums: 3 {
281
+ TestNode others: /dummy {
282
+ childs: [
283
+ |
284
+ END
285
+ assert_equal("", c.prefix)
286
+ assert_equal("childs", c.feature.name)
287
+ assert(c.in_array)
288
+ assert(c.in_block)
289
+ assert_simple_model(c.element)
290
+ assert_other_values(c.element, ["/dummy"])
291
+ end
292
+
293
+ def test_below_multi_label_started_command
294
+ c = build_context TestMM, <<-END
295
+ TestNode text: a {
296
+ TestNode nums: 3 {
297
+ TestNode others: /dummy {
298
+ childs: [
299
+ Tes|
300
+ END
301
+ assert_equal("Tes", c.prefix)
302
+ assert_equal("childs", c.feature.name)
303
+ assert(c.in_array)
304
+ assert(c.in_block)
305
+ assert_simple_model(c.element)
306
+ assert_other_values(c.element, ["/dummy"])
307
+ end
308
+
309
+ def test_in_new_line
310
+ c = build_context TestMM, <<-END
311
+ TestNode text: a {
312
+ TestNode nums: 3 {
313
+ TestNode others: /dummy {
314
+ |
315
+ END
316
+ assert_equal("", c.prefix)
317
+ assert_nil(c.feature)
318
+ assert(!c.in_array)
319
+ assert(c.in_block)
320
+ assert_simple_model(c.element)
321
+ assert_other_values(c.element, ["/dummy"])
322
+ end
323
+
324
+ def test_in_new_line_started_command
325
+ c = build_context TestMM, <<-END
326
+ TestNode text: a {
327
+ TestNode nums: 3 {
328
+ TestNode others: /dummy {
329
+ Tes|
330
+ END
331
+ assert_equal("Tes", c.prefix)
332
+ assert_nil(c.feature)
333
+ assert(!c.in_array)
334
+ assert(c.in_block)
335
+ assert_simple_model(c.element)
336
+ assert_other_values(c.element, ["/dummy"])
337
+ end
338
+
339
+ def assert_simple_model(c)
340
+ assert c.is_a?(TestMM::TestNode)
341
+ assert c.parent.is_a?(TestMM::TestNode)
342
+ assert_equal [3], c.parent.nums
343
+ assert c.parent.parent.is_a?(TestMM::TestNode)
344
+ assert_equal "a", c.parent.parent.text
345
+ end
346
+
347
+ def assert_other_values(c, values)
348
+ assert_equal values, c.others.collect{|v| v.targetIdentifier}
349
+ end
350
+
351
+ def build_context(mm, text)
352
+ context_lines = text.split("\n")
353
+ pos_in_line = context_lines.last.index("|")
354
+ context_lines.last.sub!("|", "")
355
+ lang = RText::Language.new(mm.ecore, :root_classes => mm.ecore.eAllClasses)
356
+ RText::ContextBuilder.build_context(lang, context_lines, pos_in_line)
357
+ end
358
+
359
+ end
360
+
@@ -7,12 +7,12 @@ require 'rgen/metamodel_builder'
7
7
  require 'rtext/instantiator'
8
8
  require 'rtext/language'
9
9
 
10
- class RTextInstantiatorTest < Test::Unit::TestCase
10
+ class InstantiatorTest < Test::Unit::TestCase
11
11
 
12
12
  module TestMM
13
13
  extend RGen::MetamodelBuilder::ModuleExtension
14
14
  class TestNode < RGen::MetamodelBuilder::MMBase
15
- SomeEnum = RGen::MetamodelBuilder::DataTypes::Enum.new([:A, :B, :'non-word*chars', :'2you'])
15
+ SomeEnum = RGen::MetamodelBuilder::DataTypes::Enum.new([:A, :B, :'non-word*chars'])
16
16
  has_attr 'text', String
17
17
  has_attr 'integer', Integer
18
18
  has_attr 'boolean', Boolean
@@ -80,6 +80,30 @@ class RTextInstantiatorTest < Test::Unit::TestCase
80
80
  end
81
81
  end
82
82
 
83
+ module TestMMNonRootClass
84
+ extend RGen::MetamodelBuilder::ModuleExtension
85
+ class NonRootClass < RGen::MetamodelBuilder::MMBase
86
+ end
87
+ end
88
+
89
+ module TestMMContextSensitiveCommands
90
+ extend RGen::MetamodelBuilder::ModuleExtension
91
+ module SubPackage2
92
+ extend RGen::MetamodelBuilder::ModuleExtension
93
+ class Command < RGen::MetamodelBuilder::MMBase
94
+ end
95
+ end
96
+ module SubPackage1
97
+ extend RGen::MetamodelBuilder::ModuleExtension
98
+ class Command < RGen::MetamodelBuilder::MMBase
99
+ contains_one 'command', SubPackage2::Command, 'super'
100
+ end
101
+ end
102
+ class TestNode < RGen::MetamodelBuilder::MMBase
103
+ contains_one 'command', SubPackage1::Command, 'testNode'
104
+ end
105
+ end
106
+
83
107
  def test_simple
84
108
  env, problems = instantiate(%Q(
85
109
  TestNode text: "some text", nums: [1,2] {
@@ -242,6 +266,17 @@ class RTextInstantiatorTest < Test::Unit::TestCase
242
266
  assert_model_simple(env)
243
267
  end
244
268
 
269
+ def test_child_role_empty
270
+ env, problems = instantiate(%Q(
271
+ TestNode {
272
+ childs: [
273
+ ]
274
+ }
275
+ ), TestMM)
276
+ assert_no_problems(problems)
277
+ end
278
+
279
+
245
280
  #
246
281
  # whitespace
247
282
  #
@@ -410,6 +445,41 @@ class RTextInstantiatorTest < Test::Unit::TestCase
410
445
  assert_no_problems(problems)
411
446
  assert_model_simple(env, :with_nums)
412
447
  end
448
+
449
+ #
450
+ # context sensitive commands
451
+ #
452
+
453
+ def test_context_sensitive
454
+ env, problems = instantiate(%Q(
455
+ TestNode {
456
+ Command {
457
+ Command
458
+ }
459
+ }
460
+ ), TestMMContextSensitiveCommands)
461
+ assert_no_problems(problems)
462
+ root = env.find(:class => TestMMContextSensitiveCommands::TestNode).first
463
+ assert_not_nil(root)
464
+ assert(root.command.is_a?(TestMMContextSensitiveCommands::SubPackage1::Command))
465
+ assert(root.command.command.is_a?(TestMMContextSensitiveCommands::SubPackage2::Command))
466
+ end
467
+
468
+ def test_context_sensitive_command_name_mapping
469
+ env, problems = instantiate(%Q(
470
+ Command {
471
+ Command {
472
+ Command
473
+ }
474
+ }
475
+ ), TestMMContextSensitiveCommands, :command_name_provider => lambda do |c|
476
+ "Command" end)
477
+ assert_no_problems(problems)
478
+ root = env.find(:class => TestMMContextSensitiveCommands::TestNode).first
479
+ assert_not_nil(root)
480
+ assert(root.command.is_a?(TestMMContextSensitiveCommands::SubPackage1::Command))
481
+ assert(root.command.command.is_a?(TestMMContextSensitiveCommands::SubPackage2::Command))
482
+ end
413
483
 
414
484
  #
415
485
  # problems
@@ -508,6 +578,17 @@ class RTextInstantiatorTest < Test::Unit::TestCase
508
578
  ], problems)
509
579
  end
510
580
 
581
+ def test_child_role_without_child
582
+ env, problems = instantiate(%Q(
583
+ TestNode {
584
+ singleChild:
585
+ }
586
+ ), TestMM2)
587
+ assert_problems([
588
+ /unexpected }, expected identifier/i
589
+ ], problems)
590
+ end
591
+
511
592
  def test_wrong_child
512
593
  env, problems = instantiate(%Q(
513
594
  TestNode {
@@ -515,7 +596,7 @@ class RTextInstantiatorTest < Test::Unit::TestCase
515
596
  }
516
597
  ), TestMM2)
517
598
  assert_problems([
518
- /this kind of element can not be contained here/i,
599
+ /command 'TestNode3' can not be used in this context/i,
519
600
  ], problems)
520
601
  end
521
602
 
@@ -619,6 +700,13 @@ class RTextInstantiatorTest < Test::Unit::TestCase
619
700
  assert_problems([/unexpected \}, expected identifier/i], problems)
620
701
  end
621
702
 
703
+ def test_invalid_root
704
+ env, problems = instantiate(%Q(
705
+ NonRootClass
706
+ ), TestMMNonRootClass)
707
+ assert_problems([/command 'NonRootClass' can not be used on root level/i], problems)
708
+ end
709
+
622
710
  #
623
711
  # command name provider
624
712
  #
@@ -773,13 +861,6 @@ class RTextInstantiatorTest < Test::Unit::TestCase
773
861
  assert_equal "something", env.elements.first.text
774
862
  end
775
863
 
776
- def test_subpackage_no_shortname_opt
777
- env, problems = instantiate(%q(
778
- TestNodeSub text: "something"
779
- ), TestMMSubpackage, :short_class_names => false)
780
- assert_problems([/Unknown command 'TestNodeSub'/], problems)
781
- end
782
-
783
864
  #
784
865
  # values
785
866
  #
@@ -859,11 +940,10 @@ class RTextInstantiatorTest < Test::Unit::TestCase
859
940
  TestNode enum: A
860
941
  TestNode enum: B
861
942
  TestNode enum: "non-word*chars"
862
- TestNode enum: "2you"
863
943
  }
864
944
  ), TestMM)
865
945
  assert_no_problems(problems)
866
- assert_equal [:A, :B, :'non-word*chars', :'2you'], env.find(:text => "root").first.childs.collect{|c| c.enum}
946
+ assert_equal [:A, :B, :'non-word*chars'], env.find(:text => "root").first.childs.collect{|c| c.enum}
867
947
  end
868
948
 
869
949
  #
@@ -890,7 +970,9 @@ class RTextInstantiatorTest < Test::Unit::TestCase
890
970
 
891
971
  def instantiate(text, mm, options={})
892
972
  env = RGen::Environment.new
893
- lang = RText::Language.new(mm.ecore, options)
973
+ lang = RText::Language.new(mm.ecore, options.merge(
974
+ :root_classes => mm.ecore.eAllClasses.select{|c|
975
+ c.name == "TestNode" || c.name == "Data" || c.name == "TestNodeSub" || c.name == "SubNode"}))
894
976
  inst = RText::Instantiator.new(lang)
895
977
  problems = []
896
978
  inst.instantiate(text, options.merge({:env => env, :problems => problems}))