###############################################################################
# Test for the command-line tool `e2ansi-cat'.
#

# This test use the Johan Holmberg's "cmdtest" tool. This is a
# XUNIT-like framework for testing command line tools. It is written
# in Ruby but it can test command line tools written in any language.
#
# URL: https://bitbucket.org/holmberg556/cmdtest.git
#
# Usage:
#
#     Run `cmdtest' in the shell.
#

MAC_EMACS_EXE = "/Applications/Emacs.app/Contents/MacOS/Emacs"

EMACS_EXE = File.exist?(MAC_EMACS_EXE) ? MAC_EMACS_EXE : "emacs"

class CMDTEST_e2ansi_cat < Cmdtest::Testcase
  USAGE = "\
Syntax highlight files using ANSI escape sequences using Emacs as
the engine. See the file \"e2ansi.el\" for more information.

Usage:
    emacs [...Emacs options...] -l bin/e2ansi-cat [options] file ...

  Or:

    e2ansi-cat [options] file ...

When \"file\" is \"-\", standard input is read.

Available options:

--colors          Number of colors (e.g. 8, 16, or 256), or \"rgb24\" for\
 24 bit colors
--background-mode Background mode, \"light\" (default) or \"dark\"
--color-class     Color class, \"color\" (default), \"grayscale\", or \"mono\"
--mode            Use major mode
--theme           Load custom color theme
--usage           Display this help text
--help            Display this help text
"

  def cmd_e2ansi_cat(*args, &block)
    output_encoding "utf-8" do
      cmd([EMACS_EXE,
           "--batch",
           "-Q",
           "-L #{__dir__}/..",
           # Assumes that `face-explorer' is located side-by-side with
           # `e2ansi'.
           "-L #{__dir__}/../../face-explorer",
           "--eval '(setq e2ansi-suppress-init-file t)'",
           "-l bin/e2ansi-cat",
           *args].join(" "),
          &block)
    end
  end

  def test_no_options
    cmd_e2ansi_cat do
      stderr_equal(USAGE + "\n")
      exit_nonzero
    end
  end

  def test_usage
    cmd_e2ansi_cat("--usage") do
      stdout_equal(USAGE)
    end
  end

  def test_bad_option
    cmd_e2ansi_cat("--BAD-OPTION") do
      stderr_equal("e2ansi: Unknown command line option: \"--BAD-OPTION\"\n")
      exit_nonzero
    end
  end

  def test_missing_file
    cmd_e2ansi_cat("THIS-FILE-DOES-NOT-EXIST") do
      stderr_equal("e2ansi: File not found: THIS-FILE-DOES-NOT-EXIST\n")
      exit_nonzero
    end
  end

  def test_bad_mode
    cmd_e2ansi_cat("--mode THIS-MODE-DOES-NOT-EXIST",
                   "#{__dir__}/files/hello/hello.c") do
      stderr_equal(
        "e2ansi: Unknown major mode: \"THIS-MODE-DOES-NOT-EXIST\"\n")
      exit_nonzero
    end
  end

  def test_bad_colors
    cmd_e2ansi_cat("--colors NOT-A-COLOR-COUNT",
                   "#{__dir__}/files/hello/hello.c") do
      stderr_equal(
        "e2ansi: Incorrect argument to --colors: \"NOT-A-COLOR-COUNT\"\n")
      exit_nonzero
    end
  end

  def test_bad_color_class
    cmd_e2ansi_cat("--color-class BAD-CLASS",
                   "#{__dir__}/files/hello/hello.c") do
      stderr_equal(
        "e2ansi: Incorrect argument to --color-class: \"BAD-CLASS\"\n")
      exit_nonzero
    end
  end

  def test_bad_theme
    cmd_e2ansi_cat("--theme BAD-THEME",
                   "#{__dir__}/files/hello/hello.c") do
      stderr_equal("e2ansi: Failed to load theme \"BAD-THEME\"\n")
      exit_nonzero
    end
  end

  def test_bad_background_mode
    cmd_e2ansi_cat("--background-mode BAD-BACKGROUND",
                   "#{__dir__}/files/hello/hello.c") do
      stderr_equal("e2ansi: Incorrect background mode: \"BAD-BACKGROUND\"\n")
      exit_nonzero
    end
  end

  def test_plain_file
    cmd_e2ansi_cat("#{__dir__}/files/hello/hello.c") do
      # Generated by Emacs 29.4
      stdout_equal("\
\e[38;5;124m/*\e[0m
\e[38;5;124m * Example \"Hello World\" source file,\e[0m
\e[38;5;124m * written in C to demonstrade e2ansi,\e[0m
\e[38;5;124m * that provides Emacs powered syntax\e[0m
\e[38;5;124m * highlighting for terminals.\e[0m
\e[38;5;124m */\e[0m

\e[38;5;60m#include\e[0m \e[38;5;89m<stdio.h>\e[0m

\e[38;5;28mint\e[0m \e[38;5;21mmain\e[0m(\e[38;5;28mvoid\e[0m)
{
  \e[38;5;28mint\e[0m \e[38;5;130mcount\e[0m;

  \e[38;5;129mfor\e[0m (count = 0; count < 10; count++)
  {
    printf(\e[38;5;89m\"Hello World!\\n\"\e[0m);
  }

  \e[38;5;129mreturn\e[0m 0;
}
")

    end
  end

  def test_colors_8
    cmd_e2ansi_cat("--colors 8",
                   "#{__dir__}/files/hello/hello.c") do
      stdout_equal(/^\e\[31m\/\*\e\[0m/)
    end
  end

  def test_colors_16
    cmd_e2ansi_cat("--colors 16",
                   "#{__dir__}/files/hello/hello.c") do
      stdout_equal(/^\e\[31m\/\*\e\[0m/)
    end
  end

  def test_colors_256
    cmd_e2ansi_cat("--colors 256",
                   "#{__dir__}/files/hello/hello.c") do
      stdout_equal(/^\e\[38;5;124m\/\*\e\[0m/)
    end
  end

  def test_colors_rgb24
    cmd_e2ansi_cat("--colors rgb24",
                   "#{__dir__}/files/hello/hello.c") do
      stdout_equal(/^\e\[38;2;178;34;34m\/\*\e\[0m/)
    end
  end
end

# CMDTEST_e2ansi.rb ends here.
