Skip to content

Commit

Permalink
Initial Minitest and RSpec adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
joeldrapper committed Dec 17, 2024
1 parent cfc1c4d commit c075357
Show file tree
Hide file tree
Showing 9 changed files with 219 additions and 34 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ source "https://rubygems.org"
# Specify your gem's dependencies in quickdraw.gemspec
gemspec

gem "rspec-expectations"

if RUBY_ENGINE == "ruby"
group :development do
gem "rubocop"
Expand Down
2 changes: 2 additions & 0 deletions lib/quickdraw.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ module Quickdraw
autoload :Configuration, "quickdraw/configuration"
autoload :Platform, "quickdraw/platform"
autoload :Queue, "quickdraw/queue"
autoload :RSpecAdapter, "quickdraw/rspec_adapter"
autoload :Runner, "quickdraw/runner"
autoload :Test, "quickdraw/test"
autoload :Timer, "quickdraw/timer"
autoload :Worker, "quickdraw/worker"
autoload :MinitestAdapter, "quickdraw/minitest_adapter"

Null = Object.new.freeze
Error = Module.new
Expand Down
54 changes: 36 additions & 18 deletions lib/quickdraw/assertions.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,39 @@
# frozen_string_literal: true

module Quickdraw::Assertions
def assert_equal(expected, actual)
assert(expected == actual) do
"expected #{expected.inspect} to == #{actual.inspect}"
def assert_equal(actual, expected)
assert(actual == expected) do
"expected #{actual.inspect} to == #{expected.inspect}"
end
end

def refute_equal(expected, actual)
refute(expected == actual) do
"expected #{expected.inspect} to not == #{actual.inspect}"
def refute_equal(actual, expected)
refute(actual == expected) do
"expected #{actual.inspect} not to == #{expected.inspect}"
end
end

def assert_includes(collection, member)
assert(collection.include?(member)) do
"expected #{collection.inspect} to include #{member.inspect}"
end
end

def refute_includes(collection, member)
refute(collection.include?(member)) do
"expected #{collection.inspect} not to include #{member.inspect}"
end
end

def assert_operator(object, operator, other)
assert object.public_send(operator, other) do
"expected #{object.inspect} to #{operator} #{other.inspect}"
end
end

def refute_operator(object, operator, other)
refute object.public_send(operator, other) do
"expected #{object.inspect} not to #{operator} #{other.inspect}"
end
end

Expand All @@ -31,21 +55,15 @@ def refute_raises
failure! { "expected block not to raise, but #{error.class.inspect} was raised" }
end

def assert_operator(object, operator, other)
assert object.public_send(operator, other) do
"expected #{object.inspect} to #{operator} #{other.inspect}"
end
end

def assert_same(expected, actual)
assert expected.equal?(actual) do
"expected #{expected.inspect} to be the same object as #{actual.inspect}"
def assert_same(actual, expected)
assert actual.equal?(expected) do
"expected #{actual.inspect} to be the same object as #{expected.inspect}"
end
end

def refute_same(expected, actual)
refute expected.equal?(actual) do
"expected #{expected.inspect} not to be the same object as #{actual.inspect}"
def refute_same(actual, expected)
refute actual.equal?(expected) do
"expected #{actual.inspect} not to be the same object as #{expected.inspect}"
end
end
end
38 changes: 38 additions & 0 deletions lib/quickdraw/minitest_adapter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

module Quickdraw::MinitestAdapter
def self.extended(base)
base.extend(ClassMethods)
base.include(InstanceMethods)
super
end

def self.included(...)
extended(...)
super
end

module ClassMethods
def method_added(name)
name = name.to_s

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

module InstanceMethods
def assert_equal(expected, actual, message = nil)
assert(actual == expected) do
message || "expcted #{actual.inspect} to equal #{expected.inspect}"
end
end

def refute_equal(expected, actual, message = nil)
refute(actual == expected) do
message || "expcted #{actual.inspect} to not equal #{expected.inspect}"
end
end
end
end
115 changes: 115 additions & 0 deletions lib/quickdraw/rspec_adapter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# frozen_string_literal: true

require "rspec/expectations"

module Quickdraw::RSpecAdapter
def self.extended(base)
base.extend(ClassMethods)
base.include(InstanceMethods)
base.include(Matchers)
super
end

def self.included(...)
extended(...)
super
end

module ClassMethods
def describe(description, &)
Class.new(self) do
if respond_to?(:set_temporary_name)
case description
when Class
set_temporary_name "(#{description.name})"
else
set_temporary_name description.to_s
end
end
class_exec(&)
end
end

def it(description, &)
test(description, &)
end
end

module InstanceMethods
def expect(subject)
Expectation.new(subject, context: self)
end
end

class Expectation
def initialize(subject, context:)
@subject = subject
@context = context
end

def to(matcher)
assert(matcher.matches?(@subject)) do
if matcher.respond_to?(:failure_message)
matcher.failure_message
else
"Failure"
end
end
end

def not_to(matcher)
refute(matcher.matches?(@subject)) do
if matcher.respond_to?(:failure_message_when_negated)
matcher.failure_message_when_negated
else
"Failure"
end
end
end

