Class: RSpec::Memory::Matchers::LimitAllocations
- Inherits:
-
Object
- Object
- RSpec::Memory::Matchers::LimitAllocations
- Includes:
- RSpec::Matchers::Composable
- Defined in:
- lib/rspec/memory/matchers/limit_allocations.rb
Instance Method Summary collapse
- #failure_message ⇒ Object
-
#initialize(allocations = {}, count: nil, size: nil) ⇒ LimitAllocations
constructor
A new instance of LimitAllocations.
- #matches?(given_proc) ⇒ Boolean
- #of(klass, **limits) ⇒ Object
- #supports_block_expectations? ⇒ Boolean
Constructor Details
#initialize(allocations = {}, count: nil, size: nil) ⇒ LimitAllocations
Returns a new instance of LimitAllocations.
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/rspec/memory/matchers/limit_allocations.rb', line 32 def initialize(allocations = {}, count: nil, size: nil) @count = count @size = size @allocations = {} @errors = [] allocations.each do |klass, count| self.of(klass, count: count) end end |
Instance Method Details
#failure_message ⇒ Object
103 104 105 |
# File 'lib/rspec/memory/matchers/limit_allocations.rb', line 103 def "exceeded allocation limit: #{@errors.join(', ')}" end |
#matches?(given_proc) ⇒ Boolean
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/rspec/memory/matchers/limit_allocations.rb', line 67 def matches?(given_proc) return true unless trace = Trace.capture(@allocations.keys, &given_proc) if @count or @size # If the spec specifies a total limit, we have a limit which we can enforce which takes all allocations into account: total = trace.total check(total.count, @count) do |expected| @errors << "allocated #{total.count} instances, #{total.size} bytes, #{expected} instances" end if @count check(total.size, @size) do |expected| @errors << "allocated #{total.count} instances, #{total.size} bytes, #{expected} bytes" end if @size else # Otherwise unspecified allocations are considered an error: trace.ignored.each do |klass, allocation| @errors << "allocated #{allocation.count} #{klass} instances, #{allocation.size} bytes, but it was not specified" end end trace.allocated.each do |klass, allocation| next unless acceptable = @allocations[klass] check(allocation.count, acceptable[:count]) do |expected| @errors << "allocated #{allocation.count} #{klass} instances, #{allocation.size} bytes, #{expected} instances" end check(allocation.size, acceptable[:size]) do |expected| @errors << "allocated #{allocation.count} #{klass} instances, #{allocation.size} bytes, #{expected} bytes" end end return @errors.empty? end |
#of(klass, **limits) ⇒ Object
48 49 50 51 52 |
# File 'lib/rspec/memory/matchers/limit_allocations.rb', line 48 def of(klass, **limits) @allocations[klass] = limits return self end |
#supports_block_expectations? ⇒ Boolean
44 45 46 |
# File 'lib/rspec/memory/matchers/limit_allocations.rb', line 44 def supports_block_expectations? true end |