PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/mspec/lib/mspec/runner/formatters/junit.rb

http://github.com/rubinius/rubinius
Ruby | 96 lines | 83 code | 11 blank | 2 comment | 1 complexity | d637477b2d3cd57c10c7cd385ba62473 MD5 | raw file
Possible License(s): BSD-3-Clause, MPL-2.0-no-copyleft-exception, 0BSD, GPL-2.0, LGPL-2.1
  1. require 'mspec/expectations/expectations'
  2. require 'mspec/utils/ruby_name'
  3. require 'mspec/runner/formatters/yaml'
  4. class JUnitFormatter < YamlFormatter
  5. def initialize(out=nil)
  6. super
  7. @tests = []
  8. end
  9. def after(state = nil)
  10. super
  11. @tests << {:test => state, :exception => false} unless exception?
  12. end
  13. def exception(exception)
  14. super
  15. @tests << {:test => exception, :exception => true}
  16. end
  17. def finish
  18. switch
  19. time = @timer.elapsed
  20. tests = @tally.counter.examples
  21. errors = @tally.counter.errors
  22. failures = @tally.counter.failures
  23. printf <<-XML
  24. <?xml version="1.0" encoding="UTF-8" ?>
  25. <testsuites
  26. testCount="#{tests}"
  27. errorCount="#{errors}"
  28. failureCount="#{failures}"
  29. timeCount="#{time}" time="#{time}">
  30. <testsuite
  31. tests="#{tests}"
  32. errors="#{errors}"
  33. failures="#{failures}"
  34. time="#{time}"
  35. name="Spec Output For #{::RUBY_NAME} (#{::RUBY_VERSION})">
  36. XML
  37. @tests.each do |h|
  38. description = encode_for_xml h[:test].description
  39. printf <<-XML, "Spec", description, 0.0
  40. <testcase classname="%s" name="%s" time="%f">
  41. XML
  42. if h[:exception]
  43. outcome = h[:test].failure? ? "failure" : "error"
  44. message = encode_for_xml h[:test].message
  45. backtrace = encode_for_xml h[:test].backtrace
  46. print <<-XML
  47. <#{outcome} message="error in #{description}" type="#{outcome}">
  48. #{message}
  49. #{backtrace}
  50. </#{outcome}>
  51. XML
  52. end
  53. print <<-XML
  54. </testcase>
  55. XML
  56. end
  57. print <<-XML
  58. </testsuite>
  59. </testsuites>
  60. XML
  61. end
  62. private
  63. LT = "&lt;"
  64. GT = "&gt;"
  65. QU = "&quot;"
  66. AP = "&apos;"
  67. AM = "&amp;"
  68. TARGET_ENCODING = "ISO-8859-1"
  69. def encode_for_xml(str)
  70. encode_as_latin1(str).gsub("<", LT).gsub(">", GT).
  71. gsub('"', QU).gsub("'", AP).gsub("&", AM).
  72. gsub(/[#{Regexp.escape("\0\1\2\3\4\5\6\7\8")}]/, "?")
  73. end
  74. if defined? Encoding
  75. def encode_as_latin1(str)
  76. str.encode(TARGET_ENCODING, :undef => :replace, :invalid => :replace)
  77. end
  78. else
  79. require 'iconv'
  80. def encode_as_latin1(str)
  81. Iconv.conv("#{TARGET_ENCODING}//TRANSLIT//IGNORE", "UTF-8", str)
  82. end
  83. end
  84. end