class JUnitFormatter

Constants

AM
AP
GT
LT
QU
TARGET_ENCODING

Public Class Methods

new(out=nil) click to toggle source
Calls superclass method YamlFormatter.new
# File lib/mspec/runner/formatters/junit.rb, line 6
def initialize(out=nil)
  super
  @tests = []
end

Public Instance Methods

after(state = nil) click to toggle source
Calls superclass method YamlFormatter#after
# File lib/mspec/runner/formatters/junit.rb, line 11
def after(state = nil)
  super
  @tests << {:test => state, :exception => false} unless exception?
end
exception(exception) click to toggle source
Calls superclass method DottedFormatter#exception
# File lib/mspec/runner/formatters/junit.rb, line 16
def exception(exception)
  super
  @tests << {:test => exception, :exception => true}
end
finish() click to toggle source
# File lib/mspec/runner/formatters/junit.rb, line 21
  def finish
    switch

    time = @timer.elapsed
    tests = @tally.counter.examples
    errors = @tally.counter.errors
    failures = @tally.counter.failures

    printf <<-XML

<?xml version="1.0" encoding="UTF-8" ?>
    <testsuites
        testCount="#{tests}"
        errorCount="#{errors}"
        failureCount="#{failures}"
        timeCount="#{time}" time="#{time}">
      <testsuite
          tests="#{tests}"
          errors="#{errors}"
          failures="#{failures}"
          time="#{time}"
          name="Spec Output For #{::RUBY_NAME} (#{::RUBY_VERSION})">
    XML
    @tests.each do |h|
      description = encode_for_xml h[:test].description

      printf "        <testcase classname="%s" name="%s" time="%f">
", "Spec", description, 0.0
      if h[:exception]
        outcome = h[:test].failure? ? "failure" : "error"
        message = encode_for_xml h[:test].message
        backtrace = encode_for_xml h[:test].backtrace
        print <<-XML
          <#{outcome} message="error in #{description}" type="#{outcome}">
            #{message}
            #{backtrace}
          </#{outcome}>
        XML
      end
      print <<-XML
        </testcase>
      XML
    end

    print <<-XML
      </testsuite>
    </testsuites>
    XML
  end

Private Instance Methods

encode_as_latin1(str) click to toggle source
# File lib/mspec/runner/formatters/junit.rb, line 87
def encode_as_latin1(str)
  str.encode(TARGET_ENCODING, :undef => :replace, :invalid => :replace)
end
encode_for_xml(str) click to toggle source
# File lib/mspec/runner/formatters/junit.rb, line 80
def encode_for_xml(str)
  encode_as_latin1(str).gsub("<", LT).gsub(">", GT).
    gsub('"', QU).gsub("'", AP).gsub("&", AM).
    gsub(/[#{Regexp.escape("\0\1\2\3\4\5\6\7\8")}]/, "?")
end