lib/difects.rb in difects-3.0.0 vs lib/difects.rb in difects-3.0.1

- old
+ new

@@ -582,11 +582,11 @@ raise "Cannot inject code block #{block.inspect} shared under "\ "identifier #{identifier.inspect} outside of a DIFECTS test." else # find the closest insulated parent test; this should always # succeed because root-level tests are insulated by default - test = @tests.reverse.find {|t| t.sandbox } + test = @tests.reverse.find {|t| t.sandbox } or raise IndexError test.sandbox.instance_eval(&block) end else raise ArgumentError, "No code block is shared under identifier "\ @@ -993,31 +993,29 @@ pretty = region.map do |n| format % [('=>' if n == source_line), n, source[n-1].chomp] end.unshift "[#{region.inspect}] in #{source_file}" - pretty.extend FailureDetailsCodeListing + pretty.extend FailureDetails::CodeListing end ) ] if binding # binding location details[:bind] = [source_file, binding_line].join(':') # variable values - names = eval('::Kernel.local_variables', binding, __FILE__, __LINE__) + variables = eval('::Kernel.local_variables', binding, __FILE__, __LINE__) - pairs = names.inject([]) do |pair, name| - value = eval(name.to_s, binding, __FILE__, __LINE__) - pair.push name.to_sym, value - end - - details[:vars] = Hash[*pairs].extend(FailureDetailsVariablesListing) + details[:vars] = variables.inject(Hash.new) do |hash, variable| + hash[variable.to_sym] = eval(variable.to_s, binding, __FILE__, __LINE__) + hash + end.extend(FailureDetails::VariablesListing) end - details.reject! {|k,v| v.nil? } + details.reject! {|key, value| (value || details[key]).nil? } @trace << details # show all failure details to the user display build_fail_trace(details) @@ -1065,40 +1063,40 @@ { outer.desc => inner } end end ## - # Logic to pretty print the code listing in a failure's details. + # Logic to pretty print the details of an assertion failure. # - module FailureDetailsCodeListing # @private - def to_yaml options = {} - # - # strip because to_yaml() will render the paragraph without escaping - # newlines ONLY IF the first and last character are non-whitespace! - # - join("\n").strip.to_yaml(options) - end + module FailureDetails # @private + module CodeListing + def to_yaml options = {} + # + # strip because to_yaml() will render the paragraph without escaping + # newlines ONLY IF the first and last character are non-whitespace! + # + join("\n").strip.to_yaml(options) + end - def pretty_print printer - margin = ' ' * printer.indent - printer.text [ - first, self[1..-1].map {|line| margin + line }, margin - ].join(printer.newline) + def pretty_print printer + margin = ' ' * printer.indent + printer.text [ + first, self[1..-1].map {|line| margin + line }, margin + ].join(printer.newline) + end end - end - module FailureDetailsVariablesListing # @private - def to_yaml options = {} - require 'pp' - require 'stringio' + module VariablesListing + def to_yaml options = {} + require 'pp' + require 'stringio' - pairs = [] - each do |variable, value| - pretty = PP.pp(value, StringIO.new).string.chomp - pairs.push variable, "(#{value.class}) #{pretty}" + inject(Hash.new) do |hash, (variable, value)| + pretty = PP.pp(value, StringIO.new).string.chomp + hash[variable] = "(#{value.class}) #{pretty}" + hash + end.to_yaml(options) end - - Hash[*pairs].to_yaml(options) end end class Suite # @private attr_reader :tests, :before_each, :after_each, :before_all, :after_all