Skip to content

Commit a4f13d2

Browse files
authored
Methods are sorted symbols-first (#1219)
There are three distinct ranges of symbols in ASCII: - the range below "A", 0..64 in decimal - the range between "Z" and "a", 91..96 in decimal - the range above "z", 123..127 in decimal With this change, any method starting with a character in these "symbol ranges" will be sorted before a method starting with an alpha ASCII character. The remaining methods, all starting with alpha or 8-bit characters, will be sorted against each other exactly as before. Specifically this addresses the issue from #1204 which is that `#[]` and `#^` were previously sorted _after_ the alpha methods. These methods will now be sorted before alpha methods. Fixes #1204
1 parent 1ecd958 commit a4f13d2

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

lib/rdoc/code_object/method_attr.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ def <=>(other)
114114
return unless other.respond_to?(:singleton) &&
115115
other.respond_to?(:name)
116116

117-
[ @singleton ? 0 : 1, name] <=>
118-
[other.singleton ? 0 : 1, other.name]
117+
[@singleton ? 0 : 1, name_codepoint_range, name] <=>
118+
[other.singleton ? 0 : 1, other.name_codepoint_range, other.name]
119119
end
120120

121121
def == other # :nodoc:
@@ -415,4 +415,16 @@ def to_s # :nodoc:
415415
end
416416
end
417417

418+
def name_codepoint_range # :nodoc:
419+
case name.codepoints[0]
420+
when 0..64 # anything below "A"
421+
1
422+
when 91..96 # the symbols between "Z" and "a"
423+
2
424+
when 123..126 # 7-bit symbols above "z": "{", "|", "}", "~"
425+
3
426+
else # everythig else can be sorted as normal
427+
4
428+
end
429+
end
418430
end

test/rdoc/test_rdoc_method_attr.rb

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,36 @@ def test_search_record
148148
assert_equal expected, @c1_m.search_record
149149
end
150150

151-
def test_spaceship
151+
def test_spaceship_returns_nil_on_inappropriate_types
152152
assert_nil @c1_m.<=>(RDoc::CodeObject.new)
153153
end
154154

155+
def test_spaceship_orders_symbols_first
156+
# in the desired sort order
157+
m_plus = RDoc::AnyMethod.new nil, '+'
158+
m_eqeq = RDoc::AnyMethod.new nil, '=='
159+
m_bracket = RDoc::AnyMethod.new nil, '[]'
160+
m_caret = RDoc::AnyMethod.new nil, '^'
161+
m_bar = RDoc::AnyMethod.new nil, '|'
162+
m_tilde = RDoc::AnyMethod.new nil, '~'
163+
m_Alpha = RDoc::AnyMethod.new nil, 'Alpha'
164+
m_Zero = RDoc::AnyMethod.new nil, 'Zero'
165+
m_alpha = RDoc::AnyMethod.new nil, 'alpha'
166+
m_zero = RDoc::AnyMethod.new nil, 'zero'
167+
m_konnichiwa = RDoc::AnyMethod.new nil, 'こんにちは'
168+
169+
assert_equal(-1, m_plus <=> m_eqeq)
170+
assert_equal(-1, m_eqeq <=> m_bracket)
171+
assert_equal(-1, m_bracket <=> m_caret)
172+
assert_equal(-1, m_caret <=> m_bar)
173+
assert_equal(-1, m_bar <=> m_tilde)
174+
assert_equal(-1, m_tilde <=> m_Alpha)
175+
assert_equal(-1, m_Alpha <=> m_Zero)
176+
assert_equal(-1, m_Zero <=> m_alpha)
177+
assert_equal(-1, m_alpha <=> m_zero)
178+
assert_equal(-1, m_zero <=> m_konnichiwa)
179+
end
180+
155181
def test_equals2
156182
assert_equal @c1_m, @c1_m
157183
refute_equal @c1_m, @parent_m

0 commit comments

Comments
 (0)