Fix errors with ref/4 IEx helper (#8016)

* Fix errors with ref/4 IEx helper

In addition, include documentation for that function.

* Update helpers.ex

* Add IEx.Helpers.ref/* tests

* Use examples that do not return ArgumentError

Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
This commit is contained in:
Fernando Tapia Rico
2018-08-04 14:07:37 +02:00
committed by José Valim
parent b0cf76c74c
commit b9d6f4f9c1
2 changed files with 33 additions and 4 deletions
+13 -4
View File
@@ -1204,8 +1204,8 @@ defmodule IEx.Helpers do
## Examples
iex> ref("0.21.32.43")
#Reference<0.21.32.43>
iex> ref("0.1.2.3")
#Reference<0.1.2.3>
"""
@doc since: "1.6.0"
@@ -1213,16 +1213,25 @@ defmodule IEx.Helpers do
:erlang.list_to_ref('#Ref<#{string}>')
end
@doc """
Creates a Reference from its 4 non-negative integers components.
## Examples
iex> ref(0, 1, 2, 3)
#Reference<0.1.2.3>
"""
@doc since: "1.6.0"
def ref(w, x, y, z)
when is_integer(w) and w >= 0 and is_integer(x) and x >= 0 and is_integer(y) and y >= 0 and
is_integer(z) and z >= 0 do
:erlang.list_to_ref(
'<' ++
'#Ref<' ++
Integer.to_charlist(w) ++
'.' ++
Integer.to_charlist(x) ++
'.' ++ '.' ++ Integer.to_charlist(y) ++ '.' ++ '.' ++ Integer.to_charlist(z) ++ '.' ++ '>'
'.' ++ Integer.to_charlist(y) ++ '.' ++ Integer.to_charlist(z) ++ '>'
)
end
+20
View File
@@ -1090,6 +1090,26 @@ defmodule IEx.HelpersTest do
end
end
describe "ref" do
test "builds a ref from string" do
assert inspect(ref("0.419006.1377304581.1")) == "#Reference<0.419006.1377304581.1>"
assert inspect(ref("0.1.2.3")) == "#Reference<0.1.2.3>"
assert_raise ArgumentError, fn ->
ref("0.6.6.-6")
end
end
test "builds a ref from integers" do
assert inspect(ref(0, 419_006, 1_377_304_581, 1)) == "#Reference<0.419006.1377304581.1>"
assert inspect(ref(0, 1, 2, 3)) == "#Reference<0.1.2.3>"
assert_raise FunctionClauseError, fn ->
ref(0, 6, 6, -6)
end
end
end
describe "i" do
test "prints information about the data type" do
assert capture_io(fn -> i(:ok) end) =~ """