prop_check 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: add08010531772da827ece44a91bdeb52d259374cf9bb966f05104f29e5a8ae8
4
+ data.tar.gz: 246d3a3df6d8c53f198f489dcd6174160379dfe504558c79932bd0d0c78a2733
5
+ SHA512:
6
+ metadata.gz: 6b95914aab13287ce63e98a21db6add53a05f5944d432206e706d8ba6a748ea9fd243f2b1d4044456b68111c3adb0dc41e1cd9a829383145d2590b0ea1a82b28
7
+ data.tar.gz: 86d94f972980c806721f39a914a8d527364809b532a940c826e050e59ee5b4661b57824e74da950537ae6d6ff2c0046b048f64a9a5a6ad197e4c94f937cc3ba8
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,4 @@
1
+ Metrics/LineLength:
2
+ Max: 120
3
+ Style/AccessModifierDeclarations:
4
+ EnforcedStyle: inline
data/.tool-versions ADDED
@@ -0,0 +1 @@
1
+ ruby 2.5.1
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.4.2
7
+ before_install: gem install bundler -v 2.0.1
data/CHANGELOG.md ADDED
File without changes
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at w-m@wmcode.nl. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in prop_check.gemspec
4
+ gemspec
5
+
6
+ gem 'simplecov', require: false, group: :test
7
+ gem 'doctest-rspec', require: false, group: :test
data/Gemfile.lock ADDED
@@ -0,0 +1,48 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ prop_check (0.6.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.3)
10
+ docile (1.3.2)
11
+ doctest-core (0.0.2)
12
+ doctest-rspec (0.0.3)
13
+ doctest-core (~> 0.0.2)
14
+ rspec
15
+ json (2.2.0)
16
+ rake (10.5.0)
17
+ rspec (3.8.0)
18
+ rspec-core (~> 3.8.0)
19
+ rspec-expectations (~> 3.8.0)
20
+ rspec-mocks (~> 3.8.0)
21
+ rspec-core (3.8.1)
22
+ rspec-support (~> 3.8.0)
23
+ rspec-expectations (3.8.4)
24
+ diff-lcs (>= 1.2.0, < 2.0)
25
+ rspec-support (~> 3.8.0)
26
+ rspec-mocks (3.8.1)
27
+ diff-lcs (>= 1.2.0, < 2.0)
28
+ rspec-support (~> 3.8.0)
29
+ rspec-support (3.8.2)
30
+ simplecov (0.16.1)
31
+ docile (~> 1.1)
32
+ json (>= 1.8, < 3)
33
+ simplecov-html (~> 0.10.0)
34
+ simplecov-html (0.10.2)
35
+
36
+ PLATFORMS
37
+ ruby
38
+
39
+ DEPENDENCIES
40
+ bundler (~> 2.0)
41
+ doctest-rspec
42
+ prop_check!
43
+ rake (~> 10.0)
44
+ rspec (~> 3.0)
45
+ simplecov
46
+
47
+ BUNDLED WITH
48
+ 2.0.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Qqwy/Wiebe-Marten Wijnja
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,182 @@
1
+ # PropCheck
2
+
3
+ PropCheck allows you to do Property Testing in Ruby.
4
+
5
+ It features:
6
+
7
+ - Generators for common datatypes.
8
+ - An easy DSL to define your own generators (by combining existing ones, or completely custom).
9
+ - Shrinking to a minimal counter-example on failure.
10
+
11
+
12
+ ## TODOs before release
13
+
14
+ Before releasing this gem on Rubygems, the following things need to be finished:
15
+
16
+ - [x] Finalize the testing DSL.
17
+ - [x] Testing the library itself (against known 'true' axiomatically correct Ruby code.)
18
+ - [x] Customization of common settings
19
+ - [x] Filtering generators.
20
+ - [x] Customize the max. of samples to run.
21
+ - [x] Stop after a ludicrous amount of generator runs, to prevent malfunctioning (infinitely looping) generators from blowing up someone's computer.
22
+ - [x] Look into customization of settings from e.g. command line arguments.
23
+ - [x] Good, unicode-compliant, string generators.
24
+ - [ ] Filtering generator outputs.
25
+
26
+ # Nice-to-haves
27
+
28
+ - [ ] Basic integration with RSpec. See also https://groups.google.com/forum/#!msg/rspec/U-LmL0OnO-Y/iW_Jcd6JBAAJ for progress on this.
29
+ - [ ] `aggregate` , `resize` and similar generator-modifying calls (c.f. PropEr's variants of these) which will help with introspection/metrics.
30
+ - [ ] Integration with other Ruby test frameworks.
31
+ - Stateful property testing. If implemented at some point, will probably happen in a separate add-on library.
32
+
33
+
34
+ ## Installation
35
+
36
+ Add this line to your application's Gemfile:
37
+
38
+ ```ruby
39
+ gem 'prop_check'
40
+ ```
41
+
42
+ And then execute:
43
+
44
+ $ bundle
45
+
46
+ Or install it yourself as:
47
+
48
+ $ gem install prop_check
49
+
50
+ ## Usage
51
+
52
+
53
+ ### Using PropCheck for basic testing
54
+
55
+ Propcheck exposes the `forall` method.
56
+ It takes generators as keyword arguments and a block to run.
57
+ Inside the block, each of the names in the keyword-argument-list is available by its name.
58
+
59
+ _(to be precise: a method on the execution context is defined which returns the current generated value for that name)_
60
+
61
+ Raise an exception from the block if there is a problem. If there is no problem, just return normally.
62
+
63
+ ```ruby
64
+ # testing that Enumerable#sort sorts in ascending order
65
+ PropCheck.forall(numbers: array(integer())) do
66
+ sorted_numbers = numbers.sort
67
+
68
+ # Check that no number is smaller than the previous number
69
+ sorted_numbers.each_cons(2) do |former, latter|
70
+ raise "Elements are not sorted! #{latter} is < #{former}" if latter < former
71
+ end
72
+ end
73
+ ```
74
+
75
+ #### Shrinking
76
+
77
+ When a failure is found, PropCheck will re-run the block given to `forall` to test
78
+ 'smaller' inputs, in an attempt to give you a minimal counter-example,
79
+ from which the problem can be easily understood.
80
+
81
+ For instance, when a failure happens with the input `x = 100`,
82
+ PropCheck will see if the failure still happens with `x = 50`.
83
+ If it does , it will try `x = 25`. If not, it will try `x = 75`, and so on.
84
+
85
+ This means if something only goes wrong for `x = 2`, the program will try:
86
+ - `x = 100`(fails),`
87
+ - x = 50`(fails),
88
+ - `x = 25`(fails),
89
+ - `x = 12`(fails),
90
+ - `x = 6`(fails),
91
+ - `x = 3`(fails),
92
+ - `x = 1` (succeeds), `x = 2` (fails).
93
+
94
+ and thus the simplified case of `x = 2` is shown in the output.
95
+
96
+ The documentation of the provided generators explain how they shrink.
97
+ A short summary:
98
+ - Integers shrink to numbers closer to zero.
99
+ - Negative integers also attempt their positive alternative.
100
+ - Floats shrink similarly to integers.
101
+ - Arrays and hashes shrink to fewer elements, as well as shrinking their elements.
102
+ - Strings shrink to shorter strings, as well as characters earlier in their alphabet.
103
+
104
+
105
+ ### Writing Custom Generators
106
+
107
+ PropCheck comes bundled with a bunch of common generators, for:
108
+ - integers
109
+ - floats
110
+ - strings
111
+ - symbols
112
+ - arrays
113
+ - hashes
114
+ etc.
115
+
116
+ However, you can easily adapt them to generate your own datatypes:
117
+
118
+ #### Generator#wrap
119
+
120
+ Always returns the given value. No shrinking.
121
+
122
+ #### Generator#map
123
+
124
+ Allows you to take the result of one generator and transform it into something else.
125
+
126
+ >> Generators.choose(32..128).map(&:chr).call(10, Random.new(42))
127
+ => "S"
128
+
129
+ #### Generator#bind
130
+
131
+ Allows you to create one or another generator conditionally on the output of another generator.
132
+
133
+ >> Generators.integer.bind { |a| Generators.integer.bind { |b| Generator.wrap([a , b]) } }.call(100, Random.new(42))
134
+ => [2, 79]
135
+
136
+
137
+ #### Generators.one_of
138
+
139
+ Useful if you want to be able to generate a value to be one of multiple possibilities:
140
+
141
+
142
+ >> Generators.one_of(Generators.constant(true), Generators.constant(false)).sample(5, size: 10, rng: Random.new(42))
143
+ => [true, false, true, true, true]
144
+
145
+ (note that for this example, you can also use `Generators.boolean`. The example happens to show how it is implemented under the hood.)
146
+
147
+ #### Generators.frequency
148
+
149
+ If `one_of` does not give you enough flexibility because you want some results to be more common than others,
150
+ you can use `Generators.frequency` which takes a hash of (integer_frequency => generator) keypairs.
151
+
152
+ >> Generators.frequency(5 => Generators.integer, 1 => Generators.printable_ascii_char).sample(size: 10, rng: Random.new(42))
153
+ => [4, -3, 10, 8, 0, -7, 10, 1, "E", 10]
154
+
155
+ #### Others
156
+
157
+ There are even more functions in the `Generator` class and the `Generators` module that you might want to use,
158
+ although above are the most generally useful ones.
159
+
160
+ ## Development
161
+
162
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
163
+
164
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
165
+
166
+ ## Contributing
167
+
168
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Qqwy/ruby-prop_check . This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
169
+
170
+ ## License
171
+
172
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
173
+
174
+ ## Code of Conduct
175
+
176
+ Everyone interacting in the PropCheck project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/prop_check/blob/master/CODE_OF_CONDUCT.md).
177
+
178
+ ## Attribution and Thanks
179
+
180
+ I want to thank the original creators of QuickCheck (Koen Claessen, John Hughes) as well as the authors of many great property testing libraries that I was/am able to use as inspiration.
181
+ I also want to greatly thank Thomasz Kowal who made me excited about property based testing [with his great talk about stateful property testing](https://www.youtube.com/watch?v=q0wZzFUYCuM),
182
+ as well as Fred Herbert for his great book [Property-Based Testing with PropEr, Erlang and Elixir](https://propertesting.com/) which is really worth the read (regardless of what language you are using).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "prop_check"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/prop_check.rb ADDED
@@ -0,0 +1,17 @@
1
+ require "prop_check/version"
2
+ require 'prop_check/property'
3
+ require 'prop_check/generator'
4
+ require 'prop_check/generators'
5
+ require 'prop_check/helper'
6
+ module PropCheck
7
+ class Error < StandardError; end
8
+ class UserError < Error; end
9
+ class GeneratorExhaustedError < UserError; end
10
+ class MaxShrinkStepsExceededError < UserError; end
11
+
12
+ extend self
13
+
14
+ def forall(*args, **kwargs, &block)
15
+ PropCheck::Property.forall(*args, **kwargs, &block)
16
+ end
17
+ end
@@ -0,0 +1,95 @@
1
+ module PropCheck
2
+ ##
3
+ # A `Generator` is a special kind of 'proc' that,
4
+ # given a size an random number generator state,
5
+ # will generate a (finite) LazyTree of output values:
6
+ #
7
+ # The root of this tree is the value to be used during testing,
8
+ # and the children are 'smaller' values related to the root,
9
+ # to be used during the shrinking phase.
10
+ class Generator
11
+ @@default_size = 10
12
+ @@default_rng = Random.new
13
+
14
+ ##
15
+ # Being a special kind of Proc, a Generator wraps a block.
16
+ def initialize(&block)
17
+ @block = block
18
+ end
19
+
20
+ ##
21
+ # Given a `size` (integer) and a random number generator state `rng`,
22
+ # generate a LazyTree.
23
+ def generate(size = @@default_size, rng = @@default_rng)
24
+ @block.call(size, rng)
25
+ end
26
+
27
+ ##
28
+ # Generates a value, and only return this value
29
+ # (drop information for shrinking)
30
+ #
31
+ # >> Generators.integer.call(1000, Random.new(42))
32
+ # => 126
33
+ def call(size = @@default_size, rng = @@default_rng)
34
+ generate(size, rng).root
35
+ end
36
+
37
+ ##
38
+ # Returns `num_of_samples` values from calling this Generator.
39
+ # This is mostly useful for debugging if a generator behaves as you intend it to.
40
+ def sample(num_of_samples = 10, size: @@default_size, rng: @@default_rng)
41
+ num_of_samples.times.map do
42
+ call(size, rng)
43
+ end
44
+ end
45
+
46
+ ##
47
+ # Creates a 'constant' generator that always returns the same value,
48
+ # regardless of `size` or `rng`.
49
+ #
50
+ # Keen readers may notice this as the Monadic 'pure'/'return' implementation for Generators.
51
+ #
52
+ # >> Generators.integer.bind { |a| Generators.integer.bind { |b| Generator.wrap([a , b]) } }.call(100, Random.new(42))
53
+ # => [2, 79]
54
+ def self.wrap(val)
55
+ Generator.new { |_size, _rng| LazyTree.wrap(val) }
56
+ end
57
+
58
+ ##
59
+ # Create a generator whose implementation depends on the output of another generator.
60
+ # this allows us to compose multiple generators.
61
+ #
62
+ # Keen readers may notice this as the Monadic 'bind' (sometimes known as '>>=') implementation for Generators.
63
+ #
64
+ # >> Generators.integer.bind { |a| Generators.integer.bind { |b| Generator.wrap([a , b]) } }.call(100, Random.new(42))
65
+ # => [2, 79]
66
+ def bind(&generator_proc)
67
+ # Generator.new do |size, rng|
68
+ # outer_result = generate(size, rng)
69
+ # outer_result.map do |outer_val|
70
+ # inner_generator = generator_proc.call(outer_val)
71
+ # inner_generator.generate(size, rng)
72
+ # end.flatten
73
+ # end
74
+ Generator.new do |size, rng|
75
+ outer_result = generate(size, rng)
76
+ outer_result.bind do |outer_val|
77
+ inner_generator = generator_proc.call(outer_val)
78
+ inner_generator.generate(size, rng)
79
+ end
80
+ end
81
+ end
82
+
83
+ ##
84
+ # Creates a new Generator that returns a value by running `proc` on the output of the current Generator.
85
+ #
86
+ # >> Generators.choose(32..128).map(&:chr).call(10, Random.new(42))
87
+ # => "S"
88
+ def map(&proc)
89
+ Generator.new do |size, rng|
90
+ result = self.generate(size, rng)
91
+ result.map(&proc)
92
+ end
93
+ end
94
+ end
95
+ end