rtext 0.8.2 → 0.9.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG +116 -89
  3. data/Project.yaml +15 -0
  4. data/RText_Protocol +47 -4
  5. data/lib/rtext/context_builder.rb +49 -8
  6. data/lib/rtext/default_completer.rb +212 -163
  7. data/lib/rtext/default_service_provider.rb +3 -3
  8. data/lib/rtext/frontend/connector.rb +130 -56
  9. data/lib/rtext/instantiator.rb +8 -4
  10. data/lib/rtext/language.rb +5 -5
  11. data/lib/rtext/serializer.rb +3 -3
  12. data/lib/rtext/service.rb +281 -253
  13. data/lib/rtext/tokenizer.rb +1 -1
  14. metadata +32 -44
  15. data/Rakefile +0 -46
  16. data/test/completer_test.rb +0 -606
  17. data/test/context_builder_test.rb +0 -948
  18. data/test/frontend/context_test.rb +0 -301
  19. data/test/instantiator_test.rb +0 -1735
  20. data/test/integration/backend.out +0 -13
  21. data/test/integration/crash_on_request_editor.rb +0 -12
  22. data/test/integration/ecore_editor.rb +0 -50
  23. data/test/integration/frontend.log +0 -40502
  24. data/test/integration/model/invalid_encoding.invenc +0 -2
  25. data/test/integration/model/test.crash_on_request +0 -18
  26. data/test/integration/model/test.crashing_backend +0 -18
  27. data/test/integration/model/test.dont_open_socket +0 -0
  28. data/test/integration/model/test.invalid_cmd_line +0 -0
  29. data/test/integration/model/test.not_in_rtext +0 -0
  30. data/test/integration/model/test_large_with_errors.ect3 +0 -43523
  31. data/test/integration/model/test_metamodel.ect +0 -24
  32. data/test/integration/model/test_metamodel2.ect +0 -5
  33. data/test/integration/model/test_metamodel3.ect4 +0 -7
  34. data/test/integration/model/test_metamodel_error.ect2 +0 -3
  35. data/test/integration/model/test_metamodel_ok.ect2 +0 -18
  36. data/test/integration/test.rb +0 -966
  37. data/test/link_detector_test.rb +0 -287
  38. data/test/message_helper_test.rb +0 -118
  39. data/test/rtext_test.rb +0 -11
  40. data/test/serializer_test.rb +0 -1004
  41. data/test/tokenizer_test.rb +0 -173
