Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit a7cfcf2

Browse files
committed
Replace VersionChecker with ComparableVersion
1 parent 9bdb4ab commit a7cfcf2

File tree

5 files changed

+67
-82
lines changed

5 files changed

+67
-82
lines changed

Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Enhancements:
44

55
* Improve formatting of `Delegator` based objects (e.g. `SimpleDelgator`) in
66
failure messages and diffs. (Andrew Horner, #215)
7+
* Replace `VersionChecker` with `ComparableVersion` (Yuji Nakayama, #245).
78

89
Bug Fixes:
910

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
module RSpec
2+
module Support
3+
# @private
4+
class ComparableVersion
5+
include Comparable
6+
7+
attr_reader :string
8+
9+
def initialize(string)
10+
@string = string
11+
end
12+
13+
def <=>(other)
14+
other = self.class.new(other) unless other.is_a?(self.class)
15+
16+
return 0 if string == other.string
17+
18+
longer_segment_count = [self, other].map { |version| version.segments.count }.max
19+
20+
longer_segment_count.times do |index|
21+
self_segment = segments[index] || 0
22+
other_segment = other.segments[index] || 0
23+
result = self_segment <=> other_segment
24+
return result unless result == 0
25+
end
26+
27+
0
28+
end
29+
30+
def segments
31+
@segments ||= string.split('.').map { |v| v.to_i }
32+
end
33+
end
34+
end
35+
end

lib/rspec/support/version_checker.rb

Lines changed: 0 additions & 53 deletions
This file was deleted.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
require 'rspec/support/comparable_version'
2+
3+
module RSpec::Support
4+
describe ComparableVersion do
5+
describe '#<=>' do
6+
[
7+
['1.2.3', '1.2.3', 0],
8+
['1.2.4', '1.2.3', 1],
9+
['1.3.0', '1.2.3', 1],
10+
['1.2.3', '1.2.4', -1],
11+
['1.2.3', '1.3.0', -1],
12+
['1.2.10', '1.2.3', 1],
13+
['1.2.3', '1.2.10', -1],
14+
['1.2.3.0', '1.2.3', 0],
15+
['1.2.3', '1.2.3.0', 0],
16+
['1.2.3.1', '1.2.3', 1],
17+
['1.2.3.1', '1.2.3.0', 1],
18+
['1.2.3', '1.2.3.1', -1],
19+
['1.2.3.0', '1.2.3.1', -1]
20+
].each do |subject_string, other_string, expected|
21+
context "with #{subject_string.inspect} and #{other_string.inspect}" do
22+
subject do
23+
ComparableVersion.new(subject_string) <=> ComparableVersion.new(other_string)
24+
end
25+
26+
it { should eq(expected) }
27+
end
28+
end
29+
end
30+
end
31+
end

spec/rspec/support/version_checker_spec.rb

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)