Skip to content

Commit bdab804

Browse files
author
José Valim
committed
Rename StringIO API to be more IO like (start -> open, stop -> close)
1 parent e8ea196 commit bdab804

File tree

4 files changed

+34
-52
lines changed

4 files changed

+34
-52
lines changed

lib/elixir/lib/string_io.ex

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ defmodule StringIO do
44
55
## Examples
66
7-
iex> { :ok, pid } = StringIO.start("foo")
7+
iex> { :ok, pid } = StringIO.open("foo")
88
iex> IO.read(pid, 2)
99
"fo"
1010
"""
@@ -22,31 +22,21 @@ defmodule StringIO do
2222
2323
## Examples
2424
25-
iex> { :ok, pid } = StringIO.start("foo")
25+
iex> { :ok, pid } = StringIO.open("foo")
2626
iex> IO.gets(pid, ">")
2727
"foo"
28-
iex> StringIO.peek(pid)
28+
iex> StringIO.contents(pid)
2929
{ "", "" }
3030
31-
iex> { :ok, pid } = StringIO.start("foo", capture_prompt: true)
31+
iex> { :ok, pid } = StringIO.open("foo", capture_prompt: true)
3232
iex> IO.gets(pid, ">")
3333
"foo"
34-
iex> StringIO.peek(pid)
34+
iex> StringIO.contents(pid)
3535
{ "", ">" }
3636
3737
"""
38-
@spec start(binary, Keyword.t) :: { :ok, pid }
39-
def start(string, options \\ []) when is_binary(string) do
40-
:gen_server.start(__MODULE__, { string, options }, [])
41-
end
42-
43-
@doc """
44-
Creates an IO device.
45-
46-
See also `start/2`
47-
"""
48-
@spec start_link(binary, Keyword.t) :: { :ok, pid }
49-
def start_link(string, options \\ []) when is_binary(string) do
38+
@spec open(binary, Keyword.t) :: { :ok, pid }
39+
def open(string, options \\ []) when is_binary(string) do
5040
:gen_server.start_link(__MODULE__, { string, options }, [])
5141
end
5242

@@ -55,29 +45,29 @@ defmodule StringIO do
5545
5646
## Examples
5747
58-
iex> { :ok, pid } = StringIO.start("in")
48+
iex> { :ok, pid } = StringIO.open("in")
5949
...> IO.write(pid, "out")
60-
...> StringIO.peek(pid)
50+
...> StringIO.contents(pid)
6151
{ "in", "out" }
6252
"""
63-
@spec peek(pid) :: { binary, binary }
64-
def peek(pid) when is_pid(pid) do
65-
:gen_server.call(pid, :peek)
53+
@spec contents(pid) :: { binary, binary }
54+
def contents(pid) when is_pid(pid) do
55+
:gen_server.call(pid, :contents)
6656
end
6757

6858
@doc """
6959
Stops the IO device and returns remaining buffers.
7060
7161
## Examples
7262
73-
iex> { :ok, pid } = StringIO.start("in")
63+
iex> { :ok, pid } = StringIO.open("in")
7464
...> IO.write(pid, "out")
75-
...> StringIO.stop(pid)
65+
...> StringIO.close(pid)
7666
{ :ok, { "in", "out" } }
7767
"""
78-
@spec stop(pid) :: { :ok, { binary, binary } }
79-
def stop(pid) when is_pid(pid) do
80-
:gen_server.call(pid, :stop)
68+
@spec close(pid) :: { :ok, { binary, binary } }
69+
def close(pid) when is_pid(pid) do
70+
:gen_server.call(pid, :close)
8171
end
8272

8373
## callbacks
@@ -96,11 +86,11 @@ defmodule StringIO do
9686
super(msg, s)
9787
end
9888

99-
def handle_call(:peek, _from, state(input: input, output: output) = s) do
89+
def handle_call(:contents, _from, state(input: input, output: output) = s) do
10090
{ :reply, { input, output }, s }
10191
end
10292

103-
def handle_call(:stop, _from, state(input: input, output: output) = s) do
93+
def handle_call(:close, _from, state(input: input, output: output) = s) do
10494
{ :stop, :normal, { :ok, { input, output } }, s }
10595
end
10696

@@ -164,7 +154,7 @@ defmodule StringIO do
164154
end
165155

166156
defp io_request(:getopts, s) do
167-
{ [ binary: true, encoding: :unicode ], s }
157+
{ { :ok, [binary: true, encoding: :unicode] }, s }
168158
end
169159

170160
defp io_request({ :get_geometry, :columns }, s) do

lib/elixir/test/elixir/string_io_test.exs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,29 @@ defmodule StringIOTest do
44
use ExUnit.Case, async: true
55

66
test "start and stop" do
7-
{ :ok, pid } = StringIO.start("")
8-
assert StringIO.stop(pid) == { :ok, { "", "" } }
7+
{ :ok, pid } = StringIO.open("")
8+
assert StringIO.close(pid) == { :ok, { "", "" } }
99
end
1010

1111
test "start_link and stop" do
12-
{ :ok, pid } = StringIO.start_link("")
13-
assert StringIO.stop(pid) == { :ok, { "", "" } }
12+
{ :ok, pid } = StringIO.open("")
13+
assert StringIO.close(pid) == { :ok, { "", "" } }
1414
end
1515

1616
test "peek" do
17-
{ :ok, pid } = StringIO.start("abc")
17+
{ :ok, pid } = StringIO.open("abc")
1818
IO.write(pid, "edf")
19-
assert StringIO.peek(pid) == { "abc", "edf" }
19+
assert StringIO.contents(pid) == { "abc", "edf" }
2020
end
2121

2222
## IO module
2323

2424
def start(string, opts \\ []) do
25-
StringIO.start(string, opts) |> elem(1)
25+
StringIO.open(string, opts) |> elem(1)
2626
end
2727

2828
def contents(pid) do
29-
StringIO.peek(pid)
29+
StringIO.contents(pid)
3030
end
3131

3232
test "IO.read :line with \\n" do

lib/ex_unit/lib/ex_unit/capture_io.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,12 @@ defmodule ExUnit.CaptureIO do
8989
input = Keyword.get(options, :input, "")
9090

9191
original_gl = :erlang.group_leader
92-
{ :ok, capture_gl } = StringIO.start_link(input, capture_prompt: prompt_config)
92+
{ :ok, capture_gl } = StringIO.open(input, capture_prompt: prompt_config)
9393
:erlang.group_leader(capture_gl, self)
9494

9595
try do
9696
fun.()
97-
StringIO.stop(capture_gl) |> elem(1) |> elem(1)
97+
StringIO.close(capture_gl) |> elem(1) |> elem(1)
9898
after
9999
:erlang.group_leader(original_gl, self)
100100
end
@@ -112,12 +112,12 @@ defmodule ExUnit.CaptureIO do
112112
input = Keyword.get(options, :input, "")
113113

114114
Process.unregister(device)
115-
{ :ok, capture_io } = StringIO.start_link(input)
115+
{ :ok, capture_io } = StringIO.open(input)
116116
Process.register(capture_io, device)
117117

118118
try do
119119
fun.()
120-
StringIO.stop(capture_io) |> elem(1) |> elem(1)
120+
StringIO.close(capture_io) |> elem(1) |> elem(1)
121121
after
122122
try do
123123
Process.unregister(device)

lib/ex_unit/test/ex_unit/capture_io_test.exs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -250,22 +250,14 @@ defmodule ExUnit.CaptureIOTest do
250250

251251
test "with setopts" do
252252
assert capture_io(fn ->
253-
:io.setopts({ :encoding, :latin1 })
253+
assert :io.setopts({ :encoding, :latin1 }) == {:error, :enotsup}
254254
end) == ""
255-
256-
capture_io(fn ->
257-
assert :io.setopts({ :encoding, :latin1 }) == :ok
258-
end)
259255
end
260256

261257
test "with getopts" do
262258
assert capture_io(fn ->
263-
:io.getopts
259+
assert :io.getopts == { :ok, [binary: true, encoding: :unicode] }
264260
end) == ""
265-
266-
capture_io(fn ->
267-
assert :io.getopts == { :error, :enotsup }
268-
end)
269261
end
270262

271263
test "with columns" do

0 commit comments

Comments
 (0)