Skip to content

WIP: Format method expectation errors #67

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Metrics/BlockLength:
- shared_context
- shared_examples
- shared_examples_for
- xit
Metrics/ClassLength:
Enabled: false
Metrics/CyclomaticComplexity:
Expand All @@ -62,6 +63,7 @@ Metrics/LineLength:
- "^\\s*context"
- "^\\s*describe"
- "^\\s*it"
- "^\\s*xit"
Metrics/MethodLength:
Enabled: false
Metrics/ModuleLength:
Expand All @@ -85,6 +87,10 @@ Style/AccessModifierDeclarations:
Enabled: false
Style/Alias:
Enabled: false
Style/AndOr:
Enabled: false
Style/BlockComments:
Enabled: false
Style/CollectionMethods:
Enabled: true
Style/Documentation:
Expand Down Expand Up @@ -113,12 +119,16 @@ Style/MultilineBlockChain:
Enabled: false
Style/NegatedIf:
Enabled: false
Style/Next:
Enabled: false
Style/NegatedWhile:
Enabled: false
Style/NumericPredicate:
Enabled: false
Style/OneLineConditional:
Enabled: false
Style/ParenthesesAroundCondition:
Enabled: false
Style/PercentLiteralDelimiters:
Enabled: false
Style/PreferredHashMethods:
Expand Down
10 changes: 3 additions & 7 deletions lib/super_diff.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module SuperDiff
autoload :Csi, "super_diff/csi"
autoload :DiffFormatter, "super_diff/diff_formatter"
autoload :DiffFormatters, "super_diff/diff_formatters"
autoload :DiffLegendBuilder, "super_diff/diff_legend_builder"
autoload :Differ, "super_diff/differ"
autoload :Differs, "super_diff/differs"
autoload :EqualityMatcher, "super_diff/equality_matcher"
Expand All @@ -26,11 +27,6 @@ module SuperDiff
autoload :OperationSequences, "super_diff/operation_sequences"
autoload :Operations, "super_diff/operations"
autoload :RecursionGuard, "super_diff/recursion_guard"

COLORS = {
alpha: :magenta,
beta: :yellow,
border: :blue,
header: :white,
}.freeze
end

require "super_diff/colors"
12 changes: 12 additions & 0 deletions lib/super_diff/colorized_document_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,17 @@ def alpha(*args, **opts, &block)
def beta(*args, **opts, &block)
colorize(*args, **opts, fg: SuperDiff::COLORS.fetch(:beta), &block)
end

def highlight(*args, **opts, &block)
colorize(*args, **opts, fg: SuperDiff::COLORS.fetch(:highlight), &block)
end

def header(*args, **opts, &block)
colorize(*args, **opts, fg: SuperDiff::COLORS.fetch(:header), &block)
end

def border(*args, **opts, &block)
colorize(*args, **opts, fg: SuperDiff::COLORS.fetch(:border), &block)
end
end
end
9 changes: 9 additions & 0 deletions lib/super_diff/colors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module SuperDiff
COLORS = {
alpha: :magenta,
beta: :yellow,
border: :blue,
highlight: :blue,
header: :white,
}.freeze
end
1 change: 1 addition & 0 deletions lib/super_diff/csi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module Csi
autoload :ResetSequence, "super_diff/csi/reset_sequence"
autoload :TwentyFourBitColor, "super_diff/csi/twenty_four_bit_color"
autoload :UncolorizedDocument, "super_diff/csi/uncolorized_document"
autoload :UnderlineSequence, "super_diff/csi/underline_sequence"

class << self
attr_writer :color_enabled
Expand Down
2 changes: 1 addition & 1 deletion lib/super_diff/csi/colorized_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def colorize_block(colors, opts, &block)

add_part(color_sequence)
color_sequences_open_in_parent << color_sequence
evaluate_block(&block)
apply(&block)
add_part(Csi.reset_sequence)

color_sequence_to_reopen = color_sequences_open_in_parent.pop
Expand Down
45 changes: 29 additions & 16 deletions lib/super_diff/csi/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ def initialize(&block)
@indentation_stack = []

if block
evaluate_block(&block)
apply(&block)
end
end

def +(other)
to_s + other.to_s
end

def each(&block)
parts.each(&block)
end
Expand All @@ -20,6 +24,10 @@ def bold(*args, **opts, &block)
colorize(BoldSequence.new, *args, **opts, &block)
end

def underline(*args, **opts, &block)
colorize(UnderlineSequence.new, *args, **opts, &block)
end

def colorize(*args, **opts, &block)
contents, colors = args.partition do |arg|
arg.is_a?(String) || arg.is_a?(self.class)
Expand All @@ -45,7 +53,7 @@ def colorize(*args, **opts, &block)

def text(*contents, **, &block)
if block
evaluate_block(&block)
apply(&block)
elsif contents.any?
contents.each do |part|
add_part(part)
Expand All @@ -60,10 +68,14 @@ def text(*contents, **, &block)

def line(*contents, indent_by: 0, &block)
indent(by: indent_by) do
# if block
# binding.pry
# end

add_part(indentation_stack.join)

if block
evaluate_block(&block)
apply(&block)
elsif contents.any?
text(*contents)
else
Expand All @@ -75,6 +87,7 @@ def line(*contents, indent_by: 0, &block)

add_part("\n")
end
alias_method :add_line, :line

def newline
add_part("\n")
Expand All @@ -83,10 +96,18 @@ def newline
def indent(by:, &block)
# TODO: This won't work if using `text` manually to add lines
indentation_stack << (by.is_a?(String) ? by : " " * by)
evaluate_block(&block)
apply(&block)
indentation_stack.pop
end

