diff --git a/HISTORY.md b/HISTORY.md index 7ba87eb..abe43dc 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -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 diff --git a/Rakefile b/Rakefile index 297c4f9..81f5d96 100644 --- a/Rakefile +++ b/Rakefile @@ -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] diff --git a/lib/assay.rb b/lib/assay.rb index 6ba1475..5a4108b 100644 --- a/lib/assay.rb +++ b/lib/assay.rb @@ -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] 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. # diff --git a/lib/assay/ae/assertor.rb b/lib/assay/ae/assertor.rb index 6070c1f..ff0d7e3 100644 --- a/lib/assay/ae/assertor.rb +++ b/lib/assay/ae/assertor.rb @@ -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 diff --git a/lib/assay/equal_assay.rb b/lib/assay/equal_assay.rb index a85e994..e3a74a0 100644 --- a/lib/assay/equal_assay.rb +++ b/lib/assay/equal_assay.rb @@ -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 diff --git a/lib/assay/unequal_assay.rb b/lib/assay/unequal_assay.rb index d54fcc6..f65c7e7 100644 --- a/lib/assay/unequal_assay.rb +++ b/lib/assay/unequal_assay.rb @@ -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 diff --git a/lib/assay/version.rb b/lib/assay/version.rb index 7a6f462..7e8b6d4 100644 --- a/lib/assay/version.rb +++ b/lib/assay/version.rb @@ -1,3 +1,3 @@ module Assay - VERSION = '0.5.0' + VERSION = '0.5.1' end diff --git a/test/color.rb b/test/color.rb new file mode 100644 index 0000000..62a53ac --- /dev/null +++ b/test/color.rb @@ -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