prop_check 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8e5aeef61ddf82569ca885327d32b9fe8fd939f2fdbef48709cb19504b5cf041
4
+ data.tar.gz: d914dcac32f7a3a976661a0280c71f0a7a665411550aed9902317b04c82ee290
5
+ SHA512:
6
+ metadata.gz: 85c5b76ac37ba8e03b2c41c04c4cc359844fa2e52d0af8893787531c23f3a54b046eb7eed13b055fed44c8983f87cbfe492d1524746624a3744300a73caf01da
7
+ data.tar.gz: 9beb7ac4e605c72cc8e9b3907e727ae532c2eec4a530b6a9b7c31445135e37c8849c9f186d4571aaf38c24e28989ebe856ed2e5e873baf2ed7b740a73561dae7
@@ -0,0 +1,14 @@
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
12
+
13
+ # .gem version files
14
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,4 @@
1
+ Metrics/LineLength:
2
+ Max: 120
3
+ Style/AccessModifierDeclarations:
4
+ EnforcedStyle: inline
@@ -0,0 +1 @@
1
+ ruby 2.6.5
@@ -0,0 +1,18 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.1
7
+ before_install: gem install bundler -v 2.0.2
8
+ env:
9
+ global:
10
+ - CC_TEST_REPORTER_ID=9d18f5b43e49eecd6c3da64d85ea9c765d3606c129289d7c8cadf6d448713311
11
+ before_script:
12
+ - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
13
+ - chmod +x ./cc-test-reporter
14
+ - ./cc-test-reporter before-build
15
+ script:
16
+ - bundle exec rspec
17
+ after_script:
18
+ - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
@@ -0,0 +1 @@
1
+ - 0.8.0 New syntax that is more explicit, passng generated values to blocks as parameters.
@@ -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,8 @@
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
8
+ gem 'awesome_print', require: true
@@ -0,0 +1,50 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ prop_check (0.9.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ awesome_print (1.8.0)
10
+ diff-lcs (1.3)
11
+ docile (1.3.2)
12
+ doctest-core (0.0.2)
13
+ doctest-rspec (0.0.3)
14
+ doctest-core (~> 0.0.2)
15
+ rspec
16
+ json (2.2.0)
17
+ rake (12.3.3)
18
+ rspec (3.8.0)
19
+ rspec-core (~> 3.8.0)
20
+ rspec-expectations (~> 3.8.0)
21
+ rspec-mocks (~> 3.8.0)
22
+ rspec-core (3.8.1)
23
+ rspec-support (~> 3.8.0)
24
+ rspec-expectations (3.8.4)
25
+ diff-lcs (>= 1.2.0, < 2.0)
26
+ rspec-support (~> 3.8.0)
27
+ rspec-mocks (3.8.1)
28
+ diff-lcs (>= 1.2.0, < 2.0)
29
+ rspec-support (~> 3.8.0)
30
+ rspec-support (3.8.2)
31
+ simplecov (0.16.1)
32
+ docile (~> 1.1)
33
+ json (>= 1.8, < 3)
34
+ simplecov-html (~> 0.10.0)
35
+ simplecov-html (0.10.2)
36
+
37
+ PLATFORMS
38
+ ruby
39
+
40
+ DEPENDENCIES
41
+ awesome_print
42
+ bundler (~> 2.0)
43
+ doctest-rspec
44
+ prop_check!
45
+ rake (~> 12.3)
46
+ rspec (~> 3.0)
47
+ simplecov
48
+
49
+ BUNDLED WITH
50
+ 2.1.4
@@ -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.
@@ -0,0 +1,242 @@
1
+ # PropCheck
2
+
3
+ PropCheck allows you to do Property Testing in Ruby.
4
+
5
+ [![Gem](https://img.shields.io/gem/v/prop_check.svg)](https://rubygems.org/gems/prop_check)
6
+ [![Build Status](https://travis-ci.org/Qqwy/ruby-prop_check.svg?branch=master)](https://travis-ci.org/Qqwy/ruby-prop_check)
7
+ [![Maintainability](https://api.codeclimate.com/v1/badges/71897f5e6193a5124a53/maintainability)](https://codeclimate.com/github/Qqwy/ruby-prop_check/maintainability)
8
+ [![RubyDoc](https://img.shields.io/badge/%F0%9F%93%9ARubyDoc-documentation-informational.svg)](https://www.rubydoc.info/github/Qqwy/ruby-prop_check/master/PropCheck)
9
+
10
+ It features:
11
+
12
+ - Generators for common datatypes.
13
+ - An easy DSL to define your own generators (by combining existing ones, or completely custom).
14
+ - Shrinking to a minimal counter-example on failure.
15
+
16
+
17
+ ## TODOs before stable release
18
+
19
+ Before releasing this gem on Rubygems, the following things need to be finished:
20
+
21
+ - [x] Finalize the testing DSL.
22
+ - [x] Testing the library itself (against known 'true' axiomatically correct Ruby code.)
23
+ - [x] Customization of common settings
24
+ - [x] Filtering generators.
25
+ - [x] Customize the max. of samples to run.
26
+ - [x] Stop after a ludicrous amount of generator runs, to prevent malfunctioning (infinitely looping) generators from blowing up someone's computer.
27
+ - [x] Look into customization of settings from e.g. command line arguments.
28
+ - [x] Good, unicode-compliant, string generators.
29
+ - [x] Filtering generator outputs.
30
+
31
+ # Nice-to-haves
32
+
33
+ - [x] Basic integration with RSpec. See also https://groups.google.com/forum/#!msg/rspec/U-LmL0OnO-Y/iW_Jcd6JBAAJ for progress on this.
34
+ - [ ] `aggregate` , `resize` and similar generator-modifying calls (c.f. PropEr's variants of these) which will help with introspection/metrics.
35
+ - [ ] Integration with other Ruby test frameworks.
36
+ - Stateful property testing. If implemented at some point, will probably happen in a separate add-on library.
37
+
38
+
39
+ ## Installation
40
+
41
+ Add this line to your application's Gemfile:
42
+
43
+ ```ruby
44
+ gem 'prop_check'
45
+ ```
46
+
47
+ And then execute:
48
+
49
+ $ bundle
50
+
51
+ Or install it yourself as:
52
+
53
+ $ gem install prop_check
54
+
55
+ ## Usage
56
+
57
+
58
+ ### Using PropCheck for basic testing
59
+
60
+ Propcheck exposes the `forall` method.
61
+ It takes generators as keyword arguments and a block to run.
62
+ Inside the block, each of the names in the keyword-argument-list is available by its name.
63
+
64
+ _(to be precise: a method on the execution context is defined which returns the current generated value for that name)_
65
+
66
+ Raise an exception from the block if there is a problem. If there is no problem, just return normally.
67
+
68
+ ```ruby
69
+ include PropCheck::Generators
70
+ # testing that Enumerable#sort sorts in ascending order
71
+ PropCheck.forall(array(integer)) do |numbers|
72
+ sorted_numbers = numbers.sort
73
+
74
+ # Check that no number is smaller than the previous number
75
+ sorted_numbers.each_cons(2) do |former, latter|
76
+ raise "Elements are not sorted! #{latter} is < #{former}" if latter < former
77
+ end
78
+ end
79
+ ```
80
+
81
+
82
+ Here is another example, using it inside a test case.
83
+ Here we check if `naive_average` indeed always returns an integer for all arrays of numbers we can pass it:
84
+
85
+ ```ruby
86
+ # Somewhere you have this function definition:
87
+ def naive_average(array)
88
+ array.sum / array.length
89
+ end
90
+
91
+ # And then in a test case:
92
+ include PropCheck::Generators
93
+ PropCheck.forall(numbers: array(integer)) do |numbers:|
94
+ result = naive_average(numbers)
95
+ unless result.is_a?(Integer) do
96
+ raise "Expected the average to be an integer!"
97
+ end
98
+ end
99
+ ```
100
+
101
+ When running this particular example PropCheck very quickly finds out that we have made a programming mistake:
102
+
103
+ ```ruby
104
+ ZeroDivisionError:
105
+ (after 6 successful property test runs)
106
+ Failed on:
107
+ `{
108
+ :numbers => []
109
+ }`
110
+
111
+ Exception message:
112
+ ---
113
+ divided by 0
114
+ ---
115
+
116
+ (shrinking impossible)
117
+ ---
118
+ ```
119
+
120
+ Clearly we forgot to handle the case of an empty array being passed to the function.
121
+ This is a good example of the kind of conceptual bugs that PropCheck (and property-based testing in general)
122
+ are able to check for.
123
+
124
+
125
+ #### Shrinking
126
+
127
+ When a failure is found, PropCheck will re-run the block given to `forall` to test
128
+ 'smaller' inputs, in an attempt to give you a minimal counter-example,
129
+ from which the problem can be easily understood.
130
+
131
+ For instance, when a failure happens with the input `x = 100`,
132
+ PropCheck will see if the failure still happens with `x = 50`.
133
+ If it does , it will try `x = 25`. If not, it will try `x = 75`, and so on.
134
+
135
+ This means if something only goes wrong for `x = 2`, the program will try:
136
+ - `x = 100`(fails),
137
+ - `x = 50`(fails),
138
+ - `x = 25`(fails),
139
+ - `x = 12`(fails),
140
+ - `x = 6`(fails),
141
+ - `x = 3`(fails),
142
+ - `x = 1` (succeeds), `x = 2` (fails).
143
+
144
+ and thus the simplified case of `x = 2` is shown in the output.
145
+
146
+ The documentation of the provided generators explain how they shrink.
147
+ A short summary:
148
+ - Integers shrink to numbers closer to zero.
149
+ - Negative integers also attempt their positive alternative.
150
+ - Floats shrink similarly to integers.
151
+ - Arrays and hashes shrink to fewer elements, as well as shrinking their elements.
152
+ - Strings shrink to shorter strings, as well as characters earlier in their alphabet.
153
+
154
+ ### Builtin Generators
155
+
156
+ PropCheck comes with [many builtin generators in the PropCheck::Generators](https://www.rubydoc.info/github/Qqwy/ruby-prop_check/master/PropCheck/Generators) module.
157
+
158
+ It contains generators for:
159
+ - (any, positive, negative, etc.) integers,
160
+ - (any, only real-valued) floats,
161
+ - (any, printable only, alphanumeric only, etc) strings and symbols
162
+ - fixed-size arrays and hashes
163
+ - as well as varying-size arrays and hashes.
164
+ - and many more!
165
+
166
+ It is common to call `include PropCheck::Generators` in e.g. your testing-suite files to be able to use these.
167
+ If you want to be more explicit (but somewhat more verbose) when calling these functions. feel free to e.g. create a module-alias (like `PG = PropCheck::Generators`) instead.
168
+
169
+ ### Writing Custom Generators
170
+
171
+ As described in the previous section, PropCheck already comes bundled with a bunch of common generators.
172
+
173
+ However, you can easily adapt them to generate your own datatypes:
174
+
175
+ #### Generators#constant / Generator#wrap
176
+
177
+ Always returns the given value. No shrinking.
178
+
179
+ #### Generator#map
180
+
181
+ Allows you to take the result of one generator and transform it into something else.
182
+
183
+ >> Generators.choose(32..128).map(&:chr).call(10, Random.new(42))
184
+ => "S"
185
+
186
+ #### Generator#bind
187
+
188
+ Allows you to create one or another generator conditionally on the output of another generator.
189
+
190
+ >> Generators.integer.bind { |a| Generators.integer.bind { |b| Generator.wrap([a , b]) } }.call(100, Random.new(42))
191
+ => [2, 79]
192
+
193
+
194
+ #### Generators.one_of
195
+
196
+ Useful if you want to be able to generate a value to be one of multiple possibilities:
197
+
198
+
199
+ >> Generators.one_of(Generators.constant(true), Generators.constant(false)).sample(5, size: 10, rng: Random.new(42))
200
+ => [true, false, true, true, true]
201
+
202
+ (note that for this example, you can also use `Generators.boolean`. The example happens to show how it is implemented under the hood.)
203
+
204
+ #### Generators.frequency
205
+
206
+ If `one_of` does not give you enough flexibility because you want some results to be more common than others,
207
+ you can use `Generators.frequency` which takes a hash of (integer_frequency => generator) keypairs.
208
+
209
+ >> Generators.frequency(5 => Generators.integer, 1 => Generators.printable_ascii_char).sample(size: 10, rng: Random.new(42))
210
+ => [4, -3, 10, 8, 0, -7, 10, 1, "E", 10]
211
+
212
+ #### Others
213
+
214
+ There are even more functions in the `Generator` class and the `Generators` module that you might want to use,
215
+ although above are the most generally useful ones.
216
+
217
+ [PropCheck::Generator documentation](https://www.rubydoc.info/github/Qqwy/ruby-prop_check/master/PropCheck/Generator)
218
+ [PropCheck::Generators documentation](https://www.rubydoc.info/github/Qqwy/ruby-prop_check/master/PropCheck/Generators)
219
+
220
+ ## Development
221
+
222
+ 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.
223
+
224
+ 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).
225
+
226
+ ## Contributing
227
+
228
+ 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.
229
+
230
+ ## License
231
+
232
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
233
+
234
+ ## Code of Conduct
235
+
236
+ 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).
237
+
238
+ ## Attribution and Thanks
239
+
240
+ 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.
241
+ 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),
242
+ 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).