rtext 0.9.2 → 0.9.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,287 +0,0 @@
1
- $:.unshift File.join(File.dirname(__FILE__),"..","lib")
2
-
3
- require 'minitest/autorun'
4
- require 'rgen/metamodel_builder'
5
- require 'rtext/language'
6
- require 'rtext/link_detector'
7
-
8
- class LinkDetectorTest < MiniTest::Test
9
-
10
- module TestMM
11
- extend RGen::MetamodelBuilder::ModuleExtension
12
- class TestNode2 < RGen::MetamodelBuilder::MMBase
13
- has_attr 'name', String
14
- end
15
- class TestNode < RGen::MetamodelBuilder::MMBase
16
- has_attr 'name', String
17
- has_attr 'id', Integer
18
- has_one 'related', TestNode
19
- has_many 'others', TestNode
20
- contains_many 'children', TestNode2, 'parent'
21
- end
22
- end
23
-
24
- def test_before_command
25
- ld = build_link_desc TestMM, <<-END
26
- |TestNode SomeNode, id: 1234, related: /Other/Node, others: [/NodeA, /Node/B]
27
- END
28
- assert_link_desc ld, :element => "TestNode", :feature => nil, :backward => true, :value => "TestNode", :scol => 1, :ecol => 8
29
- end
30
-
31
- def test_in_command
32
- ld = build_link_desc TestMM, <<-END
33
- Test|Node SomeNode, id: 1234, related: /Other/Node, others: [/NodeA, /Node/B]
34
- END
35
- assert_link_desc ld, :element => "TestNode", :feature => nil, :backward => true, :value => "TestNode", :scol => 1, :ecol => 8
36
- end
37
-
38
- def test_end_of_command
39
- ld = build_link_desc TestMM, <<-END
40
- TestNod|e SomeNode, id: 1234, related: /Other/Node, others: [/NodeA, /Node/B]
41
- END
42
- assert_link_desc ld, :element => "TestNode", :feature => nil, :backward => true, :value => "TestNode", :scol => 1, :ecol => 8
43
- end
44
-
45
- def test_after_command
46
- ld = build_link_desc TestMM, <<-END
47
- TestNode| SomeNode, id: 1234, related: /Other/Node, others: [/NodeA, /Node/B]
48
- END
49
- assert_nil ld
50
- end
51
-
52
- def test_in_name
53
- ld = build_link_desc TestMM, <<-END
54
- TestNode So|meNode, id: 1234, related: /Other/Node, others: [/NodeA, /Node/B]
55
- END
56
- assert_link_desc ld, :element => "TestNode", :feature => "name", :index => 0, :backward => false, :value => "SomeNode", :scol => 10, :ecol => 17
57
- end
58
-
59
- def test_in_name_with_quotes
60
- ld = build_link_desc TestMM, <<-END
61
- TestNode "So|meNode", id: 1234, related: /Other/Node, others: [/NodeA, /Node/B]
62
- END
63
- assert_link_desc ld, :element => "TestNode", :feature => "name", :index => 0, :backward => false, :value => "SomeNode", :scol => 10, :ecol => 19
64
- end
65
-
66
- def test_after_comma
67
- ld = build_link_desc TestMM, <<-END
68
- TestNode SomeNode,| id: 1234, related: /Other/Node, others: [/NodeA, /Node/B]
69
- END
70
- assert_nil ld
71
- end
72
-
73
- def test_after_label
74
- ld = build_link_desc TestMM, <<-END
75
- TestNode SomeNode, id:| 1234, related: /Other/Node, others: [/NodeA, /Node/B]
76
- END
77
- assert_nil ld
78
- end
79
-
80
- def test_beginning_of_labled_argument
81
- ld = build_link_desc TestMM, <<-END
82
- TestNode SomeNode, id: |1234, related: /Other/Node, others: [/NodeA, /Node/B]
83
- END
84
- assert_link_desc ld, :element => "TestNode", :feature => "id", :index => 0, :backward => false, :value => 1234, :scol => 24, :ecol => 27
85
- end
86
-
87
- def test_in_labled_argument
88
- ld = build_link_desc TestMM, <<-END
89
- TestNode SomeNode, id: 123|4, related: /Other/Node, others: [/NodeA, /Node/B]
90
- END
91
- assert_link_desc ld, :element => "TestNode", :feature => "id", :index => 0, :backward => false, :value => 1234, :scol => 24, :ecol => 27
92
- end
93
-
94
- def test_after_labled_argument
95
- ld = build_link_desc TestMM, <<-END
96
- TestNode SomeNode, id: 1234|, related: /Other/Node, others: [/NodeA, /Node/B]
97
- END
98
- assert_nil ld
99
- end
100
-
101
- def test_in_label
102
- ld = build_link_desc TestMM, <<-END
103
- TestNode SomeNode, id: 1234, re|lated: /Other/Node, others: [/NodeA, /Node/B]
104
- END
105
- assert_nil ld
106
- end
107
-
108
- def test_in_reference
109
- ld = build_link_desc TestMM, <<-END
110
- TestNode SomeNode, id: 1234, related: /O|ther/Node, others: [/NodeA, /Node/B]
111
- END
112
- assert_link_desc ld, :element => "TestNode", :feature => "related", :index => 0, :backward => false, :value => "/Other/Node", :scol => 39, :ecol => 49
113
- end
114
-
115
- def test_before_array
116
- ld = build_link_desc TestMM, <<-END
117
- TestNode SomeNode, id: 1234, related: /Other/Node, others: |[/NodeA, /Node/B]
118
- END
119
- assert_nil ld
120
- end
121
-
122
- def test_ref_in_array
123
- ld = build_link_desc TestMM, <<-END
124
- TestNode SomeNode, id: 1234, related: /Other/Node, others: [|/NodeA, /Node/B]
125
- END
126
- assert_link_desc ld, :element => "TestNode", :feature => "others", :index => 0, :backward => false, :value => "/NodeA", :scol => 61, :ecol => 66
127
- end
128
-
129
- def test_second_ref_in_array
130
- ld = build_link_desc TestMM, <<-END
131
- TestNode SomeNode, id: 1234, related: /Other/Node, others: [/NodeA, /Node/|B]
132
- END
133
- assert_link_desc ld, :element => "TestNode", :feature => "others", :index => 1, :backward => false, :value => "/Node/B", :scol => 69, :ecol => 75
134
- end
135
-
136
- def test_backward_ref_name_in_command
137
- ld = build_link_desc(TestMM, {:backward_ref => "name"}, <<-END
138
- |TestNode SomeNode, id: 1234, related: /Other/Node, others: [/NodeA, /Node/B]
139
- END
140
- )
141
- assert_link_desc ld, :element => "TestNode", :feature => nil, :backward => false, :value => "TestNode", :scol => 1, :ecol => 8
142
- end
143
-
144
- def test_backward_ref_name_in_name
145
- ld = build_link_desc(TestMM, {:backward_ref => "name"}, <<-END
146
- TestNode Som|eNode, id: 1234, related: /Other/Node, others: [/NodeA, /Node/B]
147
- END
148
- )
149
- assert_link_desc ld, :element => "TestNode", :feature => "name", :index => 0, :backward => true, :value => "SomeNode", :scol => 10, :ecol => 17
150
- end
151
-
152
- def test_backward_ref_name_with_quotes
153
- ld = build_link_desc(TestMM, {:backward_ref => "name"}, <<-END
154
- TestNode "Som|eNode", id: 1234, related: /Other/Node, others: [/NodeA, /Node/B]
155
- END
156
- )
157
- assert_link_desc ld, :element => "TestNode", :feature => "name", :index => 0, :backward => true, :value => "SomeNode", :scol => 10, :ecol => 19
158
- end
159
-
160
- def test_backward_ref_id_in_id
161
- ld = build_link_desc(TestMM, {:backward_ref => "id"}, <<-END
162
- TestNode SomeNode, id: |1234, related: /Other/Node, others: [/NodeA, /Node/B]
163
- END
164
- )
165
- assert_link_desc ld, :element => "TestNode", :feature => "id", :index => 0, :backward => true, :value => 1234, :scol => 24, :ecol => 27
166
- end
167
-
168
- def test_command_only
169
- ld = build_link_desc(TestMM, <<-END
170
- Tes|tNode
171
- END
172
- )
173
- assert_link_desc ld, :element => "TestNode", :feature => nil, :backward => true, :value => "TestNode", :scol => 1, :ecol => 8
174
- end
175
-
176
- def test_command_and_name_only
177
- ld = build_link_desc(TestMM, <<-END
178
- TestNode |SomeNode
179
- END
180
- )
181
- assert_link_desc ld, :element => "TestNode", :feature => "name", :index => 0, :backward => false, :value => "SomeNode", :scol => 10, :ecol => 17
182
- end
183
-
184
- def test_command_and_name_only_backward_ref_name
185
- ld = build_link_desc(TestMM, {:backward_ref => "name"}, <<-END
186
- TestNode |SomeNode
187
- END
188
- )
189
- assert_link_desc ld, :element => "TestNode", :feature => "name", :index => 0, :backward => true, :value => "SomeNode", :scol => 10, :ecol => 17
190
- end
191
-
192
- def test_command_and_name_curly_no_ws
193
- ld = build_link_desc(TestMM, {:backward_ref => "name"}, <<-END
194
- TestNode Some|Node{
195
- END
196
- )
197
- assert_link_desc ld, :element => "TestNode", :element_name => "SomeNode", :feature => "name", :index => 0, :backward => true, :value => "SomeNode", :scol => 10, :ecol => 17
198
- end
199
-
200
- def test_child_within_command
201
- ld = build_link_desc(TestMM, {:backward_ref => "name"}, <<-END
202
- TestNode SomeNode {
203
- Test|Node2 SomeOtherNode
204
- END
205
- )
206
- assert_link_desc ld, :element => "TestNode2", :feature => nil, :backward => false, :value => "TestNode2", :scol => 3, :ecol => 11
207
- end
208
-
209
- def test_child_with_label_after_command
210
- ld = build_link_desc(TestMM, {:backward_ref => "name"}, <<-END
211
- TestNode SomeNode {
212
- children:
213
- TestNode2 |SomeOtherNode
214
- END
215
- )
216
- assert_link_desc ld, :element => "TestNode2", :feature => "name", :index => 0, :backward => true, :value => "SomeOtherNode", :scol => 15, :ecol => 27
217
- end
218
-
219
- def test_child_with_label_within_command
220
- ld = build_link_desc(TestMM, {:backward_ref => "name"}, <<-END
221
- TestNode SomeNode {
222
- children:
223
- Test|Node2 SomeOtherNode
224
- END
225
- )
226
- assert_link_desc ld, :element => "TestNode2", :feature => nil, :backward => false, :value => "TestNode2", :scol => 5, :ecol => 13
227
- end
228
-
229
- def test_child_with_label_within_command_square_brackets
230
- ld = build_link_desc(TestMM, {:backward_ref => "name"}, <<-END
231
- TestNode SomeNode {
232
- children: [
233
- Test|Node2 SomeOtherNode
234
- END
235
- )
236
- assert_link_desc ld, :element => "TestNode2", :feature => nil, :backward => false, :value => "TestNode2", :scol => 5, :ecol => 13
237
- end
238
-
239
- def build_link_desc(mm, text, text2=nil)
240
- if text.is_a?(Hash)
241
- options = text
242
- context_lines = text2.split("\n")
243
- else
244
- options = {}
245
- context_lines = text.split("\n")
246
- end
247
- pos_in_line = context_lines.last.index("|")+1
248
- context_lines.last.sub!("|", "")
249
- lang = RText::Language.new(mm.ecore,
250
- :unlabled_arguments => proc {|c| ["name"]},
251
- :backward_ref_attribute => proc {|c|
252
- if options[:backward_ref]
253
- options[:backward_ref]
254
- else
255
- nil
256
- end
257
- })
258
- RText::LinkDetector.new(lang).detect(context_lines, pos_in_line)
259
- end
260
-
261
- def assert_link_desc(ld, options)
262
- if options[:element]
263
- assert_equal options[:element], ld.element.class.ecore.name
264
- else
265
- assert_nil ld.element
266
- end
267
- if options[:element_name]
268
- assert_equal options[:element_name], ld.element.name
269
- end
270
- if options[:feature]
271
- assert_equal options[:feature], ld.feature.name
272
- else
273
- assert_nil ld.feature
274
- end
275
- if options[:index]
276
- assert_equal options[:index], ld.index
277
- else
278
- assert_nil ld.index
279
- end
280
- assert_equal options[:backward], ld.backward
281
- assert_equal options[:value], ld.value
282
- assert_equal options[:scol], ld.scol
283
- assert_equal options[:ecol], ld.ecol
284
- end
285
-
286
- end
287
-
@@ -1,116 +0,0 @@
1
- $:.unshift File.join(File.dirname(__FILE__),"..","lib")
2
-
3
- require 'minitest/autorun'
4
- require 'rtext/message_helper'
5
-
6
- class MessageHelperTest < MiniTest::Test
7
- include RText::MessageHelper
8
-
9
- def test_serialize
10
- str = serialize_message({"key" => 1})
11
- assert_equal '9{"key":1}', str
12
- str = serialize_message({"key" => true})
13
- assert_equal '12{"key":true}', str
14
- str = serialize_message({"key" => "value"})
15
- assert_equal '15{"key":"value"}', str
16
- str = serialize_message({"key" => {"nested" => "value"}})
17
- assert_equal '26{"key":{"nested":"value"}}', str
18
- str = serialize_message({"key" => ["value1", "value2"]})
19
- assert_equal '27{"key":["value1","value2"]}', str
20
-
21
- # a iso-8859-1 'ä'
22
- str = serialize_message({"key" => "umlaut\xe4".force_encoding("binary")})
23
- assert_equal '19{"key":"umlaut%e4"}', str
24
- str = serialize_message({"key" => "umlaut\xe4".force_encoding("iso-8859-1")})
25
- assert_equal '19{"key":"umlaut%e4"}', str
26
- str = serialize_message({"key" => "umlaut\xe4".force_encoding("cp850")})
27
- assert_equal '19{"key":"umlaut%e4"}', str
28
- str = serialize_message({"key" => "umlaut\xe4".force_encoding("utf-8")})
29
- assert_equal '19{"key":"umlaut%e4"}', str
30
-
31
- # a utf-8 'ä'
32
- str = serialize_message({"key" => "umlaut\xc3\xa4".force_encoding("binary")})
33
- assert_equal '22{"key":"umlaut%c3%a4"}', str
34
- str = serialize_message({"key" => "umlaut\xc3\xa4".force_encoding("iso-8859-1")})
35
- assert_equal '22{"key":"umlaut%c3%a4"}', str
36
- str = serialize_message({"key" => "umlaut\xc3\xa4".force_encoding("cp850")})
37
- assert_equal '22{"key":"umlaut%c3%a4"}', str
38
- str = serialize_message({"key" => "umlaut\xc3\xa4".force_encoding("utf-8")})
39
- assert_equal '22{"key":"umlaut%c3%a4"}', str
40
-
41
- # %
42
- str = serialize_message({"key" => "%"})
43
- assert_equal '13{"key":"%25"}', str
44
- end
45
-
46
- def test_extract
47
- # specified length too short
48
- assert_raises JSON::ParserError do
49
- extract_message('8{"key":1}')
50
- end
51
- # specified length too long
52
- assert_raises JSON::ParserError do
53
- extract_message('10{"key":1}x')
54
- end
55
- # message data shorter than length specifie, waits for more data
56
- extract_message('10{"key":1}')
57
-
58
- obj = extract_message('9{"key":1}')
59
- assert_equal({"key" => 1}, obj)
60
- obj = extract_message('12{"key":true}')
61
- assert_equal({"key" => true}, obj)
62
- obj = extract_message('15{"key":"value"}')
63
- assert_equal({"key" => "value"}, obj)
64
- obj = extract_message('26{"key":{"nested":"value"}}')
65
- assert_equal({"key" => {"nested" => "value"}}, obj)
66
- obj = extract_message('27{"key":["value1","value2"]}')
67
- assert_equal({"key" => ["value1", "value2"]}, obj)
68
-
69
- # a iso-8859-1 'ä'
70
- obj = extract_message('19{"key":"umlaut%e4"}'.force_encoding("binary"))
71
- assert_equal "ASCII-8BIT", obj["key"].encoding.name
72
- assert_equal "umlaut\xe4".force_encoding("ascii-8bit"), obj["key"]
73
- obj = extract_message('19{"key":"umlaut%e4"}'.force_encoding("utf-8"))
74
- assert_equal "ASCII-8BIT", obj["key"].encoding.name
75
- assert_equal "umlaut\xe4".force_encoding("ascii-8bit"), obj["key"]
76
-
77
- # a utf-8 'ä'
78
- obj = extract_message('22{"key":"umlaut%c3%a4"}'.force_encoding("binary"))
79
- assert_equal "ASCII-8BIT", obj["key"].encoding.name
80
- assert_equal "umlaut\xc3\xa4".force_encoding("ascii-8bit"), obj["key"]
81
- obj = extract_message('22{"key":"umlaut%c3%a4"}'.force_encoding("utf-8"))
82
- assert_equal "ASCII-8BIT", obj["key"].encoding.name
83
- assert_equal "umlaut\xc3\xa4".force_encoding("ascii-8bit"), obj["key"]
84
-
85
- # %
86
- obj = extract_message('13{"key":"%25"}')
87
- assert_equal "ASCII-8BIT", obj["key"].encoding.name
88
- assert_equal "%", obj["key"]
89
-
90
- # invalid escape sequence
91
- obj = extract_message('11{"key":"%"}')
92
- assert_equal "ASCII-8BIT", obj["key"].encoding.name
93
- assert_equal "%", obj["key"]
94
- obj = extract_message('13{"key":"%xx"}')
95
- assert_equal "ASCII-8BIT", obj["key"].encoding.name
96
- assert_equal "%xx", obj["key"]
97
- obj = extract_message('14{"key":"%%25"}')
98
- assert_equal "ASCII-8BIT", obj["key"].encoding.name
99
- assert_equal "%%", obj["key"]
100
-
101
- # invalid characters (protocol should use 7-bit ascii only)
102
- obj = extract_message(%Q(14{"key":"\xe4345"}).force_encoding("binary"))
103
- assert_equal "ASCII-8BIT", obj["key"].encoding.name
104
- assert_equal "?345", obj["key"]
105
- end
106
-
107
- def test_roundtrip
108
- value = (0..255).collect{|b| b.chr("binary")}.join
109
- str = serialize_message({"key" => value})
110
- obj = extract_message(str)
111
- assert_equal "ASCII-8BIT", obj["key"].encoding.name
112
- assert_equal (0..255).collect{|i| i}, obj["key"].bytes.to_a
113
- end
114
-
115
- end
116
-
data/test/rtext_test.rb DELETED
@@ -1,11 +0,0 @@
1
- $:.unshift File.dirname(__FILE__)
2
-
3
- require 'serializer_test'
4
- require 'instantiator_test'
5
- require 'context_builder_test'
6
- require 'completer_test'
7
- require 'tokenizer_test'
8
- require 'link_detector_test'
9
- require 'integration/test'
10
- require 'message_helper_test'
11
- require 'frontend/context_test'