Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup Warning #1477

Merged
merged 1 commit into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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