diff options
Diffstat (limited to 'spec/ruby/core/warning')
-rw-r--r-- | spec/ruby/core/warning/element_reference_spec.rb | 17 | ||||
-rw-r--r-- | spec/ruby/core/warning/element_set_spec.rb | 20 | ||||
-rw-r--r-- | spec/ruby/core/warning/performance_warning_spec.rb | 28 | ||||
-rw-r--r-- | spec/ruby/core/warning/warn_spec.rb | 103 |
4 files changed, 128 insertions, 40 deletions
diff --git a/spec/ruby/core/warning/element_reference_spec.rb b/spec/ruby/core/warning/element_reference_spec.rb index 67728ca0f6..c0ed37ef13 100644 --- a/spec/ruby/core/warning/element_reference_spec.rb +++ b/spec/ruby/core/warning/element_reference_spec.rb @@ -1,10 +1,19 @@ require_relative '../../spec_helper' describe "Warning.[]" do - ruby_version_is '2.7.2' do - it "returns default values for categories :deprecated and :experimental" do - ruby_exe('p Warning[:deprecated]').chomp.should == "false" - ruby_exe('p Warning[:experimental]').chomp.should == "true" + 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 end diff --git a/spec/ruby/core/warning/element_set_spec.rb b/spec/ruby/core/warning/element_set_spec.rb index d20ee215ad..d59a7d4c9e 100644 --- a/spec/ruby/core/warning/element_set_spec.rb +++ b/spec/ruby/core/warning/element_set_spec.rb @@ -8,13 +8,7 @@ describe "Warning.[]=" do describe ":experimental" do before do - ruby_version_is ""..."3.0" do - @src = 'case [0, 1]; in [a, b]; end' - end - - ruby_version_is "3.0" do - @src = 'warn "This is experimental warning.", category: :experimental' - end + @src = 'warn "This is experimental warning.", category: :experimental' end it "emits and suppresses warnings for :experimental" do @@ -23,6 +17,18 @@ 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 + end + end + it "raises for unknown category" do -> { Warning[:noop] = false }.should raise_error(ArgumentError, /unknown category: noop/) 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 5ccff3aa2b..8f96fe9287 100644 --- a/spec/ruby/core/warning/warn_spec.rb +++ b/spec/ruby/core/warning/warn_spec.rb @@ -51,40 +51,85 @@ describe "Warning.warn" do end end - 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 + 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 + 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 - 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 + it "warns when category is :deprecated and Warning[:deprecated] is true" do + warn_deprecated = Warning[:deprecated] + Warning[:deprecated] = true + begin + -> { + Warning.warn("foo", category: :deprecated) + }.should complain("foo") + ensure + Warning[:deprecated] = warn_deprecated end end + + it "warns when category is :experimental and Warning[:experimental] is true" do + warn_experimental = Warning[:experimental] + Warning[:experimental] = true + begin + -> { + Warning.warn("foo", category: :experimental) + }.should complain("foo") + ensure + Warning[:experimental] = warn_experimental + end + end + + it "doesn't print message when category is :deprecated but Warning[:deprecated] is false" do + warn_deprecated = Warning[:deprecated] + Warning[:deprecated] = false + begin + -> { + Warning.warn("foo", category: :deprecated) + }.should_not complain + ensure + Warning[:deprecated] = warn_deprecated + end + end + + it "doesn't print message when category is :experimental but Warning[:experimental] is false" do + warn_experimental = Warning[:experimental] + Warning[:experimental] = false + begin + -> { + Warning.warn("foo", category: :experimental) + }.should_not complain + ensure + Warning[:experimental] = warn_experimental + end + end + + it "prints the message when VERBOSE is false" do + -> { Warning.warn("foo") }.should complain("foo") + end + + it "prints the message when VERBOSE is nil" do + -> { Warning.warn("foo") }.should complain("foo", verbose: nil) + end + + it "prints the message when VERBOSE is true" do + -> { Warning.warn("foo") }.should complain("foo", verbose: true) + end end |