prop_check 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.rubocop.yml +4 -0
- data/.tool-versions +1 -0
- data/.travis.yml +7 -0
- data/CHANGELOG.md +0 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +48 -0
- data/LICENSE.txt +21 -0
- data/README.md +182 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/prop_check.rb +17 -0
- data/lib/prop_check/generator.rb +95 -0
- data/lib/prop_check/generators.rb +415 -0
- data/lib/prop_check/helper.rb +27 -0
- data/lib/prop_check/lazy_tree.rb +142 -0
- data/lib/prop_check/property.rb +207 -0
- data/lib/prop_check/property/check_evaluator.rb +45 -0
- data/lib/prop_check/property/configuration.rb +14 -0
- data/lib/prop_check/rspec.rb +14 -0
- data/lib/prop_check/version.rb +3 -0
- data/prop_check.gemspec +40 -0
- metadata +119 -0
@@ -0,0 +1,207 @@
|
|
1
|
+
require 'stringio'
|
2
|
+
|
3
|
+
require 'prop_check/property/configuration'
|
4
|
+
require 'prop_check/property/check_evaluator'
|
5
|
+
module PropCheck
|
6
|
+
class Property
|
7
|
+
|
8
|
+
def self.forall(**bindings, &block)
|
9
|
+
|
10
|
+
property = new(bindings)
|
11
|
+
|
12
|
+
return property.check(&block) if block_given?
|
13
|
+
|
14
|
+
property
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.configuration
|
18
|
+
@configuration ||= Configuration.new
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.configure
|
22
|
+
yield(configuration)
|
23
|
+
end
|
24
|
+
|
25
|
+
attr_reader :bindings, :condition
|
26
|
+
|
27
|
+
def initialize(**bindings)
|
28
|
+
raise ArgumentError, 'No bindings specified!' if bindings.empty?
|
29
|
+
|
30
|
+
@bindings = bindings
|
31
|
+
@condition = -> { true }
|
32
|
+
@config = self.class.configuration
|
33
|
+
end
|
34
|
+
|
35
|
+
def configuration
|
36
|
+
@config
|
37
|
+
end
|
38
|
+
|
39
|
+
def with_config(**config, &block)
|
40
|
+
@config = @config.merge(config)
|
41
|
+
|
42
|
+
return self.check(&block) if block_given?
|
43
|
+
|
44
|
+
self
|
45
|
+
end
|
46
|
+
|
47
|
+
def where(&new_condition)
|
48
|
+
original_condition = @condition.dup
|
49
|
+
@condition = -> { instance_exec(&original_condition) && instance_exec(&new_condition) }
|
50
|
+
|
51
|
+
self
|
52
|
+
end
|
53
|
+
|
54
|
+
def check(&block)
|
55
|
+
binding_generator = PropCheck::Generators.fixed_hash(bindings)
|
56
|
+
|
57
|
+
n_runs = 0
|
58
|
+
n_successful = 0
|
59
|
+
|
60
|
+
# Loop stops at first exception
|
61
|
+
attempts_enumerator(binding_generator).each do |generator_result|
|
62
|
+
n_runs += 1
|
63
|
+
check_attempt(generator_result, n_successful, &block)
|
64
|
+
n_successful += 1
|
65
|
+
end
|
66
|
+
|
67
|
+
ensure_not_exhausted!(n_runs)
|
68
|
+
end
|
69
|
+
|
70
|
+
private def ensure_not_exhausted!(n_runs)
|
71
|
+
return if n_runs >= @config.n_runs
|
72
|
+
|
73
|
+
raise GeneratorExhaustedError, """
|
74
|
+
Could not perform `n_runs = #{@config.n_runs}` runs,
|
75
|
+
(exhausted #{@config.max_generate_attempts} tries)
|
76
|
+
because too few generator results were adhering to
|
77
|
+
the `where` condition.
|
78
|
+
|
79
|
+
Try refining your generators instead.
|
80
|
+
"""
|
81
|
+
end
|
82
|
+
|
83
|
+
private def check_attempt(generator_result, n_successful, &block)
|
84
|
+
CheckEvaluator.new(generator_result.root, &block).call
|
85
|
+
|
86
|
+
# immediately stop (without shrinnking) for when the app is asked
|
87
|
+
# to close by outside intervention
|
88
|
+
rescue SignalException, SystemExit
|
89
|
+
raise
|
90
|
+
|
91
|
+
# We want to capture _all_ exceptions (even low-level ones) here,
|
92
|
+
# so we can shrink to find their cause.
|
93
|
+
# don't worry: they all get reraised
|
94
|
+
rescue Exception => e
|
95
|
+
output, shrunken_result, shrunken_exception, n_shrink_steps = show_problem_output(e, generator_result, n_successful, &block)
|
96
|
+
output_string = output.is_a?(StringIO) ? output.string : e.message
|
97
|
+
|
98
|
+
e.define_singleton_method :prop_check_info do
|
99
|
+
{
|
100
|
+
original_input: generator_result.root,
|
101
|
+
original_exception_message: e.message,
|
102
|
+
shrunken_input: shrunken_result,
|
103
|
+
shrunken_exception: shrunken_exception,
|
104
|
+
n_successful: n_successful,
|
105
|
+
n_shrink_steps: n_shrink_steps
|
106
|
+
}
|
107
|
+
end
|
108
|
+
|
109
|
+
raise e, output_string, e.backtrace
|
110
|
+
end
|
111
|
+
|
112
|
+
private def attempts_enumerator(binding_generator)
|
113
|
+
|
114
|
+
rng = Random::DEFAULT
|
115
|
+
n_runs = 0
|
116
|
+
size = 1
|
117
|
+
(0...@config.max_generate_attempts)
|
118
|
+
.lazy
|
119
|
+
.map { binding_generator.generate(size, rng) }
|
120
|
+
.reject { |val| val.root == :"_PropCheck.filter_me" }
|
121
|
+
.select { |val| CheckEvaluator.new(val.root, &@condition).call }
|
122
|
+
.map do |result|
|
123
|
+
n_runs += 1
|
124
|
+
size += 1
|
125
|
+
|
126
|
+
result
|
127
|
+
end
|
128
|
+
.take_while { n_runs <= @config.n_runs }
|
129
|
+
end
|
130
|
+
|
131
|
+
private def show_problem_output(problem, generator_results, n_successful, &block)
|
132
|
+
output = @config.verbose ? STDOUT : StringIO.new
|
133
|
+
output = pre_output(output, n_successful, generator_results.root, problem)
|
134
|
+
shrunken_result, shrunken_exception, n_shrink_steps = shrink2(generator_results, output, &block)
|
135
|
+
output = post_output(output, n_shrink_steps, shrunken_result, shrunken_exception)
|
136
|
+
|
137
|
+
[output, shrunken_result, shrunken_exception, n_shrink_steps]
|
138
|
+
end
|
139
|
+
|
140
|
+
private def pre_output(output, n_successful, generated_root, problem)
|
141
|
+
output.puts ""
|
142
|
+
output.puts "(after #{n_successful} successful property test runs)"
|
143
|
+
output.puts "Failed on: "
|
144
|
+
output.puts "`#{print_roots(generated_root)}`"
|
145
|
+
output.puts ""
|
146
|
+
output.puts "Exception message:\n---\n#{problem}"
|
147
|
+
output.puts "---"
|
148
|
+
output.puts ""
|
149
|
+
|
150
|
+
output
|
151
|
+
end
|
152
|
+
|
153
|
+
private def post_output(output, n_shrink_steps, shrunken_result, shrunken_exception)
|
154
|
+
output.puts ''
|
155
|
+
output.puts "Shrunken input (after #{n_shrink_steps} shrink steps):"
|
156
|
+
output.puts "`#{print_roots(shrunken_result)}`"
|
157
|
+
output.puts ""
|
158
|
+
output.puts "Shrunken exception:\n---\n#{shrunken_exception}"
|
159
|
+
output.puts "---"
|
160
|
+
output.puts ""
|
161
|
+
|
162
|
+
output
|
163
|
+
end
|
164
|
+
|
165
|
+
private def print_roots(lazy_tree_hash)
|
166
|
+
lazy_tree_hash.map do |name, val|
|
167
|
+
"#{name} = #{val.inspect}"
|
168
|
+
end.join(", ")
|
169
|
+
end
|
170
|
+
|
171
|
+
private def shrink2(bindings_tree, io, &fun)
|
172
|
+
io.puts 'Shrinking...' if @config.verbose
|
173
|
+
problem_child = bindings_tree
|
174
|
+
siblings = problem_child.children.lazy
|
175
|
+
parent_siblings = nil
|
176
|
+
problem_exception = nil
|
177
|
+
shrink_steps = 0
|
178
|
+
(0..@config.max_shrink_steps).each do
|
179
|
+
begin
|
180
|
+
sibling = siblings.next
|
181
|
+
rescue StopIteration
|
182
|
+
break if parent_siblings.nil?
|
183
|
+
|
184
|
+
siblings = parent_siblings.lazy
|
185
|
+
parent_siblings = nil
|
186
|
+
next
|
187
|
+
end
|
188
|
+
|
189
|
+
shrink_steps += 1
|
190
|
+
io.print '.' if @config.verbose
|
191
|
+
|
192
|
+
begin
|
193
|
+
CheckEvaluator.new(sibling.root, &fun).call
|
194
|
+
rescue Exception => problem
|
195
|
+
problem_child = sibling
|
196
|
+
parent_siblings = siblings
|
197
|
+
siblings = problem_child.children.lazy
|
198
|
+
problem_exception = problem
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
io.puts "(Note: Exceeded #{@config.max_shrink_steps} shrinking steps, the maximum.)" if shrink_steps >= @config.max_shrink_steps
|
203
|
+
|
204
|
+
[problem_child.root, problem_exception, shrink_steps]
|
205
|
+
end
|
206
|
+
end
|
207
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module PropCheck
|
2
|
+
class Property
|
3
|
+
##
|
4
|
+
# A wrapper class that implements the 'Cloaker' concept
|
5
|
+
# which allows us to refer to variables set in 'bindings',
|
6
|
+
# while still being able to access things that are only in scope
|
7
|
+
# in the creator of '&block'.
|
8
|
+
#
|
9
|
+
# This allows us to bind the variables specified in `bindings`
|
10
|
+
# one way during checking and another way during shrinking.
|
11
|
+
class CheckEvaluator
|
12
|
+
include RSpec::Matchers if Object.const_defined?('RSpec')
|
13
|
+
|
14
|
+
def initialize(bindings, &block)
|
15
|
+
@caller = block.binding.receiver
|
16
|
+
@block = block
|
17
|
+
define_named_instance_methods(bindings)
|
18
|
+
end
|
19
|
+
|
20
|
+
def call
|
21
|
+
self.instance_exec(&@block)
|
22
|
+
end
|
23
|
+
|
24
|
+
private def define_named_instance_methods(results)
|
25
|
+
results.each do |name, result|
|
26
|
+
define_singleton_method(name) { result }
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
##
|
31
|
+
# Dispatches to caller whenever something is not part of `bindings`.
|
32
|
+
# (No need to invoke this method manually)
|
33
|
+
def method_missing(method, *args, &block)
|
34
|
+
@caller.__send__(method, *args, &block) || super
|
35
|
+
end
|
36
|
+
|
37
|
+
##
|
38
|
+
# Checks respond_to of caller whenever something is not part of `bindings`.
|
39
|
+
# (No need to invoke this method manually)
|
40
|
+
def respond_to_missing?(*args)
|
41
|
+
@caller.respond_to?(*args) || super
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module PropCheck
|
2
|
+
class Property
|
3
|
+
Configuration = Struct.new(:verbose, :n_runs, :max_generate_attempts, :max_shrink_steps, keyword_init: true) do
|
4
|
+
|
5
|
+
def initialize(verbose: false, n_runs: 1_000, max_generate_attempts: 10_000, max_shrink_steps: 10_000)
|
6
|
+
super
|
7
|
+
end
|
8
|
+
|
9
|
+
def merge(other)
|
10
|
+
Configuration.new(**self.to_h.merge(other.to_h))
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module PropCheck
|
2
|
+
##
|
3
|
+
# Integration with RSpec
|
4
|
+
module RSpec
|
5
|
+
# To make it available within examples
|
6
|
+
def self.extend_object(obj)
|
7
|
+
obj.define_method(:forall) do |*args, **kwargs, &block|
|
8
|
+
PropCheck::Property.forall(*args, **kwargs) do
|
9
|
+
instance_exec(self, &block)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
data/prop_check.gemspec
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "prop_check/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "prop_check"
|
8
|
+
spec.version = PropCheck::VERSION
|
9
|
+
spec.authors = ["Qqwy/Wiebe-Marten Wijnja"]
|
10
|
+
spec.email = ["w-m@wmcode.nl"]
|
11
|
+
|
12
|
+
spec.summary = %q{PropCheck allows you to do property-based testing , including shrinking. (akin to Haskell's QuickCheck, Erlang's PropEr, Elixir's StreamData)}
|
13
|
+
spec.description = %q{PropCheck allows you to do property-based testing , including shrinking. (akin to Haskell's QuickCheck, Erlang's PropEr, Elixir's StreamData). This means that your test are run many times with different, autogenerated inputs, and as soon as a failing case is found, this input is simplified, in the end giving you back the simplest input that made the test fail.}
|
14
|
+
spec.homepage = "https://github.com/Qqwy/ruby-prop_check/"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
21
|
+
spec.metadata["source_code_uri"] = "https://github.com/Qqwy/ruby-prop_check/"
|
22
|
+
spec.metadata["changelog_uri"] = "https://github.com/Qqwy/ruby-prop_check/CHANGELOG.md"
|
23
|
+
else
|
24
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
25
|
+
"public gem pushes."
|
26
|
+
end
|
27
|
+
|
28
|
+
# Specify which files should be added to the gem when it is released.
|
29
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
30
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
31
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
32
|
+
end
|
33
|
+
spec.bindir = "exe"
|
34
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
35
|
+
spec.require_paths = ["lib"]
|
36
|
+
|
37
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
38
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
39
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
40
|
+
end
|
metadata
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: prop_check
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.6.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Qqwy/Wiebe-Marten Wijnja
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-07-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: PropCheck allows you to do property-based testing , including shrinking.
|
56
|
+
(akin to Haskell's QuickCheck, Erlang's PropEr, Elixir's StreamData). This means
|
57
|
+
that your test are run many times with different, autogenerated inputs, and as soon
|
58
|
+
as a failing case is found, this input is simplified, in the end giving you back
|
59
|
+
the simplest input that made the test fail.
|
60
|
+
email:
|
61
|
+
- w-m@wmcode.nl
|
62
|
+
executables: []
|
63
|
+
extensions: []
|
64
|
+
extra_rdoc_files: []
|
65
|
+
files:
|
66
|
+
- ".gitignore"
|
67
|
+
- ".rspec"
|
68
|
+
- ".rubocop.yml"
|
69
|
+
- ".tool-versions"
|
70
|
+
- ".travis.yml"
|
71
|
+
- CHANGELOG.md
|
72
|
+
- CODE_OF_CONDUCT.md
|
73
|
+
- Gemfile
|
74
|
+
- Gemfile.lock
|
75
|
+
- LICENSE.txt
|
76
|
+
- README.md
|
77
|
+
- Rakefile
|
78
|
+
- bin/console
|
79
|
+
- bin/setup
|
80
|
+
- lib/prop_check.rb
|
81
|
+
- lib/prop_check/generator.rb
|
82
|
+
- lib/prop_check/generators.rb
|
83
|
+
- lib/prop_check/helper.rb
|
84
|
+
- lib/prop_check/lazy_tree.rb
|
85
|
+
- lib/prop_check/property.rb
|
86
|
+
- lib/prop_check/property/check_evaluator.rb
|
87
|
+
- lib/prop_check/property/configuration.rb
|
88
|
+
- lib/prop_check/rspec.rb
|
89
|
+
- lib/prop_check/version.rb
|
90
|
+
- prop_check.gemspec
|
91
|
+
homepage: https://github.com/Qqwy/ruby-prop_check/
|
92
|
+
licenses:
|
93
|
+
- MIT
|
94
|
+
metadata:
|
95
|
+
homepage_uri: https://github.com/Qqwy/ruby-prop_check/
|
96
|
+
source_code_uri: https://github.com/Qqwy/ruby-prop_check/
|
97
|
+
changelog_uri: https://github.com/Qqwy/ruby-prop_check/CHANGELOG.md
|
98
|
+
post_install_message:
|
99
|
+
rdoc_options: []
|
100
|
+
require_paths:
|
101
|
+
- lib
|
102
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
requirements: []
|
113
|
+
rubyforge_project:
|
114
|
+
rubygems_version: 2.7.6
|
115
|
+
signing_key:
|
116
|
+
specification_version: 4
|
117
|
+
summary: PropCheck allows you to do property-based testing , including shrinking.
|
118
|
+
(akin to Haskell's QuickCheck, Erlang's PropEr, Elixir's StreamData)
|
119
|
+
test_files: []
|