Class: HTMLRunner

Inherits:
Test::Unit::UI::Console::TestRunner
  • Object
show all
Defined in:
lib/gorp/test.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.run(suite) ⇒ Object



111
112
113
114
# File 'lib/gorp/test.rb', line 111

def self.run suite
  @@sections = suite.sections
  super
end

Instance Method Details

#attach_to_mediatorObject



116
117
118
119
120
121
122
123
# File 'lib/gorp/test.rb', line 116

def attach_to_mediator
  super
  @html_tests = []
  @mediator.add_listener(Test::Unit::TestResult::FAULT,
    &method(:html_fault))
  @mediator.add_listener(Test::Unit::UI::TestRunnerMediator::FINISHED,
    &method(:html_summary))
end

#html_fault(fault) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/gorp/test.rb', line 125

def html_fault fault
  if fault.test_name =~ /^test_([\d.]+)_.*\(\w+\)$/
    name = $1
    sections = @@sections
    return unless sections.has_key? name

    # indicate failure in the toc
    sections[:contents][/<a href="#section-#{name}"()>/,1] = 
      ' style="color:red; font-weight:bold"'

    # provide details in the section itself
    x = Builder::XmlMarkup.new(:indent => 2)
    if fault.respond_to? :location
      x.pre fault.message.sub(".\n<false> is not true",'') +
        "\n\nTraceback:\n  " + fault.location.join("\n  "),
        :class=>'traceback'
    else
      x.pre fault.message, :class=>'traceback'
    end
    sections[name][/<\/a>()/,1] = x.target!
  end
end

#html_summary(elapsed) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/gorp/test.rb', line 148

def html_summary elapsed
  open("#{$output}.html",'w') do |output|
    sections = @@sections
    output.write(sections.delete(:head))
    output.write("<body>\n    ")
    output.write(sections.delete(:contents))
    tail = sections.delete(:tail)
    sections.keys.sort_by {|key| key.split('.').map {|n| n.to_i}}.each do |n|
      output.write(sections[n])
    end
    output.write("\n  </body>")
    output.write(tail)
  end
end