Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix each* methods to return only nil #3826

Merged
merged 4 commits into from
Jan 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion spec/compiler/semantic/block_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe "Block inference" do
assert_type("
require \"prelude\"
a = [1] || [1.1]
a.each { |x| x }
a.tap { |x| x }
") { union_of(array_of(int32), array_of(float64)) }
end

Expand Down
36 changes: 18 additions & 18 deletions spec/std/array_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ describe "Array" do
it "does each_index" do
a = [1, 1, 1]
b = 0
a.each_index { |i| b += i }
a.each_index { |i| b += i }.should be_nil
b.should eq(3)
end

Expand Down Expand Up @@ -1193,7 +1193,7 @@ describe "Array" do
a.each do
count += 1
a.clear
end
end.should be_nil
count.should eq(1)
end

Expand All @@ -1203,7 +1203,7 @@ describe "Array" do
a.each_index do
count += 1
a.clear
end
end.should be_nil
count.should eq(1)
end

Expand Down Expand Up @@ -1477,7 +1477,7 @@ describe "Array" do
sums = [] of Int32
[1, 2, 3].each_permutation(2) do |perm|
sums << perm.sum
end.should eq([1, 2, 3])
end.should be_nil
sums.should eq([3, 4, 3, 5, 4, 5])
end

Expand All @@ -1486,7 +1486,7 @@ describe "Array" do
[1, 2, 3].each_permutation(3) do |perm|
perm.map! &.+(1)
sums << perm.sum
end.should eq([1, 2, 3])
end.should be_nil
sums.should eq([9, 9, 9, 9, 9, 9])
end

Expand All @@ -1497,7 +1497,7 @@ describe "Array" do
object_ids << perm.object_id
perm.map! &.+(1)
sums << perm.sum
end.should eq([1, 2, 3])
end.should be_nil
sums.should eq([9, 9, 9, 9, 9, 9])
object_ids.size.should eq(1)
end
Expand Down Expand Up @@ -1561,7 +1561,7 @@ describe "Array" do
sums = [] of Int32
[1, 2, 3].each_combination(2) do |comb|
sums << comb.sum
end.should eq([1, 2, 3])
end.should be_nil
sums.should eq([3, 4, 5])
end

Expand All @@ -1570,7 +1570,7 @@ describe "Array" do
[1, 2, 3].each_combination(3) do |comb|
comb.map! &.+(1)
sums << comb.sum
end.should eq([1, 2, 3])
end.should be_nil
sums.should eq([9])
end

Expand All @@ -1580,7 +1580,7 @@ describe "Array" do
[1, 2, 3].each_combination(2, reuse: true) do |comb|
sums << comb.sum
object_ids << comb.object_id
end
end.should be_nil
sums.should eq([3, 4, 5])
object_ids.size.should eq(1)
end
Expand All @@ -1591,7 +1591,7 @@ describe "Array" do
[1, 2, 3].each_combination(2, reuse: reuse) do |comb|
sums << comb.sum
comb.should be(reuse)
end
end.should be_nil
sums.should eq([3, 4, 5])
end

Expand Down Expand Up @@ -1652,7 +1652,7 @@ describe "Array" do
sums = [] of Int32
[1, 2, 3].each_repeated_combination(2) do |comb|
sums << comb.sum
end.should eq([1, 2, 3])
end.should be_nil
sums.should eq([2, 3, 4, 4, 5, 6])
end

Expand All @@ -1661,7 +1661,7 @@ describe "Array" do
[1, 2, 3].each_repeated_combination(3) do |comb|
comb.map! &.+(1)
sums << comb.sum
end.should eq([1, 2, 3])
end.should be_nil
sums.should eq([6, 7, 8, 8, 9, 10, 9, 10, 11, 12])
end

Expand All @@ -1674,7 +1674,7 @@ describe "Array" do
object_ids << comb.object_id
comb.map! &.+(1)
sums << comb.sum
end.should eq([1, 2, 3])
end.should be_nil
sums.should eq([6, 7, 8, 8, 9, 10, 9, 10, 11, 12])
object_ids.size.should eq(1)
end
Expand All @@ -1686,7 +1686,7 @@ describe "Array" do
comb.should be(reuse)
comb.map! &.+(1)
sums << comb.sum
end.should eq([1, 2, 3])
end.should be_nil
sums.should eq([6, 7, 8, 8, 9, 10, 9, 10, 11, 12])
end

Expand Down Expand Up @@ -1745,7 +1745,7 @@ describe "Array" do
sums = [] of Int32
[1, 2, 3].each_repeated_permutation(2) do |a|
sums << a.sum
end.should eq([1, 2, 3])
end.should be_nil
sums.should eq([2, 3, 4, 3, 4, 5, 4, 5, 6])
end

Expand All @@ -1754,7 +1754,7 @@ describe "Array" do
[1, 2, 3].each_repeated_permutation(3) do |a|
a.map! &.+(1)
sums << a.sum
end.should eq([1, 2, 3])
end.should be_nil
sums.should eq([6, 7, 8, 7, 8, 9, 8, 9, 10, 7, 8, 9, 8, 9, 10, 9, 10, 11, 8, 9, 10, 9, 10, 11, 10, 11, 12])
end

Expand All @@ -1765,7 +1765,7 @@ describe "Array" do
object_ids << a.object_id
a.map! &.+(1)
sums << a.sum
end.should eq([1, 2, 3])
end.should be_nil
sums.should eq([6, 7, 8, 7, 8, 9, 8, 9, 10, 7, 8, 9, 8, 9, 10, 9, 10, 11, 8, 9, 10, 9, 10, 11, 10, 11, 12])
object_ids.size.should eq(1)
end
Expand All @@ -1777,7 +1777,7 @@ describe "Array" do
a.should be(reuse)
a.map! &.+(1)
sums << a.sum
end.should eq([1, 2, 3])
end.should be_nil
sums.should eq([6, 7, 8, 7, 8, 9, 8, 9, 10, 7, 8, 9, 8, 9, 10, 9, 10, 11, 8, 9, 10, 9, 10, 11, 10, 11, 12])
end

Expand Down
4 changes: 2 additions & 2 deletions spec/std/char/reader_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ describe "Char::Reader" do
sum = 0
reader.each do |char|
sum += char.ord
end
end.should be_nil
sum.should eq(294)
end

it "is an Enumerable(Char) but doesn't yield if empty" do
reader = Char::Reader.new("")
reader.each do |char|
fail "reader each shouldn't yield on empty string"
end
end.should be_nil
end

it "errors if 0x80 <= first_byte < 0xC2" do
Expand Down
4 changes: 4 additions & 0 deletions spec/std/char_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,10 @@ describe "Char" do
end
end

it "does each_byte" do
'a'.each_byte(&.should eq('a'.ord)).should be_nil
end

it "does bytes" do
'\u{FF}'.bytes.should eq([195, 191])
end
Expand Down
2 changes: 1 addition & 1 deletion spec/std/csv/csv_parse_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ describe CSV do
sum = 0
CSV.each_row("1,2\n3,4\n") do |row|
sum += row.map(&.to_i).sum
end
end.should be_nil
sum.should eq(10)
end

Expand Down
2 changes: 1 addition & 1 deletion spec/std/csv/csv_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ describe CSV do
csv.each do
csv["one"].should eq("1")
break
end
end.should be_nil
end

it "can do new with block" do
Expand Down
9 changes: 8 additions & 1 deletion spec/std/deque_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,17 @@ describe "Deque" do
a.should eq(Deque{x})
end

it "does each" do
a = Deque{1, 1, 1}
b = 0
a.each { |i| b += i }.should be_nil
b.should eq(3)
end

it "does each_index" do
a = Deque{1, 1, 1}
b = 0
a.each_index { |i| b += i }
a.each_index { |i| b += i }.should be_nil
b.should eq(3)
end

Expand Down
4 changes: 2 additions & 2 deletions spec/std/dir_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ describe "Dir" do
dir = Dir.new(__DIR__)
dir.each do |filename|
filenames << filename
end
end.should be_nil
dir.close

filenames.includes?("dir_spec.cr").should be_true
Expand All @@ -248,7 +248,7 @@ describe "Dir" do
Dir.open(__DIR__) do |dir|
dir.each do |filename|
filenames << filename
end
end.should be_nil
end

filenames.includes?("dir_spec.cr").should be_true
Expand Down
34 changes: 32 additions & 2 deletions spec/std/hash_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,36 @@ describe "Hash" do
%w(a c).should contain h3[1]
end

it "does each" do
hash = {"foo" => 1, "bar" => 2}
ks = [] of String
vs = [] of Int32
hash.each do |k, v|
ks << k
vs << v
end.should be_nil
ks.should eq(["foo", "bar"])
vs.should eq([1, 2])
end

it "does each_key" do
hash = {"foo" => 1, "bar" => 2}
ks = [] of String
hash.each_key do |k|
ks << k
end.should be_nil
ks.should eq(["foo", "bar"])
end

it "does each_value" do
hash = {"foo" => 1, "bar" => 2}
vs = [] of Int32
hash.each_value do |v|
vs << v
end.should be_nil
vs.should eq([1, 2])
end

it "gets each iterator" do
iter = {:a => 1, :b => 2}.each
iter.next.should eq({:a, 1})
Expand Down Expand Up @@ -569,14 +599,14 @@ describe "Hash" do
it "pass key, value, index values into block" do
hash = {2 => 4, 5 => 10, 7 => 14}
results = [] of Int32
hash.each_with_index { |(k, v), i| results << k + v + i }
hash.each_with_index { |(k, v), i| results << k + v + i }.should be_nil
results.should eq [6, 16, 23]
end

it "can be used with offset" do
hash = {2 => 4, 5 => 10, 7 => 14}
results = [] of Int32
hash.each_with_index(3) { |(k, v), i| results << k + v + i }
hash.each_with_index(3) { |(k, v), i| results << k + v + i }.should be_nil
results.should eq [9, 19, 26]
end
end
Expand Down
18 changes: 18 additions & 0 deletions spec/std/indexable_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,22 @@ describe Indexable do
indexable = SafeIndexable.new(3)
indexable.rindex(0, 100).should be_nil
end

it "does each" do
indexable = SafeIndexable.new(3)
is = [] of Int32
indexable.each do |i|
is << i
end.should be_nil
is.should eq([0, 1, 2])
end

it "does each_index" do
indexable = SafeIndexable.new(3)
is = [] of Int32
indexable.each_index do |i|
is << i
end.should be_nil
is.should eq([0, 1, 2])
end
end
Loading