Skip to content

Commit

Permalink
Add rb_category_warn{,ing} for warning messages with categories
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jeremyevans authored and eregon committed Sep 30, 2020
1 parent 9e8d2a4 commit 05ff9d4
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 9 deletions.
34 changes: 33 additions & 1 deletion core/kernel/warn_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
Kernel.should have_private_instance_method(:warn)
end

it "requires multiple arguments" do
it "accepts multiple arguments" do
Kernel.method(:warn).arity.should < 0
end

Expand Down Expand Up @@ -114,6 +114,38 @@
end
end

ruby_version_is "3.0" do
it "accepts :category keyword with a symbol" do
-> {
$VERBOSE = true
warn("message", category: :deprecated)
}.should output(nil, "message\n")
end

it "accepts :category keyword with nil" do
-> {
$VERBOSE = true
warn("message", category: nil)
}.should output(nil, "message\n")
end

it "accepts :category keyword with object convertible to symbol" do
o = Object.new
def o.to_sym; :deprecated; end
-> {
$VERBOSE = true
warn("message", category: o)
}.should output(nil, "message\n")
end

it "raises if :category keyword is not nil and not convertible to symbol" do
-> {
$VERBOSE = true
warn("message", category: Object.new)
}.should raise_error(TypeError)
end
end

it "converts first arg using to_s" do
w = KernelSpecs::WarnInNestedCall.new

Expand Down
43 changes: 35 additions & 8 deletions core/warning/warn_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,41 @@ def Warning.warn(msg)
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

0 comments on commit 05ff9d4

Please sign in to comment.