Class: RSpecDocumentation::Spec

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

Overview

Executes specs from a Markdown code block and provides the outcome in the appropriate format.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spec:, format:, parent:, location:, path:, index:) ⇒ Spec

rubocop:disable Metrics/ParameterLists



14
15
16
17
18
19
20
21
22
# File 'lib/rspec_documentation/spec.rb', line 14

def initialize(spec:, format:, parent:, location:, path:, index:) # rubocop:disable Metrics/ParameterLists
  @spec = spec
  @format = format.empty? ? :plaintext : format.to_sym
  @parent = parent
  @location = location
  @path = path
  @index = index
  @failures = []
end

Class Attribute Details

.durationsObject

Returns the value of attribute durations.



11
12
13
# File 'lib/rspec_documentation/spec.rb', line 11

def durations
  @durations
end

.subjectsObject

Returns the value of attribute subjects.



11
12
13
# File 'lib/rspec_documentation/spec.rb', line 11

def subjects
  @subjects
end

Instance Attribute Details

#formatObject (readonly)

Returns the value of attribute format.



6
7
8
# File 'lib/rspec_documentation/spec.rb', line 6

def format
  @format
end

#indexObject (readonly)

Returns the value of attribute index.



6
7
8
# File 'lib/rspec_documentation/spec.rb', line 6

def index
  @index
end

#locationObject (readonly)

Returns the value of attribute location.



6
7
8
# File 'lib/rspec_documentation/spec.rb', line 6

def location
  @location
end

#parentObject (readonly)

Returns the value of attribute parent.



6
7
8
# File 'lib/rspec_documentation/spec.rb', line 6

def parent
  @parent
end

#pathObject (readonly)

Returns the value of attribute path.



6
7
8
# File 'lib/rspec_documentation/spec.rb', line 6

def path
  @path
end

Instance Method Details

#failureObject



35
36
37
# File 'lib/rspec_documentation/spec.rb', line 35

def failure
  failures.first
end

#reporterObject



39
40
41
# File 'lib/rspec_documentation/spec.rb', line 39

def reporter
  @reporter ||= ::RSpec::Core::Reporter.new(::RSpec::Core::Configuration.new)
end

#runObject



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rspec_documentation/spec.rb', line 43

def run
  self.class.subjects = []
  RSpec.with_failure_notifier(failure_notifier) do
    succeeded = example_group.run(reporter)
    durations << run_time if run_time
    next succeeded if succeeded

    notify_failure(reported_failure)
    succeeded
  end
end

#sourceObject



31
32
33
# File 'lib/rspec_documentation/spec.rb', line 31

def source
  spec
end

#subjectObject

Raises:



24
25
26
27
28
29
# File 'lib/rspec_documentation/spec.rb', line 24

def subject
  raise Error, "Code block did not define an example (e.g. with `it`).\n#{spec}" if examples.empty?
  raise Error, "Code block did not define a subject:\n#{spec}" if subjects.empty?

  subjects.last
end