Skip to content

Commit

Permalink
Use Colorize::ColorRGB instead of symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
Ragmaanir committed Jan 30, 2024
1 parent dca2f2a commit 0e3b336
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 51 deletions.
10 changes: 5 additions & 5 deletions spec/reporters/progress_reporter_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ describe Microtest::ProgressReporter do
dot = Microtest::TerminalReporter::DOTS[:success]

assert result.stdout == [
dot.colorize(:green),
dot.colorize(:red),
dot.colorize(:yellow),
dot.colorize(:red),
dot.colorize(GREEN),
dot.colorize(RED),
dot.colorize(YELLOW),
dot.colorize(RED),
"\n\n",
].join
end
Expand All @@ -52,6 +52,6 @@ describe Microtest::ProgressReporter do

bang = Microtest::TerminalReporter::DOTS[:abortion]

assert result.stdout.includes?(bang.colorize(:yellow).to_s)
assert result.stdout.includes?(bang.colorize(YELLOW).to_s)
end
end
2 changes: 1 addition & 1 deletion spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ include Microtest::DSL
include Helpers

SPEC_ROOT = __DIR__
COLOR_REGEX = %r{\e\[\d\d?(;\d)?m}
COLOR_REGEX = %r{\e\[\d\d?(;\d+)*m}

def uncolor(str)
str.gsub(COLOR_REGEX, "")
Expand Down
16 changes: 8 additions & 8 deletions spec/termart_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ describe Microtest::Termart do
end

test "grouped_lines without color" do
res = grouped_lines(["A: aaa"], :red)
res = grouped_lines(["A: aaa"], RED)

assert res == %{◆ A: aaa\n}

res = grouped_lines(["A: aaa", "B: bbb", "C: ccc"], :white)
res = grouped_lines(["A: aaa", "B: bbb", "C: ccc"], WHITE)

assert res == <<-STR
┏ A: aaa
Expand All @@ -23,16 +23,16 @@ describe Microtest::Termart do
end

test "grouped_lines with color" do
res = grouped_lines(["A: aaa"], :red, colorize: true)
res = grouped_lines(["A: aaa"], RED, colorize: true)

assert res == %{\e[31m\e[0m A: aaa\n}
assert res == %{\e[38;2;220;0;0m\e[0m A: aaa\n}

res = grouped_lines(["A: aaa", "B: bbb", "C: ccc"], :red, colorize: true)
res = grouped_lines(["A: aaa", "B: bbb", "C: ccc"], RED, colorize: true)

assert res == <<-STR
\e[31m\e[0m A: aaa
\e[31m\e[0m B: bbb
\e[31m\e[0m C: ccc\n
\e[38;2;220;0;0m\e[0m A: aaa
\e[38;2;220;0;0m\e[0m B: bbb
\e[38;2;220;0;0m\e[0m C: ccc\n
STR
end
end
20 changes: 10 additions & 10 deletions src/microtest/backtrace_printer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -63,28 +63,28 @@ module Microtest
Termart.string(colorize) { |t|
t.w(entry.path, fg: BACKTRACE_KIND_COLORS[entry.kind])

t.w(":", entry.line.to_s, " ", fg: :dark_gray)
t.w(":", entry.line.to_s, " ", fg: DARK_GRAY)

m = Colorize::Mode::None
m = Colorize::Mode::Bold if highlight && entry.func.includes?(highlight)

case entry.kind
when :app, :spec then t.w(entry.func, fg: :yellow, m: m)
else t.w(entry.func, fg: :dark_gray, m: m)
when :app, :spec then t.w(entry.func, fg: YELLOW, m: m)
else t.w(entry.func, fg: DARK_GRAY, m: m)
end
}
end

Termart.string(colorize) { |t| t.grouped_lines(list, :dark_gray) }
Termart.string(colorize) { |t| t.grouped_lines(list, DARK_GRAY) }
end

BACKTRACE_KIND_COLORS = {
:app => :light_magenta,
:spec => :light_magenta,
:eval => :dark_gray,
:crystal => :dark_gray,
:lib => :magenta,
:unknown => :cyan,
:app => LIGHT_MAGENTA,
:spec => LIGHT_MAGENTA,
:eval => DARK_GRAY,
:crystal => DARK_GRAY,
:lib => MAGENTA,
:unknown => CYAN,
}

def simplify(backtrace : Array(String)) : Array(Entry)
Expand Down
12 changes: 7 additions & 5 deletions src/microtest/power_assert_formatter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ module Microtest
end

class ListFormatter < Formatter
BAR_COLOR = :light_red
BAR_COLOR = RGB.new(200, 50, 50)
ASSERT_COLOR = RGB.new(200, 0, 0)
ASSERT_EXP_COLOR = RGB.new(250, 80, 80)

getter? colorize

Expand Down Expand Up @@ -37,9 +39,9 @@ module Microtest
# Returns colorized "assert x == y"
private def formatted_assert_statement(node : CallNode | TerminalNode) : String
build_string { |t|
t.w("assert", fg: :red)
t.w("assert", fg: ASSERT_COLOR)
t.w(" ")
t.w(node.expression, fg: :red, m: :bold)
t.w(node.expression, fg: ASSERT_EXP_COLOR, m: :bold)
}
end

Expand Down Expand Up @@ -151,7 +153,7 @@ module Microtest
else
lines << build_string { |t|
t.w(lval)
t.w(" ", node.method_name, " ", fg: :dark_gray)
t.w(" ", node.method_name, " ", fg: DARK_GRAY)
t.w(rval)
}
end
Expand All @@ -165,7 +167,7 @@ module Microtest

build_string { |a|
a.w(parts[0])
a.w(parts[1], fg: :white, bg: :red)
a.w(parts[1], fg: WHITE, bg: RED)
a.w(parts[2])
}
end
Expand Down
2 changes: 1 addition & 1 deletion src/microtest/reporters/description_reporter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Microtest

def suite_started(ctx : ExecutionContext, cls : String)
br
writeln(cls, fg: :magenta, m: Colorize::Mode::Underline)
writeln(cls, fg: MAGENTA, m: Colorize::Mode::Underline)
end

def report(result : TestResult)
Expand Down
10 changes: 5 additions & 5 deletions src/microtest/reporters/error_list_reporter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ module Microtest
ctx.skips.each do |skip|
ex = skip.exception

write(skip.test.sanitized_name, ": ", ex.message, fg: :yellow)
write(" in ", BacktracePrinter.simplify_path(ex.file)[1], ":", ex.line, fg: :dark_gray)
write(skip.test.sanitized_name, ": ", ex.message, fg: YELLOW)
write(" in ", BacktracePrinter.simplify_path(ex.file)[1], ":", ex.line, fg: DARK_GRAY)
br
end

Expand All @@ -29,17 +29,17 @@ module Microtest
test = failure.test
bold = Colorize::Mode::Bold

write("# %-3d" % (number + 1), test.full_name, " ", fg: :red, m: bold)
write("# %-3d" % (number + 1), test.full_name, " ", fg: RED, m: bold)

case ex
when AssertionFailure
path = BacktracePrinter.simplify_path(ex.file)[1]
write(path, ":", ex.line, fg: :light_gray, m: bold)
write(path, ":", ex.line, fg: LIGHT_GRAY, m: bold)
br
writeln(ex.message)
when UnexpectedError
path = BacktracePrinter.simplify_path(test.filename)[1]
write(path, ":", test.line_number, fg: :light_gray, m: bold)
write(path, ":", test.line_number, fg: LIGHT_GRAY, m: bold)
br
writeln(exception_to_string(ex.exception, test.method_name))
else Microtest.bug("Invalid Exception")
Expand Down
4 changes: 2 additions & 2 deletions src/microtest/reporters/slow_tests_reporter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ module Microtest

if res.empty?
num, unit = Formatter.format_duration(threshold)
writeln("No slow tests (threshold: #{num}#{unit})", fg: :dark_gray)
writeln("No slow tests (threshold: #{num}#{unit})", fg: DARK_GRAY)
else
writeln("Slowest #{res.size} tests", fg: :light_blue)
writeln("Slowest #{res.size} tests", fg: LIGHT_BLUE)
br

res.each do |r|
Expand Down
16 changes: 8 additions & 8 deletions src/microtest/reporters/summary_reporter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,32 @@ module Microtest
total, unit = Formatter.format_duration(ctx.duration)

if ctx.focus?
write("USING FOCUS:", bg: :red)
write("USING FOCUS:", bg: RED)
write(" ")
end

fg = :light_blue
fg = LIGHT_BLUE

write("Executed", fg: fg)
write(" #{ctx.executed_tests}/#{ctx.total_tests} ", fg: (ctx.executed_tests < ctx.total_tests) ? :red : fg)
write(" #{ctx.executed_tests}/#{ctx.total_tests} ", fg: (ctx.executed_tests < ctx.total_tests) ? RED : fg)
write("tests in #{total}#{unit} with seed #{ctx.random_seed}", fg: fg)
br

write("Success: ", ctx.total_success, fg: (:green if ctx.total_success > 0))
write("Success: ", ctx.total_success, fg: (GREEN if ctx.total_success > 0))
write(", ")

write("Skips: ", ctx.total_skip, fg: (:yellow if ctx.total_skip > 0))
write("Skips: ", ctx.total_skip, fg: (YELLOW if ctx.total_skip > 0))
write(", ")

write("Failures: ", ctx.total_failure, fg: (:red if ctx.total_failure > 0))
write("Failures: ", ctx.total_failure, fg: (RED if ctx.total_failure > 0))
br

if ctx.manually_aborted?
br
writeln("Test run was aborted manually", fg: :white, bg: :red)
writeln("Test run was aborted manually", fg: WHITE, bg: RED)
elsif a = ctx.abortion_info
br
writeln("Test run was aborted by exception in hooks for ", a.test.name.inspect, fg: :white, bg: :red)
writeln("Test run was aborted by exception in hooks for ", a.test.name.inspect, fg: WHITE, bg: RED)
end

br
Expand Down
6 changes: 3 additions & 3 deletions src/microtest/reporters/terminal_reporter.cr
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module Microtest
abstract class TerminalReporter < Reporter
alias ResultSymbols = {success: String, failure: String, skip: String, abortion: String}
alias ResultColors = {success: Symbol, failure: Symbol, skip: Symbol, abortion: Symbol}
alias ResultColors = {success: RGB, failure: RGB, skip: RGB, abortion: RGB}

DEFAULT_COLORS = {success: :green, failure: :red, skip: :yellow, abortion: :yellow}
DEFAULT_COLORS = {success: GREEN, failure: RED, skip: YELLOW, abortion: YELLOW}

DOT = "" # Bullet "\u2022"
TICK = "" # Check Mark "\u2713"
Expand Down Expand Up @@ -41,7 +41,7 @@ module Microtest
result : TestResult,
symbols : ResultSymbols = TICKS,
colors : ResultColors = DEFAULT_COLORS
) : Tuple(String, Symbol)
) : Tuple(String, RGB)
{
symbols[result.kind],
colors[result.kind],
Expand Down
19 changes: 16 additions & 3 deletions src/microtest/termart.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
require "colorize"

module Microtest
alias RGB = Colorize::ColorRGB

GREEN = RGB.new(0, 220, 0)
RED = RGB.new(220, 0, 0)
YELLOW = RGB.new(220, 220, 0)
WHITE = RGB.new(255, 255, 255)
DARK_GRAY = RGB.new(100, 100, 100)
LIGHT_GRAY = RGB.new(200, 200, 200)
MAGENTA = RGB.new(205, 0, 205)
LIGHT_MAGENTA = RGB.new(220, 0, 220)
CYAN = RGB.new(0, 205, 205)
LIGHT_BLUE = RGB.new(90, 90, 250)

class Termart
def self.string(colorize : Bool) : String
String.build do |io|
Expand All @@ -21,7 +34,7 @@ module Microtest
def initialize(@io, @colorize : Bool = true)
end

private def colorized_io(fg : Symbol? = nil, bg : Symbol? = nil, m : Colorize::Mode? = nil)
private def colorized_io(fg : RGB? = nil, bg : RGB? = nil, m : Colorize::Mode? = nil)
if colorize?
c = Colorize.with
c = c.fore(fg) if fg
Expand All @@ -36,7 +49,7 @@ module Microtest
end
end

def w(*strs : String | Int32 | Nil, fg : Symbol? = nil, bg : Symbol? = nil, m : Colorize::Mode? = nil)
def w(*strs : String | Int32 | Nil, fg : RGB? = nil, bg : RGB? = nil, m : Colorize::Mode? = nil)
colorized_io(fg, bg, m) do |cio|
strs.each { |s| cio << s }
end
Expand All @@ -55,7 +68,7 @@ module Microtest
io.flush
end

def grouped_lines(lines : Array(String), bar_color : Symbol? = nil)
def grouped_lines(lines : Array(String), bar_color : RGB? = nil)
if lines.size == 1
w("", fg: bar_color)
w(" ", lines.shift, "\n")
Expand Down

0 comments on commit 0e3b336

Please sign in to comment.