1require_relative '../../spec_helper'
2require_relative 'fixtures/classes'
3
4describe "Kernel#warn" do
5  before :each do
6    @before_verbose = $VERBOSE
7    @before_separator = $/
8  end
9
10  after :each do
11    $VERBOSE = @before_verbose
12    $/ = @before_separator
13  end
14
15  it "is a private method" do
16    Kernel.should have_private_instance_method(:warn)
17  end
18
19  it "requires multiple arguments" do
20    Kernel.method(:warn).arity.should < 0
21  end
22
23  it "does not append line-end if last character is line-end" do
24    lambda {
25      $VERBOSE = true
26      warn("this is some simple text with line-end\n")
27    }.should output(nil, "this is some simple text with line-end\n")
28  end
29
30  it "calls #write on $stderr if $VERBOSE is true" do
31    lambda {
32      $VERBOSE = true
33      warn("this is some simple text")
34    }.should output(nil, "this is some simple text\n")
35  end
36
37  it "calls #write on $stderr if $VERBOSE is false" do
38    lambda {
39      $VERBOSE = false
40      warn("this is some simple text")
41    }.should output(nil, "this is some simple text\n")
42  end
43
44  it "does not call #write on $stderr if $VERBOSE is nil" do
45    lambda {
46      $VERBOSE = nil
47      warn("this is some simple text")
48    }.should output(nil, "")
49  end
50
51  it "writes each argument on a line when passed multiple arguments" do
52    lambda {
53      $VERBOSE = true
54      warn("line 1", "line 2")
55    }.should output(nil, "line 1\nline 2\n")
56  end
57
58  it "writes each array element on a line when passes an array" do
59    lambda {
60      $VERBOSE = true
61      warn(["line 1", "line 2"])
62    }.should output(nil, "line 1\nline 2\n")
63  end
64
65  it "does not write strings when passed no arguments" do
66    lambda {
67      $VERBOSE = true
68      warn
69    }.should output("", "")
70  end
71
72  it "writes the default record separator and NOT $/ to $stderr after the warning message" do
73    lambda {
74      $VERBOSE = true
75      $/ = 'rs'
76      warn("")
77    }.should output(nil, /\n/)
78  end
79
80  ruby_version_is "2.5" do
81    describe ":uplevel keyword argument" do
82      before :each do
83        $VERBOSE = true
84      end
85
86      it "prepends a message with specified line from the backtrace" do
87        w = KernelSpecs::WarnInNestedCall.new
88
89        -> { w.f4("foo", 0) }.should output(nil, %r|core/kernel/fixtures/classes.rb:#{w.warn_call_lineno}: warning: foo|)
90        -> { w.f4("foo", 1) }.should output(nil, %r|core/kernel/fixtures/classes.rb:#{w.f1_call_lineno}: warning: foo|)
91        -> { w.f4("foo", 2) }.should output(nil, %r|core/kernel/fixtures/classes.rb:#{w.f2_call_lineno}: warning: foo|)
92        -> { w.f4("foo", 3) }.should output(nil, %r|core/kernel/fixtures/classes.rb:#{w.f3_call_lineno}: warning: foo|)
93      end
94
95      ruby_bug "#14846", "2.5"..."2.6" do
96        it "does not prepend caller information if line number is too big" do
97          w = KernelSpecs::WarnInNestedCall.new
98          -> { w.f4("foo", 100) }.should output(nil, "warning: foo\n")
99        end
100      end
101
102      it "prepends even if a message is empty or nil" do
103        w = KernelSpecs::WarnInNestedCall.new
104
105        -> { w.f4("", 0) }.should output(nil, %r|core/kernel/fixtures/classes.rb:#{w.warn_call_lineno}: warning: \n$|)
106        -> { w.f4(nil, 0) }.should output(nil, %r|core/kernel/fixtures/classes.rb:#{w.warn_call_lineno}: warning: \n$|)
107      end
108
109      it "converts value to Integer" do
110        w = KernelSpecs::WarnInNestedCall.new
111
112        -> { w.f4(0.1) }.should output(nil, %r|classes.rb:#{w.warn_call_lineno}:|)
113        -> { w.f4(Rational(1, 2)) }.should output(nil, %r|classes.rb:#{w.warn_call_lineno}:|)
114      end
115
116      it "raises ArgumentError if passed negative value" do
117        -> { warn "", uplevel: -2 }.should raise_error(ArgumentError)
118        -> { warn "", uplevel: -100 }.should raise_error(ArgumentError)
119      end
120
121      ruby_bug "#14846", "2.5"..."2.6" do
122        it "raises ArgumentError if passed -1" do
123          -> { warn "", uplevel: -1 }.should raise_error(ArgumentError)
124        end
125      end
126
127      it "raises TypeError if passed not Integer" do
128        -> { warn "", uplevel: "" }.should raise_error(TypeError)
129        -> { warn "", uplevel: [] }.should raise_error(TypeError)
130        -> { warn "", uplevel: {} }.should raise_error(TypeError)
131        -> { warn "", uplevel: Object.new }.should raise_error(TypeError)
132      end
133    end
134  end
135end
136