Class: Rantly::Property

Inherits:
Object
  • Object
show all
Defined in:
lib/rantly/property.rb

Constant Summary collapse

VERBOSITY =
ENV.fetch('RANTLY_VERBOSE'){ 1 }.to_i
RANTLY_COUNT =
ENV.fetch('RANTLY_COUNT'){ 100 }.to_i

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(property) ⇒ Property


23
24
25
# File 'lib/rantly/property.rb', line 23

def initialize(property)
  @property = property
end

Instance Attribute Details

#failed_dataObject (readonly)

Returns the value of attribute failed_data


6
7
8
# File 'lib/rantly/property.rb', line 6

def failed_data
  @failed_data
end

#ioObject (readonly)

Returns the value of attribute io


6
7
8
# File 'lib/rantly/property.rb', line 6

def io
  @io
end

#shrunk_failed_dataObject (readonly)

Returns the value of attribute shrunk_failed_data


6
7
8
# File 'lib/rantly/property.rb', line 6

def shrunk_failed_data
  @shrunk_failed_data
end

Instance Method Details

#check(n = RANTLY_COUNT, limit = 10, &assertion) ⇒ Object


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rantly/property.rb', line 27

def check(n=RANTLY_COUNT,limit=10,&assertion)
  i = 0
  test_data = nil
  begin
    Rantly.singleton.generate(n,limit,@property) do |val|
      test_data = val
      assertion.call(val) if assertion
      io.puts "" if i % 100 == 0
      io.print "." if i % 10 == 0
      i += 1
    end
    io.puts
    io.puts "success: #{i} tests"
  rescue Rantly::TooManyTries => e
    io.puts
    io.puts "too many tries: #{e.tries}"
    raise e
  rescue Exception => boom
    io.puts
    io.puts "failure: #{i} tests, on:"
    pretty_print test_data
    @failed_data = test_data
    if @failed_data.respond_to?(:shrink)
      @shrunk_failed_data, @depth = shrinkify(assertion, @failed_data)
      io.puts "minimal failed data (depth #{@depth}) is:"
      pretty_print @shrunk_failed_data
    end
    raise boom
  end
end

#pretty_print(object) ⇒ Object


19
20
21
# File 'lib/rantly/property.rb', line 19

def pretty_print(object)
  PP.pp(object, io)
end

#reportObject


85
86
87
88
89
90
91
# File 'lib/rantly/property.rb', line 85

def report
  distribs = self.classifiers.sort { |a,b| b[1] <=> a[1] }
  total = distribs.inject(0) { |sum,pair| sum + pair[1]}
  distribs.each do |(classifier,count)|
    format "%10.5f%% of => %s", count, classifier
  end
end

#shrinkify(assertion, data, depth = 0, iteration = 0) ⇒ Object

Explore the failures tree


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/rantly/property.rb', line 59

def shrinkify(assertion, data, depth=0, iteration=0)
  io.puts "Shrinking at depth #{depth}:"
  pretty_print data

  min_data = data
  max_depth = depth
  if data.shrinkable?
    while iteration < 1024 do
      # We assume that data.shrink is non-destructive
      shrunk_data = data.shrink
      begin
        assertion.call(shrunk_data)
      rescue Exception
        # If the assertion was verified, recursively shrink failure case
        branch_data, branch_depth, iteration = shrinkify(assertion, shrunk_data, depth + 1, iteration + 1)
        if branch_depth > max_depth
          min_data = branch_data
          max_depth = branch_depth
        end
      end
      break if !data.retry?
    end
  end
  return min_data, max_depth, iteration
end