diff options
author | Eric Meadows-Jönsson <eric.meadows.jonsson@gmail.com> | 2021-11-26 15:38:52 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-26 15:38:52 +0100 |
commit | 0fd981a4989f0c1b669605c59d83b77497c337d6 (patch) | |
tree | 75271c4215bce83625cae6b14cf8b66cfa21c221 | |
parent | 209688fc52bd35c65c892fc13d32fd1196f4039a (diff) | |
download | elixir-0fd981a4989f0c1b669605c59d83b77497c337d6.tar.gz |
Add Version.to_string/1 (#11422)
-rw-r--r-- | lib/elixir/lib/version.ex | 50 | ||||
-rw-r--r-- | lib/elixir/test/elixir/version_test.exs | 16 |
2 files changed, 40 insertions, 26 deletions
diff --git a/lib/elixir/lib/version.ex b/lib/elixir/lib/version.ex index 74ea4f3f7..d798efbeb 100644 --- a/lib/elixir/lib/version.ex +++ b/lib/elixir/lib/version.ex @@ -458,6 +458,36 @@ defmodule Version do end end + @doc """ + Converts the given version to a string. + + ### Examples + + iex> Version.to_string(%Version{major: 1, minor: 2, patch: 3}) + "1.2.3" + iex> Version.to_string(Version.parse!("1.14.0-rc.0+build0")) + "1.14.0-rc.0+build0" + """ + @doc since: "1.14.0" + @spec to_string(Version.t()) :: String.t() + def to_string(%Version{} = version) do + pre = pre_to_string(version.pre) + build = if build = version.build, do: "+#{build}" + "#{version.major}.#{version.minor}.#{version.patch}#{pre}#{build}" + end + + defp pre_to_string([]) do + "" + end + + defp pre_to_string(pre) do + "-" <> + Enum.map_join(pre, ".", fn + int when is_integer(int) -> Integer.to_string(int) + string when is_binary(string) -> string + end) + end + defmodule Parser do @moduledoc false @@ -627,28 +657,12 @@ defmodule Version do end defimpl String.Chars, for: Version do - def to_string(version) do - pre = pre(version.pre) - build = if build = version.build, do: "+#{build}" - "#{version.major}.#{version.minor}.#{version.patch}#{pre}#{build}" - end - - defp pre([]) do - "" - end - - defp pre(pre) do - "-" <> - Enum.map_join(pre, ".", fn - int when is_integer(int) -> Integer.to_string(int) - string when is_binary(string) -> string - end) - end + defdelegate to_string(version), to: Version end defimpl Inspect, for: Version do def inspect(self, _opts) do - "#Version<" <> to_string(self) <> ">" + "#Version<" <> Version.to_string(self) <> ">" end end diff --git a/lib/elixir/test/elixir/version_test.exs b/lib/elixir/test/elixir/version_test.exs index 9fc4afb0a..60b073e01 100644 --- a/lib/elixir/test/elixir/version_test.exs +++ b/lib/elixir/test/elixir/version_test.exs @@ -99,14 +99,14 @@ defmodule VersionTest do assert Version.parse("0.1.0-&&pre") == :error end - test "Kernel.to_string/1" do - assert Version.parse!("1.0.0") |> to_string == "1.0.0" - assert Version.parse!("1.0.0-dev") |> to_string == "1.0.0-dev" - assert Version.parse!("1.0.0+lol") |> to_string == "1.0.0+lol" - assert Version.parse!("1.0.0-dev+lol") |> to_string == "1.0.0-dev+lol" - assert Version.parse!("1.0.0-0") |> to_string == "1.0.0-0" - assert Version.parse!("1.0.0-rc.0") |> to_string == "1.0.0-rc.0" - assert %Version{major: 1, minor: 0, patch: 0} |> to_string() == "1.0.0" + test "to_string/1" do + assert Version.parse!("1.0.0") |> Version.to_string() == "1.0.0" + assert Version.parse!("1.0.0-dev") |> Version.to_string() == "1.0.0-dev" + assert Version.parse!("1.0.0+lol") |> Version.to_string() == "1.0.0+lol" + assert Version.parse!("1.0.0-dev+lol") |> Version.to_string() == "1.0.0-dev+lol" + assert Version.parse!("1.0.0-0") |> Version.to_string() == "1.0.0-0" + assert Version.parse!("1.0.0-rc.0") |> Version.to_string() == "1.0.0-rc.0" + assert %Version{major: 1, minor: 0, patch: 0} |> Version.to_string() == "1.0.0" end test "match?/2 with invalid versions" do |