Skip to content

Commit

Permalink
Merge pull request #1477 from sampersand/swesterman/23-08-28/update-w…
Browse files Browse the repository at this point in the history
…arning

Cleanup `Warning`
  • Loading branch information
soutaro authored Aug 29, 2023
2 parents fe6898d + 2e9e655 commit 44fad0b
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 6 deletions.
33 changes: 33 additions & 0 deletions core/warning.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,41 @@
# The `warning` gem provides convenient ways to customize Warning.warn.
#
module Warning
# The types of categories the `Warning` module understands.
#
type category = :deprecated | :experimental

# <!--
# rdoc-file=error.c
# - Warning[category] -> true or false
# -->
# Returns the flag to show the warning messages for `category`. Supported
# categories are:
#
# `:deprecated`
# : deprecation warnings
#
# * assignment of non-nil value to `$,` and `$;`
# * keyword arguments
# * proc/lambda without block
#
# etc.
#
# `:experimental`
# : experimental features
#
# * Pattern matching
#
def self.[]: (category) -> bool

# <!--
# rdoc-file=error.c
# - Warning[category] = flag -> flag
# -->
# Sets the warning flags for `category`. See Warning.[] for the categories.
#
def self.[]=: [T] (category, T flag) -> T

# <!--
# rdoc-file=error.c
# - warn(msg, category: nil) -> nil
Expand Down
49 changes: 43 additions & 6 deletions test/stdlib/Warning_test.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,39 @@
require_relative "test_helper"

WARNING_CATEGORIES = %i[deprecated experimental]

class WarningSingletonTest < Test::Unit::TestCase
include TypeAssertions

testing "singleton(::Warning)"

def test_aref
WARNING_CATEGORIES.each do |category|
assert_send_type "(#{category.inspect}) -> bool",
Warning, :[], category
end

refute_send_type "(Symbol) -> bool",
Warning, :[], :unknown_category

refute_send_type "(ToSym) -> bool",
Warning, :[], ToSym.new(WARNING_CATEGORIES.first)
end

def test_aset
WARNING_CATEGORIES.each do |category|
assert_send_type "(#{category.inspect}, Rational) -> Rational",
Warning, :[]=, category, 1r
end

refute_send_type "(Symbol, Rational) -> Rational",
Warning, :[]=, :unknown_category, 1r

refute_send_type "(ToSym, Rational) -> Rational",
Warning, :[]=, ToSym.new(WARNING_CATEGORIES.first), 1r
end
end

class WarningTest < Test::Unit::TestCase
include TypeAssertions

Expand All @@ -10,6 +44,7 @@ class TestClass
end

def test_warn
old_stderr = $stderr
$stderr = StringIO.new

assert_send_type "(::String) -> nil",
Expand All @@ -23,18 +58,20 @@ def test_warn

omit_if(RUBY_VERSION < "3.0")

assert_send_type "(::String, category: :deprecated) -> nil",
Warning, :warn, 'message', category: :deprecated

assert_send_type "(::String, category: :experimental) -> nil",
Warning, :warn, 'message', category: :experimental
WARNING_CATEGORIES.each do |category|
assert_send_type "(::String, category: #{category.inspect}) -> nil",
Warning, :warn, 'message', category: category
end

assert_send_type "(::String, category: nil) -> nil",
Warning, :warn, 'message', category: nil

refute_send_type "(::String, category: ToSym) -> nil",
Warning, :warn, 'message', category: ToSym.new(WARNING_CATEGORIES.first)

refute_send_type "(::String, category: ::Symbol) -> nil",
Warning, :warn, 'message', category: :unknown_category
ensure
$stderr = STDERR
$stderr = old_stderr
end
end
11 changes: 11 additions & 0 deletions test/stdlib/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,17 @@ def to_s
end
end


class ToSym
def initialize(value = :&)
@value = value
end

def to_sym
@value
end
end

class ToA
def initialize(*args)
@args = args
Expand Down

0 comments on commit 44fad0b

Please sign in to comment.