diff options
author | Cory Schmitt <cory@schmitty.me> | 2018-12-01 13:22:58 -0800 |
---|---|---|
committer | Michał Muskała <michal@muskala.eu> | 2018-12-01 22:22:58 +0100 |
commit | e1b84b4cfe71b455ab44646c2c665c7065c32a78 (patch) | |
tree | 64062547a6ed38dac3376a375e416a3ca59ce175 | |
parent | f83fb2e7e33c9af0afb44713ff8cd7e8085442e3 (diff) | |
download | elixir-e1b84b4cfe71b455ab44646c2c665c7065c32a78.tar.gz |
IEx port/1,2 helpers (#8439)
* iex port helper
* updated for consistency
-rw-r--r-- | lib/iex/lib/iex/helpers.ex | 33 | ||||
-rw-r--r-- | lib/iex/test/iex/helpers_test.exs | 20 |
2 files changed, 53 insertions, 0 deletions
diff --git a/lib/iex/lib/iex/helpers.ex b/lib/iex/lib/iex/helpers.ex index 5829edcec..9a990e848 100644 --- a/lib/iex/lib/iex/helpers.ex +++ b/lib/iex/lib/iex/helpers.ex @@ -38,6 +38,8 @@ defmodule IEx.Helpers do * `open/1` - opens the source for the given module or function in your editor * `pid/1` - creates a PID from a string * `pid/3` - creates a PID with the 3 integer arguments passed + * `port/1` - creates a Port from a string + * `port/2` - creates a Port with the 2 non-negative integers passed * `ref/1` - creates a Reference from a string * `ref/4` - creates a Reference with the 4 integer arguments passed * `pwd/0` - prints the current working directory @@ -1210,6 +1212,37 @@ defmodule IEx.Helpers do end @doc """ + Creates a Port from `string`. + + ## Examples + + iex> port("0.4") + #Port<0.4> + + """ + def port(string) when is_binary(string) do + :erlang.list_to_port('#Port<#{string}>') + end + + @doc """ + Creates a Port from two non-negative integers. + + ## Examples + + iex> port(0, 8080) + #Port<0.8080> + iex> port(0, 443) + #Port<0.443> + + """ + def port(major, minor) + when is_integer(major) and major >= 0 and is_integer(minor) and minor >= 0 do + :erlang.list_to_port( + '#Port<' ++ Integer.to_charlist(major) ++ '.' ++ Integer.to_charlist(minor) ++ '>' + ) + end + + @doc """ Creates a Reference from `string`. ## Examples diff --git a/lib/iex/test/iex/helpers_test.exs b/lib/iex/test/iex/helpers_test.exs index c5d3e6dd2..233f3c38b 100644 --- a/lib/iex/test/iex/helpers_test.exs +++ b/lib/iex/test/iex/helpers_test.exs @@ -1143,6 +1143,26 @@ defmodule IEx.HelpersTest do end end + describe "port" do + test "builds a port from string" do + assert inspect(port("0.8080")) == "#Port<0.8080>" + assert inspect(port("0.0")) == "#Port<0.0>" + + assert_raise ArgumentError, fn -> + port("0.-6") + end + end + + test "builds a port from integers" do + assert inspect(port(0, 8080)) == "#Port<0.8080>" + assert inspect(port(0, 0)) == "#Port<0.0>" + + assert_raise FunctionClauseError, fn -> + port(-1, -6) + end + end + end + describe "ref" do test "builds a ref from string" do ref = make_ref() |