Skip to content

Commit ecd3c19

Browse files
committed
Add more tests for shell coloring
1 parent 7f9a2ed commit ecd3c19

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

lib/iex/lib/iex/introspection.ex

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ defmodule IEx.Introspection do
99
{ :module, _ } ->
1010
case module.__info__(:moduledoc) do
1111
{ _, binary } when is_binary(binary) ->
12-
# FIXME: add tests for `binary` containing ANSI escapes
13-
IO.write IEx.color(:info, "# #{inspect module}\n" <> binary)
12+
IO.write IEx.color(:info, "# #{inspect module}\n\n" <> binary)
1413
{ _, _ } ->
1514
IO.puts IEx.color(:error, "No docs for #{inspect module} have been found")
1615
_ ->
@@ -148,7 +147,6 @@ defmodule IEx.Introspection do
148147
defp print_doc({ { fun, _ }, _line, kind, args, doc }) do
149148
args = Enum.map_join(args, ", ", print_doc_arg(&1))
150149
IO.puts IEx.color(:info, "* #{kind} #{fun}(#{args})\n")
151-
# FIXME: add tests for `doc` containing ANSI escapes
152150
if doc, do: IO.write IEx.color(:info, doc)
153151
end
154152

lib/iex/lib/iex/server.ex

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@ defmodule IEx.Server do
157157
end
158158

159159
defp io_put(result) do
160-
# FIXME: add tests for `result` containing ANSI escapes
161160
IO.puts IEx.color(:eval_result, inspect(result, IEx.Options.get(:inspect)))
162161
end
163162

lib/iex/test/iex/options_test.exs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,32 @@ Code.require_file "../test_helper.exs", __DIR__
33
defmodule IEx.OptionsTest do
44
use IEx.Case
55

6+
@doc """
7+
Hello, I have %{red}ANSI%{reset} escapes.
8+
"""
9+
def ansi_escapes
10+
611
test "color" do
712
opts = [colors: [enabled: true, eval_result: "red"]]
813
assert capture_iex("1 + 2", opts) == "\e[31m3\e[0m"
14+
15+
# Sanity checks
16+
assert capture_iex("IO.ANSI.escape(\"%{blue}hello\", true)", opts)
17+
== "\e[31m\"\\e[34mhello\\e[0m\"\e[0m"
18+
assert capture_iex("IO.puts IO.ANSI.escape(\"%{blue}hello\", true)", opts)
19+
== "\e[34mhello\e[0m\n\e[31m:ok\e[0m"
20+
assert capture_iex("IO.puts IO.ANSI.escape(\"%{blue}hello\", true)", [colors: [enabled: false]])
21+
== "\e[34mhello\e[0m\n:ok"
22+
23+
# Test that ANSI escapes in the docs are left alone
24+
opts = [colors: [enabled: true, info: "red", eval_result: "red"]]
25+
assert capture_iex("h IEx.OptionsTest.ansi_escapes", opts)
26+
== "\e[31m* def ansi_escapes()\n\e[0m\n\e[31mHello, I have %{red}ANSI%{reset} escapes.\n\e[0m\n\e[31m:ok\e[0m"
27+
28+
# Test that ANSI escapes in iex output are left alone
29+
assert capture_iex("\"%{red} %{blue}\"", opts) == "\e[31m\"%{red} %{blue}\"\e[0m"
30+
assert capture_iex("IO.puts IEx.color(:info, \"%{red} %{blue}\")", opts)
31+
== "\e[31m%{red} %{blue}\e[0m\n\e[31m:ok\e[0m"
932
end
1033

1134
test "inspect opts" do

0 commit comments

Comments
 (0)