Class: QueenCheck::Testable

Inherits:
Object
  • Object
show all
Defined in:
lib/queencheck/core.rb

Overview

QueenCheck Testable Object

Examples:

QueenCheck::Testable.new(Integer, Integer) do | x, y |
  x + y == y + x
end

Constant Summary collapse

DEFAULT_TEST_COUNT =
100
DEFAULT_RETRY_COUNT =
100

Instance Method Summary collapse

Constructor Details

#initialize(*arbitraries, &assertion) ⇒ QueenCheck::Testable

Returns new Testable instance.

Examples:

QueenCheck::Testable(Integer, Integer.arbitrary, Integer.arbitrary.gen, :Integer)

Parameters:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/queencheck/core.rb', line 18

def initialize(*arbitraries, &assertion)
  @arbitraries = arbitraries.map { | arbitrary |
    arb = (
      if arbitrary.instance_of?(QueenCheck::Arbitrary)
        arbitrary.gen
      elsif arbitrary.instance_of?(QueenCheck::Gen)
        arbitrary
      else
        QueenCheck::Arbitrary(arbitrary)
      end
    )
    raise TypeError, "Not Implemented Arbitrary or Gen: #{arbitrary}" if arb.nil?
    arb.instance_of?(QueenCheck::Arbitrary) ? arb.gen : arb
  }
  @assertion = assertion
end

Instance Method Details

#assert(progress) ⇒ QueenCheck::Result

run assert

Parameters:

  • progress (Float)

    0 .. 1

Returns:



89
90
91
92
93
94
95
96
97
98
# File 'lib/queencheck/core.rb', line 89

def assert(progress)
  begin
    props = properties(progress)
    is_success = @assertion.call(*props)
  rescue => ex
    is_success = false
    exception = ex
  end
  QueenCheck::Result.new(props, is_success, exception)
end

#check(count = DEFAULT_TEST_COUNT) ⇒ QueenCheck::ResultReport

check assert

Parameters:

  • count (Integer) (defaults to: DEFAULT_TEST_COUNT)

    number of tests

Returns:



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/queencheck/core.rb', line 40

def check(count = DEFAULT_TEST_COUNT)
  results = QueenCheck::ResultReport.new
  count.times do | n |
    begin
      results << self.assert(n.to_f / count)
    rescue QueenCheck::CanNotRetryMore
      next
    end
  end
  return results
end

#check_with_label(labels, count = DEFAULT_TEST_COUNT) ⇒ QueenCheck::ResultReport

check assert with label for report

Examples:

prop_int = QueenCheck::Testable(Integer, Integer){|x,y| true }
prop_int.check_with_label(
  'x > y' => proc {|x,y| x > y}
)

Parameters:

  • labels (Hash)

    key is String. value is Proc.

  • count (Integer) (defaults to: DEFAULT_TEST_COUNT)

    number of tests

Returns:



61
62
63
64
65
66
67
# File 'lib/queencheck/core.rb', line 61

def check_with_label(labels, count = DEFAULT_TEST_COUNT)
  sets = check(count)
  labels.each_pair do | label, proc |
    sets.labeling(label, &proc)
  end
  return sets
end

#properties(progress, retry_count = DEFAULT_RETRY_COUNT) ⇒ Array<Object, Object ...>

Returns generated values.

Parameters:

  • progress (Float)

    0 .. 1

  • retry_count (Integer) (defaults to: DEFAULT_RETRY_COUNT)

Returns:

  • (Array<Object, Object ...>)

    generated values



75
76
77
78
79
80
81
82
83
84
# File 'lib/queencheck/core.rb', line 75

def properties(progress, retry_count = DEFAULT_RETRY_COUNT)
  @arbitraries.map { | gen |
    c = 0
    until (v = gen.value(progress))[1]
      c += 1
      raise QueenCheck::CanNotRetryMore, "can not retry generate: #{gen.inspect}" if c >= retry_count
    end
    v[0]
  }
end