Skip to content

Instantly share code, notes, and snippets.

@pnc
Created September 12, 2010 16:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pnc/576203 to your computer and use it in GitHub Desktop.
Save pnc/576203 to your computer and use it in GitHub Desktop.
require "tempfile"
require 'spec/runner/formatter/html_formatter'
require "cgi"
# When we're running specs, modify our class under test to create
# an image tag when it is inspected.
class Tree
def inspect
file = Tempfile.new("debug-tree")
self.to_image(file.path)
"<img src=\"file://#{file.path}.svg\"/>"
end
end
# Reopen the TextMateFormatter and slightly change the way it escapes —
# this is regrettably duplicated from the original.
module Spec
module Runner
module Formatter
# Formats backtraces so they're clickable by TextMate
class TextMateFormatter < HtmlFormatter
def example_failed(example, counter, failure)
extra = extra_failure_content(failure)
failure_style = failure.pending_fixed? ? 'pending_fixed' : 'failed'
@output.puts " <script type=\"text/javascript\">makeRed('rspec-header');</script>" unless @header_red
@header_red = true
@output.puts " <script type=\"text/javascript\">makeRed('example_group_#{example_group_number}');</script>" unless @example_group_red
@example_group_red = true
move_progress
@output.puts " <dd class=\"spec #{failure_style}\">"
@output.puts " <span class=\"failed_spec_name\">#{h(example.description)}</span>"
@output.puts " <div class=\"failure\" id=\"failure_#{counter}\">"
# This is the magic: escape the message, but then allow image tags through
@output.puts " <div class=\"message\"><pre>#{CGI::unescapeElement(h(failure.exception.message), "img")}</pre></div>" unless failure.exception.nil?
@output.puts " <div class=\"backtrace\"><pre>#{format_backtrace(failure.exception.backtrace)}</pre></div>" unless failure.exception.nil?
@output.puts extra unless extra == ""
@output.puts " </div>"
@output.puts " </dd>"
@output.flush
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment