File: test_freeze_warning.rb

package info (click to toggle)
ruby-warning 1.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 156 kB
  • sloc: ruby: 754; makefile: 4
file content (67 lines) | stat: -rw-r--r-- 1,685 bytes parent folder | download
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
require_relative 'test_helper'

class WarningFreezeTest < Minitest::Test
  module EnvUtil
    def verbose_warning
      class << (stderr = "")
        alias write <<
      end
      stderr, $stderr, verbose, $VERBOSE = $stderr, stderr, $VERBOSE, true
      yield stderr
      return $stderr
    ensure
      stderr, $stderr, $VERBOSE = $stderr, stderr, verbose
    end
    module_function :verbose_warning

    def with_default_internal(enc)
      verbose, $VERBOSE = $VERBOSE, nil
      origenc, Encoding.default_internal = Encoding.default_internal, enc
      $VERBOSE = verbose
      yield
    ensure
      verbose, $VERBOSE = $VERBOSE, nil
      Encoding.default_internal = origenc
      $VERBOSE = verbose
    end
    module_function :with_default_internal
  end

  def assert_warning(pat, msg = nil)
    stderr = EnvUtil.verbose_warning {
      EnvUtil.with_default_internal(pat.encoding) {
        yield
      }
    }
    msg = message(msg) {diff pat, stderr}
    assert(pat === stderr, msg)
  end

  def test_warning_ignore
    w = nil

    Warning.ignore(/global variable `\$test_warning_ignore' not initialized/)
    Warning.process do |warning|
      w = [4, warning]
    end
    Warning.freeze

    assert_raises RuntimeError do
      Warning.ignore(/global variable `\$test_warning_ignore' not initialized/)
    end
    assert_raises RuntimeError do
      Warning.process{|warning| w = [4, warning]}
    end

    assert_warning '' do
      $test_warning_ignore
    end
    assert_nil w

    assert_warning '' do
      $test_warning_ignore2
    end
    assert_equal(4, w.first)
    assert_match(/global variable `\$test_warning_ignore2' not initialized/, w.last)
  end
end