Skip to content

Commit 609fca1

Browse files
committed
Remove some cruft from Version and fix to_string/1
1 parent eb7c541 commit 609fca1

File tree

2 files changed

+32
-29
lines changed

2 files changed

+32
-29
lines changed

lib/elixir/lib/version.ex

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -417,49 +417,45 @@ defmodule Version do
417417
defp to_condition([:'>', version | _]) do
418418
{major, minor, patch, pre} = parse_condition(version)
419419

420-
{:andalso, {:not, {:is_binary, :'$1'}},
421-
{:orelse, {:'>', {{:'$1', :'$2', :'$3'}},
422-
{:const, {major, minor, patch}}},
423-
{:andalso, {:'==', {{:'$1', :'$2', :'$3'}},
424-
{:const, {major, minor, patch}}},
425-
{:orelse, {:andalso, {:'==', {:length, :'$4'}, 0},
426-
{:'/=', length(pre), 0}},
427-
{:andalso, {:'/=', length(pre), 0},
428-
{:orelse, {:'>', {:length, :'$4'}, length(pre)},
429-
{:andalso, {:'==', {:length, :'$4'}, length(pre)},
430-
{:'>', :'$4', {:const, pre}}}}}}}}}
420+
{:orelse, {:'>', {{:'$1', :'$2', :'$3'}},
421+
{:const, {major, minor, patch}}},
422+
{:andalso, {:'==', {{:'$1', :'$2', :'$3'}},
423+
{:const, {major, minor, patch}}},
424+
{:orelse, {:andalso, {:'==', {:length, :'$4'}, 0},
425+
{:'/=', length(pre), 0}},
426+
{:andalso, {:'/=', length(pre), 0},
427+
{:orelse, {:'>', {:length, :'$4'}, length(pre)},
428+
{:andalso, {:'==', {:length, :'$4'}, length(pre)},
429+
{:'>', :'$4', {:const, pre}}}}}}}}
431430
end
432431

433432
defp to_condition([:'>=', version | _]) do
434433
matchable = parse_condition(version)
435434

436-
{:orelse, {:andalso, {:not, {:is_binary, :'$1'}},
437-
{:'==', :'$_', {:const, matchable}}},
438-
to_condition([:'>', version])}
435+
{:orelse, {:'==', :'$_', {:const, matchable}},
436+
to_condition([:'>', version])}
439437
end
440438

441439
defp to_condition([:'<', version | _]) do
442440
{major, minor, patch, pre} = parse_condition(version)
443441

444-
{:andalso, {:not, {:is_binary, :'$1'}},
445-
{:orelse, {:'<', {{:'$1', :'$2', :'$3'}},
446-
{:const, {major, minor, patch}}},
447-
{:andalso, {:'==', {{:'$1', :'$2', :'$3'}},
448-
{:const, {major, minor, patch}}},
449-
{:orelse, {:andalso, {:'/=', {:length, :'$4'}, 0},
450-
{:'==', length(pre), 0}},
451-
{:andalso, {:'/=', {:length, :'$4'}, 0},
452-
{:orelse, {:'<', {:length, :'$4'}, length(pre)},
453-
{:andalso, {:'==', {:length, :'$4'}, length(pre)},
454-
{:'<', :'$4', {:const, pre}}}}}}}}}
442+
{:orelse, {:'<', {{:'$1', :'$2', :'$3'}},
443+
{:const, {major, minor, patch}}},
444+
{:andalso, {:'==', {{:'$1', :'$2', :'$3'}},
445+
{:const, {major, minor, patch}}},
446+
{:orelse, {:andalso, {:'/=', {:length, :'$4'}, 0},
447+
{:'==', length(pre), 0}},
448+
{:andalso, {:'/=', {:length, :'$4'}, 0},
449+
{:orelse, {:'<', {:length, :'$4'}, length(pre)},
450+
{:andalso, {:'==', {:length, :'$4'}, length(pre)},
451+
{:'<', :'$4', {:const, pre}}}}}}}}
455452
end
456453

457454
defp to_condition([:'<=', version | _]) do
458455
matchable = parse_condition(version)
459456

460-
{:orelse, {:andalso, {:not, {:is_binary, :'$1'}},
461-
{:'==', :'$_', {:const, matchable}}},
462-
to_condition([:'<', version])}
457+
{:orelse, {:'==', :'$_', {:const, matchable}},
458+
to_condition([:'<', version])}
463459
end
464460

465461
defp to_condition(current, []) do
@@ -491,7 +487,7 @@ end
491487

492488
defimpl String.Chars, for: Version do
493489
def to_string(version) do
494-
pre = if pre = version.pre, do: "-#{pre}"
490+
pre = unless Enum.empty?(pre = version.pre), do: "-#{pre}"
495491
build = if build = version.build, do: "+#{build}"
496492
"#{version.major}.#{version.minor}.#{version.patch}#{pre}#{build}"
497493
end

lib/elixir/test/elixir/version_test.exs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ defmodule VersionTest do
6767
assert :error = V.parse("2.3.0-01")
6868
end
6969

70+
test "to_string" do
71+
assert V.parse("1.0.0") |> elem(1) |> to_string == "1.0.0"
72+
assert V.parse("1.0.0-dev") |> elem(1) |> to_string == "1.0.0-dev"
73+
assert V.parse("1.0.0+lol") |> elem(1) |> to_string == "1.0.0+lol"
74+
assert V.parse("1.0.0-dev+lol") |> elem(1) |> to_string == "1.0.0-dev+lol"
75+
end
76+
7077
test "invalid match" do
7178
assert_raise V.InvalidVersion, fn ->
7279
V.match?("foo", "2.3.0")

0 commit comments

Comments
 (0)