Module: RubyCheck

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

VERSION =

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

'0.0.9'
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


180
181
182
183
184
185
186
187
188
189
# File 'lib/rubycheck.rb', line 180

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]


122
123
124
125
126
# File 'lib/rubycheck.rb', line 122

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


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

def self.gen_bool
  Random.rand < 0.5
end

.gen_byteObject

Generate a random byte in [0, 255].

Example:

RubyCheck::gen_byte
=> 96


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

def self.gen_byte
  gen_uint % 256
end

.gen_charObject

Generate a random ASCII character.

Example:

RubyCheck::gen_char
=> "Q"


109
110
111
# File 'lib/rubycheck.rb', line 109

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


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

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


77
78
79
80
81
82
83
84
85
# File 'lib/rubycheck.rb', line 77

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!@#"


137
138
139
# File 'lib/rubycheck.rb', line 137

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


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

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

.gen_uint16Object

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

Example:

RubyCheck::gen_uint16
=> 4


64
65
66
# File 'lib/rubycheck.rb', line 64

def self.gen_uint16
  Random.rand(65535).to_i
end