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.
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/rspec/memory/matchers/limit_allocations.rb', line 16 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
87 88 89 |
# File 'lib/rspec/memory/matchers/limit_allocations.rb', line 87 def "exceeded allocation limit: #{@errors.join(', ')}" end |
#matches?(given_proc) ⇒ Boolean
51 52 53 54 55 56 57 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 |
# File 'lib/rspec/memory/matchers/limit_allocations.rb', line 51 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
32 33 34 35 36 |
# File 'lib/rspec/memory/matchers/limit_allocations.rb', line 32 def of(klass, **limits) @allocations[klass] = limits return self end |
#supports_block_expectations? ⇒ Boolean
28 29 30 |
# File 'lib/rspec/memory/matchers/limit_allocations.rb', line 28 def supports_block_expectations? true end |