Class: Propr::RSpecAdapter

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

Instance Method Summary collapse

Constructor Details

#initialize(group, options, property) ⇒ RSpecAdapter

Returns a new instance of RSpecAdapter.



4
5
6
7
8
9
10
11
12
13
# File 'lib/propr/rspec.rb', line 4

def initialize(group, options, property)
  @options, @group, @property =
    options, group, property

  # Run each property 100 times, allow 50 retries, and
  # start the scale at 0, grow suddenly towards the end
  @runner = Runner.new(100, 50,
  # lambda{|p,s,t,_| (p+s <= t ? p+s : t) / t })
    lambda{|p,s,t,_| (BigDecimal(p+s <= t ? p+s : t) / t) })
end

Instance Method Details

#check(*args, &generator) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/propr/rspec.rb', line 15

def check(*args, &generator)
  m        = self
  runner   = @runner
  property = @property

  if block_given?
    location = location(generator)

    @group.example(@property.name, @options.merge(caller: location)) do
      success, passed, skipped, counterex =
        runner.run(property, generator)

      unless success
        if skipped >= runner.maxskip
          raise NoMoreTries.new(runner.maxskip), nil, location
        else
          raise Falsifiable.new(counterex, m.shrink(counterex), passed, skipped), nil, location
        end
      end
    end
  else
    location = location(caller)

    @group.example(@property.name, @options.merge(caller: location)) do
      unless property.call(*args)
        raise Falsifiable.new(args, m.shrink(args), 0, 0), nil, location
      end
    end
  end

  # Return `self` to allow chaining calls to `check`
  self
end

#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