Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# HISTORY

## Unreleased (0.5.1)

* Fix a `NameError` (`uninitialized constant ANSI::Diff`) raised in
place of a failed comparison when the values are long and color is
on but was not turned on with `Assay.color = true`. That is the usual
case under Rubytest, since the ANSI gem turns color on when it loads.
It affected `EqualAssay`, `UnequalAssay` and the AE notation.

## 0.5.0 | 2026-11-23

Assay is one library again. The notations that were spun off as separate
Expand Down
7 changes: 6 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@ task :adapters do
sh 'bundle exec ruby -Ilib test/adapters.rb'
end

task :default => [:demo, :test, :adapters]
desc "Check failure messages when color is turned on from outside"
task :color do
sh 'bundle exec ruby -Ilib test/color.rb'
end

task :default => [:demo, :test, :adapters, :color]
14 changes: 14 additions & 0 deletions lib/assay.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@ def self.color=(boolean)
end
end

#
# Two inspected values with the differences between them colored, for
# failure messages when color is on. Color can be turned on from
# outside Assay (the ANSI gem sets `$ansi` when it loads), so the diff
# library is loaded here, where it is used.
#
# @return [Array<String>] the two values, colored
#
def self.color_diff(a, b)
require 'ansi/diff'
diff = ::ANSI::Diff.new(a, b)
return diff.diff1, diff.diff2
end

#
# Lookup assay class by operator or name.
#
Expand Down
4 changes: 1 addition & 3 deletions lib/assay/ae/assertor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -417,9 +417,7 @@ def compare_message(operator, *args, &blk)
operator = COMPARISON_OPERATORS[operator] if @negated
if a.size > 13 or b.size > 13
if defined?($ansi) && $ansi && a.size * b.size <= DIFF_LIMIT
diff = ::ANSI::Diff.new(a,b)
a = diff.diff1
b = diff.diff2
a, b = ::Assay.color_diff(a, b)
end
"a #{operator} b\na) " + a + "\nb) " + b
else
Expand Down
3 changes: 1 addition & 2 deletions lib/assay/equal_assay.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ def self.assert_message(subject, criterion)

if a.size > SIZE_LIMIT or b.size > SIZE_LIMIT
if $ansi
d = ANSI::Diff.new(a, b)
a, b = d.diff1, d.diff2 # *d.to_a
a, b = Assay.color_diff(a, b)
end
"a == b\na) #{a}\nb) #{b}"
else
Expand Down
3 changes: 1 addition & 2 deletions lib/assay/unequal_assay.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ def self.assert_message(subject, criterion)

if a.size > SIZE_LIMIT or b.size > SIZE_LIMIT
if $ansi
d = ANSI::Diff.new(a, b)
a, b = d.diff1, d.diff2 # *d.to_a
a, b = Assay.color_diff(a, b)
end
"a != b\na) #{a}\nb) #{b}"
else
Expand Down
2 changes: 1 addition & 1 deletion lib/assay/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Assay
VERSION = '0.5.0'
VERSION = '0.5.1'
end
46 changes: 46 additions & 0 deletions test/color.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Color can be turned on from outside Assay: the ANSI gem sets `$ansi`
# when it loads, as it does under Rubytest. A failed comparison of long
# values must then still raise its assertion, with the differences
# colored. Each check runs in a process of its own, so that nothing else
# has loaded the diff library first.

require 'rbconfig'

long_a = 'a' * 20
long_b = 'b' * 20

checks = {
'EqualAssay' => %[
require 'assay'
$ansi = true
EqualAssay.assert!(#{long_a.inspect}, #{long_b.inspect})
],
'UnequalAssay' => %[
require 'assay'
$ansi = true
UnequalAssay.assert!(#{long_a.inspect}, #{long_a.inspect})
],
'AE notation' => %[
require 'assay/ae'
$ansi = true
#{long_a.inspect}.assert == #{long_b.inspect}
]
}

failed = false

checks.each do |name, script|
script = "begin; #{script}; rescue Exception => e; " \
"puts [e.class.name, e.assertion?.inspect, e.message].join('|'); end"
output = IO.popen([RbConfig.ruby, '-Ilib', '-e', script], err: [:child, :out], &:read)
klass, assertion, message = output.split('|', 3)
expected = (name == 'UnequalAssay' ? 'UnequalAssay' : 'EqualAssay')
if klass == expected && assertion == 'true' && message.to_s.include?("\e[")
puts "ok #{name}"
else
failed = true
puts "not ok #{name}: expected a colored #{expected}, got\n#{output}"
end
end

exit 1 if failed
Loading