Skip to content

Commit

Permalink
Added assert_equal_with_term
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu committed Apr 8, 2021
1 parent 2c94813 commit b690da9
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions test/irb/test_color.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,10 @@ def test_colorize_code

tests.each do |code, result|
if colorize_code_supported?
actual = with_term { IRB::Color.colorize_code(code, complete: true) }
assert_equal(result, actual, "Case: IRB::Color.colorize_code(#{code.dump}, complete: true)\nResult: #{humanized_literal(actual)}")

actual = with_term { IRB::Color.colorize_code(code, complete: false) }
assert_equal(result, actual, "Case: IRB::Color.colorize_code(#{code.dump}, complete: false)\nResult: #{humanized_literal(actual)}")
assert_equal_with_term(result, code, complete: true)
assert_equal_with_term(result, code, complete: false)
else
actual = with_term { IRB::Color.colorize_code(code) }
assert_equal(code, actual)
assert_equal_with_term(code, code)
end
end
end
Expand All @@ -127,8 +123,7 @@ def test_colorize_code_complete_true
"'foo' + 'bar" => "#{RED}#{BOLD}'#{CLEAR}#{RED}foo#{CLEAR}#{RED}#{BOLD}'#{CLEAR} + #{RED}#{BOLD}'#{CLEAR}#{RED}#{REVERSE}bar#{CLEAR}",
"('foo" => "(#{RED}#{BOLD}'#{CLEAR}#{RED}#{REVERSE}foo#{CLEAR}",
}.each do |code, result|
actual = with_term { IRB::Color.colorize_code(code, complete: true) }
assert_equal(result, actual, "Case: colorize_code(#{code.dump}, complete: true)\nResult: #{humanized_literal(actual)}")
assert_equal_with_term(result, code, complete: true)
end
end

Expand All @@ -139,16 +134,13 @@ def test_colorize_code_complete_false
"('foo" => "(#{RED}#{BOLD}'#{CLEAR}#{RED}foo#{CLEAR}",
}.each do |code, result|
if colorize_code_supported?
actual = with_term { IRB::Color.colorize_code(code, complete: false) }
assert_equal(result, actual, "Case: colorize_code(#{code.dump}, complete: false)\nResult: #{humanized_literal(actual)}")
assert_equal_with_term(result, code, complete: false)

unless complete_option_supported?
actual = with_term { IRB::Color.colorize_code(code, complete: true) }
assert_equal(result, actual, "Case: colorize_code(#{code.dump}, complete: false)\nResult: #{humanized_literal(actual)}")
assert_equal_with_term(result, code, complete: true)
end
else
actual = with_term { IRB::Color.colorize_code(code) }
assert_equal(code, actual)
assert_equal_with_term(code, code)
end
end
end
Expand Down Expand Up @@ -200,6 +192,16 @@ def io.tty?; true; end
ENV.replace(env) if env
end

def assert_equal_with_term(result, code, **opts)
actual = with_term { IRB::Color.colorize_code(code, **opts) }
message = -> {
args = [code.dump]
opts.each {|kwd, val| args << "#{kwd}: #{val}"}
"Case: colorize_code(#{args.join(', ')})\nResult: #{humanized_literal(actual)}"
}
assert_equal(result, actual, message)
end

def humanized_literal(str)
str
.gsub(CLEAR, '@@@{CLEAR}')
Expand Down

0 comments on commit b690da9

Please sign in to comment.