@@ -1,1735 +0,0 @@
1
- # coding: binary
2
- $:.unshift File.join(File.dirname(__FILE__),"..","lib")
3
-
4
- require 'test/unit'
5
- require 'bigdecimal'
6
- require 'rgen/environment'
7
- require 'rgen/metamodel_builder'
8
- require 'rtext/instantiator'
9
- require 'rtext/language'
10
-
11
- class InstantiatorTest < Test::Unit::TestCase
12
-
13
- module TestMM
14
- extend RGen::MetamodelBuilder::ModuleExtension
15
- class TestNode < RGen::MetamodelBuilder::MMBase
16
- SomeEnum = RGen::MetamodelBuilder::DataTypes::Enum.new([:A, :B, :'non-word*chars', :'2you'])
17
- has_attr 'text', String
18
- has_attr 'integer', Integer
19
- has_attr 'boolean', Boolean
20
- has_attr 'enum', SomeEnum
21
- has_many_attr 'nums', Integer
22
- has_attr 'float', Float
23
- has_one 'related', TestNode
24
- has_many 'others', TestNode
25
- contains_many 'childs', TestNode, 'parent'
26
- end
27
- class SubNode < TestNode
28
- end
29
- end
30
-
31
- module TestMM2
32
- extend RGen::MetamodelBuilder::ModuleExtension
33
- class TestNode < RGen::MetamodelBuilder::MMBase
34
- contains_one 'singleChild', TestNode, 'parent'
35
- end
36
- class TestNode2 < RGen::MetamodelBuilder::MMBase
37
- end
38
- class TestNode3 < RGen::MetamodelBuilder::MMBase
39
- end
40
- class TestNode4 < TestNode
41
- end
42
- TestNode.contains_one 'singleChild2a', TestNode2, 'parentA'
43
- TestNode.contains_one 'singleChild2b', TestNode2, 'parentB'
44
- end
45
-
46
- module TestMMLinenoFilenameFragment
47
- extend RGen::MetamodelBuilder::ModuleExtension
48
- class TestNode < RGen::MetamodelBuilder::MMBase
49
- has_attr 'text', String
50
- has_attr 'lineno', Integer
51
- has_attr 'filename', String
52
- has_attr 'fragmentr', String
53
- contains_many 'childs', TestNode, 'parent'
54
- end
55
- end
56
-
57
- module TestMMAbstract
58
- extend RGen::MetamodelBuilder::ModuleExtension
59
- class TestNode < RGen::MetamodelBuilder::MMBase
60
- abstract
61
- end
62
- end
63
-
64
- module TestMMData
65
- extend RGen::MetamodelBuilder::ModuleExtension
66
- # class "Data" exists in the standard Ruby namespace
67
- class Data < RGen::MetamodelBuilder::MMBase
68
- has_attr 'notTheBuiltin', String
69
- end
70
- end
71
-
72
- module TestMMSubpackage
73
- extend RGen::MetamodelBuilder::ModuleExtension
74
- module SubPackage
75
- extend RGen::MetamodelBuilder::ModuleExtension
76
- class TestNodeSub < RGen::MetamodelBuilder::MMBase
77
- has_attr 'text', String
78
- end
79
- class Data < RGen::MetamodelBuilder::MMBase
80
- has_attr 'notTheBuiltin', String
81
- end
82
- end
83
- end
84
-
85
- module TestMMNonRootClass
86
- extend RGen::MetamodelBuilder::ModuleExtension
87
- class NonRootClass < RGen::MetamodelBuilder::MMBase
88
- end
89
- end
90
-
91
- module TestMMContextSensitiveCommands
92
- extend RGen::MetamodelBuilder::ModuleExtension
93
- module SubPackage2
94
- extend RGen::MetamodelBuilder::ModuleExtension
95
- class Command < RGen::MetamodelBuilder::MMBase
96
- end
97
- end
98
- module SubPackage1
99
- extend RGen::MetamodelBuilder::ModuleExtension
100
- class Command < RGen::MetamodelBuilder::MMBase
101
- contains_one 'command', SubPackage2::Command, 'super'
102
- end
103
- end
104
- class TestNode < RGen::MetamodelBuilder::MMBase
105
- contains_one 'command', SubPackage1::Command, 'testNode'
106
- end
107
- end
108
-
109
- def test_int_float
110
- env, problems = instantiate(%Q(
111
- TestNode float: "abc"
112
- ), TestMM)
113
- assert_equal(1, problems.length)
114
-
115
- root_elements = []
116
- env, problems = instantiate(%Q(
117
- TestNode text: "some text", float: -25 {
118
- TestNode text: "child"
119
- TestNode text: "child2"
120
- }
121
- ), TestMM, :root_elements => root_elements)
122
- assert_no_problems(problems)
123
- assert_model_simple(env)
124
- assert(root_elements.first.float.is_a?(Float))
125
- assert_equal(-25.0, root_elements.first.float)
126
-
127
- root_elements = []
128
- env, problems = instantiate(%Q(
129
- TestNode text: "some text", float: -72.001 {
130
- TestNode text: "child"
131
- TestNode text: "child2"
132
- }
133
- ), TestMM, :root_elements => root_elements)
134
- assert_no_problems(problems)
135
- assert_model_simple(env)
136
- assert(root_elements.first.float.is_a?(Float))
137
- assert_equal(-72.001, root_elements.first.float)
138
- end
139
-
140
- def test_simple
141
- env, problems = instantiate(%Q(
142
- TestNode text: "some text", nums: [1,2] {
143
- TestNode text: "child"
144
- TestNode text: "child2"
145
- }
146
- ), TestMM)
147
- assert_no_problems(problems)
148
- assert_model_simple(env, :with_nums)
149
- end
150
-
151
- def test_multiple_roots
152
- env, problems = instantiate(%Q(
153
- TestNode
154
- TestNode
155
- ), TestMM)
156
- assert_no_problems(problems)
157
- assert_equal 2, env.elements.size
158
- end
159
-
160
- def test_comment
161
- env, problems = instantiate(%Q(
162
- # comment 1
163
- TestNode text: "some text" {# comment 1.1
164
- childs: [ # comment 1.2
165
- # comment 2
166
- TestNode text: "child" # comment 2.1
167
- # comment 3
168
- TestNode text: "child2" #comment 3.1
169
- # unassociated
170
- ] # comment 1.3
171
- # unassociated
172
- } # comment 1.4
173
- #comment 1
174
- TestNode { #comment 1.1
175
- childs: # comment 1.2
176
- TestNode text: "child" #comment2
177
- # unassociated
178
- }# comment 1.3
179
- # unassociated
180
- ), TestMM)
181
- assert_no_problems(problems)
182
- assert_model_simple(env)
183
- end
184
-
185
- def test_comment_only
186
- env, problems = instantiate(%Q(
187
- # comment 1
188
- ), TestMM)
189
- assert_no_problems(problems)
190
- end
191
-
192
- def test_empty
193
- env, problems = instantiate("", TestMM)
194
- assert_no_problems(problems)
195
- end
196
-
197
- #
198
- # options
199
- #
200
-
201
- def test_line_number_setter
202
- env, problems = instantiate(%q(
203
- TestNode text: "node1" {
204
- TestNode text: "node2"
205
-
206
- #some comment
207
- TestNode text: "node3"
208
- }
209
- TestNode text: "node4"
210
- ), TestMMLinenoFilenameFragment, :line_number_attribute => "lineno")
211
- assert_no_problems(problems)
212
- assert_equal 2, env.find(:text => "node1").first.lineno
213
- assert_equal 3, env.find(:text => "node2").first.lineno
214
- assert_equal 6, env.find(:text => "node3").first.lineno
215
- assert_equal 8, env.find(:text => "node4").first.lineno
216
- end
217
-
218
- def test_missing_line_number_setter
219
- env, problems = instantiate(%Q(
220
- TestNode text: A
221
- ), TestMMLinenoFilenameFragment, :line_number_attribute => "wrong_attribute_name")
222
- assert_no_problems(problems)
223
- assert_nil env.elements.first.lineno
224
- end
225
-
226
- def test_root_elements
227
- root_elements = []
228
- env, problems = instantiate(%Q(
229
- TestNode text: A
230
- TestNode text: B
231
- TestNode text: C
232
- ), TestMM, :root_elements => root_elements)
233
- assert_no_problems(problems)
234
- assert_equal ["A", "B", "C"], root_elements.text
235
- end
236
-
237
- def test_file_name_option
238
- env, problems = instantiate(%Q(
239
- TestNode text: A
240
- TestNode text: B
241
- TestNode a problem here
242
- ), TestMM, :file_name => "some_file")
243
- assert_equal "some_file", problems.first.file
244
- end
245
-
246
- def test_file_name_setter
247
- env, problems = instantiate(%Q(
248
- TestNode text: A
249
- ), TestMMLinenoFilenameFragment, :file_name => "some_file", :file_name_attribute => "filename")
250
- assert_equal "some_file", env.elements.first.filename
251
- end
252
-
253
- def test_missing_file_name_setter
254
- env, problems = instantiate(%Q(
255
- TestNode text: A
256
- ), TestMMLinenoFilenameFragment, :file_name => "some_file", :file_name_attribute => "wrong_attribute_name")
257
- assert_nil env.elements.first.filename
258
- end
259
-
260
- def test_fragment_ref_setter
261
- the_ref = "is a string here but would normally be an RGen fragment"
262
- env, problems = instantiate(%Q(
263
- TestNode text: A
264
- ), TestMMLinenoFilenameFragment, :fragment_ref => the_ref, :fragment_ref_attribute => "fragmentr")
265
- assert_equal the_ref.object_id, env.elements.first.fragmentr.object_id
266
- end
267
-
268
- def test_missing_fragment_ref_setter
269
- the_ref = "is a string here but would normally be an RGen fragment"
270
- env, problems = instantiate(%Q(
271
- TestNode text: A
272
- ), TestMMLinenoFilenameFragment, :fragment_ref => the_ref, :fragment_ref_attribute => "wrong_attribute_name")
273
- assert_nil env.elements.first.fragmentr
274
- end
275
-
276
- #
277
- # children with role
278
- #
279
-
280
- def test_child_role
281
- env, problems = instantiate(%Q(
282
- TestNode text: "some text" {
283
- TestNode text: "child"
284
- childs:
285
- TestNode text: "child2"
286
- }
287
- ), TestMM)
288
- assert_no_problems(problems)
289
- assert_model_simple(env)
290
- end
291
-
292
- def test_child_role2
293
- env, problems = instantiate(%Q(
294
- TestNode text: "some text" {
295
- childs: [
296
- TestNode text: "child"
297
- TestNode text: "child2"
298
- ]
299
- }
300
- ), TestMM)
301
- assert_no_problems(problems)
302
- assert_model_simple(env)
303
- end
304
-
305
- def test_child_role3
306
- env, problems = instantiate(%Q(
307
- TestNode text: "some text" {
308
- childs:
309
- TestNode text: "child"
310
- childs:
311
- TestNode text: "child2"
312
- }
313
- ), TestMM)
314
- assert_no_problems(problems)
315
- assert_model_simple(env)
316
- end
317
-
318
- def test_child_role4
319
- env, problems = instantiate(%Q(
320
- TestNode text: "some text" {
321
- childs: [
322
- TestNode text: "child"
323
- ]
324
- childs: [
325
- TestNode text: "child2"
326
- ]
327
- }
328
- ), TestMM)
329
- assert_no_problems(problems)
330
- assert_model_simple(env)
331
- end
332
-
333
- def test_child_role_empty
334
- env, problems = instantiate(%Q(
335
- TestNode {
336
- childs: [
337
- ]
338
- }
339
- ), TestMM)
340
- assert_no_problems(problems)
341
- end
342
-
343
-
344
- #
345
- # whitespace
346
- #
347
-
348
- def test_whitespace1
349
- env, problems = instantiate(%Q(
350
- TestNode text: "some text" , nums: [ 1 , 2 ] {
351
-
352
- # comment
353
-
354
- TestNode text: "child"
355
-
356
- TestNode text: "child2"
357
-
358
- }
359
- ), TestMM)
360
- assert_no_problems(problems)
361
- assert_model_simple(env, :with_nums)
362
- end
363
-
364
- def test_whitespace2
365
- env, problems = instantiate(%Q(
366
- # comment1
367
-
368
- # comment2
369
-
370
- TestNode text: "some text" {
371
-
372
- childs:
373
-
374
- # comment
375
-
376
- TestNode text: "child"
377
-
378
- childs: [
379
-
380
- TestNode text: "child2"
381
-
382
- ]
383
-
384
- }
385
- ), TestMM)
386
- assert_no_problems(problems)
387
- assert_model_simple(env)
388
- end
389
-
390
- def test_no_newline_at_eof
391
- env, problems = instantiate(%Q(
392
- TestNode), TestMM)
393
- assert_no_problems(problems)
394
- end
395
-
396
- def test_no_newline_at_eof2
397
- env, problems = instantiate(%Q(
398
- TestNode {
399
- }), TestMM)
400
- assert_no_problems(problems)
401
- end
402
-
403
- #
404
- # references
405
- #
406
-
407
- def test_references
408
- unresolved_refs = []
409
- env, problems = instantiate(%Q(
410
- TestNode text: "root" {
411
- TestNode related: /
412
- TestNode related: //
413
- TestNode related: /some
414
- TestNode related: //some
415
- TestNode related: /some/
416
- TestNode related: some/
417
- TestNode related: some//
418
- TestNode related: some
419
- TestNode related: /some/reference
420
- TestNode related: /some/reference/
421
- TestNode related: some/reference/
422
- TestNode related: some/reference
423
- }
424
- ), TestMM, :unresolved_refs => unresolved_refs)
425
- assert_no_problems(problems)
426
- ref_targets = [
427
- "/",
428
- "//",
429
- "/some",
430
- "//some",
431
- "/some/",
432
- "some/",
433
- "some//",
434
- "some",
435
- "/some/reference",
436
- "/some/reference/",
437
- "some/reference/",
438
- "some/reference"
439
- ]
440
- assert_equal ref_targets, env.find(:text => "root").first.childs.collect{|c| c.related.targetIdentifier}
441
- assert_equal ref_targets, unresolved_refs.collect{|ur| ur.proxy.targetIdentifier}
442
- assert unresolved_refs.all?{|ur| ur.feature_name == "related"}
443
- end
444
-
445
- def test_references_many
446
- env, problems = instantiate(%Q(
447
- TestNode text: "root" {
448
- TestNode others: /other
449
- TestNode others: [ /other ]
450
- TestNode others: [ /other1, /other2 ]
451
- }
452
- ), TestMM)
453
- assert_no_problems(problems)
454
- assert_equal [
455
- [ "/other" ],
456
- [ "/other" ],
457
- [ "/other1", "/other2" ],
458
- ], env.find(:text => "root").first.childs.collect{|c| c.others.collect{|p| p.targetIdentifier}}
459
- end
460
-
461
- def test_reference_regexp
462
- env, problems = instantiate(%Q(
463
- TestNode text: "root" {
464
- TestNode related: some
465
- TestNode related: ::some
466
- TestNode related: some::reference
467
- TestNode related: ::some::reference
468
- }
469
- ), TestMM, :reference_regexp => /\A\w*(::\w*)+/)
470
- assert_no_problems(problems)
471
- assert_equal [
472
- "some",
473
- "::some",
474
- "some::reference",
475
- "::some::reference"
476
- ], env.find(:text => "root").first.childs.collect{|c| c.related.targetIdentifier}
477
- end
478
-
479
- #
480
- # unlabled arguments
481
- #
482
-
483
- def test_unlabled_arguments
484
- env, problems = instantiate(%Q(
485
- TestNode "some text", [1,2] {
486
- TestNode "child"
487
- TestNode "child2"
488
- }
489
- ), TestMM, :unlabled_arguments => proc {|clazz| ["text", "nums"]})
490
- assert_no_problems(problems)
491
- assert_model_simple(env, :with_nums)
492
- end
493
-
494
- def test_unlabled_arguments_not_in_front
495
- env, problems = instantiate(%Q(
496
- TestNode nums: [1,2], "some text" {
497
- TestNode "child"
498
- TestNode "child2"
499
- }
500
- ), TestMM, :unlabled_arguments => proc {|clazz| ["text", "nums"]})
501
- assert_no_problems(problems)
502
- assert_model_simple(env, :with_nums)
503
- end
504
-
505
- def test_unlabled_arguments_using_labled
506
- env, problems = instantiate(%Q(
507
- TestNode text: "some text", nums: [1,2] {
508
- TestNode text: "child"
509
- TestNode text: "child2"
510
- }
511
- ), TestMM, :unlabled_arguments => proc {|clazz| ["text", "nums"]})
512
- assert_no_problems(problems)
513
- assert_model_simple(env, :with_nums)
514
- end
515
-
516
- def test_unlabled_arguments_subclass
517
- env, problems = instantiate(%Q(
518
- SubNode "some text", [1, 2] {
519
- TestNode text: "child"
520
- TestNode text: "child2"
521
- }
522
- ), TestMM, :unlabled_arguments => proc {|clazz| ["text", "nums"]})
523
- assert_no_problems(problems)
524
- assert_model_simple(env, :with_nums)
525
- end
526
-
527
- #
528
- # context sensitive commands
529
- #
530
-
531
- def test_context_sensitive
532
- env, problems = instantiate(%Q(
533
- TestNode {
534
- Command {
535
- Command
536
- }
537
- }
538
- ), TestMMContextSensitiveCommands)
539
- assert_no_problems(problems)
540
- root = env.find(:class => TestMMContextSensitiveCommands::TestNode).first
541
- assert_not_nil(root)
542
- assert(root.command.is_a?(TestMMContextSensitiveCommands::SubPackage1::Command))
543
- assert(root.command.command.is_a?(TestMMContextSensitiveCommands::SubPackage2::Command))
544
- end
545
-
546
- def test_context_sensitive_command_name_mapping
547
- env, problems = instantiate(%Q(
548
- Command {
549
- Command {
550
- Command
551
- }
552
- }
553
- ), TestMMContextSensitiveCommands, :command_name_provider => lambda do |c|
554
- "Command" end)
555
- assert_no_problems(problems)
556
- root = env.find(:class => TestMMContextSensitiveCommands::TestNode).first
557
- assert_not_nil(root)
558
- assert(root.command.is_a?(TestMMContextSensitiveCommands::SubPackage1::Command))
559
- assert(root.command.command.is_a?(TestMMContextSensitiveCommands::SubPackage2::Command))
560
- end
561
-
562
- #
563
- # problems
564
- #
565
-
566
- def test_unexpected_end_of_file
567
- env, problems = instantiate(%Q(
568
- TestNode text: "some text" {
569
- ), TestMM)
570
- assert_problems([[/unexpected end of file, expected \}/i, 2]], problems)
571
- end
572
-
573
- def test_unknown_command
574
- env, problems = instantiate(%Q(
575
- NotDefined
576
- ), TestMM)
577
- assert_problems([[/unknown command 'NotDefined'/i, 2]], problems)
578
- end
579
-
580
- def test_unknown_command_abstract
581
- env, problems = instantiate(%Q(
582
- TestNode
583
- ), TestMMAbstract)
584
- assert_problems([[/unknown command 'TestNode'/i, 2]], problems)
585
- end
586
-
587
- def test_unexpected_unlabled_argument
588
- env, problems = instantiate(%Q(
589
- TestNode "more text"
590
- ), TestMM)
591
- assert_problems([[/unexpected unlabled argument, 0 unlabled arguments expected/i, 2]], problems)
592
- end
593
-
594
- def test_unknown_child_role
595
- env, problems = instantiate(%Q(
596
- TestNode {
597
- notdefined:
598
- TestNode
599
- }
600
- ), TestMM)
601
- assert_problems([[/unknown child role 'notdefined'/i, 3]], problems)
602
- end
603
-
604
- def test_not_a_child_role
605
- env, problems = instantiate(%Q(
606
- TestNode {
607
- text:
608
- TestNode
609
- others:
610
- TestNode
611
- }
612
- ), TestMM)
613
- assert_problems([
614
- [/role 'text' can not take child elements/i, 3],
615
- [/role 'others' can not take child elements/i, 5]
616
- ], problems)
617
- end
618
-
619
- def test_not_a_single_child
620
- env, problems = instantiate(%Q(
621
- TestNode {
622
- singleChild: [
623
- TestNode
624
- TestNode
625
- ]
626
- }
627
- ), TestMM2)
628
- assert_problems([
629
- [/only one child allowed in role 'singleChild'/i, 5]
630
- ], problems)
631
- end
632
-
633
- def test_not_a_single_child2
634
- env, problems = instantiate(%Q(
635
- TestNode {
636
- singleChild:
637
- TestNode
638
- singleChild: [
639
- ]
640
- singleChild:
641
- TestNode
642
- }
643
- ), TestMM2)
644
- assert_problems([
645
- [/only one child allowed in role 'singleChild'/i, 8]
646
- ], problems)
647
- end
648
-
649
- def test_wrong_child_role
650
- env, problems = instantiate(%Q(
651
- TestNode {
652
- singleChild:
653
- TestNode2
654
- }
655
- ), TestMM2)
656
- assert_problems([
657
- [/role 'singleChild' can not take a TestNode2, expected TestNode/i, 4]
658
- ], problems)
659
- end
660
-
661
- def test_child_role_without_child
662
- env, problems = instantiate(%Q(
663
- TestNode {
664
- singleChild:
665
- }
666
- ), TestMM2)
667
- assert_problems([
668
- [/unexpected \}, expected identifier/i, 4]
669
- ], problems)
670
- end
671
-
672
- def test_wrong_child
673
- env, problems = instantiate(%Q(
674
- TestNode {
675
- TestNode3
676
- }
677
- ), TestMM2)
678
- assert_problems([
679
- [/command 'TestNode3' can not be used in this context/i, 3]
680
- ], problems)
681
- end
682
-
683
- def test_ambiguous_child_role
684
- env, problems = instantiate(%Q(
685
- TestNode {
686
- TestNode2
687
- }
688
- ), TestMM2)
689
- assert_problems([
690
- [/role of element is ambiguous, use a role label/i, 3]
691
- ], problems)
692
- end
693
-
694
- def test_non_ambiguous_child_role_subclass
695
- env, problems = instantiate(%Q(
696
- TestNode {
697
- TestNode4
698
- }
699
- ), TestMM2)
700
- assert_no_problems(problems)
701
- end
702
-
703
- def test_not_a_single_child3
704
- env, problems = instantiate(%Q(
705
- TestNode {
706
- TestNode
707
- TestNode
708
- }
709
- ), TestMM2)
710
- assert_problems([
711
- [/only one child allowed in role 'singleChild'/i, 4]
712
- ], problems)
713
- end
714
-
715
- def test_unknown_argument
716
- env, problems = instantiate(%Q(
717
- TestNode unknown: "some text"
718
- ), TestMM)
719
- assert_problems([[/unknown argument 'unknown'/i, 2]], problems)
720
- end
721
-
722
- def test_attribute_in_child_reference
723
- env, problems = instantiate(%Q(
724
- TestNode singleChild: "some text"
725
- ), TestMM2)
726
- assert_problems([[/argument 'singleChild' can only take child elements/i, 2]], problems)
727
- end
728
-
729
- def test_arguments_duplicate
730
- env, problems = instantiate(%Q(
731
- TestNode text: "some text", text: "more text"
732
- ), TestMM)
733
- assert_problems([[/argument 'text' already defined/i, 2]], problems)
734
- end
735
-
736
- def test_unlabled_arguments_duplicate
737
- env, problems = instantiate(%Q(
738
- TestNode text: "some text", "more text"
739
- ), TestMM, :unlabled_arguments => proc {|c| ["text"]})
740
- assert_problems([[/argument 'text' already defined/i, 2]], problems)
741
- end
742
-
743
- def test_multiple_arguments_in_non_many_attribute
744
- env, problems = instantiate(%Q(
745
- TestNode text: ["text1", "text2"]
746
- ), TestMM)
747
- assert_problems([[/argument 'text' can take only one value/i, 2]], problems)
748
- end
749
-
750
- def test_wrong_argument_type
751
- env, problems = instantiate(%Q(
752
- TestNode text: 1
753
- TestNode integer: "text"
754
- TestNode integer: true
755
- TestNode integer: 1.2
756
- TestNode integer: a
757
- TestNode integer: /a
758
- TestNode enum: 1
759
- TestNode enum: x
760
- TestNode related: 1
761
- ), TestMM)
762
- assert_problems([
763
- [/argument 'text' can not take a integer, expected string/i, 2],
764
- [/argument 'integer' can not take a string, expected integer/i, 3],
765
- [/argument 'integer' can not take a boolean, expected integer/i, 4],
766
- [/argument 'integer' can not take a float, expected integer/i, 5],
767
- [/argument 'integer' can not take a identifier, expected integer/i, 6],
768
- [/argument 'integer' can not take a reference, expected integer/i, 7],
769
- [/argument 'enum' can not take a integer, expected identifier/i, 8],
770
- [/argument 'enum' can not take value x, expected A, B/i, 9],
771
- [/argument 'related' can not take a integer, expected reference, identifier/i, 10]
772
- ], problems)
773
- end
774
-
775
- def test_missing_opening_brace
776
- env, problems = instantiate(%Q(
777
- TestNode
778
- }
779
- ), TestMM)
780
- assert_problems([[/unexpected \}, expected identifier/i, 3]], problems)
781
- end
782
-
783
- def test_invalid_root
784
- env, problems = instantiate(%Q(
785
- NonRootClass
786
- ), TestMMNonRootClass)
787
- assert_problems([[/command 'NonRootClass' can not be used on root level/i, 2]], problems)
788
- end
789
-
790
- #
791
- # problem recovery
792
- #
793
-
794
- def test_missing_value
795
- root_elements = []
796
- env, problems = instantiate(%Q(
797
- TestNode nums: 1, text:
798
- TestNode nums: 2, text: {
799
- SubNode
800
- }
801
- TestNode text: ,nums: 3 {
802
- SubNode
803
- }
804
- TestNode nums: , text: , bla:
805
- ), TestMM, :root_elements => root_elements)
806
- assert_equal 4, root_elements.size
807
- assert_equal [1], root_elements[0].nums
808
- assert_nil root_elements[0].text
809
- assert_equal [2], root_elements[1].nums
810
- assert_equal 1, root_elements[1].childs.size
811
- assert_equal [3], root_elements[2].nums
812
- assert_equal 1, root_elements[2].childs.size
813
- assert_problems([
814
- [/unexpected newline, expected.*integer/i, 2],
815
- [/unexpected \{, expected.*integer/i, 3],
816
- [/unexpected ,, expected.*integer/i, 6],
817
- [/unexpected ,, expected.*integer/i, 9],
818
- [/unexpected ,, expected.*integer/i, 9],
819
- [/unexpected newline, expected.*integer/i, 9],
820
- [/unknown argument 'bla'/i, 9],
821
- ], problems)
822
- end
823
-
824
- def test_missing_comma
825
- root_elements = []
826
- env, problems = instantiate(%Q(
827
- TestNode nums: 1 text: "bla"
828
- ), TestMM, :root_elements => root_elements)
829
- assert_equal 1, root_elements.size
830
- assert_equal [1], root_elements[0].nums
831
- assert_equal "bla", root_elements[0].text
832
- assert_problems([
833
- [/unexpected label .*, expected ,/i, 2],
834
- ], problems)
835
- end
836
-
837
- def test_missing_label
838
- root_elements = []
839
- env, problems = instantiate(%Q(
840
- TestNode nums: 1 "bla"
841
- ), TestMM, :root_elements => root_elements)
842
- assert_equal 1, root_elements.size
843
- assert_equal [1], root_elements[0].nums
844
- assert_problems([
845
- [/unexpected string 'bla', expected ,/i, 2],
846
- [/unexpected unlabled argument/i, 2]
847
- ], problems)
848
- end
849
-
850
- def test_unclosed_bracket1
851
- root_elements = []
852
- env, problems = instantiate(%Q(
853
- TestNode nums: [1, "bla"
854
- ), TestMM, :root_elements => root_elements)
855
- assert_equal 1, root_elements.size
856
- assert_equal [1], root_elements[0].nums
857
- assert_nil root_elements[0].text
858
- assert_problems([
859
- [/unexpected newline, expected \]/i, 2],
860
- [/argument 'nums' can not take a string, expected integer/i, 2],
861
- ], problems)
862
- end
863
-
864
- def test_unclosed_bracket2
865
- root_elements = []
866
- env, problems = instantiate(%Q(
867
- TestNode nums: [1, text: "bla"
868
- ), TestMM, :root_elements => root_elements)
869
- assert_equal 1, root_elements.size
870
- assert_equal [1], root_elements[0].nums
871
- assert_equal "bla", root_elements[0].text
872
- assert_problems([
873
- [/unexpected label 'text', expected identifier/i, 2],
874
- ], problems)
875
- end
876
-
877
- def test_unclosed_bracket3
878
- root_elements = []
879
- env, problems = instantiate(%Q(
880
- TestNode nums: [1 text: "bla"
881
- ), TestMM, :root_elements => root_elements)
882
- assert_equal 1, root_elements.size
883
- assert_equal [1], root_elements[0].nums
884
- assert_equal "bla", root_elements[0].text
885
- assert_problems([
886
- [/unexpected label 'text', expected \]/i, 2],
887
- ], problems)
888
- end
889
-
890
- def test_unclosed_bracket4
891
- root_elements = []
892
- env, problems = instantiate(%Q(
893
- TestNode nums: [1 "bla"
894
- ), TestMM, :root_elements => root_elements)
895
- assert_equal 1, root_elements.size
896
- assert_equal [1], root_elements[0].nums
897
- assert_nil root_elements[0].text
898
- assert_problems([
899
- [/unexpected string 'bla', expected ,/i, 2],
900
- [/argument 'nums' can not take a string, expected integer/i, 2],
901
- [/unexpected newline, expected \]/i, 2],
902
- ], problems)
903
- end
904
-
905
- def test_unclosed_bracket5
906
- root_elements = []
907
- env, problems = instantiate(%Q(
908
- TestNode [1, "bla"
909
- ), TestMM, :root_elements => root_elements)
910
- assert_equal 1, root_elements.size
911
- assert_equal [], root_elements[0].nums
912
- assert_nil root_elements[0].text
913
- assert_problems([
914
- [/unexpected newline, expected \]/i, 2],
915
- [/unexpected unlabled argument/i, 2],
916
- ], problems)
917
- end
918
-
919
- def test_unclosed_bracket6
920
- root_elements = []
921
- env, problems = instantiate(%Q(
922
- TestNode [1, "bla" [
923
- ), TestMM, :root_elements => root_elements)
924
- assert_equal 1, root_elements.size
925
- assert_problems([
926
- [/unexpected \[, expected \]/i, 2],
927
- [/unexpected end of file, expected \]/i, 2],
928
- [/unexpected unlabled argument/i, 2],
929
- [/unexpected unlabled argument/i, 2],
930
- ], problems)
931
- end
932
-
933
- def test_unclosed_bracket7
934
- root_elements = []
935
- env, problems = instantiate(%Q(
936
- TestNode [1, "bla", [
937
- ), TestMM, :root_elements => root_elements)
938
- assert_equal 1, root_elements.size
939
- assert_problems([
940
- [/unexpected \[, expected identifier/i, 2],
941
- [/unexpected unlabled argument/i, 2],
942
- [/unexpected unlabled argument/i, 2],
943
- [/unexpected end of file, expected \]/i, 2],
944
- ], problems)
945
- end
946
-
947
- def test_closing_bracket
948
- root_elements = []
949
- env, problems = instantiate(%Q(
950
- TestNode ]
951
- TestNode 1 ]
952
- TestNode 1, ]
953
- TestNode nums: ]1, "bla"
954
- TestNode text: "bla" ]
955
- ), TestMM, :root_elements => root_elements)
956
- assert_equal 5, root_elements.size
957
- assert_equal [], root_elements[3].nums
958
- assert_equal "bla", root_elements[4].text
959
- assert_problems([
960
- [/unexpected \], expected newline/i, 2],
961
- [/unexpected \], expected newline/i, 3],
962
- [/unexpected unlabled argument/i, 3],
963
- [/unexpected \], expected identifier/i, 4],
964
- [/unexpected unlabled argument/i, 4],
965
- [/unexpected \], expected identifier/i, 5],
966
- [/unexpected \], expected newline/i, 6],
967
- ], problems)
968
- end
969
-
970
- def test_closing_brace
971
- root_elements = []
972
- env, problems = instantiate(%Q(
973
- TestNode }
974
- TestNode 1 }
975
- TestNode 1, }
976
- TestNode nums: }1, "bla"
977
- TestNode text: "bla" }
978
- ), TestMM, :root_elements => root_elements)
979
- assert_equal 5, root_elements.size
980
- assert_equal [], root_elements[3].nums
981
- assert_equal "bla", root_elements[4].text
982
- assert_problems([
983
- [/unexpected \}, expected newline/i, 2],
984
- [/unexpected \}, expected newline/i, 3],
985
- [/unexpected unlabled argument/i, 3],
986
- [/unexpected \}, expected identifier/i, 4],
987
- [/unexpected unlabled argument/i, 4],
988
- [/unexpected \}, expected identifier/i, 5],
989
- [/unexpected \}, expected newline/i, 6],
990
- ], problems)
991
- end
992
-
993
- def test_starting_non_command
994
- root_elements = []
995
- env, problems = instantiate(%Q(
996
- \)
997
- TestNode
998
- *
999
- TestNode
1000
- $
1001
- TestNode
1002
- ,
1003
- TestNode
1004
- [
1005
- TestNode
1006
- {
1007
- TestNode
1008
- ]
1009
- TestNode
1010
- }
1011
- TestNode
1012
- }}
1013
- ), TestMM, :root_elements => root_elements)
1014
- assert_equal 8, root_elements.size
1015
- assert_problems([
1016
- [/parse error on token '\)'/i, 2],
1017
- [/parse error on token '\*'/i, 4],
1018
- [/parse error on token '\$'/i, 6],
1019
- [/unexpected ,, expected identifier/i, 8],
1020
- [/unexpected \[, expected identifier/i, 10],
1021
- [/unexpected \{, expected identifier/i, 12],
1022
- [/unexpected \], expected identifier/i, 14],
1023
- [/unexpected \}, expected identifier/i, 16],
1024
- [/unexpected \}, expected identifier/i, 18],
1025
- ], problems)
1026
- end
1027
-
1028
- def test_parse_error_in_argument_list
1029
- root_elements = []
1030
- env, problems = instantiate(%Q(
1031
- TestNode text: "bla", * nums: 1
1032
- TestNode text: "bla" * , nums: 1
1033
- TestNode ?text: "bla"
1034
- TestNode nums: [1, * 3]
1035
- ), TestMM, :root_elements => root_elements)
1036
- assert_equal 4, root_elements.size
1037
- assert_equal "bla", root_elements[0].text
1038
- assert_equal [1], root_elements[0].nums
1039
- assert_equal "bla", root_elements[1].text
1040
- assert_equal [1], root_elements[1].nums
1041
- assert_equal "bla", root_elements[2].text
1042
- assert_equal [1, 3], root_elements[3].nums
1043
- assert_problems([
1044
- [/parse error on token '\*'/i, 2],
1045
- [/parse error on token '\*'/i, 3],
1046
- [/parse error on token '\?'/i, 4],
1047
- [/parse error on token '\*'/i, 5],
1048
- ], problems)
1049
- end
1050
-
1051
- def test_unclosed_brace
1052
- root_elements = []
1053
- env, problems = instantiate(%Q(
1054
- TestNode {
1055
- ), TestMM, :root_elements => root_elements)
1056
- assert_equal 1, root_elements.size
1057
- assert_problems([
1058
- [/unexpected end of file, expected \}/i, 2]
1059
- ], problems)
1060
- end
1061
-
1062
- def test_unclosed_brace2
1063
- root_elements = []
1064
- env, problems = instantiate(%Q(
1065
- TestNode {
1066
- *
1067
- ), TestMM, :root_elements => root_elements)
1068
- assert_equal 1, root_elements.size
1069
- assert_problems([
1070
- [/parse error on token '\*'/i, 3]
1071
- ], problems)
1072
- end
1073
-
1074
- def test_unclosed_brace3
1075
- root_elements = []
1076
- env, problems = instantiate(%Q(
1077
- TestNode {
1078
- childs:
1079
- ), TestMM, :root_elements => root_elements)
1080
- assert_equal 1, root_elements.size
1081
- assert_problems([
1082
- [/unexpected end of file, expected identifier/i, 3]
1083
- ], problems)
1084
- end
1085
-
1086
- def test_label_without_child
1087
- root_elements = []
1088
- env, problems = instantiate(%Q(
1089
- TestNode {
1090
- childs:
1091
- }
1092
- ), TestMM, :root_elements => root_elements)
1093
- assert_equal 1, root_elements.size
1094
- assert_problems([
1095
- [/unexpected \}, expected identifier/i, 4]
1096
- ], problems)
1097
- end
1098
-
1099
- def test_unclosed_child_bracket
1100
- root_elements = []
1101
- env, problems = instantiate(%Q(
1102
- TestNode {
1103
- childs: [
1104
- ), TestMM, :root_elements => root_elements)
1105
- assert_equal 1, root_elements.size
1106
- assert_problems([
1107
- [/unexpected end of file, expected \]/i, 3]
1108
- ], problems)
1109
- end
1110
-
1111
- def test_child_label_problems
1112
- root_elements = []
1113
- env, problems = instantiate(%Q(
1114
- TestNode {
1115
- childs: x
1116
- SubNode
1117
- childs: *
1118
- SubNode
1119
- childs: &
1120
- }
1121
- ), TestMM, :root_elements => root_elements)
1122
- assert_equal 1, root_elements.size
1123
- assert_equal 2, root_elements[0].childs.size
1124
- assert_problems([
1125
- [/unexpected identifier 'x', expected newline/i, 3],
1126
- [/parse error on token '\*'/i, 5],
1127
- [/parse error on token '&'/i, 7]
1128
- ], problems)
1129
- end
1130
-
1131
- def test_child_label_problems_with_bracket
1132
- root_elements = []
1133
- env, problems = instantiate(%Q(
1134
- TestNode {
1135
- childs: [ x
1136
- SubNode
1137
- ]
1138
- childs: [ *
1139
- SubNode
1140
- ]
1141
- childs: [&
1142
- ]
1143
- }
1144
- ), TestMM, :root_elements => root_elements)
1145
- assert_equal 1, root_elements.size
1146
- assert_equal 2, root_elements[0].childs.size
1147
- assert_problems([
1148
- [/unexpected identifier 'x', expected newline/i, 3],
1149
- [/parse error on token '\*'/i, 6],
1150
- [/parse error on token '&'/i, 9]
1151
- ], problems)
1152
- end
1153
-
1154
- def test_missing_closing_bracket
1155
- root_elements = []
1156
- env, problems = instantiate(%Q(
1157
- TestNode {
1158
- childs: [
1159
- SubNode
1160
- childs: [
1161
- SubNode
1162
- SubNode
1163
- }
1164
- ), TestMM, :root_elements => root_elements)
1165
- assert_equal 1, root_elements.size
1166
- assert_equal 3, root_elements[0].childs.size
1167
- assert_problems([
1168
- [/unexpected label 'childs', expected identifier/i, 5],
1169
- [/unexpected \}, expected identifier/i, 8],
1170
- ], problems)
1171
- end
1172
-
1173
- def test_missing_closing_brace
1174
- root_elements = []
1175
- env, problems = instantiate(%Q(
1176
- TestNode {
1177
- TestNode {
1178
- TestNode
1179
- }
1180
- ), TestMM, :root_elements => root_elements)
1181
- assert_equal 1, root_elements.size
1182
- assert_equal 1, root_elements[0].childs.size
1183
- assert_equal 1, root_elements[0].childs[0].childs.size
1184
- assert_problems([
1185
- [/unexpected end of file, expected \}/i, 5],
1186
- ], problems)
1187
- end
1188
-
1189
- #
1190
- # command name provider
1191
- #
1192
-
1193
- def test_command_name_provider
1194
- env, problems = instantiate(%Q(
1195
- TestNodeX text: "some text", nums: [1,2] {
1196
- TestNodeX text: "child"
1197
- TestNodeX text: "child2"
1198
- }
1199
- ), TestMM, :command_name_provider => proc do |c|
1200
- c.name + "X"
1201
- end)
1202
- assert_no_problems(problems)
1203
- assert_model_simple(env, :with_nums)
1204
- end
1205
-
1206
- def test_command_name_provider_ambiguous
1207
- begin
1208
- env, problems = instantiate(%Q(
1209
- TestNode
1210
- ), TestMM, :command_name_provider => proc do |c|
1211
- "Fixed"
1212
- end)
1213
- assert false
1214
- rescue RuntimeError => e
1215
- assert e.message =~ /ambiguous command name/
1216
- end
1217
- end
1218
-
1219
- #
1220
- # comment handler
1221
- #
1222
-
1223
- def test_comment_handler
1224
- proc_calls = 0
1225
- env, problems = instantiate(%Q(
1226
- #comment
1227
- TestNode text: "node1"
1228
- #comment
1229
- # multiline
1230
- TestNode text: "node2"
1231
- TestNode text: "node3" #comment
1232
- #above
1233
- TestNode text: "node4" {#right1
1234
- childs: [ #right2
1235
- #unassociated1
1236
- ] #right3
1237
- #unassociated2
1238
- } #below
1239
- #above1
1240
- #above2
1241
- TestNode text: "node5" { #right1
1242
- childs: #right2
1243
- TestNode
1244
- }#below
1245
- #comment without
1246
- #an element following
1247
- ), TestMM, :comment_handler => proc {|c,k,e,env|
1248
- proc_calls += 1
1249
- if e.nil?
1250
- case proc_calls
1251
- when 4
1252
- assert_equal "unassociated1", c
1253
- assert_equal :unassociated, k
1254
- when 5
1255
- assert_equal "unassociated2", c
1256
- assert_equal :unassociated, k
1257
- when 15
1258
- assert_equal "comment without\nan element following", c
1259
- assert_equal :unassociated, k
1260
- end
1261
- elsif e.text == "node1"
1262
- assert_equal "comment", c
1263
- assert_equal :above, k
1264
- elsif e.text == "node2"
1265
- assert_equal "comment\n multiline", c
1266
- assert_equal :above, k
1267
- elsif e.text == "node3"
1268
- assert_equal "comment", c
1269
- assert_equal :eol, k
1270
- elsif e.text == "node4"
1271
- case proc_calls
1272
- when 6
1273
- assert_equal "above", c
1274
- assert_equal :above, k
1275
- when 7
1276
- assert_equal "right1", c
1277
- assert_equal :eol, k
1278
- when 8
1279
- assert_equal "right2", c
1280
- assert_equal :eol, k
1281
- when 9
1282
- assert_equal "right3", c
1283
- assert_equal :eol, k
1284
- when 10
1285
- assert_equal "below", c
1286
- assert_equal :eol, k
1287
- end
1288
- elsif e.text == "node5"
1289
- case proc_calls
1290
- when 11
1291
- assert_equal "above1\nabove2", c
1292
- assert_equal :above, k
1293
- when 12
1294
- assert_equal "right1", c
1295
- assert_equal :eol, k
1296
- when 13
1297
- assert_equal "right2", c
1298
- assert_equal :eol, k
1299
- when 14
1300
- assert_equal "below", c
1301
- assert_equal :eol, k
1302
- end
1303
- else
1304
- assert false, "unexpected element in comment handler"
1305
- end
1306
- true
1307
- })
1308
- assert_no_problems(problems)
1309
- assert_equal 15, proc_calls
1310
- end
1311
-
1312
- def test_comment_handler_comment_not_allowed
1313
- env, problems = instantiate(%Q(
1314
- #comment
1315
- TestNode
1316
- ), TestMM, :comment_handler => proc {|c,k,e,env|
1317
- false
1318
- })
1319
- assert_problems([[/element can not take this comment/, 3]], problems)
1320
- end
1321
-
1322
- def test_comment_handler_comment_not_allowed_unassociated
1323
- env, problems = instantiate(%Q(
1324
- #comment
1325
- ), TestMM, :comment_handler => proc {|c,k,e,env|
1326
- false
1327
- })
1328
- assert_problems([[/Unassociated comment not allowed/, 2]], problems)
1329
- end
1330
-
1331
- #
1332
- # annotations
1333
- #
1334
-
1335
- def test_annotation_not_supported
1336
- env, problems = instantiate(%Q(
1337
- @annotation
1338
- TestNode
1339
- ), TestMM)
1340
- assert_problems([[/annotation not allowed/i, 3]], problems)
1341
- end
1342
-
1343
- def test_annotation_not_allowed
1344
- env, problems = instantiate(%Q(
1345
- @annotation
1346
- TestNode
1347
- ), TestMM, :annotation_handler => proc {|a,e,env|
1348
- false
1349
- })
1350
- assert_problems([[/annotation not allowed/i, 3]], problems)
1351
- end
1352
-
1353
- def test_annotation_in_wrong_places
1354
- env, problems = instantiate(%Q(
1355
- @annotation
1356
- #comment
1357
- TestNode {
1358
- @annotation
1359
- childs:
1360
- TestNode
1361
- @annotation
1362
- }
1363
- @annotation
1364
- ), TestMM)
1365
- assert_problems([
1366
- [/unexpected comment 'comment', expected identifier/i, 3],
1367
- [/unexpected label 'childs', expected identifier/i, 6],
1368
- [/unexpected \}, expected identifier/i, 9],
1369
- [/unexpected end of file, expected identifier/i, 10]
1370
- ], problems)
1371
- end
1372
-
1373
- def test_annotation_handler
1374
- annotations = []
1375
- elements = []
1376
- env, problems = instantiate(%Q(
1377
- @annotation
1378
- TestNode text: "aa"
1379
- @annotation
1380
- @ in a new line
1381
-
1382
- @ even with space in between
1383
-
1384
- TestNode text: "bb" {
1385
- @at child
1386
- TestNode text: "cc"
1387
- childs:
1388
- @after label
1389
- TestNode text: "dd"
1390
- @another child
1391
- TestNode text: "ee"
1392
- childs: [
1393
- @in brackets
1394
- TestNode text: "ff"
1395
- ]
1396
- }
1397
- ), TestMM, :annotation_handler => proc {|a,e,env|
1398
- annotations << a
1399
- elements << e
1400
- true
1401
- })
1402
- assert_equal "aa", elements[0].text
1403
- assert_equal "annotation", annotations[0]
1404
- assert_equal "cc", elements[1].text
1405
- assert_equal "at child", annotations[1]
1406
- assert_equal "dd", elements[2].text
1407
- assert_equal "after label", annotations[2]
1408
- assert_equal "ee", elements[3].text
1409
- assert_equal "another child", annotations[3]
1410
- assert_equal "ff", elements[4].text
1411
- assert_equal "in brackets", annotations[4]
1412
- assert_equal "bb", elements[5].text
1413
- assert_equal "annotation\n in a new line\n even with space in between", annotations[5]
1414
- assert_no_problems(problems)
1415
- end
1416
-
1417
- #
1418
- # generics
1419
- #
1420
-
1421
- def test_generics_parse_error
1422
- env, problems = instantiate(%Q(
1423
- TestNode text: <bla
1424
- TestNode text: bla>
1425
- TestNode text: <a<b>
1426
- TestNode text: <a>b>
1427
- TestNode text: <%a
1428
- TestNode text: <%a%
1429
- TestNode text: <%a%>b%>
1430
- ), TestMM, :enable_generics => true)
1431
- assert_problems([
1432
- [/parse error on token '<'/i, 2],
1433
- [/unexpected unlabled argument/i, 2],
1434
- [/parse error on token '>'/i, 3],
1435
- [/unexpected identifier 'b'/i, 5],
1436
- [/parse error on token '>'/i, 5],
1437
- [/unexpected unlabled argument/i, 5],
1438
- [/parse error on token '<'/i, 6],
1439
- [/unexpected unlabled argument/i, 6],
1440
- [/parse error on token '<'/i, 7],
1441
- [/unexpected unlabled argument/i, 7],
1442
- [/parse error on token '%'/i, 7],
1443
- [/unexpected identifier 'b'/i, 8],
1444
- [/unexpected unlabled argument/i, 8],
1445
- [/parse error on token '%'/i, 8],
1446
- ], problems)
1447
- end
1448
-
1449
- def test_generics
1450
- root_elements = []
1451
- env, problems = instantiate(%q(
1452
- TestNode text: <bla>, nums: [<1>, <%2%>], boolean: <truthy>, enum: <%option%>, float: <precise>, related: <%noderef%>, others: [<other1>, <%other2%>]
1453
- ), TestMM, :root_elements => root_elements, :enable_generics => true)
1454
- assert_no_problems(problems)
1455
- assert root_elements[0].text.is_a?(RText::Generic)
1456
- assert_equal "bla", root_elements[0].text.string
1457
- assert_equal ["1", "2"], root_elements[0].nums.collect{|n| n.string}
1458
- assert_equal "truthy", root_elements[0].boolean.string
1459
- assert_equal "option", root_elements[0].enum.string
1460
- assert_equal "precise", root_elements[0].float.string
1461
- assert_equal "noderef", root_elements[0].related.string
1462
- assert_equal ["other1", "other2"], root_elements[0].others.collect{|n| n.string}
1463
- end
1464
-
1465
- def test_generics_forbidden
1466
- env, problems = instantiate(%Q(\
1467
- TestNode text: <bla>
1468
- ), TestMM)
1469
- assert_problems([
1470
- [/generic value not allowed/i, 1],
1471
- ], problems)
1472
- end
1473
-
1474
- #
1475
- # subpackages
1476
- #
1477
-
1478
- def test_subpackage
1479
- env, problems = instantiate(%q(
1480
- TestNodeSub text: "something"
1481
- ), TestMMSubpackage)
1482
- assert_no_problems(problems)
1483
- assert_equal "something", env.elements.first.text
1484
- end
1485
-
1486
- #
1487
- # values
1488
- #
1489
-
1490
- def test_escapes
1491
- env, problems = instantiate(%q(
1492
- TestNode text: "some \" \\\\ \\\\\" text \r xx \n xx \r\n xx \t xx \b xx \f"
1493
- ), TestMM)
1494
- assert_no_problems(problems)
1495
- assert_equal %Q(some " \\ \\" text \r xx \n xx \r\n xx \t xx \b xx \f), env.elements.first.text
1496
- end
1497
-
1498
- def test_escape_single_backslash
1499
- env, problems = instantiate(%q(
1500
- TestNode text: "a single \\ will be just itself"
1501
- ), TestMM)
1502
- assert_no_problems(problems)
1503
- assert_equal %q(a single \\ will be just itself), env.elements.first.text
1504
- end
1505
-
1506
- def test_string_umlauts
1507
- env, problems = instantiate(%q(
1508
- TestNode text: "ä, ö, ü"
1509
- ), TestMM)
1510
- assert_no_problems(problems)
1511
- assert_equal %q(ä, ö, ü), env.elements.first.text
1512
- end
1513
-
1514
- def test_integer
1515
- env, problems = instantiate(%q(
1516
- TestNode integer: 7
1517
- TestNode integer: 4294967296
1518
- TestNode integer: 12345678901234567890
1519
- ), TestMM)
1520
- assert_no_problems(problems)
1521
- assert_equal 7, env.elements[0].integer
1522
- assert_equal 4294967296, env.elements[1].integer
1523
- assert_equal 12345678901234567890, env.elements[2].integer
1524
- end
1525
-
1526
- def test_integer_hex
1527
- env, problems = instantiate(%q(
1528
- TestNode text: root {
1529
- TestNode integer: 0x7
1530
- TestNode integer: 0X7
1531
- TestNode integer: 0x007
1532
- TestNode integer: 0x77
1533
- TestNode integer: 0xabCDEF
1534
- TestNode integer: 0xabcdefabcdefabcdef
1535
- }
1536
- ), TestMM)
1537
- assert_no_problems(problems)
1538
- assert_equal [7, 7, 7, 0x77, 0xABCDEF, 0xabcdefabcdefabcdef], env.find(:text => "root").first.childs.collect{|c| c.integer}
1539
- end
1540
-
1541
- def test_float
1542
- env, problems = instantiate(%q(
1543
- TestNode float: 1.23
1544
- TestNode float: 1.23e-08
1545
- TestNode float: 1.23e+10
1546
- TestNode float: 1234567890.123456789
1547
- ), TestMM)
1548
- assert_no_problems(problems)
1549
- assert_equal 1.23, env.elements[0].float
1550
- assert_equal 1.23e-08, env.elements[1].float
1551
- assert_equal 1.23e+10, env.elements[2].float
1552
- if rgen_with_bigdecimal?
1553
- assert env.elements[3].float.is_a?(BigDecimal)
1554
- assert_equal "1234567890.123456789", env.elements[3].float.to_s("F")
1555
- else
1556
- assert env.elements[3].float.is_a?(Float)
1557
- assert_equal "1234567890.1234567", env.elements[3].float.to_s
1558
- end
1559
- end
1560
-
1561
- def test_boolean
1562
- env, problems = instantiate(%q(
1563
- TestNode text: root {
1564
- TestNode boolean: true
1565
- TestNode boolean: false
1566
- }
1567
- ), TestMM)
1568
- assert_no_problems(problems)
1569
- assert_equal [true, false], env.find(:text => "root").first.childs.collect{|c| c.boolean}
1570
- end
1571
-
1572
- def test_enum
1573
- env, problems = instantiate(%q(
1574
- TestNode text: root {
1575
- TestNode enum: A
1576
- TestNode enum: B
1577
- TestNode enum: "non-word*chars"
1578
- TestNode enum: "2you"
1579
- }
1580
- ), TestMM)
1581
- assert_no_problems(problems)
1582
- assert_equal [:A, :B, :'non-word*chars', :'2you'], env.find(:text => "root").first.childs.collect{|c| c.enum}
1583
- end
1584
-
1585
- def test_with_bom
1586
- env, problems = instantiate(%Q(\xEF\xBB\xBF
1587
- TestNode text: "some text", nums: [1,2] {
1588
- TestNode text: "child"
1589
- TestNode text: "child2"
1590
- }
1591
- ), TestMM)
1592
- assert_no_problems(problems)
1593
- assert_model_simple(env, :with_nums)
1594
- end
1595
-
1596
- #
1597
- # conflicts with builtins
1598
- #
1599
-
1600
- def test_conflict_builtin
1601
- env, problems = instantiate(%q(
1602
- Data notTheBuiltin: "for sure"
1603
- ), TestMMData)
1604
- assert_no_problems(problems)
1605
- assert_equal "for sure", env.elements.first.notTheBuiltin
1606
- end
1607
-
1608
- def test_builtin_in_subpackage
1609
- env, problems = instantiate(%q(
1610
- Data notTheBuiltin: "for sure"
1611
- ), TestMMSubpackage)
1612
- assert_no_problems(problems)
1613
- assert_equal "for sure", env.elements.first.notTheBuiltin
1614
- end
1615
-
1616
- #
1617
- # encoding
1618
- #
1619
-
1620
- def test_encodings
1621
- input = %Q(TestNode text: "iso-8859-1 AE Umlaut: \xc4")
1622
- # force encoding to binary in order to prevent exceptions on invalid byte sequences
1623
- # if the encoding would be utf-8, there would be an exception with the string above
1624
- input.force_encoding("binary")
1625
- env, problems = instantiate(input, TestMM)
1626
- assert_no_problems(problems)
1627
- assert_match /AE Umlaut: /, env.elements.first.text
1628
- end
1629
-
1630
- #
1631
- # line breaks
1632
- #
1633
-
1634
- def test_linebreak
1635
- roots = []
1636
- env, problems = instantiate(%Q(
1637
- TestNode sometext,
1638
- integer: 1,
1639
- nums: [
1640
- 1,
1641
- 2
1642
- ]
1643
- ), TestMM, :root_elements => roots, :unlabled_arguments => proc {|clazz| ["text"]})
1644
- assert_no_problems(problems)
1645
- node = roots.first
1646
- assert_equal "sometext", node.text
1647
- assert_equal 1, node.integer
1648
- assert_equal [1,2], node.nums
1649
- end
1650
-
1651
- def test_linebreak_backslash
1652
- roots = []
1653
- env, problems = instantiate(%Q(
1654
- TestNode \\
1655
- sometext
1656
- ), TestMM, :root_elements => roots, :unlabled_arguments => proc {|clazz| ["text"]})
1657
- assert_no_problems(problems)
1658
- node = roots.first
1659
- assert_equal "sometext", node.text
1660
- end
1661
-
1662
- def test_linebreak_backslash_problems
1663
- roots = []
1664
- env, problems = instantiate(%Q(
1665
- TostNode \\
1666
- sometext
1667
- TostNode
1668
- ), TestMM, :root_elements => roots, :unlabled_arguments => proc {|clazz| ["text"]})
1669
- assert_problems([
1670
- [/unknown command/i, 2],
1671
- [/unknown command/i, 4],
1672
- ], problems)
1673
- end
1674
-
1675
- private
1676
-
1677
- def instantiate(text, mm, options={})
1678
- env = RGen::Environment.new
1679
- lang = RText::Language.new(mm.ecore, options.merge(
1680
- :root_classes => mm.ecore.eAllClasses.select{|c|
1681
- c.name == "TestNode" || c.name == "Data" || c.name == "TestNodeSub" || c.name == "SubNode"}))
1682
- inst = RText::Instantiator.new(lang)
1683
- problems = []
1684
- inst.instantiate(text, options.merge({:env => env, :problems => problems, :root_elements => options[:root_elements]}))
1685
- return env, problems
1686
- end
1687
-
1688
- def assert_no_problems(problems)
1689
- assert problems.empty?, problems.collect{|p| "#{p.message}, line: #{p.line}"}.join("\n")
1690
- end
1691
-
1692
- def assert_problems(expected, problems)
1693
- remaining = problems.dup
1694
- probs = []
1695
- expected.each do |e|
1696
- if e.is_a?(Array)
1697
- p = remaining.find{|p| p.message =~ e[0] && p.line == e[1]}
1698
- else
1699
- p = remaining.find{|p| p.message =~ e}
1700
- end
1701
- probs << "expected problem not present: #{e}" if !p
1702
- # make sure to not delete duplicate problems at once
1703
- idx = remaining.index(p)
1704
- remaining.delete_at(idx) if idx
1705
- end
1706
- remaining.each do |p|
1707
- probs << "unexpected problem: #{p.message}, line: #{p.line}"
1708
- end
1709
- assert probs.empty?, probs.join("\n")
1710
- end
1711
-
1712
- def assert_model_simple(env, *opts)
1713
- raise "unknown options" unless (opts - [:with_nums]).empty?
1714
- root = env.find(:class => TestMM::TestNode, :text => "some text").first
1715
- assert_not_nil root
1716
- assert_equal 2, root.childs.size
1717
- assert_equal [TestMM::TestNode, TestMM::TestNode], root.childs.collect{|c| c.class}
1718
- assert_equal ["child", "child2"], root.childs.text
1719
- if opts.include?(:with_nums)
1720
- assert_equal [1, 2], root.nums
1721
- end
1722
- end
1723
-
1724
- def rgen_with_bigdecimal?
1725
- begin
1726
- TestMM::TestNode.new.float = BigDecimal.new("0.0")
1727
- rescue StandardError
1728
- return false
1729
- end
1730
- true
1731
- end
1732
-
1733
- end
1734
-
1735
-