Class: RspecLogFormatter::Analysis::Score

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec_log_formatter/analysis/score.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(desc, opts = {}) ⇒ Score

Returns a new instance of Score.


4
5
6
7
8
9
10
11
12
# File 'lib/rspec_log_formatter/analysis/score.rb', line 4

def initialize(desc, opts={})
  @description = desc
  @runs = 0
  @failures = 0
  @failure_messages = []
  @last_fail_time = Time.at(0)
  @last_pass_time = Time.at(0)
  @max_reruns = opts[:max_reruns]
end

Instance Attribute Details

#failure_messagesObject

Returns the value of attribute failure_messages


59
60
61
# File 'lib/rspec_log_formatter/analysis/score.rb', line 59

def failure_messages
  @failure_messages
end

#failuresObject

Returns the value of attribute failures


59
60
61
# File 'lib/rspec_log_formatter/analysis/score.rb', line 59

def failures
  @failures
end

#runsObject

Returns the value of attribute runs


59
60
61
# File 'lib/rspec_log_formatter/analysis/score.rb', line 59

def runs
  @runs
end

Instance Method Details

#<=>(other) ⇒ Object


26
27
28
29
30
31
32
# File 'lib/rspec_log_formatter/analysis/score.rb', line 26

def <=>(other)
  if max_reruns
    other.cost <=> cost
  else
    other.fraction <=> fraction
  end
end

#absorb(result) ⇒ Object


38
39
40
41
42
43
44
45
46
# File 'lib/rspec_log_formatter/analysis/score.rb', line 38

def absorb(result)
  if result.failure? && result.time > @last_fail_time
    @last_fail_time = result.time
    @fail_duration = result.duration
  elsif result.success? && result.time > @last_pass_time
    @last_pass_time = result.time
    @pass_duration = result.duration
  end
end

#as_hashObject


48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rspec_log_formatter/analysis/score.rb', line 48

def as_hash
  h = {
    description: @description,
    fraction: fraction,
    failure_messages: failure_messages,
  }
  if max_reruns
    h.merge!({cost: cost})
  end
  h
end

#costObject


18
19
20
21
22
23
24
# File 'lib/rspec_log_formatter/analysis/score.rb', line 18

def cost
  sum = 0.0
  0.upto(max_reruns) do |i|
    sum += (fraction**i)*(1.0-fraction)*(i*@fail_duration + @pass_duration)
  end
  sum + (fraction**(max_reruns+1.0))*@fail_duration
end

#flaky?Boolean

Returns:

  • (Boolean)

34
35
36
# File 'lib/rspec_log_formatter/analysis/score.rb', line 34

def flaky?
  fraction > 0.0
end

#fractionObject


14
15
16
# File 'lib/rspec_log_formatter/analysis/score.rb', line 14

def fraction
  @failures.to_f/@runs
end