Class: Res::Formatters::Rspec

Inherits:
Object
  • Object
show all
Defined in:
lib/res/formatters/rspec.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output) ⇒ Rspec

Returns a new instance of Rspec.



17
18
19
20
21
# File 'lib/res/formatters/rspec.rb', line 17

def initialize output
	@io = output
  @result = Array.new
  @start_time = Time.now
end

Instance Attribute Details

#outputObject

Returns the value of attribute output.



8
9
10
# File 'lib/res/formatters/rspec.rb', line 8

def output
  @output
end

#resultObject

Returns the value of attribute result.



8
9
10
# File 'lib/res/formatters/rspec.rb', line 8

def result
  @result
end

#start_timeObject

Returns the value of attribute start_time.



8
9
10
# File 'lib/res/formatters/rspec.rb', line 8

def start_time
  @start_time
end

Instance Method Details

#add_result(test) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/res/formatters/rspec.rb', line 71

def add_result(test)
  {
    type: "Rspec::Test",
    name: test[:description],
    urn: test[:location]
  }
end

#example_failed(failed_example_notification) ⇒ Object



59
60
61
62
63
# File 'lib/res/formatters/rspec.rb', line 59

def example_failed(failed_example_notification)
  status = failed_example_notification.example.[:execution_result].status.to_s
  test = failed_example_notification.example
  set_status(result, test.[:location], status)
end

#example_group_started(group_notification) ⇒ Object

Called Once per example group



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/res/formatters/rspec.rb', line 30

def example_group_started group_notification
 if @child == 0
    result[@count] = format_structure(group_notification.group)
    @child += 1
    i = 0
    if result[@count].has_key?(:children)
      index = result[@count][:children].count 
    else
      index =0
    end
    while i < group_notification.group.examples.count
      result[@count][:children] = Array.new if !result[@count].has_key?(:children)
      result[@count][:children][index] = Hash.new
      result[@count][:children][index] = add_result(group_notification.group.examples[i].)
      index += 1
      i += 1
    end 
    @count += 1
  end
  @child -= 1  
end

#example_passed(passed_example_notification) ⇒ Object

Called One of these per example, depending on outcome



53
54
55
56
57
# File 'lib/res/formatters/rspec.rb', line 53

def example_passed(passed_example_notification)
  status = passed_example_notification.example.[:execution_result].status.to_s
  test = passed_example_notification.example
  set_status(result, test.[:location], status)
end

#example_pending(pending_example_notification) ⇒ Object



65
66
67
68
69
# File 'lib/res/formatters/rspec.rb', line 65

def example_pending(pending_example_notification)
  status = "skipped"
  test = pending_example_notification.example
  set_status(result, test.[:location], status)
end

#format_structure(group) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/res/formatters/rspec.rb', line 79

def format_structure(group)
    
  result = Hash.new
  result[:type] = "Rspec::Describe"
  result[:name] = group.description
  result[:urn] = group.location
  if !group.children.empty?
    result[:children] = Array.new
    i = 0
    while i < group.children.count
      
      result[:children][i] = format_structure(group.children[i])
      @child += 1            
      
      k = 0
      j = i
      while k < group.children[i].examples.count
      if k == 0
        result[:children][j][:children] = Array.new  
        result[:children][j][:children][k] = Hash.new
      end
        result[:children][j][:children][k] = add_result(group.children[j].examples[k].)
        k += 1
      end
      i += 1
    end
  end
  result
end

#set_status(result, urn, status) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/res/formatters/rspec.rb', line 109

def set_status(result, urn, status)
  result.each do |r|
    if r[:type] == "Rspec::Describe"
      if Res.perf_data.size > 0
        r[:values] ||= {}
        Res.perf_data.each do |p|
          r[:values].merge! p
        end
        Res.perf_data = []
      end
      set_status(r[:children], urn, status)
    elsif r[:type] == "Rspec::Test"
      if r[:urn] == urn
        r[:status] = status
        break;
      end
    end
  end
end

#start(notification) ⇒ Object

Called when rspec starts execution



24
25
26
27
# File 'lib/res/formatters/rspec.rb', line 24

def start notification
  @count = 0
  @child = 0
end

#start_dump(start_dump_notification) ⇒ Object



140
141
142
143
144
# File 'lib/res/formatters/rspec.rb', line 140

def start_dump(start_dump_notification)
  @io = File.open("./rspec.res", "w") if @io.class.to_s != "File"
  @io.puts @ir.json
  @io.close
end

#stop(stop_notification) ⇒ Object

Called At the end of the suite



131
132
133
134
135
136
137
# File 'lib/res/formatters/rspec.rb', line 131

def stop(stop_notification)
  @ir = ::Res::IR.new( :type        => 'Rspec',
                      :started     => @start_time,
                      :results     => result,
                      :finished    => Time.now()
                      )
end