File: warnings_spec.rb

package info (click to toggle)
ruby-rspec 3.13.0c0e0m0s1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,856 kB
  • sloc: ruby: 70,868; sh: 1,423; makefile: 99
file content (70 lines) | stat: -rw-r--r-- 2,477 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
RSpec.describe "rspec warnings and deprecations" do

  describe "#deprecate" do
    it "passes the hash to the reporter" do
      expect(RSpec.configuration.reporter).to receive(:deprecation).with(hash_including :deprecated => "deprecated_method", :replacement => "replacement")
      RSpec.deprecate("deprecated_method", :replacement => "replacement")
    end

    it "adds the call site" do
      expect_deprecation_with_call_site(__FILE__, __LINE__ + 1)
      RSpec.deprecate("deprecated_method")
    end

    it "doesn't override a passed call site" do
      expect_deprecation_with_call_site("some_file.rb", 17)
      RSpec.deprecate("deprecated_method", :call_site => "/some_file.rb:17")
    end
  end

  describe "#warn_deprecation" do
    it "puts message in a hash" do
      expect(RSpec.configuration.reporter).to receive(:deprecation).with(hash_including :message => "this is the message")
      RSpec.warn_deprecation("this is the message")
    end

    it "passes along additional options" do
      expect(RSpec.configuration.reporter).to receive(:deprecation).with(hash_including :type => :tag)
      RSpec.warn_deprecation("this is the message", :type => :tag)
    end
  end

  describe "#warn_with" do
    context "when :use_spec_location_as_call_site => true is passed" do
      let(:options) do
        {
          :use_spec_location_as_call_site => true,
          :call_site                      => nil,
        }
      end

      it "adds the source location of spec" do
        line = __LINE__ - 1
        file_path = RSpec::Core::Metadata.relative_path(__FILE__)
        expect(Kernel).to receive(:warn).with(/The warning. Warning generated from spec at `#{file_path}:#{line}`./)

        RSpec.warn_with("The warning.", options)
      end

      it "appends a period to the supplied message if one is not present" do
        line = __LINE__ - 1
        file_path = RSpec::Core::Metadata.relative_path(__FILE__)
        expect(Kernel).to receive(:warn).with(/The warning. Warning generated from spec at `#{file_path}:#{line}`./)

        RSpec.warn_with("The warning", options)
      end

      context "when there is no current example" do
        before do
          allow(RSpec).to receive(:current_example).and_return(nil)
        end

        it "adds no message about the spec location" do
          expect(Kernel).to receive(:warn).with(/The warning\.$/)

          RSpec.warn_with("The warning.", options)
        end
      end
    end
  end
end