Skip to content

Commit

Permalink
Simplify caller locations
Browse files Browse the repository at this point in the history
  • Loading branch information
joeldrapper committed Dec 17, 2024
1 parent bcdfafc commit b1f9b8a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 38 deletions.
40 changes: 14 additions & 26 deletions lib/quickdraw/assertions.rb
Original file line number Diff line number Diff line change
@@ -1,62 +1,50 @@
# frozen_string_literal: true

module Quickdraw::Assertions
def assert_equal(expected, actual, depth: 0)
depth += 1

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

def refute_equal(expected, actual, depth: 0)
depth += 1

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

def assert_raises(expected, depth: 0)
depth += 1
def assert_raises(expected)
yield
failure!(depth:) { "expected block to raise #{expected.inspect} but nothing was raised" }
failure! { "expected block to raise #{expected.inspect} but nothing was raised" }
rescue Exception => error
assert(expected === error, depth:) do
assert(expected === error) do
"expected block to raise #{expected.inspect} but #{error.class.inspect} was raised instead"
end

error
end

def refute_raises(depth: 0)
depth += 1
def refute_raises
yield
success!
rescue Exception => error
failure!(depth:) { "expected block not to raise, but #{error.class.inspect} was raised" }
failure! { "expected block not to raise, but #{error.class.inspect} was raised" }
end

def assert_operator(object, operator, other, depth: 0)
depth += 1

assert object.public_send(operator, other), depth: do
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, depth: 0)
depth += 1

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

def refute_same(expected, actual, depth: 0)
depth += 1

refute expected.equal?(actual), depth: do
def refute_same(expected, actual)
refute expected.equal?(actual) do
"expected #{expected.inspect} not to be the same object as #{actual.inspect}"
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/quickdraw/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ def call
puts

[
"\e[4m#{failure['location'][0]}:#{failure['location'][1]}\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",
"\e[3m#{(failure['message'])}\e[0m",
failure["caller_locations"][failure["depth"]].gsub(":in `", " in `"),
].each_with_index do |line, i|
puts "#{' ' * i}#{line}"
end
Expand Down
21 changes: 12 additions & 9 deletions lib/quickdraw/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,13 @@ def match
failure! { e.message }
end

def assert(value, depth: 0)
depth += 1

def assert(value)
if value
success!
elsif block_given?
failure!(depth:) { yield(value) }
failure! { yield(value) }
else
failure!(depth:) { "expected #{value.inspect} to be truthy" }
failure! { "expected #{value.inspect} to be truthy" }
end

nil
Expand Down Expand Up @@ -75,16 +73,21 @@ def success!
end

# Indicate that an assertion failed.
def failure!(depth: 0, &)
def failure!(&)
if @skip
@runner.success!(@description)
else
test_location = @block.source_location
locations = caller_locations
location = locations.find { |it| it.path.end_with?(".test.rb") } || locations.first

@runner.failure!({
location: @block.source_location,
test_path: test_location[0],
test_line: test_location[1],
description: @description,
message: yield,
caller_locations:,
depth:,
path: location.path,
line: location.lineno,
})
end

Expand Down
2 changes: 1 addition & 1 deletion test/assertions.test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def success!(description)
end

test "the truth" do
assert_equal "Hello", "Joel"
assert_equal "Hello", "World"
end

test "assert_equal" do
Expand Down

0 comments on commit b1f9b8a

Please sign in to comment.