Class: HeapScope::RSpecMatchers::RetainLessThan

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

Instance Method Summary collapse

Constructor Details

#initialize(limit) ⇒ RetainLessThan

Returns a new instance of RetainLessThan.



9
10
11
12
13
14
# File 'lib/heapscope/rspec.rb', line 9

def initialize(limit)
  @limit = limit
  @unit = :objects
  @klass = nil
  @report = nil
end

Instance Method Details

#bytesObject



21
22
23
24
# File 'lib/heapscope/rspec.rb', line 21

def bytes
  @unit = :bytes
  self
end

#failure_messageObject



44
45
46
47
# File 'lib/heapscope/rspec.rb', line 44

def failure_message
  suspects = Array(@report&.suspects).first(5).map { |s| "#{s[:name]}(+#{s[:delta_count]})" }.join(", ")
  "expected retained #{@unit}#{@klass ? " of #{@klass}" : ""} < #{@limit}, got #{@actual}. Suspects: #{suspects}"
end

#failure_message_when_negatedObject



49
50
51
# File 'lib/heapscope/rspec.rb', line 49

def failure_message_when_negated
  "expected retained #{@unit} >= #{@limit}, got #{@actual}"
end

#matches?(proc) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/heapscope/rspec.rb', line 31

def matches?(proc)
  @report = HeapScope.measure(force_gc: true) { proc.call }
  @actual =
    if @klass
      @unit == :bytes ? @report.retained_bytes_for(@klass) : @report.retained_count_for(@klass)
    elsif @unit == :bytes
      @report.diff&.heap_bytes_estimate_delta.to_i
    else
      @report.diff&.surviving_estimate.to_i
    end
  @actual < @limit
end

#objectsObject



16
17
18
19
# File 'lib/heapscope/rspec.rb', line 16

def objects
  @unit = :objects
  self
end

#of(klass) ⇒ Object



26
27
28
29
# File 'lib/heapscope/rspec.rb', line 26

def of(klass)
  @klass = klass
  self
end

#supports_block_expectations?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/heapscope/rspec.rb', line 53

def supports_block_expectations?
  true
end