def apply(&block)
if block.arity > 0
block.call(self)
else
instance_eval(&block)
end
end

def method_missing(name, *args, **opts, &block)
request = derive_request_from(name)

Expand Down Expand Up @@ -128,14 +149,6 @@ def derive_request_from(name)
end
end

def evaluate_block(&block)
if block.arity > 0
block.call(self)
else
instance_eval(&block)
end
end

def add_part(part)
parts.push(part)
end
Expand Down Expand Up @@ -165,16 +178,16 @@ def wrapper

class MethodRequest < Request
def resolve(doc, args, opts, &block)
doc.public_send(wrapper) do |d|
d.public_send(name, *args, **opts, &block)
doc.public_send(wrapper, **opts) do |d|
d.public_send(name, *args, &block)
end
end
end

class ColorRequest < Request
def resolve(doc, args, opts, &block)
doc.public_send(wrapper) do |d|
d.colorize(*args, **opts, fg: name, &block)
doc.public_send(wrapper, **opts) do |d|
d.colorize(*args, fg: name, &block)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/super_diff/csi/uncolorized_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class UncolorizedDocument < Document
protected

def colorize_block(*, &block)
evaluate_block(&block)
apply(&block)
end

def colorize_inline(contents, *)
Expand Down
9 changes: 9 additions & 0 deletions lib/super_diff/csi/underline_sequence.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module SuperDiff
module Csi
class UnderlineSequence
def to_s
"\e[4m"
end
end
end
end
43 changes: 43 additions & 0 deletions lib/super_diff/diff_legend_builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
module SuperDiff
class DiffLegendBuilder
extend AttrExtras.mixin

method_object :expected

def call
SuperDiff::Helpers.style do
line do
header "Diff:"
end

newline

line do
border "┌ (Key) ──────────────────────────┐"
end

line do
border "│ "
alpha "‹-› in expected, not in actual"
border " │"
end

line do
border "│ "
beta "‹+› in actual, not in expected"
border " │"
end

line do
border "│ "
plain "‹ › in both expected and actual"
border " │"
end

line do
border "└─────────────────────────────────┘"
end
end
end
end
end
4 changes: 2 additions & 2 deletions lib/super_diff/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ def self.style(*args, color_enabled: true, **opts, &block)
document = klass.new.extend(ColorizedDocumentExtensions)

if block
document.__send__(:evaluate_block, &block)
else
document.apply(&block)
elsif args.any? || opts.any?
document.colorize(*args, **opts)
end

Expand Down
19 changes: 19 additions & 0 deletions lib/super_diff/rspec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,19 @@ module RSpec
autoload :Configuration, "super_diff/rspec/configuration"
autoload :Differ, "super_diff/rspec/differ"
autoload :Differs, "super_diff/rspec/differs"
autoload(
:ExceptionMessageFormatters,
"super_diff/rspec/exception_message_formatters",
)
autoload(
:ExceptionMessageFormatterMap,
"super_diff/rspec/exception_message_formatter_map",
)
autoload :MatcherTextBuilders, "super_diff/rspec/matcher_text_builders"
autoload :MatcherTextTemplate, "super_diff/rspec/matcher_text_template"
autoload :ObjectInspection, "super_diff/rspec/object_inspection"
autoload :OperationalSequencers, "super_diff/rspec/operational_sequencers"
autoload :StringTagger, "super_diff/rspec/string_tagger"

class << self
attr_accessor :extra_differ_classes
Expand All @@ -25,6 +34,16 @@ def self.configuration
@_configuration ||= Configuration.new
end

def self.format_exception_message(exception_message)
exception_message_formatter_map.
call(exception_message).
call(exception_message)
end

def self.exception_message_formatter_map
@_exception_message_formatter_map ||= ExceptionMessageFormatterMap.new
end

def self.a_hash_including_something?(value)
fuzzy_object?(value) &&
value.respond_to?(:expecteds) &&
Expand Down
2 changes: 2 additions & 0 deletions lib/super_diff/rspec/differ.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class Differ
static_facade :diff, :actual, :expected

def diff
# binding.pry

if worth_diffing?
diff = SuperDiff::Differ.call(
expected,
Expand Down
23 changes: 23 additions & 0 deletions lib/super_diff/rspec/exception_message_formatter_map.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module SuperDiff
module RSpec
class ExceptionMessageFormatterMap
class NotFoundError < StandardError; end

def call(exception_message)
strategies = [
ExceptionMessageFormatters::ExpectationError,
ExceptionMessageFormatters::UnexpectedMessageArgsError,
ExceptionMessageFormatters::Default,
]

found_strategy = strategies.find do |strategy|
strategy.applies_to?(exception_message)
end

found_strategy or raise NotFoundError.new(
"Could not find an appropriate formatter class!",
)
end
end
end
end
16 changes: 16 additions & 0 deletions lib/super_diff/rspec/exception_message_formatters.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module SuperDiff
module RSpec
module ExceptionMessageFormatters
autoload :Base, "super_diff/rspec/exception_message_formatters/base"
autoload :Default, "super_diff/rspec/exception_message_formatters/default"
autoload(
:ExpectationError,
"super_diff/rspec/exception_message_formatters/expectation_error",
)
autoload(
:UnexpectedMessageArgsError,
"super_diff/rspec/exception_message_formatters/unexpected_message_args_error",
)
end
end
end
Loading