Class: Example

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(example) ⇒ Example

rubocop:disable Metrics/AbcSize



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/test_spec/rspec/example.rb', line 9

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

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



4
5
6
# File 'lib/test_spec/rspec/example.rb', line 4

def description
  @description
end

#durationObject (readonly)

Returns the value of attribute duration.



4
5
6
# File 'lib/test_spec/rspec/example.rb', line 4

def duration
  @duration
end

#example_groupObject (readonly)

Returns the value of attribute example_group.



4
5
6
# File 'lib/test_spec/rspec/example.rb', line 4

def example_group
  @example_group
end

#exceptionObject (readonly)

Returns the value of attribute exception.



4
5
6
# File 'lib/test_spec/rspec/example.rb', line 4

def exception
  @exception
end

#failed_screenshotObject (readonly)

Returns the value of attribute failed_screenshot.



4
5
6
# File 'lib/test_spec/rspec/example.rb', line 4

def failed_screenshot
  @failed_screenshot
end

#file_pathObject (readonly)

Returns the value of attribute file_path.



4
5
6
# File 'lib/test_spec/rspec/example.rb', line 4

def file_path
  @file_path
end

#full_descriptionObject (readonly)

Returns the value of attribute full_description.



4
5
6
# File 'lib/test_spec/rspec/example.rb', line 4

def full_description
  @full_description
end

#metadataObject (readonly)

Returns the value of attribute metadata.



4
5
6
# File 'lib/test_spec/rspec/example.rb', line 4

def 
  @metadata
end

#run_timeObject (readonly)

Returns the value of attribute run_time.



4
5
6
# File 'lib/test_spec/rspec/example.rb', line 4

def run_time
  @run_time
end

#screenrecordObject (readonly)

Returns the value of attribute screenrecord.



4
5
6
# File 'lib/test_spec/rspec/example.rb', line 4

def screenrecord
  @screenrecord
end

#screenshotsObject (readonly)

Returns the value of attribute screenshots.



4
5
6
# File 'lib/test_spec/rspec/example.rb', line 4

def screenshots
  @screenshots
end

#specObject (readonly)

Returns the value of attribute spec.



4
5
6
# File 'lib/test_spec/rspec/example.rb', line 4

def spec
  @spec
end

#statusObject (readonly)

Returns the value of attribute status.



4
5
6
# File 'lib/test_spec/rspec/example.rb', line 4

def status
  @status
end

Class Method Details

.load_spec_comments!(examples) ⇒ Object

rubocop:disable Metrics/LineLength rubocop:disable Metrics/AbcSize



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/test_spec/rspec/example.rb', line 80

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.create_spec_line(spec_lines.join) unless spec_lines.empty?
    end
  end
end

Instance Method Details

#commentObject



34
35
36
# File 'lib/test_spec/rspec/example.rb', line 34

def comment
  ERB::Util.html_escape(@comment)
end

#create_spec_line(spec_text) ⇒ Object



72
73
74
75
76
# File 'lib/test_spec/rspec/example.rb', line 72

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

#example_titleObject

rubocop:enable Metrics/AbcSize



28
29
30
31
32
# File 'lib/test_spec/rspec/example.rb', line 28

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

#has_comment?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/test_spec/rspec/example.rb', line 42

def has_comment?
  !@comment.nil?
end

#has_exception?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/test_spec/rspec/example.rb', line 58

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

#has_failed_screenshot?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/test_spec/rspec/example.rb', line 54

def has_failed_screenshot?
  !@failed_screenshot.nil?
end

#has_screenrecord?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/test_spec/rspec/example.rb', line 46

def has_screenrecord?
  !@screenrecord.nil?
end

#has_screenshots?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/test_spec/rspec/example.rb', line 50

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

#has_spec?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/test_spec/rspec/example.rb', line 38

def has_spec?
  !@spec.nil?
end

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



62
63
64
65
66
67
68
69
70
# File 'lib/test_spec/rspec/example.rb', line 62

def klass(prefix = 'label-')
  class_map = {
    passed: "#{prefix}success",
    failed: "#{prefix}danger",
    pending: "#{prefix}warning"
  }

  class_map[@status.to_sym]
end