Skip to content

Commit 84fbba7

Browse files
committed
Teach version to consider dev string when comparing
Versions now use their dev strings when comparing themselves to another version. This is currently just naive alphabetical order so "RC10" < "RC2" Versions with no dev string are deemed to be more recent than ones with dev string regardless of the content of the dev string.
1 parent 085e67b commit 84fbba7

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

app/server/sonicpi/lib/sonicpi/version.rb

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,24 @@ def <=>(other)
5959
if ((other.is_a? Version) &&
6060
(@major < other.major) or
6161
((@major == other.major) && (@minor < other.minor)) or
62-
((@major == other.major) && (@minor == other.minor) && (@patch < other.patch)))
63-
-1
62+
((@major == other.major) && (@minor == other.minor) && (@patch < other.patch)) or
63+
if (@dev && other.dev)
64+
((@major == other.major) && (@minor == other.minor) && (@patch == other.patch) && (@dev.to_s < other.dev.to_s))
65+
else
66+
((@major == other.major) && (@minor == other.minor) && (@patch == other.patch) && @dev)
67+
end)
68+
return -1
6469
elsif
6570
((other.is_a? Version) &&
66-
(@major > other.major) or
71+
(@major > other.major) or
6772
((@major == other.major) && (@minor > other.minor)) or
68-
((@major == other.major) && (@minor == other.minor) && (@patch > other.patch)))
69-
return 1
73+
((@major == other.major) && (@minor == other.minor) && (@patch > other.patch)) or
74+
if (@dev && other.dev)
75+
((@major == other.major) && (@minor == other.minor) && (@patch == other.patch) && (@dev.to_s > other.dev.to_s))
76+
else
77+
((@major == other.major) && (@minor == other.minor) && (@patch == other.patch) && other.dev)
78+
end)
79+
return 1
7080
elsif ((other.is_a? Version) &&
7181
(@major == other.major) &&
7282
(@minor == other.minor) &&

app/server/sonicpi/test/test_version.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,30 @@ def test_greater_than
6060
assert_equal(true, v1 > v2)
6161
end
6262

63+
def test_greater_than_with_dev
64+
v1 = Version.new(2, 1, 1)
65+
v2 = Version.new(2, 1, 1, "dev")
66+
assert_equal(true, v1 > v2)
67+
end
68+
69+
def test_less_than_with_dev
70+
v1 = Version.new(2, 1, 1, "dev")
71+
v2 = Version.new(2, 1, 1)
72+
assert_equal(true, v1 < v2)
73+
end
74+
75+
def test_less_than_with_both_dev
76+
v1 = Version.new(2, 1, 1, "a")
77+
v2 = Version.new(2, 1, 1, "b")
78+
assert_equal(true, v1 < v2)
79+
end
80+
81+
def test_greater_than_with_both_dev
82+
v1 = Version.new(2, 1, 1, "c")
83+
v2 = Version.new(2, 1, 1, "a")
84+
assert_equal(true, v1 > v2)
85+
end
86+
6387
def test_less_than_or_equal_equality
6488
v1 = Version.new(2, 1, 0, "RC12")
6589
v2 = Version.new(2, 1, 0, "RC12")

0 commit comments

Comments
 (0)