Class: Crspec::Example

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

Defined Under Namespace

Classes: ExampleInvocation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(description, metadata, example_group, block, pending: false, location: nil) ⇒ Example

Returns a new instance of Example.



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

def initialize(description, , example_group, block, pending: false, location: nil)
  @id = SecureRandom.uuid
  @description = description
  @metadata = .freeze
  @example_group = example_group
  @block = block
  @status = :pending
  @pending = pending || block.nil? || [:skip] || [:pending] ? true : false
  @focused = [:focus] ? true : false
  @error = nil
  @execution_time = 0
  location ||= block&.source_location
  @file_path, @line_number = location if location
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



7
8
9
# File 'lib/crspec/example.rb', line 7

def block
  @block
end

#descriptionObject (readonly)

Returns the value of attribute description.



7
8
9
# File 'lib/crspec/example.rb', line 7

def description
  @description
end

#errorObject (readonly)

Returns the value of attribute error.



7
8
9
# File 'lib/crspec/example.rb', line 7

def error
  @error
end

#example_groupObject (readonly)

Returns the value of attribute example_group.



7
8
9
# File 'lib/crspec/example.rb', line 7

def example_group
  @example_group
end

#execution_timeObject (readonly)

Returns the value of attribute execution_time.



7
8
9
# File 'lib/crspec/example.rb', line 7

def execution_time
  @execution_time
end

#file_pathObject (readonly)

Returns the value of attribute file_path.



7
8
9
# File 'lib/crspec/example.rb', line 7

def file_path
  @file_path
end

#focusedObject

Returns the value of attribute focused.



8
9
10
# File 'lib/crspec/example.rb', line 8

def focused
  @focused
end

#idObject (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/crspec/example.rb', line 7

def id
  @id
end

#line_numberObject (readonly)

Returns the value of attribute line_number.



7
8
9
# File 'lib/crspec/example.rb', line 7

def line_number
  @line_number
end

#metadataObject (readonly)

Returns the value of attribute metadata.



7
8
9
# File 'lib/crspec/example.rb', line 7

def 
  @metadata
end

#statusObject (readonly)

Returns the value of attribute status.



7
8
9
# File 'lib/crspec/example.rb', line 7

def status
  @status
end

Instance Method Details

#execute!Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/crspec/example.rb', line 58

def execute!
  return if @pending

  start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  instance = @example_group.create_instance(self)

  begin
    around_hooks = @example_group.ancestor_hooks(:around, self)
    run_around_hooks(instance, around_hooks) do
      @example_group.run_eager_lets(instance)
      run_hooks(instance, :before)
      instance.instance_exec(&@block) if @block
      @status = :passed
    end
  rescue StandardError, ExpectationNotMetError => e
    @status = :failed
    @error = e
  ensure
    begin
      run_hooks(instance, :after)
    rescue StandardError => e
      if @status == :passed
        @status = :failed
        @error = e
      end
    end
    @execution_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time
  end
end

#focused?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/crspec/example.rb', line 25

def focused?
  @focused
end

#pending?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/crspec/example.rb', line 29

def pending?
  @pending
end

#persistence_keyObject



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/crspec/example.rb', line 33

def persistence_key
  @persistence_key ||= begin
    parts = []
    group = @example_group
    while group
      parts.unshift(group.description.to_s)
      group = group.parent
    end
    parts << @description.to_s
    parts.join(" > ")
  end
end