Skip to content

Commit f6a23a2

Browse files
transclaude
andcommitted
Fix test failures: Range precedence, step, to_ranges, occurrence
- Fix Range assertion tests: wrap range literals in parens to avoid operator precedence issues with assert == - Fix Array#step tests for new 0-based offset behavior - Fix Array#to_ranges to always return Range objects (not bare values) - Update arrange/rangify tests to expect single-element ranges - Undeprecate Array#occurrence (block form is unique vs tally) - Fix Range#+ tests for new to_ranges behavior Test results: 1270 tests, 1215 passing, 36 errors, 5 failures, 14 pending Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent c07ef01 commit f6a23a2

File tree

7 files changed

+55
-37
lines changed

7 files changed

+55
-37
lines changed

lib/core/facets/array/occurrence.rb

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,22 @@ class Array
1111
# [2,2,3,4,4,4].occurrence{|i| i % 2}
1212
# #=> { 0 => 5, 1 => 1 }
1313
#
14-
def occurrence(&block)
15-
warn "Array#occurrence is deprecated. Use Array#tally instead.", uplevel: 1
16-
tally(&block)
14+
# Without a block, this is equivalent to Ruby's #tally.
15+
# With a block, the block transforms the key before counting,
16+
# which #tally does not support.
17+
#
18+
def occurrence
19+
h = Hash.new(0)
20+
if block_given?
21+
each do |e|
22+
h[yield(e)] += 1
23+
end
24+
else
25+
each do |e|
26+
h[e] += 1
27+
end
28+
end
29+
h
1730
end
1831

1932
end
20-

lib/core/facets/array/to_ranges.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ class Array
22

33
# Produces an array of ranges from the values in the array.
44
# Contiguous values and overlapping ranges are merged.
5+
# Single values are returned as single-element ranges (e.g. 1..1).
56
#
67
# Examples
78
#
89
# [1,2,3,6,7,8].to_ranges #=> [1..3, 6..8]
910
#
10-
# [3,4,5,1,6,9,8].to_ranges #=> [1..6, 8..9]
11+
# [3,4,5,1,6,9,8].to_ranges #=> [1..1, 3..6, 8..9]
1112
#
1213
# [10..15, 16..20, 21, 22].to_ranges #=> [10..22]
1314
#
@@ -20,7 +21,7 @@ class Array
2021

2122
def to_ranges
2223
array = compact.uniq.sort_by { |e| Range === e ? e.first : e }
23-
array.inject([]) do |c, value|
24+
result = array.inject([]) do |c, value|
2425
unless c.empty?
2526
last = c.last
2627
last_value = (Range === last ? last.last : last)
@@ -37,6 +38,8 @@ def to_ranges
3738
c << value
3839
end
3940
end
41+
# Ensure all elements are ranges
42+
result.map { |e| Range === e ? e : e..e }
4043
end
4144

4245
alias arrange to_ranges

test/core/array/test_arrange.rb

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,33 @@
44

55
method :rangify do
66
test 'Array of consecutive integers should return an array made up of a single range.' do
7-
[1,2,3,4,5].rangify.assert == [1..5]
7+
[1,2,3,4,5].rangify.assert == ([1..5])
88
end
9-
10-
test 'Array of non-consecutive integers should return the original array.' do
11-
[1,3,5,7].rangify.assert == [1,3,5,7]
9+
10+
test 'Array of non-consecutive integers should return single-element ranges.' do
11+
[1,3,5,7].rangify.assert == ([1..1, 3..3, 5..5, 7..7])
1212
end
1313

1414
test 'Array of ranges should return the correct ranges.' do
1515
arr = [40..45, 1..3, 4..10, 20..30, 24..28, 42..50, 1..6, 1..3, 1..1]
16-
arr.rangify.assert == [1..10, 20..30, 40..50]
16+
arr.rangify.assert == ([1..10, 20..30, 40..50])
1717
end
18-
18+
1919
test 'Array of ranges and integers should return the correct ranges.' do
2020
arr = [99, 100, 1..3, 101, 4..5, 103, 10..19, 99, 20..20, 31, 32..33, 98, 97]
21-
arr.rangify.assert == [1..5, 10..20, 31..33, 97..101, 103]
22-
end
21+
arr.rangify.assert == ([1..5, 10..20, 31..33, 97..101, 103..103])
22+
end
2323

2424
test 'Array of non-consecutive integers should return the correct ranges.' do
25-
[1,2,3,6,7,8,10,15].rangify.assert == [1..3, 6..8, 10, 15]
25+
[1,2,3,6,7,8,10,15].rangify.assert == ([1..3, 6..8, 10..10, 15..15])
2626
end
27-
27+
2828
test 'Element order should not affect the result.' do
29-
[8, 1, 15, 2, 6, 3, 7, 10].rangify.assert == [1..3, 6..8, 10, 15]
29+
[8, 1, 15, 2, 6, 3, 7, 10].rangify.assert == ([1..3, 6..8, 10..10, 15..15])
3030
end
31-
31+
3232
test 'Duplicate elements should not affect the result.' do
33-
[8, 1, 15, 2, 6, 3, 7, 10, 8, 15, 2, 3, 1, 2].rangify.assert == [1..3, 6..8, 10, 15]
33+
[8, 1, 15, 2, 6, 3, 7, 10, 8, 15, 2, 3, 1, 2].rangify.assert == ([1..3, 6..8, 10..10, 15..15])
3434
end
3535
end
3636

