-
Notifications
You must be signed in to change notification settings - Fork 42
/
block_manipulator_test.rb
361 lines (278 loc) · 8.78 KB
/
block_manipulator_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
require "test_helper"
require "pathname"
require "scaffolding/block_manipulator"
class Scaffolding::BlockManipulatorTest < ActiveSupport::TestCase
def path = Pathname.new("./test/lib/scaffolding/examples/block_manipulator_data.html.erb")
initial_file_contents =
<<~INITIAL
<% test_block do %>
<p>with some content</p>
<% end %>
INITIAL
teardown do
path.write initial_file_contents
end
private def write_source(source)
path.write source
path.readlines
end
test "inserts within a block and after the given location" do
lines = write_source <<~INITIAL
<% test_block do %>
<p>with some content</p>
<% end %>
INITIAL
new_lines = Scaffolding::BlockManipulator.insert("a new string", within: "<% test_block", after: "<p>", lines: lines)
Scaffolding::FileManipulator.write(path, new_lines, strip: false)
expected_result =
<<~RESULT
<% test_block do %>
<p>with some content</p>
a new string
<% end %>
RESULT
assert_equal path.readlines, new_lines
assert_equal path.read, expected_result
end
test "inserts multiple lines within a block using the proper indentation" do
lines = write_source <<~INITIAL
<% test_block do %>
<% end %>
INITIAL
content = ["first", "second", "third"]
new_lines = Scaffolding::BlockManipulator.insert(content, within: "<% test_block do %>", lines: lines)
Scaffolding::FileManipulator.write(path, new_lines, strip: false)
expected_result =
<<~RESULT
<% test_block do %>
first
second
third
<% end %>
RESULT
assert_equal path.readlines, new_lines
assert_equal path.read, expected_result
end
test "inserts multiple lines within a block when using a heredoc" do
lines = write_source <<~INITIAL
<% test_block do %>
<% end %>
INITIAL
content = <<~CONTENT
first
second
third
CONTENT
new_lines = Scaffolding::BlockManipulator.insert(content, within: "<% test_block do %>", lines: lines)
Scaffolding::FileManipulator.write(path, new_lines, strip: false)
expected_result =
<<~RESULT
<% test_block do %>
first
second
third
<% end %>
RESULT
assert_equal path.readlines, new_lines
assert_equal path.read, expected_result
end
test "inserts into an empty block" do
lines = write_source <<~INITIAL
<% outer_block do %>
<% inner_block do %>
<% end %>
<% end %>
INITIAL
new_lines = Scaffolding::BlockManipulator.insert("Some new content", within: "<% inner_block", lines: lines)
Scaffolding::FileManipulator.write(path, new_lines, strip: false)
expected_result =
<<~EXPECTED
<% outer_block do %>
<% inner_block do %>
Some new content
<% end %>
<% end %>
EXPECTED
assert_equal path.readlines, new_lines
assert_equal path.read, expected_result
end
test "inserts content after the given block" do
lines = write_source <<~INITIAL
<% outer_block do %>
<% inner_block do %>
<% end %>
<% end %>
INITIAL
new_lines = Scaffolding::BlockManipulator.insert("Post content", after_block: "<% inner_block", lines: lines)
Scaffolding::FileManipulator.write(path, new_lines, strip: false)
expected_result =
<<~EXPECTED
<% outer_block do %>
<% inner_block do %>
<% end %>
Post content
<% end %>
EXPECTED
assert_equal path.readlines, new_lines
assert_equal path.read, expected_result
end
test "appends a line after the block" do
lines = write_source <<~DATA
<% test_block do %>
<% end %>
DATA
new_lines = Scaffolding::BlockManipulator.insert("This is a new line", after_block: "<% test_block", lines: lines)
Scaffolding::FileManipulator.write(path, new_lines, strip: false)
expected_result =
<<~RESULT
<% test_block do %>
<% end %>
This is a new line
RESULT
assert_equal path.readlines, new_lines
assert_equal path.read, expected_result
end
test "inserts within an if statement" do
lines = write_source <<~INITIAL
<% if a_test %>
<p>with some content</p>
<% end %>
INITIAL
new_lines = Scaffolding::BlockManipulator.insert("a new string", within: "<% if a_test", after: "<p>", lines: lines)
Scaffolding::FileManipulator.write(path, new_lines, strip: false)
expected_result =
<<~RESULT
<% if a_test %>
<p>with some content</p>
a new string
<% end %>
RESULT
assert_equal path.readlines, new_lines
assert_equal path.read, expected_result
end
test "inserts a new block then adds a line to it" do
lines = write_source <<~INITIAL
<% outer_block do %>
<% inner_block do %>
<% end %>
<% end %>
INITIAL
new_lines = Scaffolding::BlockManipulator.insert_block(["<% new_block do %>", "<% end %>"], after_block: "<% inner_block", lines: lines)
new_lines = Scaffolding::BlockManipulator.insert("an inner line", within: "<% new_block", lines: new_lines)
Scaffolding::FileManipulator.write(path, new_lines, strip: false)
expected_result =
<<~EXPECTED
<% outer_block do %>
<% inner_block do %>
<% end %>
<% new_block do %>
an inner line
<% end %>
<% end %>
EXPECTED
assert_equal path.readlines, new_lines
assert_equal path.read, expected_result
end
test "wraps a block with a new block" do
lines = write_source <<~DATA
<% test_block do %>
<% end %>
DATA
new_lines = Scaffolding::BlockManipulator.wrap_block(starting: "<% test_block", with: ["<% outer_block do %>", "<% end %>"], lines: lines)
Scaffolding::FileManipulator.write(path, new_lines, strip: false)
expected_result =
<<~RESULT
<% outer_block do %>
<% test_block do %>
<% end %>
<% end %>
RESULT
assert_equal path.readlines, new_lines
assert_equal path.read, expected_result
end
test "wraps a nested block" do
lines = write_source <<~INITIAL
<% test_block do %>
<% inner_block do %>
<% end %>
<% end %>
INITIAL
new_lines = Scaffolding::BlockManipulator.wrap_block(starting: "<% inner_block do", with: ["<% wrapping_block do %>", "<% end %>"], lines: lines)
Scaffolding::FileManipulator.write(path, new_lines, strip: false)
expected_result =
<<~RESULT
<% test_block do %>
<% wrapping_block do %>
<% inner_block do %>
<% end %>
<% end %>
<% end %>
RESULT
assert_equal path.readlines, new_lines
assert_equal path.read, expected_result
end
test "unwraps a nested block" do
lines = write_source <<~INITIAL
<% block_to_remove do %>
<% block_to_unwrap do %>
<% end %>
<% end %>
INITIAL
new_lines = Scaffolding::BlockManipulator.unwrap_block(lines: lines, block_start: "block_to_unwrap")
Scaffolding::FileManipulator.write(path, new_lines, strip: false)
expected_result =
<<~RESULT
<% block_to_unwrap do %>
<% end %>
RESULT
assert_equal path.readlines, new_lines
assert_equal path.read, expected_result
end
# Using an Array instead of a Heredoc to get the test the proper spacing.
test "shifts a block's contents to the left" do
source = [
"block_with_contents do\n",
" puts 'contents'\n",
"end\n"
]
lines = write_source source.join
new_lines = Scaffolding::BlockManipulator.shift_block(
lines: lines,
block_start: source.first,
shift_contents_only: true
)
Scaffolding::FileManipulator.write(path, new_lines, strip: false)
expected_result =
[
"block_with_contents do\n",
" puts 'contents'\n",
"end\n"
]
assert_equal path.readlines, new_lines
assert_equal path.read, expected_result.join
end
# Using an Array instead of a Heredoc to get the test the proper spacing.
test "shifts a block's contents to the right" do
source = [
"block_with_contents do\n",
"puts 'contents'\n",
"end\n"
]
lines = write_source source.join
new_lines = Scaffolding::BlockManipulator.shift_block(
lines: lines,
direction: :right,
block_start: source.first,
shift_contents_only: true
)
Scaffolding::FileManipulator.write(path, new_lines, strip: false)
expected_result =
[
"block_with_contents do\n",
" puts 'contents'\n",
"end\n"
]
assert_equal path.readlines, new_lines
assert_equal path.read, expected_result.join
end
end