From ecd3c19c64d0cfcd8ba91f3a365d83e8bf923710 Mon Sep 17 00:00:00 2001 From: Alexei Sholik Date: Fri, 31 May 2013 22:25:51 +0300 Subject: [PATCH] Add more tests for shell coloring --- lib/iex/lib/iex/introspection.ex | 4 +--- lib/iex/lib/iex/server.ex | 1 - lib/iex/test/iex/options_test.exs | 23 +++++++++++++++++++++++ 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/lib/iex/lib/iex/introspection.ex b/lib/iex/lib/iex/introspection.ex index de3c8b38e0..5fa54dc5e4 100644 --- a/lib/iex/lib/iex/introspection.ex +++ b/lib/iex/lib/iex/introspection.ex @@ -9,8 +9,7 @@ defmodule IEx.Introspection do { :module, _ } -> case module.__info__(:moduledoc) do { _, binary } when is_binary(binary) -> - # FIXME: add tests for `binary` containing ANSI escapes - IO.write IEx.color(:info, "# #{inspect module}\n" <> binary) + IO.write IEx.color(:info, "# #{inspect module}\n\n" <> binary) { _, _ } -> IO.puts IEx.color(:error, "No docs for #{inspect module} have been found") _ -> @@ -148,7 +147,6 @@ defmodule IEx.Introspection do defp print_doc({ { fun, _ }, _line, kind, args, doc }) do args = Enum.map_join(args, ", ", print_doc_arg(&1)) IO.puts IEx.color(:info, "* #{kind} #{fun}(#{args})\n") - # FIXME: add tests for `doc` containing ANSI escapes if doc, do: IO.write IEx.color(:info, doc) end diff --git a/lib/iex/lib/iex/server.ex b/lib/iex/lib/iex/server.ex index e4dc1a7abb..151f38f753 100644 --- a/lib/iex/lib/iex/server.ex +++ b/lib/iex/lib/iex/server.ex @@ -157,7 +157,6 @@ defmodule IEx.Server do end defp io_put(result) do - # FIXME: add tests for `result` containing ANSI escapes IO.puts IEx.color(:eval_result, inspect(result, IEx.Options.get(:inspect))) end diff --git a/lib/iex/test/iex/options_test.exs b/lib/iex/test/iex/options_test.exs index 56e7087f2f..476700e384 100644 --- a/lib/iex/test/iex/options_test.exs +++ b/lib/iex/test/iex/options_test.exs @@ -3,9 +3,32 @@ Code.require_file "../test_helper.exs", __DIR__ defmodule IEx.OptionsTest do use IEx.Case + @doc """ + Hello, I have %{red}ANSI%{reset} escapes. + """ + def ansi_escapes + test "color" do opts = [colors: [enabled: true, eval_result: "red"]] assert capture_iex("1 + 2", opts) == "\e[31m3\e[0m" + + # Sanity checks + assert capture_iex("IO.ANSI.escape(\"%{blue}hello\", true)", opts) + == "\e[31m\"\\e[34mhello\\e[0m\"\e[0m" + assert capture_iex("IO.puts IO.ANSI.escape(\"%{blue}hello\", true)", opts) + == "\e[34mhello\e[0m\n\e[31m:ok\e[0m" + assert capture_iex("IO.puts IO.ANSI.escape(\"%{blue}hello\", true)", [colors: [enabled: false]]) + == "\e[34mhello\e[0m\n:ok" + + # Test that ANSI escapes in the docs are left alone + opts = [colors: [enabled: true, info: "red", eval_result: "red"]] + assert capture_iex("h IEx.OptionsTest.ansi_escapes", opts) + == "\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" + + # Test that ANSI escapes in iex output are left alone + assert capture_iex("\"%{red} %{blue}\"", opts) == "\e[31m\"%{red} %{blue}\"\e[0m" + assert capture_iex("IO.puts IEx.color(:info, \"%{red} %{blue}\")", opts) + == "\e[31m%{red} %{blue}\e[0m\n\e[31m:ok\e[0m" end test "inspect opts" do