Method: Propr::RSpecAdapter#shrink

Defined in:
lib/propr/rspec.rb

#shrink(counterex) ⇒ Object

TODO: this method is not RSpec specific



50
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
# File 'lib/propr/rspec.rb', line 50

def shrink(counterex)
  if @property.arity.zero?
    return []
  end

  xs = [counterex]

  while true
    # Generate simpler examples
    ys = Array.bind(xs) do |args|
      head, *tail = args.map{|x| x.respond_to?(:shrink) ? x.shrink : [x] }
      head.product(*tail)
    end

    zs = []

    # Collect counter examples
    until ys.empty? or zs.length >= 10
      args = ys.delete_at(rand(ys.size))

      unless @property.call(*args)
        zs.push(args)
      end
    end

    if zs.empty?
      # No simpler counter examples
      return xs.first
    else
      # Try to further simplify these
      xs = zs
    end
  end
end