Skip to content

Commit c8887ed

Browse files
andrykonchineregon
authored andcommitted
Move it parameter related specs from language/block_spec.rb to language/it_parameter_spec.rb
1 parent 7a164ba commit c8887ed

2 files changed

Lines changed: 46 additions & 54 deletions

File tree

language/block_spec.rb

Lines changed: 4 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,7 +1041,7 @@ def all_kwrest(arg1, arg2, *rest, post1, post2, kw1: 1, kw2: 2, okw1:, okw2:, **
10411041
end
10421042
end
10431043

1044-
describe "`it` calls without arguments in a block with no ordinary parameters" do
1044+
describe "`it` calls without arguments in a block" do
10451045
ruby_version_is "3.3"..."3.4" do
10461046
it "emits a deprecation warning" do
10471047
-> {
@@ -1094,59 +1094,11 @@ def o.it
10941094
end
10951095
end
10961096
end
1097-
1098-
ruby_version_is "3.4" do
1099-
it "does not emit a deprecation warning" do
1100-
-> {
1101-
eval "proc { it }"
1102-
}.should_not complain
1103-
end
1104-
1105-
it "acts as the first argument if no local variables exist" do
1106-
eval("proc { it * 2 }").call(5).should == 10
1107-
end
1108-
1109-
it "acts as the first argument if multiple arguments given" do
1110-
eval("proc { it * 2 }").call(5, 1, 2, 3).should == 10
1111-
end
1112-
1113-
it "can be reassigned to act as a local variable" do
1114-
eval("proc { tmp = it; it = tmp * 2; it }").call(21).should == 42
1115-
end
1116-
1117-
it "can be used in nested calls" do
1118-
it_values = []
1119-
eval("proc { it.each { it_values << it } }").call([1, 2, 3])
1120-
it_values.should == [1, 2, 3]
1121-
end
1122-
1123-
it "cannot be mixed with numbered parameters" do
1124-
-> {
1125-
eval "proc { it + _1 }"
1126-
}.should raise_error(SyntaxError, /numbered parameters are not allowed when 'it' is already used|'it' is already used in/)
1127-
1128-
-> {
1129-
eval "proc { _1 + it }"
1130-
}.should raise_error(SyntaxError, /numbered parameter is already used in|'it' is not allowed when a numbered parameter is already used/)
1131-
end
1132-
1133-
it "is available before re-assigning" do
1134-
a = nil
1135-
proc { a = it; it = 42 }.call(0)
1136-
a.should == 0
1137-
end
1138-
1139-
it "does not call the method `it` if defined" do
1140-
o = Object.new
1141-
def o.it
1142-
21
1143-
end
1144-
1145-
o.instance_eval("proc { it * 2 }").call(1).should == 2
1146-
end
1147-
end
11481097
end
11491098

1099+
# Duplicates specs in language/it_parameter_spec.rb
1100+
# Need them here to run on Ruby versions prior 3.4
1101+
# TODO: remove when the minimal supported Ruby version is 3.4
11501102
describe "if `it` is defined as a variable" do
11511103
it "treats `it` as a captured variable if defined outside of a block" do
11521104
it = 5

language/it_parameter_spec.rb

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,28 @@
1717
-> { it + -> { it * it }.call(2) }.call(3).should == 7
1818
end
1919

20+
it "can be reassigned to act as a local variable" do
21+
proc { tmp = it; it = tmp * 2; it }.call(21).should == 42
22+
end
23+
2024
it "is a regular local variable if there is already a 'it' local variable" do
21-
it = 0
22-
proc { it }.call("a").should == 0
25+
it = 0
26+
proc { it }.call("a").should == 0
27+
end
28+
29+
it "is a regular local variable if there is a method `it` defined" do
30+
o = Object.new
31+
def o.it
32+
21
33+
end
34+
35+
o.instance_eval("proc { it * 2 }").call(1).should == 2
36+
end
37+
38+
it "is not shadowed by an reassignment in a block" do
39+
a = nil
40+
proc { a = it; it = 42 }.call(0)
41+
a.should == 0 # if `it` were shadowed its value would be nil
2342
end
2443

2544
it "raises SyntaxError when block parameters are specified explicitly" do
@@ -36,6 +55,16 @@
3655
-> { eval("['a'].map { |x| it }") }.should raise_error(SyntaxError, /ordinary parameter is defined/)
3756
end
3857

58+
it "cannot be mixed with numbered parameters" do
59+
-> {
60+
eval("proc { it + _1 }")
61+
}.should raise_error(SyntaxError, /numbered parameters are not allowed when 'it' is already used|'it' is already used in/)
62+
63+
-> {
64+
eval("proc { _1 + it }")
65+
}.should raise_error(SyntaxError, /numbered parameter is already used in|'it' is not allowed when a numbered parameter is already used/)
66+
end
67+
3968
it "affects block arity" do
4069
-> {}.arity.should == 0
4170
-> { it }.arity.should == 1
@@ -62,5 +91,16 @@ def obj.foo; it; end
6291

6392
-> { obj.foo("a") }.should raise_error(ArgumentError, /wrong number of arguments/)
6493
end
94+
95+
context "given multiple arguments" do
96+
it "provides it in a block and assigns the first argument for a block" do
97+
proc { it }.call("a", "b").should == "a"
98+
end
99+
100+
it "raises ArgumentError for a proc" do
101+
-> { -> { it }.call("a", "b") }.should raise_error(ArgumentError, "wrong number of arguments (given 2, expected 1)")
102+
-> { lambda { it }.call("a", "b") }.should raise_error(ArgumentError, "wrong number of arguments (given 2, expected 1)")
103+
end
104+
end
65105
end
66106
end

0 commit comments

Comments
 (0)