glark 1.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,891 @@
1
+ #!/usr/bin/ruby -w
2
+ # -*- ruby -*-
3
+
4
+ require 'pathname'
5
+ require 'tempfile'
6
+
7
+ testdir = Pathname.new(__FILE__).expand_path.dirname.to_s
8
+ $:.unshift testdir
9
+
10
+ require 'testcase'
11
+ require 'glark/options'
12
+
13
+ class TC_Options < GlarkTestCase
14
+
15
+ def setup
16
+ # ignore what they have in ENV[HOME]
17
+ ENV['HOME'] = '/this/should/not/exist'
18
+ end
19
+
20
+ DEFAULTS = {
21
+ :after => 0,
22
+ :before => 0,
23
+ :binary_files => "binary",
24
+ :count => false,
25
+ :directory => "read",
26
+ :exclude_matching => false,
27
+ :explain => false,
28
+ :expr => nil,
29
+ :extract_matches => false,
30
+ # :file_highlight => nil,
31
+ :file_names_only => false,
32
+ :filter => true,
33
+ :highlight => "multi",
34
+ :invert_match => false,
35
+ :label => nil,
36
+ :line_number_highlight => nil,
37
+ :local_config_files => false,
38
+ :match_limit => nil,
39
+ :nocase => false,
40
+ :out => $stdout,
41
+ :quiet => false,
42
+ :range_end => nil,
43
+ :range_start => nil,
44
+ :show_break => false,
45
+ :show_file_names => nil,
46
+ :show_line_numbers => true,
47
+ :size_limit => nil,
48
+ :split_as_path => true,
49
+ :text_highlights => GlarkOptions.instance.multi_colors,
50
+ :verbose => nil,
51
+ :whole_lines => false,
52
+ :whole_words => false,
53
+ :with_basename => nil,
54
+ :with_fullname => nil,
55
+ :without_basename => nil,
56
+ :without_fullname => nil,
57
+ :write_null => false,
58
+ }
59
+
60
+ def do_test(args, exp, &blk)
61
+ expected = DEFAULTS.merge(exp)
62
+
63
+ # ignore what they have in ENV[HOME]
64
+ ENV['HOME'] = '/this/should/not/exist'
65
+
66
+ origargs = args.dup
67
+
68
+ gopt = GlarkOptions.instance
69
+ gopt.run(args)
70
+
71
+ expected.sort { |a, b| a[0].to_s <=> b[0].to_s }.each do |opt, expval|
72
+ expstr = "expclass: #{expval.class}; expval: #{expval.inspect}; origargs: #{origargs.inspect}"
73
+ [ gopt.method(opt).call, gopt[opt] ].each do |val|
74
+ if val.kind_of? Array
75
+ assert_equal expval.length, val.length, "option: #{opt}; exp length: #{expval.length}; act length: #{val.length}; " + expstr
76
+ (0 ... expval.length).each do |idx|
77
+ assert_equal expval[idx], val[idx], "option: #{opt}; index: #{idx}; exp: #{expval.inspect}; act: #{val.inspect}; " + expstr
78
+ end
79
+ else
80
+ assert_equal expval, val, "option: #{opt}; exp: #{expval.inspect}; act: #{val.inspect}; " + expstr
81
+ end
82
+ end
83
+ end
84
+
85
+ blk.call(gopt) if blk
86
+
87
+ gopt.reset
88
+ end
89
+
90
+ def test_default_values
91
+ do_test(%w{ foo file1 file2 },
92
+ {
93
+ :expr => RegexpFuncObj.new(%r{foo}, 0)
94
+ })
95
+ end
96
+
97
+ def test_extract_match
98
+ do_test(%w{ --extract-matches foo file1 file2 },
99
+ {
100
+ :extract_matches => true,
101
+ :expr => RegexpFuncObj.new(%r{foo}, 0)
102
+ })
103
+ end
104
+
105
+ def test_extract_match_incomplete
106
+ str = '--extract-matches'
107
+ (5 ... str.length - 1).each do |idx|
108
+ tag = str[0 .. idx]
109
+ do_test([ tag ] | %w{ foo file1 file2 },
110
+ {
111
+ :extract_matches => true,
112
+ :expr => RegexpFuncObj.new(%r{foo}, 0)
113
+ })
114
+ end
115
+ end
116
+
117
+ def test_record_separator
118
+ %w{ -0 -00 -000 }.each do |arg|
119
+ $/ = "\n"
120
+ do_test([ arg ] | %w{ foo file1 file2 } ,
121
+ {
122
+ :expr => RegexpFuncObj.new(%r{foo}, 0)
123
+ }) do |gopt|
124
+ assert_equals "\n\n", $/
125
+ end
126
+ end
127
+ end
128
+
129
+ def test_short_options
130
+ [
131
+ %w{ -rli },
132
+ %w{ -rl -i },
133
+ %w{ -r -li },
134
+ %w{ -r -l -i },
135
+ ].each do |args|
136
+ do_test(args | %w{ foo },
137
+ {
138
+ :directory => "recurse",
139
+ :expr => RegexpFuncObj.new(%r{foo}i, 0),
140
+ :file_names_only => true,
141
+ :nocase => true,
142
+ })
143
+ end
144
+ end
145
+
146
+ def test_context_default
147
+ %w{ -C --context }.each do |ctx|
148
+ args = [ ctx, 'foo' ]
149
+ do_test(args,
150
+ {
151
+ :after => 2,
152
+ :before => 2,
153
+ :expr => RegexpFuncObj.new(%r{foo}, 0),
154
+ })
155
+ end
156
+ end
157
+
158
+ def test_context_specified
159
+ [ 3, 1, 666 ].each do |val|
160
+ vstr = val.to_s
161
+ [
162
+ [ '-C', vstr ],
163
+ [ '--context', vstr ],
164
+ [ '--context=' + vstr, ]
165
+ ].each do |args|
166
+ do_test(args | %w{ foo },
167
+ {
168
+ :after => val,
169
+ :before => val,
170
+ :expr => RegexpFuncObj.new(%r{foo}, 0),
171
+ })
172
+ end
173
+ end
174
+
175
+ vals = (1 .. 10).to_a | (1 .. 16).collect { |x| 2 ** x }
176
+ vals.each do |val|
177
+ args = [ '-' + val.to_s, 'foo' ]
178
+ do_test(args,
179
+ {
180
+ :after => val,
181
+ :before => val,
182
+ :expr => RegexpFuncObj.new(%r{foo}, 0),
183
+ })
184
+ end
185
+ end
186
+
187
+ def test_after_context
188
+ [ 3, 1, 666 ].each do |val|
189
+ vstr = val.to_s
190
+ [
191
+ [ '-A', vstr ],
192
+ [ '--after-context', vstr ],
193
+ [ '--after-context=' + vstr ]
194
+ ].each do |args|
195
+ do_test(args | %w{ foo },
196
+ {
197
+ :after => val,
198
+ :expr => RegexpFuncObj.new(%r{foo}, 0),
199
+ })
200
+ end
201
+ end
202
+ end
203
+
204
+ def test_before_context
205
+ [ 3, 1, 666 ].each do |val|
206
+ vstr = val.to_s
207
+ [
208
+ [ '-B', vstr ],
209
+ [ '--before-context', vstr ],
210
+ [ '--before-context=' + vstr ]
211
+ ].each do |args|
212
+ do_test(args | %w{ foo },
213
+ {
214
+ :before => val,
215
+ :expr => RegexpFuncObj.new(%r{foo}, 0),
216
+ })
217
+ end
218
+ end
219
+ end
220
+
221
+ def test_highlight
222
+ %w{ -u --highlight }.each do |hlopt|
223
+ do_test([ hlopt, 'foo' ],
224
+ {
225
+ :highlight => "multi",
226
+ :expr => RegexpFuncObj.new(%r{foo}, 0),
227
+ })
228
+ end
229
+
230
+ %w{ multi }.each do |val|
231
+ [
232
+ [ '--highlight=' + val ],
233
+ [ '--highlight', val ],
234
+ ].each do |opt|
235
+ do_test(opt | [ 'foo' ],
236
+ {
237
+ :highlight => val,
238
+ :expr => RegexpFuncObj.new(%r{foo}, 0),
239
+ })
240
+ end
241
+ end
242
+
243
+ singlecolor = Text::ANSIHighlighter.make(Text::Highlighter::DEFAULT_COLORS[0])
244
+
245
+ %w{ single }.each do |val|
246
+ [
247
+ [ '--highlight=' + val ],
248
+ [ '--highlight', val ],
249
+ ].each do |opt|
250
+ do_test(opt | [ 'foo' ],
251
+ {
252
+ :highlight => val,
253
+ :text_highlights => [ singlecolor ],
254
+ :expr => RegexpFuncObj.new(%r{foo}, 0),
255
+ })
256
+ end
257
+ end
258
+
259
+ %w{ none }.each do |val|
260
+ do_test([ '--highlight=' + val, 'foo' ],
261
+ {
262
+ :highlight => nil,
263
+ :text_highlights => [],
264
+ :expr => RegexpFuncObj.new(%r{foo}, 0),
265
+ })
266
+ end
267
+ end
268
+
269
+ def test_no_highlight
270
+ %w{ -U --no-highlight }.each do |hlopt|
271
+ do_test([ hlopt, 'foo' ],
272
+ {
273
+ :highlight => nil,
274
+ :text_highlights => [],
275
+ :expr => RegexpFuncObj.new(%r{foo}, 0),
276
+ })
277
+ end
278
+ end
279
+
280
+ def test_verbose
281
+ if origverb = Log.verbose
282
+
283
+ %w{ --verbose --verbosity }.each do |vtag|
284
+ [ nil, 1, 2, 3, 4 ].each do |num|
285
+ vopt = vtag
286
+ if num
287
+ vopt += "=" + num.to_s
288
+ end
289
+ Log.verbose = nil
290
+ do_test([ vopt, 'foo' ],
291
+ {
292
+ :expr => RegexpFuncObj.new(%r{foo}, 0),
293
+ }) do |opts|
294
+ assert_equals true, Log.verbose, "log verbosity"
295
+ end
296
+ end
297
+ end
298
+
299
+ Log.verbose = origverb
300
+ end
301
+ end
302
+
303
+ def test_invert_match
304
+ %w{ -v --invert-match }.each do |vopt|
305
+ do_test([ vopt, 'foo' ],
306
+ {
307
+ :invert_match => true,
308
+ :expr => RegexpFuncObj.new(%r{foo}, 0),
309
+ })
310
+ end
311
+ end
312
+
313
+ def test_ignore_case
314
+ %w{ -i --ignore-case }.each do |opt|
315
+ do_test([ opt, 'foo' ],
316
+ {
317
+ :nocase => true,
318
+ :expr => RegexpFuncObj.new(%r{foo}i, 0),
319
+ })
320
+ end
321
+ end
322
+
323
+ def test_filter
324
+ %w{ --filter }.each do |opt|
325
+ do_test([ opt, 'foo' ],
326
+ {
327
+ :filter => true,
328
+ :expr => RegexpFuncObj.new(%r{foo}, 0),
329
+ })
330
+ end
331
+ end
332
+
333
+ def test_no_filter
334
+ %w{ --no-filter --nofilter }.each do |opt|
335
+ do_test([ opt, 'foo' ],
336
+ {
337
+ :filter => false,
338
+ :expr => RegexpFuncObj.new(%r{foo}, 0),
339
+ })
340
+ end
341
+ end
342
+
343
+ def test_output_type
344
+ %w{ -g --grep }.each do |opt|
345
+ do_test([ opt, 'foo' ],
346
+ {
347
+ :output => "grep",
348
+ :expr => RegexpFuncObj.new(%r{foo}, 0),
349
+ :highlight => false,
350
+ :text_highlights => [],
351
+ :show_line_numbers => false,
352
+ :after => 0,
353
+ :before => 0,
354
+ })
355
+ end
356
+ end
357
+
358
+ def test_line_number
359
+ %w{ -n --line-number }.each do |opt|
360
+ do_test([ opt, 'foo' ],
361
+ {
362
+ :show_line_numbers => true,
363
+ :expr => RegexpFuncObj.new(%r{foo}, 0),
364
+ })
365
+ end
366
+ end
367
+
368
+ def test_no_line_number
369
+ %w{ -N --no-line-number }.each do |opt|
370
+ do_test([ opt, 'foo' ],
371
+ {
372
+ :show_line_numbers => false,
373
+ :expr => RegexpFuncObj.new(%r{foo}, 0),
374
+ })
375
+ end
376
+ end
377
+
378
+ def test_explain
379
+ %w{ --explain }.each do |opt|
380
+ do_test([ opt, 'foo' ],
381
+ {
382
+ :explain => true,
383
+ :expr => RegexpFuncObj.new(%r{foo}, 0),
384
+ })
385
+ end
386
+ end
387
+
388
+ def test_quiet
389
+ %w{ -q -s --quiet --messages }.each do |opt|
390
+ do_test([ opt, 'foo' ],
391
+ {
392
+ :quiet => true,
393
+ :expr => RegexpFuncObj.new(%r{foo}, 0),
394
+ })
395
+ end
396
+ end
397
+
398
+ def test_no_quiet
399
+ %w{ -Q -S --no-quiet --no-messages }.each do |opt|
400
+ do_test([ opt, 'foo' ],
401
+ {
402
+ :quiet => false,
403
+ :expr => RegexpFuncObj.new(%r{foo}, 0),
404
+ })
405
+ end
406
+ end
407
+
408
+ def test_whole_words
409
+ %w{ -w --word --word-regexp }.each do |opt|
410
+ do_test([ opt, 'foo' ],
411
+ {
412
+ :whole_words => true,
413
+ :expr => RegexpFuncObj.new(%r{\bfoo\b}, 0),
414
+ })
415
+ end
416
+ end
417
+
418
+ def test_whole_lines
419
+ %w{ -x --line-regexp }.each do |opt|
420
+ do_test([ opt, 'foo' ],
421
+ {
422
+ :whole_lines => true,
423
+ :expr => RegexpFuncObj.new(%r{^foo$}, 0),
424
+ })
425
+ end
426
+ end
427
+
428
+ def test_files_with_matches
429
+ %w{ -l --files-with-matches }.each do |opt|
430
+ do_test([ opt, 'foo' ],
431
+ {
432
+ :file_names_only => true,
433
+ :invert_match => false,
434
+ :expr => RegexpFuncObj.new(%r{foo}, 0),
435
+ })
436
+ end
437
+ end
438
+
439
+ def test_files_without_matches
440
+ %w{ -L --files-without-match }.each do |opt|
441
+ do_test([ opt, 'foo' ],
442
+ {
443
+ :file_names_only => true,
444
+ :invert_match => true,
445
+ :expr => RegexpFuncObj.new(%r{foo}, 0),
446
+ })
447
+ end
448
+ end
449
+
450
+ def test_count
451
+ %w{ -c --count }.each do |opt|
452
+ do_test([ opt, 'foo' ],
453
+ {
454
+ :count => true,
455
+ :expr => RegexpFuncObj.new(%r{foo}, 0),
456
+ })
457
+ end
458
+ end
459
+
460
+ def test_write_null
461
+ %w{ -Z --null }.each do |opt|
462
+ do_test([ opt, 'foo' ],
463
+ {
464
+ :write_null => true,
465
+ :expr => RegexpFuncObj.new(%r{foo}, 0),
466
+ })
467
+ end
468
+ end
469
+
470
+ def test_exclude_matching
471
+ %w{ -M --exclude-matching }.each do |opt|
472
+ do_test([ opt, 'foo' ],
473
+ {
474
+ :exclude_matching => true,
475
+ :expr => RegexpFuncObj.new(%r{foo}, 0),
476
+ })
477
+ end
478
+ end
479
+
480
+ def test_directory_short
481
+ %w{ read recurse skip }.each do |opt|
482
+ do_test([ '-d', opt, 'foo' ],
483
+ {
484
+ :directory => opt,
485
+ :expr => RegexpFuncObj.new(%r{foo}, 0),
486
+ })
487
+ end
488
+ end
489
+
490
+ def test_recurse
491
+ %w{ -r --recurse }.each do |opt|
492
+ do_test([ opt, 'foo' ],
493
+ {
494
+ :directory => 'recurse',
495
+ :expr => RegexpFuncObj.new(%r{foo}, 0),
496
+ })
497
+ end
498
+ end
499
+
500
+ def test_extract_matches
501
+ %w{ -y --extract-matches }.each do |opt|
502
+ do_test([ opt, 'foo' ],
503
+ {
504
+ :extract_matches => true,
505
+ :expr => RegexpFuncObj.new(%r{foo}, 0),
506
+ })
507
+ end
508
+ end
509
+
510
+ def test_no_split_as_path
511
+ %w{ --no-split-as-path }.each do |opt|
512
+ do_test([ opt, 'foo' ],
513
+ {
514
+ :split_as_path => false,
515
+ :expr => RegexpFuncObj.new(%r{foo}, 0),
516
+ })
517
+ end
518
+ end
519
+
520
+ def do_split_as_path_test(exp, args)
521
+ args.each do |val|
522
+ [
523
+ [ '--split-as-path', val ],
524
+ [ '--split-as-path=' + val ]
525
+ ].each do |opt|
526
+ do_test(opt | [ 'foo' ],
527
+ {
528
+ :split_as_path => exp,
529
+ :expr => RegexpFuncObj.new(%r{foo}, 0),
530
+ })
531
+ end
532
+ end
533
+ end
534
+
535
+ def test_split_as_path
536
+ do_test([ '--split-as-path', 'foo' ],
537
+ {
538
+ :split_as_path => true,
539
+ :expr => RegexpFuncObj.new(%r{foo}, 0),
540
+ })
541
+
542
+ do_split_as_path_test(true, %w{ true on yes })
543
+ do_split_as_path_test(false, %w{ false off no })
544
+ end
545
+
546
+ def test_directory_long
547
+ %w{ read recurse skip }.each do |val|
548
+ [
549
+ [ '--directories=' + val ],
550
+ [ '--directories', val ]
551
+ ].each do |args|
552
+ do_test(args | %w{ foo },
553
+ {
554
+ :directory => val,
555
+ :expr => RegexpFuncObj.new(%r{foo}, 0),
556
+ })
557
+ end
558
+ end
559
+ end
560
+
561
+ def test_no_show_file_names
562
+ %w{ -h --no-filename }.each do |opt|
563
+ do_test([ opt, 'foo' ],
564
+ {
565
+ :show_file_names => false,
566
+ :expr => RegexpFuncObj.new(%r{foo}, 0),
567
+ })
568
+ end
569
+ end
570
+
571
+ def test_show_file_names
572
+ %w{ -H --with-filename }.each do |opt|
573
+ do_test([ opt, 'foo' ],
574
+ {
575
+ :show_file_names => true,
576
+ :expr => RegexpFuncObj.new(%r{foo}, 0),
577
+ })
578
+ end
579
+ end
580
+
581
+ def test_label
582
+ %w{ testing 123 any*char\/acters }.each do |label|
583
+ [
584
+ [ '--label=' + label ],
585
+ [ '--label', label ]
586
+ ].each do |opt|
587
+ do_test(opt | %w{ foo },
588
+ {
589
+ :label => label,
590
+ :expr => RegexpFuncObj.new(%r{foo}, 0),
591
+ })
592
+ end
593
+ end
594
+ end
595
+
596
+ def test_match_limit
597
+ %w{ 1 2 4 20 50 100 2000 30000 }.each do |num|
598
+ [
599
+ [ '-m', num ],
600
+ [ '--match-limit', num ],
601
+ [ '--match-limit=' + num ],
602
+ ].each do |args|
603
+ do_test(args | %w{ foo },
604
+ {
605
+ :match_limit => num.to_i,
606
+ :expr => RegexpFuncObj.new(%r{foo}, 0),
607
+ })
608
+ end
609
+ end
610
+ end
611
+
612
+ def test_with_basename
613
+ %w{ abc123 \w+\S* }.each do |pat|
614
+ %w{ --with-basename --basename --with-name --name }.each do |tag|
615
+ [
616
+ [ tag, pat ],
617
+ [ tag + '=' + pat ]
618
+ ].each do |args|
619
+ do_test(args | %w{ foo },
620
+ {
621
+ :with_basename => Regexp.new(pat),
622
+ :expr => RegexpFuncObj.new(%r{foo}, 0),
623
+ })
624
+ end
625
+ end
626
+ end
627
+ end
628
+
629
+ def test_without_basename
630
+ %w{ abc123 \w+\S* }.each do |pat|
631
+ %w{ --without-basename --without-name }.each do |tag|
632
+ [
633
+ [ tag, pat ],
634
+ [ tag + '=' + pat ]
635
+ ].each do |args|
636
+ do_test(args | %w{ foo },
637
+ {
638
+ :without_basename => Regexp.new(pat),
639
+ :expr => RegexpFuncObj.new(%r{foo}, 0),
640
+ })
641
+ end
642
+ end
643
+ end
644
+ end
645
+
646
+ def test_with_fullname
647
+ %w{ abc123 \w+\S* }.each do |pat|
648
+ %w{ --with-fullname --fullname --with-path --path }.each do |tag|
649
+ [
650
+ [ tag, pat ],
651
+ [ tag + '=' + pat ]
652
+ ].each do |args|
653
+ do_test(args | %w{ foo },
654
+ {
655
+ :with_fullname => Regexp.new(pat),
656
+ :expr => RegexpFuncObj.new(%r{foo}, 0),
657
+ })
658
+ end
659
+ end
660
+ end
661
+ end
662
+
663
+ def test_without_fullname
664
+ %w{ abc123 \w+\S* }.each do |pat|
665
+ %w{ --without-fullname --without-path }.each do |tag|
666
+ [
667
+ [ tag, pat ],
668
+ [ tag + '=' + pat ]
669
+ ].each do |args|
670
+ do_test(args | %w{ foo },
671
+ {
672
+ :without_fullname => Regexp.new(pat),
673
+ :expr => RegexpFuncObj.new(%r{foo}, 0),
674
+ })
675
+ end
676
+ end
677
+ end
678
+ end
679
+
680
+ def test_range_start
681
+ %w{ 5 5% 10 90% }.each do |rg|
682
+ [
683
+ [ '--after=' + rg ],
684
+ [ '--after', rg ]
685
+ ].each do |opt|
686
+ do_test(opt | %w{ foo },
687
+ {
688
+ :range_start => rg,
689
+ :expr => RegexpFuncObj.new(%r{foo}, 0),
690
+ })
691
+ end
692
+ end
693
+ end
694
+
695
+ def test_range_end
696
+ %w{ 5 5% 10 90% }.each do |rg|
697
+ [
698
+ [ '--before=' + rg ],
699
+ [ '--before', rg ],
700
+ ].each do |opt|
701
+ do_test(opt | %w{ foo },
702
+ {
703
+ :range_end => rg,
704
+ :expr => RegexpFuncObj.new(%r{foo}, 0),
705
+ })
706
+ end
707
+ end
708
+ end
709
+
710
+ def test_range_both
711
+ [
712
+ %w{ 5 10 },
713
+ %w{ 7 10% },
714
+ %w{ 90% 95% },
715
+ %w{ 18 81 }
716
+ ].each do |from, to|
717
+ [
718
+ [ '--range=' + from + ',' + to ],
719
+ [ '--range', from + ',' + to ],
720
+ ].each do |opt|
721
+ do_test(opt | %w{ foo },
722
+ {
723
+ :range_start => from,
724
+ :range_end => to,
725
+ :expr => RegexpFuncObj.new(%r{foo}, 0),
726
+ })
727
+ end
728
+ end
729
+ end
730
+
731
+ def test_binary_files
732
+ %w{ binary without-match text }.each do |val|
733
+ [
734
+ [ '--binary-files="' + val + '"' ],
735
+ [ '--binary-files=' + val ],
736
+ [ '--binary-files', val ],
737
+ ].each do |opt|
738
+ do_test(opt | %w{ foo },
739
+ {
740
+ :binary_files => val,
741
+ :expr => RegexpFuncObj.new(%r{foo}, 0),
742
+ })
743
+ end
744
+ end
745
+ end
746
+
747
+ def test_size_limit
748
+ [ 0, 1, 2, 4, 8, 16, 32, 100, 1000, 10000, 100000 ].each do |val|
749
+ [
750
+ [ '--size-limit=' + val.to_s ],
751
+ [ '--size-limit', val.to_s ],
752
+ ].each do |opt|
753
+ do_test(opt | %w{ foo },
754
+ {
755
+ :size_limit => val,
756
+ :expr => RegexpFuncObj.new(%r{foo}, 0),
757
+ })
758
+ end
759
+ end
760
+ end
761
+
762
+ def test_text_highlight
763
+ [ 'red', 'blue', 'cyan', 'bold blue', 'blue bold' ].each do |color|
764
+ [
765
+ [ '-T', color ],
766
+ [ '--text-color="' + color + '"' ],
767
+ [ '--text-color=' + color ],
768
+ ].each do |opt|
769
+ do_test(opt | [ 'foo' ],
770
+ {
771
+ :text_highlights => [ Text::ANSIHighlighter.make(color) ],
772
+ :expr => RegexpFuncObj.new(%r{foo}, 0),
773
+ })
774
+ end
775
+ end
776
+ end
777
+
778
+ # this is no longer supported.
779
+ def xxx_test_text_color_numbered
780
+ defaults = Text::Highlighter::DEFAULT_COLORS.collect { |color| Text::ANSIHighlighter.make(color) }
781
+ [ 'red', 'blue', 'cyan', 'bold blue', 'blue bold' ].each do |color|
782
+ (0 .. 5).each do |idx|
783
+ expected = defaults.dup
784
+ expected[idx] = Text::ANSIHighlighter.make(color)
785
+ [
786
+ [ '--text-color-' + idx.to_s + '="' + color + '"' ],
787
+ [ '--text-color-' + idx.to_s + '=' + color ],
788
+ [ '--text-color-' + idx.to_s, color ],
789
+ ].each do |opt|
790
+ do_test(opt | [ 'foo' ],
791
+ {
792
+ :text_highlights => expected,
793
+ :expr => RegexpFuncObj.new(%r{foo}, 0),
794
+ })
795
+ end
796
+ end
797
+ end
798
+ end
799
+
800
+ def test_file_color
801
+ [ 'red', 'blue', 'cyan', 'bold blue', 'blue bold' ].each do |color|
802
+ [
803
+ [ '-F', color ],
804
+ [ '--file-color', color ],
805
+ [ '--file-color="' + color + '"' ],
806
+ [ '--file-color=' + color ],
807
+ ].each do |opt|
808
+ do_test(opt | [ 'foo' ],
809
+ {
810
+ :file_highlight => Text::ANSIHighlighter.make(color),
811
+ :expr => RegexpFuncObj.new(%r{foo}, 0),
812
+ })
813
+ end
814
+ end
815
+ end
816
+
817
+ def test_file_expression
818
+ res = %w{ foo bar baz \w\s+\d\S\W }
819
+
820
+ t = Tempfile.new("tc_options")
821
+ res.each do |re|
822
+ t.puts re
823
+ end
824
+ t.close
825
+
826
+ pt = Pathname.new(t.path)
827
+
828
+ orexpr = nil
829
+ res.each do |re|
830
+ refo = RegexpFuncObj.new(Regexp.new(re), 0)
831
+ if orexpr
832
+ orexpr.ops << refo
833
+ else
834
+ orexpr = InclusiveOrExpression.new(refo)
835
+ end
836
+ end
837
+
838
+ begin
839
+ [
840
+ [ '-f', t.path ],
841
+ [ '--file="' + t.path + '"' ],
842
+ [ '--file=' + t.path ],
843
+ [ '--file', t.path ],
844
+ ].each do |opt|
845
+ do_test(opt,
846
+ {
847
+ :expr => orexpr,
848
+ })
849
+ end
850
+ ensure
851
+ if pt.exist?
852
+ pt.delete
853
+ end
854
+ end
855
+ end
856
+
857
+ def test_or_expression
858
+ pats = %w{ foo bar }
859
+
860
+ re0, re1 = pats.collect { |pat| RegexpFuncObj.new(Regexp.new(pat), 0) }
861
+
862
+ orexpr = InclusiveOrExpression.new(re0, re1)
863
+
864
+ [
865
+ [ '-o', *pats ],
866
+ ].each do |opt|
867
+ do_test(opt,
868
+ {
869
+ :expr => orexpr,
870
+ })
871
+ end
872
+ end
873
+
874
+ def test_and_expression
875
+ pats = %w{ foo bar }
876
+
877
+ re0, re1 = pats.collect { |pat| RegexpFuncObj.new(Regexp.new(pat), 0) }
878
+
879
+ exp = AndExpression.new(0, re0, re1)
880
+
881
+ [
882
+ [ '-a', 0, *pats ],
883
+ ].each do |opt|
884
+ do_test(opt,
885
+ {
886
+ :expr => exp,
887
+ })
888
+ end
889
+ end
890
+
891
+ end