mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Add rb_category_warn{,ing} for warning messages with categories
This adds the following C-API functions that can be used to emit warnings with categories included: ```c void rb_category_warn(const char *, const char*, ...) void rb_category_warning(const char*, const char*, ...) ``` Internally in error.c, there is an rb_warn_category function that will call Warning.warn with the string and the category keyword if it doesn't have an arity of 1, and will call Warning.warn with just the string if it has an arity of 1. This refactors the rb_warn_deprecated{,_to_remove} functions to use rb_warn_category. This makes Kernel#warn accept a category keyword and pass it to Warning.warn, so that Ruby methods can more easily emit warnings with categories. rb_warn_category makes sure that the passed category is a already defined category symbol before calling Warning.warn. The only currently defined warning category is :deprecated, since that is what is already used. More categories can be added in later commits.
This commit is contained in:
parent
92c3ad9c27
commit
346301e232
Notes:
git
2020-09-29 00:38:31 +09:00
5 changed files with 135 additions and 34 deletions
|
@ -51,14 +51,41 @@ describe "Warning.warn" do
|
|||
end
|
||||
end
|
||||
|
||||
it "is called by Kernel.warn" do
|
||||
Warning.should_receive(:warn).with("Chunky bacon!\n")
|
||||
verbose = $VERBOSE
|
||||
$VERBOSE = false
|
||||
begin
|
||||
Kernel.warn("Chunky bacon!")
|
||||
ensure
|
||||
$VERBOSE = verbose
|
||||
|
||||
ruby_version_is '3.0' do
|
||||
it "is called by Kernel.warn with nil category keyword" do
|
||||
Warning.should_receive(:warn).with("Chunky bacon!\n", category: nil)
|
||||
verbose = $VERBOSE
|
||||
$VERBOSE = false
|
||||
begin
|
||||
Kernel.warn("Chunky bacon!")
|
||||
ensure
|
||||
$VERBOSE = verbose
|
||||
end
|
||||
end
|
||||
|
||||
it "is called by Kernel.warn with given category keyword converted to a symbol" do
|
||||
Warning.should_receive(:warn).with("Chunky bacon!\n", category: :deprecated)
|
||||
verbose = $VERBOSE
|
||||
$VERBOSE = false
|
||||
begin
|
||||
Kernel.warn("Chunky bacon!", category: 'deprecated')
|
||||
ensure
|
||||
$VERBOSE = verbose
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
ruby_version_is ''...'3.0' do
|
||||
it "is called by Kernel.warn" do
|
||||
Warning.should_receive(:warn).with("Chunky bacon!\n")
|
||||
verbose = $VERBOSE
|
||||
$VERBOSE = false
|
||||
begin
|
||||
Kernel.warn("Chunky bacon!")
|
||||
ensure
|
||||
$VERBOSE = verbose
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue