Skip to content

Commit ae32bf1

Browse files
author
José Valim
committed
Merge pull request #2087 from ericmj/string-io-get-password
Support :io.get_password in StringIO
2 parents 433a46a + 9e068fb commit ae32bf1

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

lib/elixir/lib/string_io.ex

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,17 @@ defmodule StringIO do
149149
end
150150

151151
defp io_request({ :get_until, prompt, mod, fun, args }, s) do
152-
io_request({ :get_until, :latin1, prompt, mod, fun, args}, s)
152+
io_request({ :get_until, :latin1, prompt, mod, fun, args }, s)
153153
end
154154

155-
defp io_request({ :get_until, encoding, prompt, mod, fun, args}, s) do
155+
defp io_request({ :get_until, encoding, prompt, mod, fun, args }, s) do
156156
get_until(encoding, prompt, mod, fun, args, s)
157157
end
158158

159+
defp io_request({ :get_password, encoding }, s) do
160+
get_line(encoding, "", s)
161+
end
162+
159163
defp io_request({ :setopts, _opts }, s) do
160164
{ :ok, s }
161165
end

lib/elixir/test/elixir/string_io_test.exs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,12 @@ defmodule StringIOTest do
205205
assert contents(pid) == { "", ">" }
206206
end
207207

208+
test ":io.get_password" do
209+
pid = start("abc\n")
210+
assert :io.get_password(pid) == "abc\n"
211+
assert contents(pid) == { "", "" }
212+
end
213+
208214
test "IO.stream" do
209215
pid = start("abc")
210216
assert IO.stream(pid, 2) |> Enum.to_list == ["ab", "c"]

lib/ex_unit/test/ex_unit/capture_io_test.exs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,43 @@ defmodule ExUnit.CaptureIOTest do
151151
end)
152152
end
153153

154+
test "with get password" do
155+
capture_io(fn ->
156+
assert :io.get_password() == :eof
157+
end)
158+
159+
capture_io("", fn ->
160+
assert :io.get_password() == :eof
161+
end)
162+
163+
capture_io("abc", fn ->
164+
assert :io.get_password() == "abc"
165+
assert :io.get_password() == :eof
166+
end)
167+
168+
capture_io("abc\n", fn ->
169+
assert :io.get_password() == "abc\n"
170+
assert :io.get_password() == :eof
171+
end)
172+
173+
capture_io("\n", fn ->
174+
assert :io.get_password() == "\n"
175+
assert :io.get_password() == :eof
176+
end)
177+
178+
capture_io("a\nb", fn ->
179+
assert :io.get_password() == "a\n"
180+
assert :io.get_password() == "b"
181+
assert :io.get_password() == :eof
182+
end)
183+
184+
capture_io("あい\nう", fn ->
185+
assert :io.get_password() == "あい\n"
186+
assert :io.get_password() == "う"
187+
assert :io.get_password() == :eof
188+
end)
189+
end
190+
154191
test "with get until" do
155192
assert capture_io(fn ->
156193
:io.scan_erl_form('>')

0 commit comments

Comments
 (0)