Module: RubyCheck

Includes:
Contracts::Modules
Defined in:
lib/version.rb,
lib/rubycheck.rb

Overview

RubyCheck - a Ruby port of the QuickCheck unit test framework

See www.yellosoft.us/quickcheck for an introduction to QuickCheck property testing.

RubyCheck defines test case generators for several basic Ruby types, and encourages monkeypatching for defining generators for custom types.

Defined Under Namespace

Classes: PropertyFailure

Constant Summary collapse

VERSION =

VERSION is defined once, available to gemspec during packaging, and available programmatically to Ruby code.

'0.0.8'
TRIALS =

Number of test cases to generate per for_all run

10_000

Class Method Summary collapse

Class Method Details

.for_all(property, gen_syms) ⇒ Object

Evaluates property, TRIALS times, over random inputs generated by gen_syms.

Returns true, or the first failing test case.

Example:

RubyCheck::for_all( ->(i) { i.even? }, [:gen_uint])
=> [9]

RubyCheck::for_all( ->(i) { i.even? || i.odd? }, [:gen_uint])
=> true


168
169
170
171
172
173
174
175
176
177
# File 'lib/rubycheck.rb', line 168

def self.for_all(property, gen_syms)
  TRIALS.times do ||
    test_case = gen_syms.map { |gen_sym| send(gen_sym) }
    fail PropertyFailure.new(test_case), 'test case error' unless property.call(*test_case)
  end
rescue PropertyFailure => e
  e.test_case
else
  true
end

.gen_array(gen_sym) ⇒ Object

Generate a random array, populated with values generated with gen_sym.

Example:

RubyCheck::gen_array(:gen_uint)
=> [1, 3, 3, 7]


110
111
112
113
114
# File 'lib/rubycheck.rb', line 110

def self.gen_array(gen_sym)
  len = gen_uint % 100

  0.upto(len).map { || send(gen_sym) }
end

.gen_boolObject

Generate a random boolean.

Example:

RubyCheck::gen_bool
=> false


26
27
28
# File 'lib/rubycheck.rb', line 26

def self.gen_bool
  Random.rand < 0.5
end

.gen_byteObject

Generate a random byte in [0, 255].

Example:

RubyCheck::gen_byte
=> 96


84
85
86
# File 'lib/rubycheck.rb', line 84

def self.gen_byte
  gen_uint % 256
end

.gen_charObject

Generate a random ASCII character.

Example:

RubyCheck::gen_char
=> "Q"


97
98
99
# File 'lib/rubycheck.rb', line 97

def self.gen_char
  (gen_uint % 128).chr
end

.gen_floatObject

Generate a random float in [0, 10^10 - 1].

Example:

RubyCheck::gen_float
=> 0.223


39
40
41
# File 'lib/rubycheck.rb', line 39

def self.gen_float
  Random.rand(10e10)
end

.gen_intObject

Generate a random integer in [(-1 * 10^10) + 1, 10^10 - 1].

Example:

RubyCheck::gen_int
=> -4


65
66
67
68
69
70
71
72
73
# File 'lib/rubycheck.rb', line 65

def self.gen_int
  i = Random.rand(10e10).to_i

  if gen_bool
    i
  else
    i * -1
  end
end

.gen_strObject

Generate a random ASCII string.

Example:

RubyCheck::gen_str
=> "qwerty123!@#"


125
126
127
# File 'lib/rubycheck.rb', line 125

def self.gen_str
  gen_array(:gen_char).join('')
end

.gen_uintObject

Generate a random unsigned integer in [0, 10^10 - 1].

Example:

RubyCheck::gen_uint
=> 4


52
53
54
# File 'lib/rubycheck.rb', line 52

def self.gen_uint
  Random.rand(10e10).to_i
end