Class: Hypothesis::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/hypothesis/engine.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Engine

Returns a new instance of Engine.



11
12
13
14
15
16
# File 'lib/hypothesis/engine.rb', line 11

def initialize(options)
  seed = Random.rand(2**64 - 1)
  @core_engine = HypothesisCoreEngine.new(
    seed, options.fetch(:max_examples)
  )
end

Instance Attribute Details

#current_sourceObject (readonly)

Returns the value of attribute current_source.



8
9
10
# File 'lib/hypothesis/engine.rb', line 8

def current_source
  @current_source
end

#is_findObject

Returns the value of attribute is_find.



9
10
11
# File 'lib/hypothesis/engine.rb', line 9

def is_find
  @is_find
end

Instance Method Details

#runObject



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
48
49
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/hypothesis/engine.rb', line 18

def run
  loop do
    core = @core_engine.new_source
    break if core.nil?
    @current_source = TestCase.new(core)
    begin
      result = yield(@current_source)
      if is_find && result
        @core_engine.finish_interesting(core)
      else
        @core_engine.finish_valid(core)
      end
    rescue UnsatisfiedAssumption
      @core_engine.finish_invalid(core)
    rescue DataOverflow
      @core_engine.finish_overflow(core)
    rescue Exception
      raise if is_find
      @core_engine.finish_interesting(core)
    end
  end
  @current_source = nil
  core = @core_engine.failing_example
  if core.nil?
    raise Unsatisfiable if @core_engine.was_unsatisfiable
    return
  end

  if is_find
    @current_source = TestCase.new(core, record_draws: true)
    yield @current_source
  else
    @current_source = TestCase.new(core, print_draws: true)

    begin
      yield @current_source
    rescue Exception => e
      givens = @current_source.print_log
      given_str = givens.each_with_index.map do |(name, s), i|
        name = "##{i + 1}" if name.nil?
        "Given #{name}: #{s}"
      end.to_a

      if e.respond_to? :hypothesis_data
        e.hypothesis_data[0] = given_str
      else
        original_to_s = e.to_s
        original_inspect = e.inspect

        class <<e
          attr_accessor :hypothesis_data

          def to_s
            ['', hypothesis_data[0], '', hypothesis_data[1]].join("\n")
          end

          def inspect
            ['', hypothesis_data[0], '', hypothesis_data[2]].join("\n")
          end
        end
        e.hypothesis_data = [given_str, original_to_s, original_inspect]
      end
      raise e
    end
  end
end