warning 1.2.1 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: aed045b84ec748f7558ea24268aef892d0efcd7d0f0f51cca7b5cf5f95d1aae7
4
- data.tar.gz: ad651f6e4f47d4b6064f7863d145d2747fab8477a27a5234e2e3336d85d1ae17
3
+ metadata.gz: 0ea693f6c3250c25b7ba2859e7c8ea5d945e8c0560aa3c564fda63da2d52e567
4
+ data.tar.gz: 3417d55cd4807d454d37afc6360864d9d87b29874b8e87d8082b413d209a4b4e
5
5
  SHA512:
6
- metadata.gz: ead742a4b97033e1d1b6ec6460039d7f2138ba8586709a2819303ad0d6791daa26821ee7fd35a9701b2bf7d4ae01f47ef485781331ca77f116ef92d1ec06dada
7
- data.tar.gz: 935a429f2a967a57e67bf44739e0583abf186fe3baeaee0ce61532e9083844971ba956ca55831f18092d80652f7090d30f0d1da06758186fccebb98b0f78b18a
6
+ metadata.gz: 379377da9ef4d3ac871eb4bdbf08dcc245ccb94d7f987ef04f3f60944f4e1d0784aeee60bb560cbd4d744b92e675803fcd6955c354553649bd72e8d0e3dda1a2
7
+ data.tar.gz: 691e2c43099ddad0631752b66d7c401025a59a1b81de0fc1eca92627e5efd9b6d561b571b434526a325cab2f30bc9f4c7d2f625d62ec303905b19115ecf85148
data/CHANGELOG CHANGED
@@ -1,3 +1,9 @@
1
+ === 1.3.0 (2022-07-14)
2
+
3
+ * Allow Warning.clear to take a block, and restore current state after block (splattael) (#18, #20)
4
+
5
+ * Raise ArgumentError if Warning.process is passed non-String as first argument (splattael) (#17, #19)
6
+
1
7
  === 1.2.1 (2021-10-04)
2
8
 
3
9
  * Recognize additional void context warnings (kachick) (#13)
data/MIT-LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2016-2020 Jeremy Evans
1
+ Copyright (c) 2016-2022 Jeremy Evans
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  of this software and associated documentation files (the "Software"), to
data/lib/warning.rb CHANGED
@@ -24,19 +24,52 @@ module Warning
24
24
 
25
25
  # Map of action symbols to procs that return the symbol
26
26
  ACTION_PROC_MAP = {
27
+ raise: proc{|_| :raise},
27
28
  default: proc{|_| :default},
28
29
  backtrace: proc{|_| :backtrace},
29
- raise: proc{|_| :raise},
30
30
  }
31
31
  private_constant :ACTION_PROC_MAP
32
32
 
33
33
  # Clear all current ignored warnings, warning processors, and duplicate check cache.
34
34
  # Also disables deduplicating warnings if that is currently enabled.
35
+ #
36
+ # If a block is passed, the previous values are restored after the block exits.
37
+ #
38
+ # Examples:
39
+ #
40
+ # # Clear warning state
41
+ # Warning.clear
42
+ #
43
+ # Warning.clear do
44
+ # # Clear warning state inside the block
45
+ # ...
46
+ # end
47
+ # # Previous warning state restored when block exists
35
48
  def clear
36
- synchronize do
37
- @ignore.clear
38
- @process.clear
39
- @dedup = false
49
+ if block_given?
50
+ ignore = process = dedup = nil
51
+ synchronize do
52
+ ignore = @ignore.dup
53
+ process = @process.dup
54
+ dedup = @dedup.dup
55
+ end
56
+
57
+ begin
58
+ clear
59
+ yield
60
+ ensure
61
+ synchronize do
62
+ @ignore = ignore
63
+ @process = process
64
+ @dedup = dedup
65
+ end
66
+ end
67
+ else
68
+ synchronize do
69
+ @ignore.clear
70
+ @process.clear
71
+ @dedup = false
72
+ end
40
73
  end
41
74
  end
42
75
 
@@ -144,6 +177,10 @@ module Warning
144
177
  #
145
178
  # Warning.process(__FILE__, :missing_ivar=>:backtrace, :keyword_separation=>:raise)
146
179
  def process(path='', actions=nil, &block)
180
+ unless path.is_a?(String)
181
+ raise ArgumentError, "path must be a String (given an instance of #{path.class})"
182
+ end
183
+
147
184
  if block
148
185
  if actions
149
186
  raise ArgumentError, "cannot pass both actions and block to Warning.process"
@@ -173,14 +210,16 @@ module Warning
173
210
  if RUBY_VERSION >= '3.0'
174
211
  method_args = ', category: nil'
175
212
  super_ = "category ? super : super(str)"
213
+ # :nocov:
176
214
  else
177
215
  super_ = "super"
216
+ # :nocov:
178
217
  end
179
218
 
180
219
  class_eval(<<-END, __FILE__, __LINE__+1)
181
220
  def warn(str#{method_args})
182
221
  synchronize{@ignore.dup}.each do |path, regexp|
183
- if str.start_with?(path) && str =~ regexp
222
+ if str.start_with?(path) && regexp.match?(str)
184
223
  return
185
224
  end
186
225
  end
@@ -198,7 +237,7 @@ module Warning
198
237
  if str.start_with?(path)
199
238
  if block.is_a?(Hash)
200
239
  block.each do |regexp, blk|
201
- if str =~ regexp
240
+ if regexp.match?(str)
202
241
  throw :action, blk.call(str)
203
242
  end
204
243
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: warning
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Evans
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-10-04 00:00:00.000000000 Z
11
+ date: 2022-07-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest-global_expectations
@@ -41,11 +41,7 @@ files:
41
41
  - CHANGELOG
42
42
  - MIT-LICENSE
43
43
  - README.rdoc
44
- - Rakefile
45
44
  - lib/warning.rb
46
- - test/fixtures/mismatched_indentations.rb
47
- - test/test_freeze_warning.rb
48
- - test/test_warning.rb
49
45
  homepage: https://github.com/jeremyevans/ruby-warning
50
46
  licenses:
51
47
  - MIT
@@ -76,7 +72,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
76
72
  - !ruby/object:Gem::Version
77
73
  version: '0'
78
74
  requirements: []
79
- rubygems_version: 3.2.22
75
+ rubygems_version: 3.3.7
80
76
  signing_key:
81
77
  specification_version: 4
82
78
  summary: Add custom processing for warnings
data/Rakefile DELETED
@@ -1,47 +0,0 @@
1
- require "rake"
2
- require "rake/clean"
3
- require 'rake/testtask'
4
- require "rdoc/task"
5
-
6
- CLEAN.include ["warning-*.gem", "rdoc"]
7
-
8
- desc "Build warning gem"
9
- task :package=>[:clean] do |p|
10
- sh %{#{FileUtils::RUBY} -S gem build warning.gemspec}
11
- end
12
-
13
- ### Specs
14
-
15
- desc "Run test"
16
- Rake::TestTask.new do |t|
17
- t.libs.push "lib"
18
- t.test_files = FileList['test/test_warning.rb']
19
- t.verbose = true
20
- end
21
-
22
- desc "Run test"
23
- Rake::TestTask.new(:test_freeze) do |t|
24
- t.libs.push "lib"
25
- t.test_files = FileList['test/test_freeze_warning.rb']
26
- t.verbose = true
27
- end
28
-
29
- desc "Run all tests"
30
- task :default=>[:test, :test_freeze]
31
-
32
- ### RDoc
33
-
34
- RDOC_OPTS = ['--main', 'README.rdoc', "--quiet", "--line-numbers", "--inline-source", '--title', 'ruby-warning: Add custom processing for warnings']
35
-
36
- begin
37
- gem 'hanna-nouveau'
38
- RDOC_OPTS.concat(['-f', 'hanna'])
39
- rescue Gem::LoadError
40
- end
41
-
42
-
43
- RDoc::Task.new do |rdoc|
44
- rdoc.rdoc_dir = "rdoc"
45
- rdoc.options += RDOC_OPTS
46
- rdoc.rdoc_files.add %w"README.rdoc CHANGELOG MIT-LICENSE lib/**/*.rb"
47
- end
@@ -1,4 +0,0 @@
1
- # Example that will trigger the "mismatched indentations" warning from Ruby
2
- if 2 > 1
3
- true
4
- end
@@ -1,69 +0,0 @@
1
- ENV['MT_NO_PLUGINS'] = '1' # Work around stupid autoloading of plugins
2
- require 'minitest/global_expectations/autorun'
3
- require 'warning'
4
-
5
- class WarningFreezeTest < Minitest::Test
6
- module EnvUtil
7
- def verbose_warning
8
- class << (stderr = "")
9
- alias write <<
10
- end
11
- stderr, $stderr, verbose, $VERBOSE = $stderr, stderr, $VERBOSE, true
12
- yield stderr
13
- return $stderr
14
- ensure
15
- stderr, $stderr, $VERBOSE = $stderr, stderr, verbose
16
- end
17
- module_function :verbose_warning
18
-
19
- def with_default_internal(enc)
20
- verbose, $VERBOSE = $VERBOSE, nil
21
- origenc, Encoding.default_internal = Encoding.default_internal, enc
22
- $VERBOSE = verbose
23
- yield
24
- ensure
25
- verbose, $VERBOSE = $VERBOSE, nil
26
- Encoding.default_internal = origenc
27
- $VERBOSE = verbose
28
- end
29
- module_function :with_default_internal
30
- end
31
-
32
- def assert_warning(pat, msg = nil)
33
- stderr = EnvUtil.verbose_warning {
34
- EnvUtil.with_default_internal(pat.encoding) {
35
- yield
36
- }
37
- }
38
- msg = message(msg) {diff pat, stderr}
39
- assert(pat === stderr, msg)
40
- end
41
-
42
- def test_warning_ignore
43
- w = nil
44
-
45
- Warning.ignore(/global variable `\$test_warning_ignore' not initialized/)
46
- Warning.process do |warning|
47
- w = [4, warning]
48
- end
49
- Warning.freeze
50
-
51
- assert_raises RuntimeError do
52
- Warning.ignore(/global variable `\$test_warning_ignore' not initialized/)
53
- end
54
- assert_raises RuntimeError do
55
- Warning.process{|warning| w = [4, warning]}
56
- end
57
-
58
- assert_warning '' do
59
- $test_warning_ignore
60
- end
61
- assert_nil w
62
-
63
- assert_warning '' do
64
- $test_warning_ignore2
65
- end
66
- assert_equal(4, w.first)
67
- assert_match(/global variable `\$test_warning_ignore2' not initialized/, w.last)
68
- end
69
- end
data/test/test_warning.rb DELETED
@@ -1,553 +0,0 @@
1
- ENV['MT_NO_PLUGINS'] = '1' # Work around stupid autoloading of plugins
2
- require 'minitest/global_expectations/autorun'
3
- require 'warning'
4
- require 'pathname'
5
-
6
- class WarningTest < Minitest::Test
7
- module EnvUtil
8
- def verbose_warning
9
- stderr = ""
10
- class << (stderr = "")
11
- alias write <<
12
- def puts(*a)
13
- self << a.join("\n")
14
- end
15
- end
16
- stderr, $stderr, verbose, $VERBOSE = $stderr, stderr, $VERBOSE, true
17
- yield stderr
18
- return $stderr
19
- ensure
20
- stderr, $stderr, $VERBOSE = $stderr, stderr, verbose
21
- end
22
- module_function :verbose_warning
23
-
24
- def with_default_internal(enc)
25
- verbose, $VERBOSE = $VERBOSE, nil
26
- origenc, Encoding.default_internal = Encoding.default_internal, enc
27
- $VERBOSE = verbose
28
- yield
29
- ensure
30
- verbose, $VERBOSE = $VERBOSE, nil
31
- Encoding.default_internal = origenc
32
- $VERBOSE = verbose
33
- end
34
- module_function :with_default_internal
35
- end
36
-
37
- def assert_warning(pat, msg = nil)
38
- stderr = EnvUtil.verbose_warning {
39
- EnvUtil.with_default_internal(pat.encoding) {
40
- yield
41
- }
42
- }
43
- msg = message(msg) {diff pat, stderr}
44
- assert(pat === stderr, msg)
45
- end
46
-
47
- def teardown
48
- Warning.clear
49
- end
50
-
51
- def test_warning_dedup
52
- gvar = ->{$test_warning_dedup}
53
-
54
- assert_warning(/global variable `\$test_warning_dedup' not initialized/) do
55
- gvar.call
56
- end
57
- assert_warning(/global variable `\$test_warning_dedup' not initialized/) do
58
- gvar.call
59
- end
60
-
61
- Warning.dedup
62
-
63
- assert_warning(/global variable `\$test_warning_dedup' not initialized/) do
64
- gvar.call
65
- end
66
- assert_warning('') do
67
- gvar.call
68
- end
69
- end
70
-
71
- def test_warning_ignore
72
- assert_warning(/global variable `\$test_warning_ignore' not initialized/) do
73
- assert_nil($test_warning_ignore)
74
- end
75
-
76
- Warning.ignore(/global variable `\$test_warning_ignore' not initialized/)
77
-
78
- assert_warning '' do
79
- assert_nil($test_warning_ignore)
80
- end
81
-
82
- assert_warning(/global variable `\$test_warning_ignore2' not initialized/) do
83
- assert_nil($test_warning_ignore2)
84
- end
85
-
86
- Warning.ignore(/global variable `\$test_warning_ignore2' not initialized/, __FILE__)
87
-
88
- assert_warning '' do
89
- assert_nil($test_warning_ignore2)
90
- end
91
-
92
- assert_warning(/global variable `\$test_warning_ignore3' not initialized/) do
93
- assert_nil($test_warning_ignore3)
94
- end
95
-
96
- Warning.ignore(/global variable `\$test_warning_ignore3' not initialized/, __FILE__ + 'a')
97
-
98
- assert_warning(/global variable `\$test_warning_ignore3' not initialized/) do
99
- assert_nil($test_warning_ignore3)
100
- end
101
- end
102
-
103
- def test_warning_ignore_missing_ivar
104
- Warning.clear
105
-
106
- unless RUBY_VERSION >= '3.0'
107
- assert_warning(/instance variable @ivar not initialized/) do
108
- assert_nil(instance_variable_get(:@ivar))
109
- end
110
- end
111
-
112
- Warning.ignore(:missing_ivar, __FILE__)
113
-
114
- assert_warning '' do
115
- assert_nil(instance_variable_get(:@ivar))
116
- end
117
- end
118
-
119
- def test_warning_ignore_missing_gvar
120
- assert_warning(/global variable `\$gvar' not initialized/) do
121
- $gvar
122
- end
123
-
124
- Warning.ignore(:missing_gvar, __FILE__)
125
-
126
- assert_warning '' do
127
- $gvar
128
- end
129
- end
130
-
131
- def test_warning_ignore_method_redefined
132
- def self.a; end
133
-
134
- assert_warning(/method redefined; discarding old a.+previous definition of a was here/m) do
135
- def self.a; end
136
- end
137
-
138
- Warning.ignore(:method_redefined, __FILE__)
139
-
140
- assert_warning '' do
141
- def self.a; end
142
- end
143
- end
144
-
145
- def test_warning_ignore_not_reached
146
- assert_warning(/: warning: statement not reached/) do
147
- instance_eval('def self.b; return; 1 end', __FILE__)
148
- end
149
-
150
- Warning.ignore(:not_reached, __FILE__)
151
-
152
- assert_warning '' do
153
- instance_eval('def self.c; return; 1 end', __FILE__)
154
- end
155
- end
156
-
157
- def test_warning_ignore_fixnum
158
- assert_warning(/warning: constant ::Fixnum is deprecated/) do
159
- ::Fixnum
160
- end
161
-
162
- Warning.ignore(:fixnum, __FILE__)
163
-
164
- assert_warning '' do
165
- ::Fixnum
166
- end
167
- end
168
-
169
- def test_warning_ignore_bignum
170
- assert_warning(/warning: constant ::Bignum is deprecated/) do
171
- ::Bignum
172
- end
173
-
174
- Warning.ignore(:bignum, __FILE__)
175
-
176
- assert_warning '' do
177
- ::Bignum
178
- end
179
- end
180
-
181
- def test_warning_ignore_void_context
182
- assert_warning(/warning: possibly useless use of :: in void context/) do
183
- instance_eval('::Object; nil', __FILE__, __LINE__)
184
- end
185
-
186
- Warning.ignore(:void_context, __FILE__)
187
-
188
- assert_warning '' do
189
- instance_eval('::Object; nil', __FILE__, __LINE__)
190
- end
191
-
192
- assert_warning '' do
193
- instance_eval('Object; nil', __FILE__, __LINE__)
194
- end
195
-
196
- assert_warning '' do
197
- instance_eval('v = 0; v; nil', __FILE__, __LINE__)
198
- end
199
-
200
- assert_warning '' do
201
- instance_eval('1 > 1; nil', __FILE__, __LINE__)
202
- end
203
-
204
- assert_warning '' do
205
- instance_eval('defined? C; nil', __FILE__, __LINE__)
206
- end
207
-
208
- if RUBY_VERSION >= '2.6'
209
- assert_warning '' do
210
- instance_eval('1..; nil', __FILE__, __LINE__)
211
- end
212
- end
213
- end
214
-
215
- def test_warning_ignore_ambiguous_slash
216
- def self.d(re); end
217
- assert_warning(/warning: ambi/) do
218
- instance_eval('d /a/', __FILE__)
219
- end
220
-
221
- Warning.ignore(:ambiguous_slash, __FILE__)
222
-
223
- assert_warning '' do
224
- instance_eval('d /a/', __FILE__)
225
- end
226
- end
227
-
228
- def test_warning_ignore_unused_var
229
- assert_warning(/warning: assigned but unused variable - \w+/) do
230
- instance_eval('def self.e; b = 1; 2 end', __FILE__)
231
- end
232
-
233
- Warning.ignore(:unused_var, __FILE__)
234
-
235
- assert_warning '' do
236
- instance_eval('def self.f; b = 1; 2 end', __FILE__)
237
- end
238
- end
239
-
240
- def test_warning_ignore_useless_operator
241
- assert_warning(/warning: possibly useless use of == in void context/) do
242
- instance_eval('1 == 2; true', __FILE__)
243
- end
244
-
245
- Warning.ignore(:useless_operator, __FILE__)
246
-
247
- assert_warning '' do
248
- instance_eval('1 == 2; true', __FILE__)
249
- end
250
- end
251
-
252
- def test_warning_ignore_arg_prefix
253
- assert_warning(/: warning: `\*' interpreted as argument prefix/) do
254
- instance_eval('Array *[nil]', __FILE__)
255
- end
256
-
257
- assert_warning(/: warning: `&' interpreted as argument prefix/) do
258
- instance_eval('tap &proc{}', __FILE__)
259
- end
260
- Warning.ignore(:arg_prefix, __FILE__)
261
-
262
- assert_warning '' do
263
- instance_eval('Array *[nil]', __FILE__)
264
- instance_eval('tap &proc{}', __FILE__)
265
- end
266
- end
267
-
268
- def test_warning_ignore_shadow
269
- assert_warning(/warning: shadowing outer local variable - a/) do
270
- instance_eval('lambda{|a| lambda{|a|}}', __FILE__)
271
- end
272
-
273
- Warning.ignore(:shadow, __FILE__)
274
-
275
- assert_warning '' do
276
- instance_eval('lambda{|a| lambda{|a|}}', __FILE__)
277
- end
278
- end if RUBY_VERSION < '2.6'
279
-
280
- if RUBY_VERSION > '2.7' && RUBY_VERSION < '2.8'
281
- def h2kw(**kw)
282
- end
283
- def kw2h(h, **kw)
284
- end
285
- def skw(h=1, a: 1)
286
- end
287
-
288
- def test_warning_ignore_keyword
289
- assert_warning(/warning: Using the last argument as keyword parameters is deprecated; maybe \*\* should be added to the call.*The called method `h2kw' is defined here/m) do
290
- h2kw({})
291
- end
292
- assert_warning(/warning: Passing the keyword argument as the last hash parameter is deprecated.*The called method `kw2h' is defined here/m) do
293
- kw2h(a: 1)
294
- end
295
- assert_warning(/warning: Splitting the last argument into positional and keyword parameters is deprecated.*The called method `skw' is defined here/m) do
296
- skw("b" => 1, a: 2)
297
- end
298
- assert_warning(/warning: Splitting the last argument into positional and keyword parameters is deprecated.*The called method `skw' is defined here/m) do
299
- skw({"b" => 1, a: 2})
300
- end
301
-
302
- Warning.ignore(:keyword_separation, __FILE__)
303
-
304
- assert_warning '' do
305
- h2kw({})
306
- kw2h(a: 1)
307
- skw("b" => 1, a: 2)
308
- skw({"b" => 1, a: 2})
309
- end
310
- end
311
-
312
- def test_warning_ignore_safe
313
- assert_warning(/\$SAFE will become a normal global variable in Ruby 3\.0/) do
314
- $SAFE = 0
315
- end
316
-
317
- Warning.ignore(:safe, __FILE__)
318
-
319
- assert_warning("") do
320
- $SAFE = 0
321
- end
322
- end
323
- end
324
-
325
- if RUBY_VERSION > '2.7' && RUBY_VERSION < '3.2'
326
-
327
- def test_warning_ignore_taint
328
- o = Object.new
329
-
330
- assert_warning(/Object#taint is deprecated and will be removed in Ruby 3\.2/) do
331
- o.taint
332
- end
333
- assert_warning(/Object#untaint is deprecated and will be removed in Ruby 3\.2/) do
334
- o.untaint
335
- end
336
- assert_warning(/Object#tainted\? is deprecated and will be removed in Ruby 3\.2/) do
337
- o.tainted?
338
- end
339
- assert_warning(/Object#trust is deprecated and will be removed in Ruby 3\.2/) do
340
- o.trust
341
- end
342
- assert_warning(/Object#untrust is deprecated and will be removed in Ruby 3\.2/) do
343
- o.untrust
344
- end
345
- assert_warning(/Object#untrusted\? is deprecated and will be removed in Ruby 3\.2/) do
346
- o.untrusted?
347
- end
348
-
349
- path = Pathname.new(__FILE__)
350
- assert_warning(/Pathname#taint is deprecated and will be removed in Ruby 3\.2/) do
351
- path.taint
352
- end
353
- assert_warning(/Pathname#untaint is deprecated and will be removed in Ruby 3\.2/) do
354
- path.untaint
355
- end
356
-
357
- Warning.ignore(:taint, __FILE__)
358
-
359
- assert_warning("") do
360
- o.taint
361
- o.untaint
362
- o.tainted?
363
- o.trust
364
- o.untrust
365
- o.untrusted?
366
- p.taint
367
- p.untaint
368
- end
369
- end
370
- end
371
-
372
- def test_warning_ignore_symbol_array
373
- def self.c; end
374
-
375
- assert_warning(/statement not reached.+method redefined; discarding old c.+previous definition of c was here/m) do
376
- instance_eval('def self.c; return; 1 end', __FILE__)
377
- end
378
-
379
- Warning.ignore([:method_redefined, :not_reached], __FILE__)
380
-
381
- assert_warning '' do
382
- instance_eval('def self.c; return; 1 end', __FILE__)
383
- end
384
- end
385
-
386
- def test_warning_ignore_mismatched_indentation
387
- assert_warning(/warning: mismatched indentations/) do
388
- load 'test/fixtures/mismatched_indentations.rb'
389
- end
390
-
391
- Warning.ignore(:mismatched_indentations, 'test/fixtures/mismatched_indentations.rb')
392
-
393
- assert_warning '' do
394
- load 'test/fixtures/mismatched_indentations.rb'
395
- end
396
- end
397
-
398
- def test_warning_process
399
- warn = nil
400
-
401
- Warning.process(__FILE__+'a') do |warning|
402
- warn = [0, warning]
403
- end
404
-
405
- assert_warning(/global variable `\$test_warning_process' not initialized/) do
406
- $test_warning_process
407
- end
408
- assert_nil(warn)
409
-
410
- Warning.process(__FILE__) do |warning|
411
- warn = [1, warning]
412
- end
413
-
414
- assert_warning '' do
415
- $test_warning_process2
416
- end
417
- assert_equal(1, warn.first)
418
- assert_match(/global variable `\$test_warning_process2' not initialized/, warn.last)
419
- warn = nil
420
-
421
- Warning.process(File.dirname(__FILE__)) do |warning|
422
- warn = [2, warning]
423
- end
424
-
425
- assert_warning '' do
426
- $test_warning_process3
427
- end
428
- assert_equal(1, warn.first)
429
- assert_match(/global variable `\$test_warning_process3' not initialized/, warn.last)
430
- warn = nil
431
-
432
- Warning.process(__FILE__+':') do |warning|
433
- warn = [3, warning]
434
- end
435
-
436
- assert_warning '' do
437
- $test_warning_process4
438
- end
439
- assert_equal(3, warn.first)
440
- assert_match(/global variable `\$test_warning_process4' not initialized/, warn.last)
441
- warn = nil
442
-
443
- Warning.clear
444
-
445
- assert_warning(/global variable `\$test_warning_process5' not initialized/) do
446
- $test_warning_process5
447
- end
448
- assert_nil(warn)
449
-
450
- Warning.process do |warning|
451
- warn = [4, warning]
452
- end
453
-
454
- assert_warning '' do
455
- $test_warning_process6
456
- end
457
- assert_equal(4, warn.first)
458
- assert_match(/global variable `\$test_warning_process6' not initialized/, warn.last)
459
- end
460
-
461
- def test_warning_process_block_return_default
462
- w = nil
463
- Warning.process(__FILE__) do |warning|
464
- w = warning
465
- :default
466
- end
467
-
468
- assert_warning(/global variable `\$test_warning_process_block_return_default' not initialized/) do
469
- $test_warning_process_block_return_default
470
- end
471
- assert_match(/global variable `\$test_warning_process_block_return_default' not initialized/, w)
472
- end
473
-
474
- def test_warning_process_block_return_backtrace
475
- w = nil
476
- Warning.process(__FILE__) do |warning|
477
- w = warning
478
- :backtrace
479
- end
480
-
481
- assert_warning(/global variable `\$test_warning_process_block_return_backtrace' not initialized.*#{__FILE__}/m) do
482
- $test_warning_process_block_return_backtrace
483
- end
484
- assert_match(/global variable `\$test_warning_process_block_return_backtrace' not initialized/, w)
485
- end
486
-
487
- def test_warning_process_block_return_raise
488
- w = nil
489
- Warning.process(__FILE__) do |warning|
490
- w = warning
491
- :raise
492
- end
493
-
494
- assert_raises(RuntimeError) do
495
- $test_warning_process_block_return_raise
496
- end
497
- assert_match(/global variable `\$test_warning_process_block_return_raise' not initialized/, w)
498
- end
499
-
500
- def test_warning_process_action
501
- w = nil
502
- Warning.process(__FILE__, :method_redefined=>:default, :missing_gvar=>:backtrace, :ambiguous_slash=>:raise)
503
- Warning.process(__FILE__, :not_reached=>proc do |warning|
504
- w = warning
505
- :raise
506
- end)
507
-
508
- assert_warning(/warning: method redefined/) do
509
- Class.new do
510
- def a; end
511
- def a; end
512
- end
513
- end
514
-
515
- assert_warning(/global variable `\$test_warning_process_action' not initialized.*#{__FILE__}/m) do
516
- $test_warning_process_action
517
- end
518
-
519
- Warning.process(__FILE__) do |warning|
520
- w = warning
521
- :raise
522
- end
523
-
524
- assert_raises(RuntimeError, /warning: ambi/) do
525
- EnvUtil.verbose_warning{instance_eval('d /a/', __FILE__)}
526
- end
527
-
528
- assert_raises(RuntimeError, /warning: statement not reached/) do
529
- EnvUtil.verbose_warning{instance_eval('def self.b; return; 1 end', __FILE__)}
530
- end
531
- assert_match(/warning: statement not reached/, w)
532
- end
533
-
534
- def test_warning_process_action_and_block
535
- assert_raises(ArgumentError) do
536
- Warning.process(__FILE__)
537
- end
538
- end
539
-
540
- def test_warning_process_no_action_and_no_block
541
- assert_raises(ArgumentError) do
542
- Warning.process(__FILE__, :missing_ivar=>:default){}
543
- end
544
- end
545
-
546
- if RUBY_VERSION >= '3.0'
547
- def test_warning_warn_category_keyword
548
- assert_warning('foo') do
549
- Warning.warn("foo", category: :deprecated)
550
- end
551
- end
552
- end
553
- end