Skip to content
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

Introduce customizable error descriptions. #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
18 changes: 18 additions & 0 deletions bin/bacon
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Bacon; end

automatic = false
output = 'SpecDoxOutput'
error = 'InlineErrorDescription'

options = OptionParser.new("", 24, ' ') { |opts|
opts.banner = "Usage: bacon [options] [files | -a] [-- untouched arguments]"
Expand Down Expand Up @@ -57,6 +58,22 @@ options = OptionParser.new("", 24, ' ') { |opts|
"do FORMAT (SpecDox/TestUnit/Tap) output") { |format|
output = format + "Output"
}

opts.on("-i", "--inline", "show errors inlined (default)") {
error = 'InlineErrorDescription'
}
opts.on("-l", "--newline", "show errors with newlines") {
error = 'NewlineErrorDescription'
}
opts.on("-f", "--diff", "show errors with diff command") {
error = 'DiffErrorDescription'
}

opts.on("--error DESCRIPTOR",
"show errors with DESCRIPTOR (Inline/Newline/Diff)") { |descriptor|
error = descriptor + "ErrorDescription"
}

opts.on("-Q", "--no-backtrace", "don't print backtraces") {
Bacon.const_set :Backtraces, false
}
Expand Down Expand Up @@ -110,6 +127,7 @@ end
require 'bacon'

Bacon.extend Bacon.const_get(output) rescue abort "No such formatter: #{output}"
Bacon.extend Bacon.const_get(error) rescue abort "No such error descriptor: #{error}"
Bacon.summary_on_exit

files.each { |file|
Expand Down
42 changes: 37 additions & 5 deletions lib/bacon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,42 @@ def handle_summary; end

extend SpecDoxOutput # default

module InlineErrorDescription
def handle_error_description negated, object, name, args
desc = negated ? "not " : ""
desc << object.inspect << "." << name
desc << "(" << args.map{|x|x.inspect}.join(", ") << ") failed"
end
end

module NewlineErrorDescription
def handle_error_description negated, object, name, args
desc = negated ? "not " : ""
desc << "\n#{object.inspect}\n.#{name}(\n"
desc << args.map{|x|x.inspect}.join(",\n") << "\n) failed"
end
end

module DiffErrorDescription
def handle_error_description negated, object, name, args
require 'tempfile'
desc = negated ? "not " : ""
Tempfile.open('expect') do |expect|
Tempfile.open('was') do |was|
expect.puts(object.to_s)
expect.close
was.puts(args.map{|x|x.to_s}.join(",\n"))
was.close
desc << "#{object.class}##{name}(\n"
desc << `diff #{expect.path} #{was.path}`
end
end
desc << ") failed"
end
end

extend InlineErrorDescription # default

class Error < RuntimeError
attr_accessor :count_as

Expand Down Expand Up @@ -334,11 +370,7 @@ def satisfy(description="", &block)

def method_missing(name, *args, &block)
name = "#{name}?" if name.to_s =~ /\w[^?]\z/

desc = @negated ? "not " : ""
desc << @object.inspect << "." << name.to_s
desc << "(" << args.map{|x|x.inspect}.join(", ") << ") failed"

desc = Bacon.handle_error_description(@negated, @object, name.to_s, args)
satisfy(desc) { |x| x.__send__(name, *args, &block) }
end

Expand Down