|
| 1 | +# Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com> |
| 2 | +# Copyright, 2018, by Janko Marohnić. |
| 3 | +# |
| 4 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +# of this software and associated documentation files (the "Software"), to deal |
| 6 | +# in the Software without restriction, including without limitation the rights |
| 7 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +# copies of the Software, and to permit persons to whom the Software is |
| 9 | +# furnished to do so, subject to the following conditions: |
| 10 | +# |
| 11 | +# The above copyright notice and this permission notice shall be included in |
| 12 | +# all copies or substantial portions of the Software. |
| 13 | +# |
| 14 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | +# THE SOFTWARE. |
| 21 | + |
| 22 | +require_relative '../trace' |
| 23 | + |
| 24 | +require 'rspec/expectations' |
| 25 | + |
| 26 | +module RSpec |
| 27 | +module Memory |
| 28 | +module Matchers |
| 29 | +class LimitAllocations |
| 30 | +include ::RSpec::Matchers::Composable |
| 31 | + |
| 32 | +def initialize(allocations = {}, count: nil, size: nil) |
| 33 | +@count = count |
| 34 | +@size = size |
| 35 | + |
| 36 | +@allocations = {} |
| 37 | +@errors = [] |
| 38 | + |
| 39 | +allocations.each do |klass, count| |
| 40 | +self.of(klass, count: count) |
| 41 | +end |
| 42 | +end |
| 43 | + |
| 44 | +def supports_block_expectations? |
| 45 | +true |
| 46 | +end |
| 47 | + |
| 48 | +def of(klass, **limits) |
| 49 | +@allocations[klass] = limits |
| 50 | + |
| 51 | +return self |
| 52 | +end |
| 53 | + |
| 54 | +private def check(value, limit) |
| 55 | +case limit |
| 56 | +when Range |
| 57 | +unless limit.include? value |
| 58 | +yield "expected within #{limit}" |
| 59 | +end |
| 60 | +when Integer |
| 61 | +unless value == limit |
| 62 | +yield "expected exactly #{limit}" |
| 63 | +end |
| 64 | +end |
| 65 | +end |
| 66 | + |
| 67 | +def matches?(given_proc) |
| 68 | +return true unless trace = Trace.capture(@allocations.keys, &given_proc) |
| 69 | + |
| 70 | +if @count or @size |
| 71 | +# If the spec specifies a total limit, we have a limit which we can enforce which takes all allocations into account: |
| 72 | +total = trace.total |
| 73 | + |
| 74 | +check(total.count, @count) do |expected| |
| 75 | +@errors << "allocated #{total.count} instances, #{total.size} bytes, #{expected} instances" |
| 76 | +end if @count |
| 77 | + |
| 78 | +check(total.size, @size) do |expected| |
| 79 | +@errors << "allocated #{total.count} instances, #{total.size} bytes, #{expected} bytes" |
| 80 | +end if @size |
| 81 | +else |
| 82 | +# Otherwise unspecified allocations are considered an error: |
| 83 | +trace.ignored.each do |klass, allocation| |
| 84 | +@errors << "allocated #{allocation.count} #{klass} instances, #{allocation.size} bytes, but it was not specified" |
| 85 | +end |
| 86 | +end |
| 87 | + |
| 88 | +trace.allocated.each do |klass, allocation| |
| 89 | +next unless acceptable = @allocations[klass] |
| 90 | + |
| 91 | +check(allocation.count, acceptable[:count]) do |expected| |
| 92 | +@errors << "allocated #{allocation.count} #{klass} instances, #{allocation.size} bytes, #{expected} instances" |
| 93 | +end |
| 94 | + |
| 95 | +check(allocation.size, acceptable[:size]) do |expected| |
| 96 | +@errors << "allocated #{allocation.count} #{klass} instances, #{allocation.size} bytes, #{expected} bytes" |
| 97 | +end |
| 98 | +end |
| 99 | + |
| 100 | +return @errors.empty? |
| 101 | +end |
| 102 | + |
| 103 | +def failure_message |
| 104 | +"exceeded allocation limit: #{@errors.join(', ')}" |
| 105 | +end |
| 106 | +end |
| 107 | + |
| 108 | +def limit_allocations(*args) |
| 109 | +LimitAllocations.new(*args) |
| 110 | +end |
| 111 | +end |
| 112 | +end |
| 113 | +end |
0 commit comments