Skip to content

Commit

Permalink
Minitest adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
joeldrapper committed Dec 17, 2024
1 parent c075357 commit 9ed65e9
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 13 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ source "https://rubygems.org"
gemspec

gem "rspec-expectations"
gem "minitest"

if RUBY_ENGINE == "ruby"
group :development do
Expand Down
79 changes: 69 additions & 10 deletions lib/quickdraw/minitest_adapter.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require "minitest/assertions"

module Quickdraw::MinitestAdapter
def self.extended(base)
base.extend(ClassMethods)
Expand All @@ -16,23 +18,80 @@ module ClassMethods
def method_added(name)
name = name.to_s

if name.start_with?("test_")
test(name[5..]) { public_send(name) }
end
return unless name.start_with?("test_")

location = instance_method(name).source_location

class_eval(<<~RUBY, *location)
test("#{name[5..].tr('_', ' ')}") do
#{name}
end
RUBY
end
end

module InstanceMethods
def assert_equal(expected, actual, message = nil)
assert(actual == expected) do
message || "expcted #{actual.inspect} to equal #{expected.inspect}"
def assert(value, message = nil)
case message
when nil
super(value) { yield }
when Proc
super(value, &message)
else
super(value) { message }
end
end

def refute_equal(expected, actual, message = nil)
refute(actual == expected) do
message || "expcted #{actual.inspect} to not equal #{expected.inspect}"
end
def refute(value, message = nil)
super(value) { message || yield }
end

define_method :assert_empty, Minitest::Assertions.instance_method(:assert_empty)
define_method :assert_equal, Minitest::Assertions.instance_method(:assert_equal)
define_method :assert_in_delta, Minitest::Assertions.instance_method(:assert_in_delta)
define_method :assert_in_epsilon, Minitest::Assertions.instance_method(:assert_in_epsilon)
define_method :assert_includes, Minitest::Assertions.instance_method(:assert_includes)
define_method :assert_instance_of, Minitest::Assertions.instance_method(:assert_instance_of)
define_method :assert_kind_of, Minitest::Assertions.instance_method(:assert_kind_of)
define_method :assert_match, Minitest::Assertions.instance_method(:assert_match)
define_method :assert_nil, Minitest::Assertions.instance_method(:assert_nil)
define_method :assert_operator, Minitest::Assertions.instance_method(:assert_operator)
define_method :assert_path_exists, Minitest::Assertions.instance_method(:assert_path_exists)
define_method :assert_pattern, Minitest::Assertions.instance_method(:assert_pattern)
define_method :assert_predicate, Minitest::Assertions.instance_method(:assert_predicate)
define_method :assert_raises, Minitest::Assertions.instance_method(:assert_raises)
define_method :assert_respond_to, Minitest::Assertions.instance_method(:assert_respond_to)
define_method :assert_same, Minitest::Assertions.instance_method(:assert_same)
define_method :assert_throws, Minitest::Assertions.instance_method(:assert_throws)

define_method :refute_empty, Minitest::Assertions.instance_method(:refute_empty)
define_method :refute_equal, Minitest::Assertions.instance_method(:refute_equal)
define_method :refute_in_delta, Minitest::Assertions.instance_method(:refute_in_delta)
define_method :refute_in_epsilon, Minitest::Assertions.instance_method(:refute_in_epsilon)
define_method :refute_includes, Minitest::Assertions.instance_method(:refute_includes)
define_method :refute_instance_of, Minitest::Assertions.instance_method(:refute_instance_of)
define_method :refute_kind_of, Minitest::Assertions.instance_method(:refute_kind_of)
define_method :refute_match, Minitest::Assertions.instance_method(:refute_match)
define_method :refute_nil, Minitest::Assertions.instance_method(:refute_nil)
define_method :refute_operator, Minitest::Assertions.instance_method(:refute_operator)
define_method :refute_path_exists, Minitest::Assertions.instance_method(:refute_path_exists)
define_method :refute_pattern, Minitest::Assertions.instance_method(:refute_pattern)
define_method :refute_predicate, Minitest::Assertions.instance_method(:refute_predicate)
define_method :refute_respond_to, Minitest::Assertions.instance_method(:refute_respond_to)
define_method :refute_same, Minitest::Assertions.instance_method(:refute_same)

