rfs 0.2 → 0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,613 +0,0 @@
1
- require '../lib/test/test_helper'
2
-
3
- class TestBasics < BaseTest
4
- def test_list
5
- r :list, :rename_replace, 5, :test_r_nofilter => true do |h|
6
- if ['.', '..'].include? @file
7
- assert_equal @file + '/', h[:result]
8
- else
9
- assert_equal @file, h[:result]
10
- end
11
- end
12
- end
13
-
14
- def test_wrong_iterations
15
- assert_raise(RuntimeError) {
16
- r :list, :rename_replace, 1000, :test_r_count => true do |h|
17
- if ['.', '..'].include? @file
18
- assert_equal @file + '/', h[:result]
19
- else
20
- assert_equal @file, h[:result]
21
- end
22
- end
23
- }
24
- end
25
-
26
- def test_bad_action
27
- assert_raise(ActionError) {
28
- r :no, :rename_replace
29
- }
30
- end
31
-
32
- def test_bad_method
33
- assert_raise(NameError) {
34
- r :preview, :foo_bar
35
- }
36
- end
37
-
38
- def test_missing_option
39
- assert_raise(MissingOptionError) {
40
- r :preview, :rename_replace
41
- }
42
- end
43
- def test_missing_option1
44
- # this doesn't raise MissingOptionError because the error is caught and given as result feedback
45
- r :preview, :rename_replace, 3, :search => /file/ do |h|
46
- assert_match(/MissingOptionError/, h[:result])
47
- end
48
- end
49
- def test_missing_option2
50
- assert_raise(MissingOptionError) {
51
- r :preview, :rename_replace, nil, :replace => 'good'
52
- }
53
- end
54
-
55
- def test_bad_search
56
- assert_raise(BadOptionValueError) {
57
- r :preview, :rename_replace, nil, :search => 3
58
- }
59
- end
60
- def test_bad_search1
61
- # this doesn't raise BadOptionValueError because the error is caught and given as result feedback
62
- r :preview, :rename_replace, 3, :search => /file/, :replace => /aeu/ do |h|
63
- assert_match(/BadOptionValueError/, h[:result])
64
- end
65
- end
66
- end
67
-
68
-
69
- class TestReplace < BaseTest
70
- def rr(expected_yields = nil, options = {}, &b)
71
- r :preview, :rename_replace, expected_yields, options, &b
72
- end
73
-
74
- def test_replace
75
- rr 0, :search => (/none/)
76
- rr 1, :search => (/1/), :replace => '9' do |h|
77
- assert_equal 'file9', h[:result]
78
- end
79
- end
80
-
81
- def test_replace_commit
82
- r :commit, :rename_replace, 3, :search => (/file/), :replace => 'boob' do |h|
83
- assert_match(/boob[1-3]/, h[:result])
84
- end
85
- assert_equal [["dir/file1", "dir/boob1"],
86
- ["dir/file2", "dir/boob2"],
87
- ["dir/file3", "dir/boob3"]], @r.renames
88
- end
89
-
90
- def test_replace_cap
91
- rr 3, :search => (/(f)ile/), :replace => 'sm' do |h|
92
- assert_match(/smile[1-3]/, h[:result])
93
- end
94
- end
95
-
96
- def test_replace_caps
97
- rr 3, :search => (/((f)(i)(l)(e)(\d))/),
98
- :replace => '->%6%5%4%3%2<-' do |h|
99
- assert_match(/->[1-3]elif<-/, h[:result])
100
- end
101
- end
102
-
103
- def test_replace_other_cap
104
- rr 3, :search => (/((f)(i)(l)(e)(\d))/),
105
- :replace => '%6%5%4%3%2',
106
- :capture_num => 2 do |h|
107
- assert_match(/[1-3]elifile[1-3]/, h[:result])
108
- end
109
- end
110
- end
111
-
112
-
113
- class TestFilter < BaseTest
114
- def rr(expected_yields = nil, options = {}, &b)
115
- r :preview, :rename_replace, expected_yields, options, &b
116
- end
117
-
118
- def test_filter
119
- rr 2, :search => (/(.*)\d/), :replace => 'new', :filter => Filter.new(/2/) do |h|
120
- assert_match(/new[13]/, h[:newfn])
121
- end
122
- end
123
-
124
- def test_filter2
125
- rr(5, :search => (/(.*)\d/), :replace => 'new', :filter => Filter.new(/2/),
126
- :verbose => true) do |h|
127
- if h[:newfn]
128
- assert_match(/new[13]/, h[:newfn])
129
- else
130
- assert_match(/filtered: (\.\.?|file2)/, h[:result])
131
- end
132
- end
133
- end
134
-
135
- def test_filter3
136
- rr(5, :search => (/(.*)\d/), :replace => 'new', :filter => FilterNonMatches.new(/2/),
137
- :verbose => true) do |h|
138
- if h[:newfn]
139
- assert_equal 'new2' , h[:newfn]
140
- else
141
- assert_match(/filtered: (\.\.?|file[13])/, h[:result])
142
- end
143
- end
144
- end
145
- end
146
-
147
-
148
- class TestConfirm < BaseTest
149
- def rr(expected_yields = nil, options = {}, &b)
150
- r :commit, :rename_replace, expected_yields, options, &b
151
- end
152
-
153
- def test_confirm_yes
154
- confirms = 0
155
- rr 3, :confirm => (proc {|path, oldfn, newfn, full_name, exists, default|
156
- confirms += 1
157
- assert_equal 'dir', path
158
- assert_match(/file[1-3]/, oldfn)
159
- assert_match(/new[1-3]/, newfn)
160
- assert_equal false, full_name
161
- assert_equal((oldfn[-1] == ?1) ? :yes : :yes, default)
162
- :yes
163
- }),
164
- :search => (/file/), :replace => 'new' do |h|
165
- assert_match(/new[123]/, h[:newfn])
166
- assert_equal :ok, h[:result_type]
167
- assert_equal h[:newfn], h[:result]
168
- assert_equal h[:confirm], :yes
169
- end
170
- assert_equal 3, confirms
171
- end
172
-
173
- def test_confirm_all
174
- confirms = 0
175
- rr 3, :confirm => (proc {|path, oldfn, newfn, full_name, exists, default|
176
- confirms += 1
177
- assert_equal :yes, default
178
- :all
179
- }),
180
- :search => (/file/), :replace => 'new' do |h|
181
- assert_match(/new[123]/, h[:newfn])
182
- assert_equal :ok, h[:result_type]
183
- assert_equal h[:newfn], h[:result]
184
- assert_equal h[:confirm], :all
185
- end
186
- assert_equal 1, confirms
187
- end
188
-
189
- def test_confirm_no
190
- confirms = 0
191
- rr 3, :confirm => (proc {|path, oldfn, newfn, full_name, exists, default|
192
- confirms += 1
193
- assert_equal((oldfn[-1] == ?1) ? :yes : :no, default)
194
- :no
195
- }),
196
- :search => (/file/), :replace => 'new' do |h|
197
- assert_match(/new[123]/, h[:newfn])
198
- assert_equal :skipped, h[:result_type]
199
- assert_equal h[:oldfn], h[:result]
200
- assert_equal h[:confirm], :no
201
- end
202
- assert_equal 3, confirms
203
- end
204
-
205
- def test_confirm_cancel
206
- confirms = 0
207
- rr 0, :confirm => (proc {|path, oldfn, newfn, full_name, exists, default|
208
- confirms += 1
209
- assert_equal :yes, default
210
- :cancel
211
- }),
212
- :search => (/file/), :replace => 'new' do |h|
213
- fail
214
- end
215
- assert_equal 1, confirms
216
- end
217
-
218
- def test_confirm_exists
219
- raise NotImplementedError.new
220
- end
221
- end
222
-
223
-
224
- class TestFilesTxt < BaseTest
225
- def rr(ey, o, &b)
226
- r :preview, :rename_name_source, ey, o, &b
227
- end
228
-
229
- def t(s, items, results)
230
- rr 3, :search => s,
231
- :source => NameStreamSource.new(StringIO.new(items.join("\n"))) do |h|
232
- assert_equal results.read1next, h[:newfn]
233
- end
234
- end
235
-
236
- def test_nocap
237
- items = %w{ the_first the_second the_third }
238
- results = items.to_cursor
239
- t(/.*/, items, results)
240
- end
241
-
242
- def test_cap
243
- items = %w{ the_first the_second the_third }
244
- results = %w{ filethe_first1 filethe_second2 filethe_third3 }.to_cursor
245
- t(/file()/, items, results)
246
- end
247
-
248
- def test_not_enough
249
- items = %w{ the_first the_second }
250
- results = ['filethe_first1', 'filethe_second2', 'file--no data--3'].to_cursor
251
- assert_raise(EOFError) {
252
- t(/file()/, items, results)
253
- }
254
- end
255
-
256
- def test_too_many
257
- items = %w{ the_first the_second the_third the_extra }
258
- results = %w{ filethe_first1 filethe_second2 filethe_third3 nevergethere }.to_cursor
259
- t(/file()/, items, results)
260
- end
261
-
262
- def test_real_file
263
- raise NotImplementedError.new
264
- end
265
-
266
- def test_real_file_recursive
267
- raise NotImplementedError.new
268
- end
269
- end
270
-
271
-
272
- class TestAdd < BaseTest
273
- def rr(ey = nil, o = {}, &b)
274
- r :preview, :rename_add, ey, o, &b
275
- end
276
-
277
- def test_add
278
- rr 3, :search => (/\d/), :add => 5, :action => :commit do |h|
279
- assert_equal h[:oldfn].succ.succ.succ.succ.succ, h[:newfn]
280
- end
281
- #make sure it went in reverse
282
- assert_equal [['dir/file3', 'dir/file8'],
283
- ['dir/file2', 'dir/file7'],
284
- ['dir/file1', 'dir/file6']], @r.renames
285
- end
286
-
287
- def test_add_neg
288
- c = %w{ file-2 file-1 file0 }.to_cursor
289
- rr 3, :search => (/\d/), :add => -3, :action => :commit do |h|
290
- assert_equal c.read1next, h[:newfn]
291
- end
292
- #make sure it went in reverse
293
- assert_equal [['dir/file1', 'dir/file-2'],
294
- ['dir/file2', 'dir/file-1'],
295
- ['dir/file3', 'dir/file0']], @r.renames
296
- end
297
-
298
- def test_add_cap
299
- rr 3, :search => (/(\d)/), :add => 5 do |h|
300
- assert_equal h[:oldfn].succ.succ.succ.succ.succ, h[:newfn]
301
- end
302
- end
303
-
304
- def test_missing_add
305
- assert_raise MissingOptionError do
306
- rr(3, :search => (/(\d)/)) { |h| fail "shouldn't get here" }
307
- end
308
- assert_raise BadOptionValueError do
309
- rr(3, :search => (/(\d)/), :add => 'something') { |h| fail "shouldn't get here" }
310
- end
311
- end
312
- end
313
-
314
-
315
- class TestCount < BaseTest
316
- def test_count
317
- count = 0
318
- r :preview, :rename_count, 3, :search => (/()./), :count_start => 1 do |h|
319
- count += 1
320
- assert_equal "#{count}#{h[:oldfn]}", h[:newfn]
321
- end
322
- end
323
-
324
- def test_count_string
325
- count = "b.9 "
326
- r :preview, :rename_count, 3, :search => (/()./), :count_start => count do |h|
327
- assert_equal "#{count}#{h[:oldfn]}", h[:newfn]
328
- count.succ!
329
- end
330
- end
331
- end
332
-
333
-
334
- class TestFullName < BaseTest
335
- def rr(ey = nil, o = {}, &b)
336
- r :preview, :rename_from_full_name, ey, o, &b
337
- end
338
-
339
- def test_no_cap
340
- rr 3, :search => (/file/), :pattern => (/dir/) do |h|
341
- assert_match(/dir[1-3]/, h[:newfn], h[:result])
342
- end
343
- end
344
-
345
- def test_cap
346
- rr 3, :search => (/(file)/), :pattern => (/(dir)/) do |h|
347
- assert_match(/dir[1-3]/, h[:newfn])
348
- end
349
- end
350
- end
351
-
352
-
353
- class TestMoveUp < BaseTest
354
- def rr(ey = nil, o = {}, &b)
355
- r :preview, :rename_move_up, ey, o, &b
356
- end
357
-
358
- def test_match
359
- rr 3, :search => (/./) do |h|
360
- assert_match(%r{^\./file[1-3]$}, h[:newfn], h[:result])
361
- end
362
- end
363
-
364
- def test_no_match
365
- rr 0, :search => (/nothing/)
366
- end
367
- end
368
-
369
-
370
- class TestRoman < BaseTest
371
- def rr(ey, o, &b)
372
- r :preview, :rename_roman, ey, o, &b
373
- end
374
-
375
- def test_to_roman
376
- count = 0
377
- rr 3, :search => (/(\d)/) do |h|
378
- count += 1
379
- rn = 'file' + case count
380
- when 1; 'I'
381
- when 2; 'II'
382
- when 3; 'III'
383
- end
384
- assert_equal rn, h[:newfn]
385
- end
386
- end
387
-
388
- def test_from_roman
389
- rr 1, :search => (/of (roman)/i), :file_provider =>
390
- TestFileProvider.new(['glory/great war of mcmxiv']) do |h|
391
- assert_equal 'great war of 1914', h[:result]
392
- end
393
- end
394
- end
395
-
396
-
397
- class TestMarkChange < BaseTest
398
- def rr(ey, o, &b)
399
- r :preview, :rename_mark_change, ey, o, &b
400
- end
401
-
402
- def setup
403
- super
404
- @changes = %w{ SECTION1.1 SECTION2.1
405
- SECTION3.5 SECTION4.1 }.to_cursor
406
- new_files(%w{ section1.1
407
- section1.2 section1.3
408
- section2.1 section3.5 hello section3.6
409
- section4.1 section4.2 })
410
- end
411
-
412
- def test_changes
413
- rr 4, :search => (/section(\d)\./), :replace_pattern => (/section/),
414
- :replace_text => 'SECTION' do |h|
415
- if h[:newfn]
416
- assert_equal @changes.read1next, h[:newfn]
417
- end
418
- end
419
- end
420
-
421
- def test_changes_rcap
422
- rr 4, :search => (/section(\d)\./), :replace_pattern => (/(\w*)\d\./),
423
- :replace_text => 'SECTION' do |h|
424
- if h[:newfn]
425
- assert_equal @changes.read1next, h[:newfn]
426
- end
427
- end
428
- end
429
-
430
- def test_changes_rcap_other
431
- rr 4, :search => (/section(\d)\./), :replace_pattern => (/()()()(\w*)\d\./),
432
- :replace_text => 'SECTION',
433
- :capture_num => 4 do |h|
434
- if h[:newfn]
435
- assert_equal @changes.read1next, h[:newfn]
436
- end
437
- end
438
- end
439
-
440
- def test_changes_no_cap
441
- rr 4, :search => (/\d/), :replace_pattern => (/section/),
442
- :replace_text => 'SECTION' do |h|
443
- if h[:newfn]
444
- assert_equal @changes.read1next, h[:newfn]
445
- end
446
- end
447
- end
448
- end
449
-
450
-
451
- class TestTapeNumbers < BaseTest
452
- def t(r)
453
- results = %w{ tape1 tape2 tape3 tape4}.to_cursor
454
- new_files(%w{ tape1A tape1B tape2a tape2b })
455
- r(:preview, :rename_tape_numbers, 4, :search => r) do |h|
456
- assert_equal results.read1next, h[:newfn]
457
- end
458
- end
459
-
460
- def test_cap
461
- t(/tape(..)/)
462
- end
463
-
464
- def test_no_cap
465
- t(/\d./)
466
- end
467
- end
468
-
469
-
470
- class TestCapitalize < BaseTest
471
- def rr(ey, o, &b)
472
- new_files(
473
- ['this sentence should be capitalized',
474
- 'here is another one. the best is to see what will happen! is it good?',
475
- 'how many more do i need here? a few.',
476
- "i'll have to get this done soon, it may get boring..."])
477
- r :preview, :rename_capitalize, ey, o, &b
478
- end
479
-
480
- def test_capitalize
481
- changes = ['This Sentence Should Be Capitalized',
482
- 'Here is Another One. The Best is to See What Will Happen! Is it Good?',
483
- 'How Many More Do I Need Here? A Few.',
484
- "I'll Have to Get This Done Soon, it May Get Boring..."].to_cursor
485
- rr 4, :search => (/.*/) do |h|
486
- assert_equal changes.read1next, h[:newfn]
487
- end
488
- end
489
- end
490
-
491
-
492
- class TestFill < BaseTest
493
- def t(re, f, res = nil)
494
- new_files(%w{ first1.1 section1.2 section1.3
495
- second2.1 third3.5 hello section3.6
496
- fourth4.1 section4.2 })
497
- results = res || (%w{ first1.1 first1.2 first1.3
498
- second2.1 third3.5 third3.6
499
- fourth4.1 fourth4.2 }).to_cursor
500
- r :preview, :rename_fill, 9, :search => re, :fill => f do |h|
501
- if h[:newfn]
502
- assert_equal results.read1next, h[:newfn]
503
- elsif h[:result] == '* No capture to fill * hello'
504
- # ok...
505
- else
506
- assert_equal 'source: ' + results.read1next, h[:result]
507
- end
508
- end
509
- end
510
-
511
- def test_cap_nocap
512
- t(/(.*?)\d/, /section/)
513
- end
514
- def test_nocap_nocap
515
- t(/first|second|third|fourth/, /section/)
516
- end
517
- def test_cap_cap
518
- t(/(.*?)\d/, /(section)/)
519
- end
520
- def test_cap_cap2
521
- t(/(.*?)\d/, /section()/, %w{ first1.1 sectionfirst1.2 sectionfirst1.3
522
- second2.1 third3.5 sectionthird3.6
523
- fourth4.1 sectionfourth4.2 }.to_cursor)
524
- end
525
- end
526
-
527
-
528
- class TestDelete < BaseTest
529
- def teardown
530
- super
531
- unless File.exists? 'dir'
532
- Dir.mkdir 'dir'
533
- end
534
- %w{file1 file2 file3}.each do |f|
535
- fn = File.join('dir', f)
536
- unless File.exists? fn
537
- File.open(fn, 'w') {|f| f.puts fn }
538
- end
539
- end
540
- end
541
-
542
- def rr(expected_yields = nil, options = {}, &b)
543
- r :preview, :rename_remove, expected_yields, options, &b
544
- end
545
-
546
- def test_delete_file
547
- rr 1, :search => (/^file2$/), :action => :commit do |h|
548
- assert_equal [["dir/file2", :deleted]], @r.renames
549
- assert_equal :ok, h[:result_type]
550
- assert_equal '<<file2>>', h[:result]
551
- assert_equal 'dir', h[:result_path]
552
- assert_equal 'file2', h[:result_file]
553
- end
554
- end
555
-
556
- def test_delete_dir_not_empty_sim
557
- new_files(%w{ . .. dir })
558
- rr 1, :search => (/^dir$/), :action => :commit do |h|
559
- assert_equal [["./dir", :deleted]], @r.renames
560
- assert_equal :ok, h[:result_type]
561
- assert_equal '<<dir>>', h[:result]
562
- assert_equal '.', h[:result_path]
563
- assert_equal 'dir', h[:result_file]
564
- end
565
- end
566
-
567
- def test_delete_dir_not_empty_real
568
- new_files(%w{ . .. dir })
569
- class << @r
570
- alias actual_delete actual_delete_orig
571
- end
572
- rr 1, :search => (/^dir$/), :action => :commit do |h|
573
- assert_equal :fileerror, h[:result_type]
574
- assert_equal "Errno::ENOTEMPTY: Directory not empty - ./dir", h[:result]
575
- assert_equal '.', h[:result_path]
576
- assert_equal 'dir', h[:result_file]
577
- end
578
- end
579
-
580
- def test_delete_dir_not_empty_real
581
- new_files(%w{ . .. dir })
582
- class << @r
583
- alias actual_delete actual_delete_orig
584
- end
585
- rr 1, :search => (/^dir$/), :action => :commit do |h|
586
- assert_equal :fileerror, h[:result_type]
587
- assert_equal "Errno::ENOTEMPTY: Directory not empty - ./dir", h[:result]
588
- assert_equal '.', h[:result_path]
589
- assert_equal 'dir', h[:result_file]
590
- end
591
- end
592
-
593
- def test_delete_recursive
594
- @files = Provider::File::Recursive.new(['.'])
595
- add_file_events
596
- class << @r
597
- #alias actual_delete actual_delete_orig
598
- end
599
- c = %w{ ./dir/file1 ./dir/file2 ./dir/file3 ./dir }.to_cursor
600
- rr 4, :search => (/^(dir|file[1-3])$/), :action => :commit do |h|
601
- assert_equal :ok, h[:result_type]
602
- p, f = File.split c.read1next
603
- assert_equal p, h[:result_path]
604
- assert_equal f, h[:result_file]
605
- end
606
- c.pos = 0
607
- @r.renames.each do |a|
608
- fn, r = a
609
- assert_equal c.read1next, fn
610
- assert_equal :deleted, r
611
- end
612
- end
613
- end