defmodule LivebookWeb.Helpers.ANSITest do use ExUnit.Case, async: true alias LivebookWeb.Helpers.ANSI describe "ansi_string_to_html/2" do test "converts ANSI escape codes to span tags" do assert ~s{cat} == ANSI.ansi_string_to_html("\e[34mcat\e[0m") |> Phoenix.HTML.safe_to_string() assert ~s{cat} == ANSI.ansi_string_to_html("\e[44mcat\e[0m") |> Phoenix.HTML.safe_to_string() assert ~s{cat} == ANSI.ansi_string_to_html("\e[1mcat\e[0m") |> Phoenix.HTML.safe_to_string() assert ~s{cat} == ANSI.ansi_string_to_html("\e[4mcat\e[0m") |> Phoenix.HTML.safe_to_string() end test "renders 8-bit rgb colors as regular rgb" do assert ~s{cat} == ANSI.ansi_string_to_html("\e[38;5;67mcat\e[0m") |> Phoenix.HTML.safe_to_string() end test "renders 8-bit grayscale as regular rgb" do assert ~s{cat} == ANSI.ansi_string_to_html("\e[38;5;240mcat\e[0m") |> Phoenix.HTML.safe_to_string() end test "escapes HTML in the resulting string" do assert ~s{<div>} == ANSI.ansi_string_to_html("
") |> Phoenix.HTML.safe_to_string() end end describe "ansi_string_to_html_lines/1" do test "renders every line as complete HTML" do assert ["cool", "cat"] == ANSI.ansi_string_to_html_lines("cool\ncat") |> Enum.map(&Phoenix.HTML.safe_to_string/1) assert [ ~s{cool}, ~s{cat} ] == ANSI.ansi_string_to_html_lines("\e[34mcool\ncat\e[0m") |> Enum.map(&Phoenix.HTML.safe_to_string/1) assert [ ~s{coolcats}, ~s{chillin} ] == ANSI.ansi_string_to_html_lines("\e[34mcool\e[32mcats\n\e[0mchillin") |> Enum.map(&Phoenix.HTML.safe_to_string/1) end end end