private define_method :message, Minitest::Assertions.instance_method(:message)
private define_method :diff, Minitest::Assertions.instance_method(:diff)
private define_method :things_to_diff, Minitest::Assertions.instance_method(:things_to_diff)
private define_method :mu_pp_for_diff, Minitest::Assertions.instance_method(:mu_pp_for_diff)
private define_method :mu_pp, Minitest::Assertions.instance_method(:mu_pp)

def flunk(message)
failure! { message }
end

def pass
success!
end
end
end
4 changes: 3 additions & 1 deletion lib/quickdraw/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,12 @@ def call
"\e[4m#{failure['test_path']}:#{failure['test_line']}\e[0m",
"\e[1m#{(failure['description'])}\e[0m",
"\e[4m#{failure['path']}:#{failure['line']}\e[0m",
"\e[3m#{(failure['message'])}\e[0m",
].each_with_index do |line, i|
puts "#{' ' * i}#{line}"
end

puts
puts "\e[3m#{(failure['message'])}\e[0m"
end

puts
Expand Down
128 changes: 126 additions & 2 deletions test/minitest.test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,131 @@
class MinitestTest < Quickdraw::Test
include Quickdraw::MinitestAdapter

def test_something
assert_equal(1, 2)
def test_assert_equal
assert_equal(1, 1)
end

def test_assert_empty
assert_empty([])
end

def test_assert_in_delta
assert_in_delta(1.0, 1.0, 0.1)
end

def test_assert_in_epsilon
assert_in_epsilon(1.0, 1.0, 0.1)
end

def test_assert_includes
assert_includes([1, 2, 3], 3)
end

def test_assert_instance_of
assert_instance_of(String, "Hello")
end

def test_assert_kind_of
assert_kind_of(String, "Hello")
end

def test_assert_match
assert_match(/Hello/, "Hello")
end

def test_assert_nil
assert_nil(nil)
end

def test_assert_operator
assert_operator(1, :<, 2)
end

def test_assert_path_exists
assert_path_exists("test/minitest.test.rb")
end

def test_assert_pattern
assert_pattern { 1 => Integer }
end

def test_assert_predicate
assert_predicate(2, :even?)
end

def test_assert_raises
assert_raises(ArgumentError) { raise ArgumentError.new("Hello") }
end

def test_assert_respond_to
assert_respond_to(String, :new)
end

def test_assert_same
assert_same(1, 1)
end

def test_assert_throws
assert_throws(:foo) { throw :foo }
end

def test_refute_empty
refute_empty([1, 2, 3])
end

def test_refute_equal
refute_equal(1, 2)
end

def test_refute_in_delta
refute_in_delta(1.0, 1.2, 0.1)
end

def test_refute_in_epsilon
refute_in_epsilon(1.0, 1.2, 0.1)
end

def test_refute_includes
refute_includes([1, 2, 3], 4)
end

def test_refute_instance_of
refute_instance_of(String, 1)
end

def test_refute_kind_of
refute_kind_of(String, 1)
end

def test_refute_match
refute_match(/Hello/, "World")
end

def test_refute_nil
refute_nil(1)
end

def test_refute_operator
refute_operator(1, :>, 2)
end

def test_refute_path_exists
refute_path_exists("test/not_a_file.rb")
end

def test_refute_pattern
refute_pattern { 1 => String }
end

def test_refute_predicate
refute_predicate(2, :odd?)
end

def test_refute_respond_to
refute_respond_to(String, :to_i)
end

def test_refute_same
refute_same(1, 2)
end
end

0 comments on commit 9ed65e9

Please sign in to comment.