test/core/array/test_step.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,22 @@
1010

1111
a.step(2){ |e| r << e }
1212

13-
r.assert == [:b, :d]
13+
r.assert == [:a, :c]
1414
end
1515

1616
test "conversion to array" do
1717
a = [:a, :b, :c, :d]
1818
a.step(1).to_a.assert == [:a, :b, :c, :d]
19-
a.step(2).to_a.assert == [:b, :d]
20-
a.step(3).to_a.assert == [:c]
21-
a.step(5).to_a.assert == []
19+
a.step(2).to_a.assert == [:a, :c]
20+
a.step(3).to_a.assert == [:a, :d]
21+
a.step(5).to_a.assert == [:a]
22+
end
23+
24+
test "with offset" do
25+
a = [:a, :b, :c, :d, :e, :f]
26+
a.step(2, 1).to_a.assert == [:b, :d, :f]
2227
end
2328

2429
end
2530

2631
end
27-
28-

test/core/array/test_to_ranges.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
method :to_ranges do
66

77
test "flat values" do
8-
[3,4,5,1,6,9,8].to_ranges.assert == [1..1, 3..6, 8..9]
8+
[3,4,5,1,6,9,8].to_ranges.assert == ([1..1, 3..6, 8..9])
99
end
1010

1111
test "consecutive" do
12-
[1,2,3,4,5].to_ranges.assert == [1..5]
12+
[1,2,3,4,5].to_ranges.assert == ([1..5])
1313
end
1414

1515
test "mixed ranges and values" do
16-
[10..15, 16..20, 21, 22].to_ranges.assert == [10..22]
16+
[10..15, 16..20, 21, 22].to_ranges.assert == ([10..22])
1717
end
1818

1919
end

test/core/range/test_intersection.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,39 @@
55
class_method :intersection do
66

77
test "two overlapping ranges" do
8-
Range.intersection(1..10, 5..15).assert == 5..10
8+
Range.intersection(1..10, 5..15).assert == (5..10)
99
end
1010

1111
test "three overlapping ranges" do
12-
Range.intersection(1..5, 3..7, 4..9).assert == 4..5
12+
Range.intersection(1..5, 3..7, 4..9).assert == (4..5)
1313
end
1414

1515
test "non-overlapping ranges" do
1616
Range.intersection(1..3, 5..7).assert == nil
1717
end
1818

1919
test "floats" do
20-
Range.intersection(1.0..5.0, 2.5..4.5).assert == 2.5..4.5
20+
Range.intersection(1.0..5.0, 2.5..4.5).assert == (2.5..4.5)
2121
end
2222

2323
test "identical ranges" do
24-
Range.intersection(1..5, 1..5).assert == 1..5
24+
Range.intersection(1..5, 1..5).assert == (1..5)
2525
end
2626

2727
test "one range contains the other" do
28-
Range.intersection(1..10, 3..5).assert == 3..5
28+
Range.intersection(1..10, 3..5).assert == (3..5)
2929
end
3030

3131
end
3232

3333
method :intersection do
3434

3535
test "instance method" do
36-
(1..10).intersection(5..15).assert == 5..10
36+
(1..10).intersection(5..15).assert == (5..10)
3737
end
3838

3939
test "multiple arguments" do
40-
(1..10).intersection(5..15, 8..20).assert == 8..10
40+
(1..10).intersection(5..15, 8..20).assert == (8..10)
4141
end
4242

4343
end

test/core/range/test_op_add.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
#test { lambda{ (1..10) + 'a' }.assert raise_error }
77

88
test { ((1..10) + (11..11)).assert == [1..11] }
9-
test { ((1..10) + 12).assert == [1..10, 12] }
9+
test { ((1..10) + 12).assert == ([1..10, 12..12]) }
1010
test { ((1..10) + (10..12)).assert == [1..12] }
1111
test { ((1..10) + (11..12)).assert == [1..12] }
1212
test { ((1..10) + (9..12)).assert == [1..12] }
1313
test { ((1..10) + (1..12)).assert == [1..12] }
1414
test { ((1..10) + (12..20)).assert == [1..10, 12..20] }
1515
test { ((1..10) + 0).assert == [0..10] }
16-
test { ((1..10) + -1).assert == [-1, 1..10] }
16+
test { ((1..10) + -1).assert == ([-1..-1, 1..10]) }
1717
test { ((1..10) + (-10..-1)).assert == [-10..-1, 1..10] }
1818
test { ((1..10) + (-10..0)).assert == [-10..10] }
1919
test { ((1..10) + (-10..1)).assert == [-10..10] }

0 commit comments

Comments
 (0)