Skip to content

Commit

Permalink
Use difftastic
Browse files Browse the repository at this point in the history
  • Loading branch information
joeldrapper committed Jan 22, 2025
1 parent c4f13f7 commit 6e349a3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
2 changes: 2 additions & 0 deletions lib/quickdraw.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require "difftastic"

module Quickdraw
autoload :ArgumentError, "quickdraw/errors/argument_error"
autoload :Assertions, "quickdraw/assertions"
Expand Down
35 changes: 29 additions & 6 deletions lib/quickdraw/assertions.rb
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/quickdraw/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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:)
Expand Down
1 change: 1 addition & 0 deletions quickdraw.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 6e349a3

Please sign in to comment.