private

def assert(...)
@context.assert(...)
end

def refute(...)
@context.refute(...)
end
end

module Matchers
define_method :all, ::RSpec::Matchers.instance_method(:all)
define_method :be, ::RSpec::Matchers.instance_method(:be)
define_method :be_a, ::RSpec::Matchers.instance_method(:be_a)
define_method :be_a_kind_of, ::RSpec::Matchers.instance_method(:be_a_kind_of)
define_method :be_an_instance_of, ::RSpec::Matchers.instance_method(:be_an_instance_of)
define_method :be_between, ::RSpec::Matchers.instance_method(:be_between)
define_method :be_falsey, ::RSpec::Matchers.instance_method(:be_falsey)
define_method :be_nil, ::RSpec::Matchers.instance_method(:be_nil)
define_method :be_truthy, ::RSpec::Matchers.instance_method(:be_truthy)
define_method :be_within, ::RSpec::Matchers.instance_method(:be_within)
define_method :change, ::RSpec::Matchers.instance_method(:change)
define_method :contain_exactly, ::RSpec::Matchers.instance_method(:contain_exactly)
define_method :cover, ::RSpec::Matchers.instance_method(:cover)
define_method :end_with, ::RSpec::Matchers.instance_method(:end_with)
define_method :eq, ::RSpec::Matchers.instance_method(:eq)
define_method :eql, ::RSpec::Matchers.instance_method(:eql)
define_method :equal, ::RSpec::Matchers.instance_method(:equal)
define_method :exist, ::RSpec::Matchers.instance_method(:exist)
define_method :have_attributes, ::RSpec::Matchers.instance_method(:have_attributes)
define_method :include, ::RSpec::Matchers.instance_method(:include)
define_method :match, ::RSpec::Matchers.instance_method(:match)
define_method :match_array, ::RSpec::Matchers.instance_method(:match_array)
define_method :output, ::RSpec::Matchers.instance_method(:output)
define_method :raise_error, ::RSpec::Matchers.instance_method(:raise_error)
define_method :respond_to, ::RSpec::Matchers.instance_method(:respond_to)
define_method :satisfy, ::RSpec::Matchers.instance_method(:satisfy)
define_method :start_with, ::RSpec::Matchers.instance_method(:start_with)
define_method :throw_symbol, ::RSpec::Matchers.instance_method(:throw_symbol)
define_method :yield_control, ::RSpec::Matchers.instance_method(:yield_control)
define_method :yield_successive_args, ::RSpec::Matchers.instance_method(:yield_successive_args)
define_method :yield_with_args, ::RSpec::Matchers.instance_method(:yield_with_args)
define_method :yield_with_no_args, ::RSpec::Matchers.instance_method(:yield_with_no_args)
end
end
9 changes: 3 additions & 6 deletions lib/quickdraw/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def call
puts

[
"\e[1m#{(failure['class_name'])}\e[0m",
"\e[4m#{failure['test_path']}:#{failure['test_line']}\e[0m",
"\e[1m#{(failure['description'])}\e[0m",
"\e[4m#{failure['path']}:#{failure['line']}\e[0m",
Expand All @@ -76,7 +77,7 @@ def call

puts "Passed: #{@successes.value} | Failed: #{@failures.size} | Errors: #{@errors.size}"

exit(1) if @failures.any?
exit(1) if @failures.any? || @errors.any?
end

def load_tests
Expand Down Expand Up @@ -150,10 +151,7 @@ def work(socket)
socket.write Message::Fetch

case socket.read(1)
when nil
puts "EOF"
break
when Message::Stop
when nil, Message::Stop
threads.each { queue.push(:stop) }
threads.each(&:join)
socket.write Message::Stopping
Expand All @@ -180,7 +178,6 @@ def work(socket)
def supervise(worker)
socket = worker.socket
mutex = @mutex
results = nil
batch = @batch
progress = 0

Expand Down
13 changes: 3 additions & 10 deletions lib/quickdraw/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,8 @@ def assert(value)
nil
end

def refute(value)
if !value
success!
elsif block_given?
failure! { yield(value) }
else
failure! { "expected #{value.inspect} to be falsy" }
end

nil
def refute(value, ...)
assert(!value, ...)
end

# Indicate that an assertion passed successfully.
Expand All @@ -82,6 +74,7 @@ def failure!(&)
location = locations.find { |it| it.path.end_with?(".test.rb") } || locations.first

@runner.failure!({
class_name: self.class.name,
test_path: test_location[0],
test_line: test_location[1],
description: @description,
Expand Down
9 changes: 9 additions & 0 deletions test/minitest.test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

class MinitestTest < Quickdraw::Test
include Quickdraw::MinitestAdapter

def test_something
assert_equal(1, 2)
end
end
11 changes: 11 additions & 0 deletions test/rspec.test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

include Quickdraw::RSpecAdapter

describe String do
describe "test" do
it "works" do
expect("Hello").to eq("Hello")
end
end
end

0 comments on commit c075357

Please sign in to comment.