Class: Example

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

Overview

Processes the array of example information for all of the example group that has been run

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(example) ⇒ Example

Returns a new instance of Example.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rspec_pretty_html_reporter/example.rb', line 24

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_fs(: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.



21
22
23
# File 'lib/rspec_pretty_html_reporter/example.rb', line 21

def description
  @description
end

#durationObject (readonly)

Returns the value of attribute duration.



21
22
23
# File 'lib/rspec_pretty_html_reporter/example.rb', line 21

def duration
  @duration
end

#example_groupObject (readonly)

Returns the value of attribute example_group.



21
22
23
# File 'lib/rspec_pretty_html_reporter/example.rb', line 21

def example_group
  @example_group
end

#exceptionObject (readonly)

Returns the value of attribute exception.



21
22
23
# File 'lib/rspec_pretty_html_reporter/example.rb', line 21

def exception
  @exception
end

#failed_screenshotObject (readonly)

Returns the value of attribute failed_screenshot.



21
22
23
# File 'lib/rspec_pretty_html_reporter/example.rb', line 21

def failed_screenshot
  @failed_screenshot
end

#file_pathObject (readonly)

Returns the value of attribute file_path.



21
22
23
# File 'lib/rspec_pretty_html_reporter/example.rb', line 21

def file_path
  @file_path
end

#full_descriptionObject (readonly)

Returns the value of attribute full_description.



21
22
23
# File 'lib/rspec_pretty_html_reporter/example.rb', line 21

def full_description
  @full_description
end

#metadataObject (readonly)

Returns the value of attribute metadata.



21
22
23
# File 'lib/rspec_pretty_html_reporter/example.rb', line 21

def 
  @metadata
end

#run_timeObject (readonly)

Returns the value of attribute run_time.



21
22
23
# File 'lib/rspec_pretty_html_reporter/example.rb', line 21

def run_time
  @run_time
end

#screenrecordObject (readonly)

Returns the value of attribute screenrecord.



21
22
23
# File 'lib/rspec_pretty_html_reporter/example.rb', line 21

def screenrecord
  @screenrecord
end

#screenshotsObject (readonly)

Returns the value of attribute screenshots.



21
22
23
# File 'lib/rspec_pretty_html_reporter/example.rb', line 21

def screenshots
  @screenshots
end

#specObject (readonly)

Returns the value of attribute spec.



21
22
23
# File 'lib/rspec_pretty_html_reporter/example.rb', line 21

def spec
  @spec
end

#statusObject (readonly)

Returns the value of attribute status.



21
22
23
# File 'lib/rspec_pretty_html_reporter/example.rb', line 21

def status
  @status
end

Class Method Details

.load_spec_comments!(examples) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/rspec_pretty_html_reporter/example.rb', line 5

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



41
42
43
44
45
46
# File 'lib/rspec_pretty_html_reporter/example.rb', line 41

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

  title_arr.join('')
end

#has_exception?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/rspec_pretty_html_reporter/example.rb', line 48

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

#has_failed_screenshot?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/rspec_pretty_html_reporter/example.rb', line 64

def has_failed_screenshot?
  !@failed_screenshot.nil?
end

#has_screenrecord?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/rspec_pretty_html_reporter/example.rb', line 60

def has_screenrecord?
  !@screenrecord.nil?
end

#has_screenshots?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/rspec_pretty_html_reporter/example.rb', line 56

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

#has_spec?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/rspec_pretty_html_reporter/example.rb', line 52

def has_spec?
  !@spec.nil?
end

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



74
75
76
77
# File 'lib/rspec_pretty_html_reporter/example.rb', line 74

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

#set_spec(spec_text) ⇒ Object



68
69
70
71
72
# File 'lib/rspec_pretty_html_reporter/example.rb', line 68

def set_spec(spec_text)
  formatter = Rouge::Formatters::HTML.new
  lexer = Rouge::Lexers::Gherkin.new
  @spec = %(<div class="highlight"><pre class="highlight"><code>#{formatter.format(lexer.lex(spec_text.gsub('#->', '')))}</code></pre></div>)
end