Class: Example

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(example) ⇒ Example

Returns a new instance of Example.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/rspec_html_reporter.rb', line 106

def initialize(example)
  @example_group = example.example_group.to_s
  @description = example.description
  @full_description = example.full_description
  @execution_result = example.execution_result
  @run_time = (@execution_result.run_time).round(5)
  @duration = @execution_result.run_time.to_s(:rounded, precision: 5)
  @status = @execution_result.status.to_s
  @metadata = example.
  @file_path = @metadata[:file_path]
  @exception = Oopsy.new(example, @file_path)
  @spec = nil
  @screenshots = @metadata[:screenshots]
  @screenrecord = @metadata[:screenrecord]
  @failed_screenshot = @metadata[:failed_screenshot]
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



104
105
106
# File 'lib/rspec_html_reporter.rb', line 104

def description
  @description
end

#durationObject (readonly)

Returns the value of attribute duration.



104
105
106
# File 'lib/rspec_html_reporter.rb', line 104

def duration
  @duration
end

#example_groupObject (readonly)

Returns the value of attribute example_group.



104
105
106
# File 'lib/rspec_html_reporter.rb', line 104

def example_group
  @example_group
end

#exceptionObject (readonly)

Returns the value of attribute exception.



104
105
106
# File 'lib/rspec_html_reporter.rb', line 104

def exception
  @exception
end

#failed_screenshotObject (readonly)

Returns the value of attribute failed_screenshot.



104
105
106
# File 'lib/rspec_html_reporter.rb', line 104

def failed_screenshot
  @failed_screenshot
end

#file_pathObject (readonly)

Returns the value of attribute file_path.



104
105
106
# File 'lib/rspec_html_reporter.rb', line 104

def file_path
  @file_path
end

#full_descriptionObject (readonly)

Returns the value of attribute full_description.



104
105
106
# File 'lib/rspec_html_reporter.rb', line 104

def full_description
  @full_description
end

#metadataObject (readonly)

Returns the value of attribute metadata.



104
105
106
# File 'lib/rspec_html_reporter.rb', line 104

def 
  @metadata
end

#run_timeObject (readonly)

Returns the value of attribute run_time.



104
105
106
# File 'lib/rspec_html_reporter.rb', line 104

def run_time
  @run_time
end

#screenrecordObject (readonly)

Returns the value of attribute screenrecord.



104
105
106
# File 'lib/rspec_html_reporter.rb', line 104

def screenrecord
  @screenrecord
end

#screenshotsObject (readonly)

Returns the value of attribute screenshots.



104
105
106
# File 'lib/rspec_html_reporter.rb', line 104

def screenshots
  @screenshots
end

#specObject (readonly)

Returns the value of attribute spec.



104
105
106
# File 'lib/rspec_html_reporter.rb', line 104

def spec
  @spec
end

#statusObject (readonly)

Returns the value of attribute status.



104
105
106
# File 'lib/rspec_html_reporter.rb', line 104

def status
  @status
end

Class Method Details

.load_spec_comments!(examples) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/rspec_html_reporter.rb', line 88

def self.load_spec_comments!(examples)
  examples.group_by(&:file_path).each do |file_path, file_examples|
    lines = File.readlines(file_path)

    file_examples.zip(file_examples.rotate).each do |ex, next_ex|
      lexically_next = next_ex &&
        next_ex.file_path == ex.file_path &&
        next_ex.[:line_number] > ex.[:line_number]
      start_line_idx = ex.[:line_number] - 1
      next_start_idx = (lexically_next ? next_ex.[:line_number] : lines.size) - 1
      spec_lines = lines[start_line_idx...next_start_idx].select { |l| l.match(/#->/) }
      ex.set_spec(spec_lines.join) unless spec_lines.empty?
    end
  end
end

Instance Method Details

#example_titleObject



123
124
125
126
127
128
# File 'lib/rspec_html_reporter.rb', line 123

def example_title
  title_arr = @example_group.to_s.split('::') - ['RSpec', 'ExampleGroups']
  title_arr.push @description

  title_arr.join('')
end

#has_exception?Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/rspec_html_reporter.rb', line 130

def has_exception?
  !@exception.klass.nil?
end

#has_failed_screenshot?Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/rspec_html_reporter.rb', line 146

def has_failed_screenshot?
  !@failed_screenshot.nil?
end

#has_screenrecord?Boolean

Returns:

  • (Boolean)


142
143
144
# File 'lib/rspec_html_reporter.rb', line 142

def has_screenrecord?
  !@screenrecord.nil?
end

#has_screenshots?Boolean

Returns:

  • (Boolean)


138
139
140
# File 'lib/rspec_html_reporter.rb', line 138

def has_screenshots?
  !@screenshots.nil? && !@screenshots.empty?
end

#has_spec?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/rspec_html_reporter.rb', line 134

def has_spec?
  !@spec.nil?
end

#klass(prefix = 'label-') ⇒ Object



156
157
158
159
# File 'lib/rspec_html_reporter.rb', line 156

def klass(prefix='label-')
  class_map = {passed: "#{prefix}success", failed: "#{prefix}danger", pending: "#{prefix}warning"}
  class_map[@status.to_sym]
end

#set_spec(spec_text) ⇒ Object



150
151
152
153
154
# File 'lib/rspec_html_reporter.rb', line 150

def set_spec(spec_text)
  formatter = Rouge::Formatters::HTML.new(css_class: 'highlight')
  lexer = Rouge::Lexers::Gherkin.new
  @spec = formatter.format(lexer.lex(spec_text.gsub('#->', '')))
end