rtext 0.8.0 → 0.9.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,205 +1,301 @@
1
- $:.unshift File.join(File.dirname(__FILE__),"..","..","lib")
2
-
3
- require 'test/unit'
4
- require 'rtext/frontend/context'
5
-
6
- class ContextTest < Test::Unit::TestCase
7
-
8
- def test_simple
9
- assert_context(
10
- %Q(
11
- A {
12
- B {
13
- |F bla
14
- ),
15
- %Q(
16
- A {
17
- B {
18
- C a1: v1, a2: "v2"
19
- D {
20
- E a1: 5
21
- }
22
- |F bla
23
- ))
24
- end
25
-
26
- def test_child_label
27
- assert_context(
28
- %Q(
29
- A {
30
- sub:
31
- B {
32
- F bla|
33
- ),
34
- %Q(
35
- A {
36
- sub:
37
- B {
38
- C a1: v1, a2: "v2"
39
- D {
40
- E a1: 5
41
- }
42
- F bla|
43
- ))
44
- end
45
-
46
- def test_child_label_array
47
- assert_context(
48
- %Q(
49
- A {
50
- sub: [
51
- B {
52
- F| bla
53
- ),
54
- %Q(
55
- A {
56
- sub: [
57
- B {
58
- C
59
- }
60
- B {
61
- C a1: v1, a2: "v2"
62
- D {
63
- E a1: 5
64
- }
65
- F| bla
66
- ))
67
- end
68
-
69
- def test_ignore_child_lables
70
- assert_context(
71
- %Q(
72
- A {
73
- B {
74
- F bl|a
75
- ),
76
- %Q(
77
- A {
78
- B {
79
- sub:
80
- C a1: v1, a2: "v2"
81
- sub2: [
82
- D {
83
- E a1: 5
84
- }
85
- ]
86
- F bl|a
87
- ))
88
- end
89
-
90
- def test_linebreak
91
- assert_context(
92
- %Q(
93
- A {
94
- B {
95
- C name,a1: v1,a2: "v2"|
96
- ),
97
- %Q(
98
- A {
99
- B {
100
- C name,
101
- a1: v1,
102
- a2: "v2"|
103
- ))
104
- end
105
-
106
- def test_linebreak_arg_array
107
- assert_context(
108
- %Q(
109
- A {
110
- B {
111
- C name,a1: [v1,v2],a2: |5
112
- ),
113
- %Q(
114
- A {
115
- B {
116
- C name,
117
- a1: [
118
- v1,
119
- v2
120
- ],
121
- a2: |5
122
- ))
123
- end
124
-
125
- def test_linebreak_empty_last_line
126
- assert_context(
127
- %Q(
128
- A {
129
- B name,|
130
- ),
131
- %Q(
132
- A {
133
- B name,
134
- |
135
- ))
136
- end
137
-
138
- def test_linebreak_empty_last_line2
139
- assert_context(
140
- %Q(
141
- A {
142
- B name,|
143
- ),
144
- %Q(
145
- A {
146
- B name,
147
- |
148
- ))
149
- end
150
-
151
- def test_linebreak_empty_lines
152
- assert_context(
153
- %Q(
154
- A {
155
- B name,a1: |
156
- ),
157
- %Q(
158
- A {
159
- B name,
160
-
161
- a1: |
162
- ))
163
- end
164
-
165
- def test_comment_annotation
166
- assert_context(
167
- %Q(
168
- A {
169
- B {
170
- |F bla
171
- ),
172
- %Q(
173
- A {
174
- # bla
175
- B {
176
- C a1: v1, a2: "v2"
177
- # bla
178
- D {
179
- E a1: 5
180
- }
181
- @ anno
182
- |F bla
183
- ))
184
- end
185
-
186
- def assert_context(expected, text)
187
- # remove first and last lines
188
- # these are empty because of the use of %Q
189
- exp_lines = expected.split("\n")[1..-2]
190
- exp_col = exp_lines.last.index("|")
191
- exp_lines.last.sub!("|","")
192
- in_lines = text.split("\n")[1..-2]
193
- in_col = in_lines.last.index("|")
194
- in_lines.last.sub!("|","")
195
- ctx = RText::Frontend::Context.new
196
- lines, out_col = ctx.extract(in_lines, in_col)
197
- assert_equal exp_lines, lines
198
- if exp_col && in_col
199
- assert_equal exp_col, out_col
200
- end
201
- end
202
-
203
- end
204
-
205
-
1
+ $:.unshift File.join(File.dirname(__FILE__),"..","..","lib")
2
+
3
+ require 'minitest/autorun'
4
+ require 'rtext/frontend/context'
5
+
6
+ class ContextTest < MiniTest::Test
7
+
8
+ def test_simple
9
+ assert_context(
10
+ %Q(
11
+ A {
12
+ B {
13
+ |F bla
14
+ ),
15
+ %Q(
16
+ A {
17
+ B {
18
+ C a1: v1, a2: "v2"
19
+ D {
20
+ E a1: 5
21
+ }
22
+ |F bla
23
+ ))
24
+ end
25
+
26
+ def test_child_label
27
+ assert_context(
28
+ %Q(
29
+ A {
30
+ sub:
31
+ B {
32
+ F bla|
33
+ ),
34
+ %Q(
35
+ A {
36
+ sub:
37
+ B {
38
+ C a1: v1, a2: "v2"
39
+ D {
40
+ E a1: 5
41
+ }
42
+ F bla|
43
+ ))
44
+ end
45
+
46
+ def test_child_label_array
47
+ assert_context(
48
+ %Q(
49
+ A {
50
+ sub: [
51
+ B {
52
+ F| bla
53
+ ),
54
+ %Q(
55
+ A {
56
+ sub: [
57
+ B {
58
+ C
59
+ }
60
+ B {
61
+ C a1: v1, a2: "v2"
62
+ D {
63
+ E a1: 5
64
+ }
65
+ F| bla
66
+ ))
67
+ end
68
+
69
+ def test_child_label_array2
70
+ assert_context(
71
+ %Q(
72
+ A {
73
+ B |
74
+ ),
75
+ %Q(
76
+ A {
77
+ sub: [
78
+ ]
79
+ B |
80
+ ))
81
+ end
82
+
83
+ def test_child_label_array3
84
+ assert_context(
85
+ %Q(
86
+ A {
87
+ sub: [
88
+ ] |
89
+ ),
90
+ %Q(
91
+ A {
92
+ sub: [
93
+ ] |
94
+ ))
95
+ end
96
+
97
+
98
+ def test_ignore_child_lables
99
+ assert_context(
100
+ %Q(
101
+ A {
102
+ B {
103
+ F bl|a
104
+ ),
105
+ %Q(
106
+ A {
107
+ B {
108
+ sub:
109
+ C a1: v1, a2: "v2"
110
+ sub2: [
111
+ D {
112
+ E a1: 5
113
+ }
114
+ ]
115
+ F bl|a
116
+ ))
117
+ end
118
+
119
+ def test_linebreak
120
+ assert_context(
121
+ %Q(
122
+ A {
123
+ B {
124
+ C name, a1: v1, a2: "v2"|
125
+ ),
126
+ %Q(
127
+ A {
128
+ B {
129
+ C name,
130
+ a1: v1,
131
+ a2: "v2"|
132
+ ))
133
+ end
134
+
135
+ def test_linebreak_arg_array
136
+ assert_context(
137
+ %Q(
138
+ A {
139
+ B {
140
+ C name, a1: [ v1, v2 ], a2: |5
141
+ ),
142
+ %Q(
143
+ A {
144
+ B {
145
+ C name,
146
+ a1: [
147
+ v1,
148
+ v2
149
+ ],
150
+ a2: |5
151
+ ))
152
+ end
153
+
154
+ def test_linebreak_empty_last_line
155
+ assert_context(
156
+ %Q(
157
+ A {
158
+ B name, |
159
+ ),
160
+ %Q(
161
+ A {
162
+ B name,
163
+ |
164
+ ))
165
+ end
166
+
167
+ def test_linebreak_empty_last_line2
168
+ assert_context(
169
+ %Q(
170
+ A {
171
+ B name, |
172
+ ),
173
+ %Q(
174
+ A {
175
+ B name,
176
+ |
177
+ ))
178
+ end
179
+
180
+ def test_linebreak_empty_lines
181
+ assert_context(
182
+ %Q(
183
+ A {
184
+ B name, a1: |
185
+ ),
186
+ %Q(
187
+ A {
188
+ B name,
189
+
190
+ a1: |
191
+ ))
192
+ end
193
+
194
+ def test_linebreak_first_arg_array
195
+ assert_context(
196
+ %Q(
197
+ A {
198
+ B [ |
199
+ ),
200
+ %Q(
201
+ A {
202
+ B [
203
+ |
204
+ ))
205
+ end
206
+
207
+ def test_linebreak_first_arg_array2
208
+ assert_context(
209
+ %Q(
210
+ A {
211
+ B [ 2, |
212
+ ),
213
+ %Q(
214
+ A {
215
+ B [
216
+ 2,
217
+ |
218
+ ))
219
+ end
220
+
221
+ def test_linebreak_first_arg_array3
222
+ assert_context(
223
+ %Q(
224
+ A {
225
+ B [ 2 ], |
226
+ ),
227
+ %Q(
228
+ A {
229
+ B [
230
+ 2
231
+ ], |
232
+ ))
233
+ end
234
+
235
+ def test_linebreak_backslash
236
+ assert_context(
237
+ %Q(
238
+ A {
239
+ B arg: 1,|
240
+ ),
241
+ %Q(
242
+ A {
243
+ B \\
244
+ arg: 1,|
245
+ ))
246
+ end
247
+
248
+ def test_linebreak_whitespace_after_backslash
249
+ assert_context(
250
+ %Q(
251
+ A {
252
+ B arg: 1,|
253
+ ),
254
+ %Q(
255
+ A {
256
+ B \\
257
+ arg: 1,|
258
+ ))
259
+ end
260
+
261
+ def test_comment_annotation
262
+ assert_context(
263
+ %Q(
264
+ A {
265
+ B {
266
+ |F bla
267
+ ),
268
+ %Q(
269
+ A {
270
+ # bla
271
+ B {
272
+ C a1: v1, a2: "v2"
273
+ # bla
274
+ D {
275
+ E a1: 5
276
+ }
277
+ @ anno
278
+ |F bla
279
+ ))
280
+ end
281
+
282
+ def assert_context(expected, text)
283
+ # remove first and last lines
284
+ # these are empty because of the use of %Q
285
+ exp_lines = expected.split("\n")[1..-2]
286
+ exp_col = exp_lines.last.index("|")
287
+ exp_lines.last.sub!("|","")
288
+ in_lines = text.split("\n")[1..-2]
289
+ in_col = in_lines.last.index("|")
290
+ in_lines.last.sub!("|","")
291
+ ctx = RText::Frontend::Context.new
292
+ lines, out_col = ctx.extract(in_lines, in_col)
293
+ assert_equal exp_lines, lines
294
+ if exp_col && in_col
295
+ assert_equal exp_col, out_col
296
+ end
297
+ end
298
+
299
+ end
300
+
301
+