Class: Expectations::SuiteResults

Inherits:
Object
  • Object
show all
Defined in:
lib/expectations/suite_results.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(out) ⇒ SuiteResults

Returns a new instance of SuiteResults.



4
5
6
7
# File 'lib/expectations/suite_results.rb', line 4

def initialize(out)
  self.out, self.expectations = out, []
  out.print "Expectations "
end

Instance Attribute Details

#expectationsObject

Returns the value of attribute expectations.



2
3
4
# File 'lib/expectations/suite_results.rb', line 2

def expectations
  @expectations
end

#outObject

Returns the value of attribute out.



2
3
4
# File 'lib/expectations/suite_results.rb', line 2

def out
  @out
end

Instance Method Details

#<<(expectation_result) ⇒ Object



9
10
11
12
13
# File 'lib/expectations/suite_results.rb', line 9

def <<(expectation_result)
  out.print expectation_result.char
  self.expectations << expectation_result
  self
end

#errorsObject



23
24
25
# File 'lib/expectations/suite_results.rb', line 23

def errors
  expectations.select { |expectation| expectation.error? }
end

#failuresObject



27
28
29
# File 'lib/expectations/suite_results.rb', line 27

def failures
  expectations.select { |expectation| expectation.failure? }
end

#filter_backtrace(trace) ⇒ Object



93
94
95
96
97
98
99
100
101
# File 'lib/expectations/suite_results.rb', line 93

def filter_backtrace(trace)
  patterns_to_strip = [/\/expectations\/lib\/expectations\//, /\/lib\/ruby\/1\.[89]/]
  result = patterns_to_strip.inject(trace) do |result, element|
    result = result.select { |line| line !~ element}
  end
  result.collect do |line|
    "\n#{line}"
  end
end

#fulfilledObject



19
20
21
# File 'lib/expectations/suite_results.rb', line 19

def fulfilled
  expectations.select { |expectation| expectation.fulfilled? }
end


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/expectations/suite_results.rb', line 46

def print_fail
  out.puts "\nFailure: #{failures.size} failed, #{errors.size} errors, #{fulfilled.size} fulfilled"
  out.puts "\n--Errors--" if errors.any?
  errors.each do |error|
    out.puts "#{error.file}:#{error.line}:in `expect'" if ENV["TM_MODE"]
    out.puts "file <#{error.file}>"
    out.puts "line <#{error.line}>"
    out.puts "error <#{error.exception.message}>"
    out.puts "trace #{filter_backtrace(error.exception.backtrace).join('')}"
    out.puts "#{error.message}" if error.message && error.message.any?
    out.puts "\n"
  end
  out.puts "\n--Failures--" if failures.any?
  failures.each do |failure|
    out.puts "#{failure.file}:#{failure.line}:in `expect'" if ENV["TM_MODE"]
    out.puts "file <#{failure.file}>"
    out.puts "line <#{failure.line}>"
    out.puts "#{failure.message}\n\n"
  end
end


31
32
33
34
35
36
37
38
39
40
# File 'lib/expectations/suite_results.rb', line 31

def print_results(benchmark)
  run_time = benchmark.real
  run_time = 0.001 if run_time < 0.001
  out.puts "\nFinished in #{run_time.to_s.gsub(/(\d*)\.(\d{0,5}).*/,'\1.\2')} seconds"
  if succeeded?
    print_success
  else
    print_fail
  end
end


42
43
44
# File 'lib/expectations/suite_results.rb', line 42

def print_success
  out.puts "\nSuccess: #{fulfilled.size} fulfilled"
end

#succeeded?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/expectations/suite_results.rb', line 15

def succeeded?
  expectations.all? { |expectation| expectation.fulfilled? }
end

#write_junit_xml(path) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/expectations/suite_results.rb', line 67

def write_junit_xml(path)
  FileUtils.rm_rf path if File.exist?(path)
  FileUtils.mkdir_p path
  grouped_expectations = expectations.inject({}) do |result, expectation|
    result[expectation.file] = [] if result[expectation.file].nil?
    result[expectation.file] << expectation
    result
  end
  grouped_expectations.keys.each do |file_name|
    class_name = "#{File.basename(file_name, ".rb")}.xml"
    File.open("#{path}/TEST-#{class_name}", "w") do |file|
      file << '<?xml version="1.0" encoding="UTF-8" ?>'
      grouped_fulfilled = grouped_expectations[file_name].select { |expectation| expectation.fulfilled? }
      grouped_errors = grouped_expectations[file_name].select { |expectation| expectation.error? }
      grouped_failures = grouped_expectations[file_name].select { |expectation| expectation.failure? }
      file << %[<testsuite errors="#{grouped_errors.size}" skipped="0" tests="#{grouped_expectations[file_name].size}" time="0.00" failures="#{grouped_failures.size}" name="#{class_name}">]
      grouped_expectations[file_name].each do |expectation|
        file << %[<testcase time="0.0" name="line:#{expectation.line}">]
        file << %[<failure type="java.lang.AssertionError" message="#{ERB::Util.h(expectation.message)}"/>] if expectation.failure?
        file << %[</testcase>]
      end
      file << %[</testsuite>]
    end
  end
end