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 (66 lines) | stat: -rw-r--r-- 2,187 bytes parent folder | download | duplicates (2)
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
require "spec_helper"
require "rspec/support/warnings"
require 'rspec/support/spec/shell_out'

RSpec.describe "rspec warnings and deprecations" do
  include RSpec::Support::ShellOut
  let(:warning_object) do
    Object.new.tap { |o| o.extend(RSpec::Support::Warnings) }
  end

  it 'works when required in isolation' do
    out, err, status = run_ruby_with_current_load_path("RSpec.deprecate('foo')", "-rrspec/support/warnings")
    expect(out).to eq("")
    expect(err).to start_with("DEPRECATION: foo is deprecated")
    expect(status.exitstatus).to eq(0)
  end

  context "when rspec-core is not available" do
    shared_examples "falling back to Kernel.warn" do |args|
      let(:method_name) { args.fetch(:method_name) }

      it 'falls back to warning with a plain message' do
        expect(::Kernel).to receive(:warn).with(/message/)
        warning_object.send(method_name, 'message')
      end

      it "handles being passed options" do
        expect(::Kernel).to receive(:warn).with(/message/)
        warning_object.send(method_name, "this is the message", :type => :test)
      end
    end

    it_behaves_like 'falling back to Kernel.warn', :method_name => :deprecate
    it_behaves_like 'falling back to Kernel.warn', :method_name => :warn_deprecation
  end

  shared_examples "warning helper" do |helper|
    it 'warns with the message text' do
      expect(::Kernel).to receive(:warn).with(/Message/)
      warning_object.send(helper, 'Message')
    end

    it 'sets the calling line' do
      expect(::Kernel).to receive(:warn).with(/#{__FILE__}:#{__LINE__+1}/)
      warning_object.send(helper, 'Message')
    end

    it 'optionally sets the replacement' do
      expect(::Kernel).to receive(:warn).with(/Use Replacement instead./)
      warning_object.send(helper, 'Message', :replacement => 'Replacement')
    end
  end

  describe "#warning" do
    it 'prepends WARNING:' do
      expect(::Kernel).to receive(:warn).with(/WARNING: Message\./)
      warning_object.warning 'Message'
    end

    it_should_behave_like 'warning helper', :warning
  end

  describe "#warn_with message, options" do
    it_should_behave_like 'warning helper', :warn_with
  end
end