diff options
Diffstat (limited to 'spec/ruby/core/warning')
| -rw-r--r-- | spec/ruby/core/warning/categories_spec.rb | 12 | ||||
| -rw-r--r-- | spec/ruby/core/warning/element_reference_spec.rb | 20 | ||||
| -rw-r--r-- | spec/ruby/core/warning/element_set_spec.rb | 24 | ||||
| -rw-r--r-- | spec/ruby/core/warning/performance_warning_spec.rb | 28 | ||||
| -rw-r--r-- | spec/ruby/core/warning/warn_spec.rb | 54 |
5 files changed, 115 insertions, 23 deletions
diff --git a/spec/ruby/core/warning/categories_spec.rb b/spec/ruby/core/warning/categories_spec.rb new file mode 100644 index 0000000000..1e310ef38b --- /dev/null +++ b/spec/ruby/core/warning/categories_spec.rb @@ -0,0 +1,12 @@ +require_relative '../../spec_helper' + +ruby_version_is "3.4" do + describe "Warning.categories" do + # There might be more, but these are standard across Ruby implementations + it "returns the list of possible warning categories" do + Warning.categories.should.include? :deprecated + Warning.categories.should.include? :experimental + Warning.categories.should.include? :performance + end + end +end diff --git a/spec/ruby/core/warning/element_reference_spec.rb b/spec/ruby/core/warning/element_reference_spec.rb index 8cb4018c20..5f977759ec 100644 --- a/spec/ruby/core/warning/element_reference_spec.rb +++ b/spec/ruby/core/warning/element_reference_spec.rb @@ -2,24 +2,26 @@ require_relative '../../spec_helper' describe "Warning.[]" do it "returns default values for categories :deprecated and :experimental" do + # If any warning options were set on the Ruby that will be executed, then + # it's possible this test will fail. In this case we will skip this test. + skip if ruby_exe.any? { |opt| opt.start_with?("-W") } + ruby_exe('p [Warning[:deprecated], Warning[:experimental]]').chomp.should == "[false, true]" ruby_exe('p [Warning[:deprecated], Warning[:experimental]]', options: "-w").chomp.should == "[true, true]" end - ruby_version_is '3.3' do - it "returns default values for :performance category" do - ruby_exe('p Warning[:performance]').chomp.should == "false" - ruby_exe('p Warning[:performance]', options: "-w").chomp.should == "false" - end + it "returns default values for :performance category" do + ruby_exe('p Warning[:performance]').chomp.should == "false" + ruby_exe('p Warning[:performance]', options: "-w").chomp.should == "false" end it "raises for unknown category" do - -> { Warning[:noop] }.should raise_error(ArgumentError, /unknown category: noop/) + -> { Warning[:noop] }.should.raise(ArgumentError, /unknown category: noop/) end it "raises for non-Symbol category" do - -> { Warning[42] }.should raise_error(TypeError) - -> { Warning[false] }.should raise_error(TypeError) - -> { Warning["noop"] }.should raise_error(TypeError) + -> { Warning[42] }.should.raise(TypeError) + -> { Warning[false] }.should.raise(TypeError) + -> { Warning["noop"] }.should.raise(TypeError) end end diff --git a/spec/ruby/core/warning/element_set_spec.rb b/spec/ruby/core/warning/element_set_spec.rb index d59a7d4c9e..3c8ceb721e 100644 --- a/spec/ruby/core/warning/element_set_spec.rb +++ b/spec/ruby/core/warning/element_set_spec.rb @@ -17,25 +17,23 @@ describe "Warning.[]=" do end end - ruby_version_is '3.3' do - it "enables or disables performance warnings" do - original = Warning[:performance] - begin - Warning[:performance] = !original - Warning[:performance].should == !original - ensure - Warning[:performance] = original - end + it "enables or disables performance warnings" do + original = Warning[:performance] + begin + Warning[:performance] = !original + Warning[:performance].should == !original + ensure + Warning[:performance] = original end end it "raises for unknown category" do - -> { Warning[:noop] = false }.should raise_error(ArgumentError, /unknown category: noop/) + -> { Warning[:noop] = false }.should.raise(ArgumentError, /unknown category: noop/) end it "raises for non-Symbol category" do - -> { Warning[42] = false }.should raise_error(TypeError) - -> { Warning[false] = false }.should raise_error(TypeError) - -> { Warning["noop"] = false }.should raise_error(TypeError) + -> { Warning[42] = false }.should.raise(TypeError) + -> { Warning[false] = false }.should.raise(TypeError) + -> { Warning["noop"] = false }.should.raise(TypeError) end end diff --git a/spec/ruby/core/warning/performance_warning_spec.rb b/spec/ruby/core/warning/performance_warning_spec.rb new file mode 100644 index 0000000000..ab0badcd3d --- /dev/null +++ b/spec/ruby/core/warning/performance_warning_spec.rb @@ -0,0 +1,28 @@ +require_relative '../../spec_helper' + + +describe "Performance warnings" do + guard -> { ruby_version_is("3.4") || RUBY_ENGINE == "truffleruby" } do + # Optimising Integer, Float or Symbol methods is kind of implementation detail + # but multiple implementations do so. So it seems reasonable to have a test case + # for at least one such common method. + # See https://bugs.ruby-lang.org/issues/20429 + context "when redefined optimised methods" do + it "emits performance warning for redefining Integer#+" do + code = <<~CODE + Warning[:performance] = true + + class Integer + ORIG_METHOD = instance_method(:+) + + def +(...) + ORIG_METHOD.bind(self).call(...) + end + end + CODE + + ruby_exe(code, args: "2>&1").should.include?("warning: Redefining 'Integer#+' disables interpreter and JIT optimizations") + end + end + end +end diff --git a/spec/ruby/core/warning/warn_spec.rb b/spec/ruby/core/warning/warn_spec.rb index 8f96fe9287..62f36e3454 100644 --- a/spec/ruby/core/warning/warn_spec.rb +++ b/spec/ruby/core/warning/warn_spec.rb @@ -16,7 +16,7 @@ describe "Warning.warn" do end it "extends itself" do - Warning.singleton_class.ancestors.should include(Warning) + Warning.singleton_class.ancestors.should.include?(Warning) end it "has Warning as the method owner" do @@ -97,6 +97,20 @@ describe "Warning.warn" do end end + ruby_version_is "3.4" do + it "warns when category is :strict_unused_block but Warning[:strict_unused_block] is false" do + warn_strict_unused_block = Warning[:strict_unused_block] + Warning[:strict_unused_block] = true + begin + -> { + Warning.warn("foo", category: :strict_unused_block) + }.should complain("foo") + ensure + Warning[:strict_unused_block] = warn_strict_unused_block + end + end + end + it "doesn't print message when category is :deprecated but Warning[:deprecated] is false" do warn_deprecated = Warning[:deprecated] Warning[:deprecated] = false @@ -121,6 +135,44 @@ describe "Warning.warn" do end end + ruby_version_is "3.4" do + it "doesn't print message when category is :strict_unused_block but Warning[:strict_unused_block] is false" do + warn_strict_unused_block = Warning[:strict_unused_block] + Warning[:strict_unused_block] = false + begin + -> { + Warning.warn("foo", category: :strict_unused_block) + }.should_not complain + ensure + Warning[:strict_unused_block] = warn_strict_unused_block + end + end + end + + ruby_bug '#20573', ''...'3.4' do + it "isn't called by Kernel.warn when category is :deprecated but Warning[:deprecated] is false" do + warn_deprecated = Warning[:deprecated] + begin + Warning[:deprecated] = false + Warning.should_not_receive(:warn) + Kernel.warn("foo", category: :deprecated) + ensure + Warning[:deprecated] = warn_deprecated + end + end + + it "isn't called by Kernel.warn when category is :experimental but Warning[:experimental] is false" do + warn_experimental = Warning[:experimental] + begin + Warning[:experimental] = false + Warning.should_not_receive(:warn) + Kernel.warn("foo", category: :experimental) + ensure + Warning[:experimental] = warn_experimental + end + end + end + it "prints the message when VERBOSE is false" do -> { Warning.warn("foo") }.should complain("foo") end |
