From 6e349a33bf17a57229c9fa14b82a565403438871 Mon Sep 17 00:00:00 2001 From: Joel Drapper Date: Wed, 22 Jan 2025 13:45:19 +0000 Subject: [PATCH] Use difftastic --- lib/quickdraw.rb | 2 ++ lib/quickdraw/assertions.rb | 35 +++++++++++++++++++++++++++++------ lib/quickdraw/test.rb | 2 +- quickdraw.gemspec | 1 + 4 files changed, 33 insertions(+), 7 deletions(-) diff --git a/lib/quickdraw.rb b/lib/quickdraw.rb index d04067f..04979bd 100644 --- a/lib/quickdraw.rb +++ b/lib/quickdraw.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require "difftastic" + module Quickdraw autoload :ArgumentError, "quickdraw/errors/argument_error" autoload :Assertions, "quickdraw/assertions" diff --git a/lib/quickdraw/assertions.rb b/lib/quickdraw/assertions.rb index fecb504..584802f 100644 --- a/lib/quickdraw/assertions.rb +++ b/lib/quickdraw/assertions.rb @@ -1,17 +1,40 @@ # frozen_string_literal: true module Quickdraw::Assertions + DIFFER = Difftastic::Differ.new( + color: :always, + tab_width: 2, + ) + def assert_equal(actual, expected) assert(actual == expected) do - <<~MESSAGE - \e[34mExpected:\e[0m + diff = DIFFER.diff_objects(actual, expected) - #{actual.inspect} + "Expected objects to be equal (compared with `==`):\n\n#{diff}" + end + end - \e[34mto be == to:\e[0m + def assert_equal_sql(actual, expected) + unless String === actual && String === expected + raise ArgumentError.new("expected both actual and expected to be strings") + end - #{expected.inspect} - MESSAGE + assert(actual == expected) do + diff = DIFFER.diff_sql(actual, expected) + + "Expected SQL strings to be equal (compared with `==`):\n\n#{diff}" + end + end + + def assert_equal_html(actual, expected) + unless String === actual && String === expected + raise ArgumentError.new("expected both actual and expected to be strings") + end + + assert(actual == expected) do + diff = DIFFER.diff_html(actual, expected) + + "Expected HTML strings to be equal (compared with `==`):\n\n#{diff}" end end diff --git a/lib/quickdraw/test.rb b/lib/quickdraw/test.rb index 67b57d2..6d190ab 100644 --- a/lib/quickdraw/test.rb +++ b/lib/quickdraw/test.rb @@ -4,7 +4,7 @@ class Quickdraw::Test include Quickdraw::Assertions def self.test(description = nil, skip: false, &block) - $quickdraw_runner << [self, description, skip, block] + $quickdraw_runner << [self, description, skip, block].freeze end def initialize(description:, skip:, block:) diff --git a/quickdraw.gemspec b/quickdraw.gemspec index 6747f72..219df2d 100644 --- a/quickdraw.gemspec +++ b/quickdraw.gemspec @@ -35,4 +35,5 @@ Gem::Specification.new do |spec| spec.metadata["rubygems_mfa_required"] = "true" spec.add_dependency "io-watch", "~> 0.6" + spec.add_dependency "difftastic", "~> 0.1" end