quick-sampler 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -10
- data/lib/quick/sampler/dsl/fluidiom.rb +6 -9
- data/lib/quick/sampler/dsl/simple_combinators.rb +8 -14
- data/lib/quick/sampler/dsl/simple_values.rb +1 -1
- data/lib/quick/sampler/dsl.rb +1 -2
- data/lib/quick/sampler/version.rb +1 -1
- metadata +1 -2
- data/lib/quick/sampler/config.rb +0 -25
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fb39b55d1e1f2a633a7b3caae39a0f0908c4de9a
|
4
|
+
data.tar.gz: 2e9d62fabcd8657a637fb1ea0b16946af929eee1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5182a2e7a859f535df1bfe10cc004efe6cef7beb59d124ee8e8275abcddb508cc092af3516bcc202f094279e9716a39316ed0dacadd3d7ae0e3568624e27f21c
|
7
|
+
data.tar.gz: ba991af6e96aa6cfa743aadd4475a8b256c92d2f959c32de0e9a42597fd3e38dff4809471b2befc7cbd2c9cf4168a5af17d1bc067c089b5c092d0e90a80b2c43
|
data/README.md
CHANGED
@@ -53,7 +53,7 @@ But [will it blend?][8]
|
|
53
53
|
[8]: https://github.com/jessitron/gerald#gerald
|
54
54
|
|
55
55
|
```irb
|
56
|
-
pry> sampler2 = Quick::Sampler.compile {
|
56
|
+
pry> sampler2 = Quick::Sampler.compile { string(:lower, size: 3..5) }
|
57
57
|
=> #<Enumerator::Lazy: #<Enumerator: #<Enumerator::Generator:0x007f295fd88058>:each>>
|
58
58
|
pry> sampler2.zip(sampler).first(5).to_h
|
59
59
|
=> {"bjm"=>-4027257104748747508,
|
@@ -88,15 +88,6 @@ Generators.one_of_weighted Generators.integer => 10,
|
|
88
88
|
Generators.vector_of(5, Generators.integer) => 5
|
89
89
|
```
|
90
90
|
|
91
|
-
### Sampler configuration
|
92
|
-
|
93
|
-
Some sampling parameters can be passed as arguments to a sampler function (like
|
94
|
-
character class `:lower` in the example above). Others - that affect multiple
|
95
|
-
sub-samplers in a definition - may be injected with a call to `config(...)` (like
|
96
|
-
`upper_bound` above).
|
97
|
-
|
98
|
-
(**Rosetta stone:** `upper_bound` is what in Haskell QuickCheck is known as `size`)
|
99
|
-
|
100
91
|
### Sampler composability
|
101
92
|
|
102
93
|
The composition using Enumerable API as demonstrated above is pretty flexible and familiar to a
|
@@ -6,15 +6,13 @@ module Quick
|
|
6
6
|
# passed to {Quick::Sampler.compile}.
|
7
7
|
class DSL::Fluidiom < SimpleDelegator
|
8
8
|
# SimpleDelegator so that it can unwrap the "original" sampler with `#__getobj__`
|
9
|
-
include Quick::Sampler::Config
|
10
9
|
|
11
10
|
# @api private
|
12
11
|
# wraps a `sampler` into a `Fluidiom` instance so it has extra methods while
|
13
12
|
# inside the block passed to {Quick::Sampler.compile}
|
14
|
-
def initialize sampler
|
13
|
+
def initialize sampler
|
15
14
|
sampler = Base.new(sampler) unless sampler.is_a? Base
|
16
15
|
super(sampler)
|
17
|
-
config.merge! _config
|
18
16
|
end
|
19
17
|
|
20
18
|
# @return [Quick::Sampler]
|
@@ -24,16 +22,15 @@ module Quick
|
|
24
22
|
end
|
25
23
|
|
26
24
|
# @return [Quick::Sampler]
|
27
|
-
# wrapped sampler
|
25
|
+
# a new fluidiom-wrapped sampler
|
28
26
|
def spawn sampler
|
29
|
-
self.class.new(sampler
|
27
|
+
self.class.new(sampler)
|
30
28
|
end
|
31
29
|
|
32
30
|
# spawn a filtering sampler
|
33
31
|
#
|
34
|
-
#
|
35
|
-
#
|
36
|
-
#
|
32
|
+
# @param [Integer] max_iterations
|
33
|
+
# try at most this many values before giving up
|
37
34
|
# @return [Quick::Sampler]
|
38
35
|
# a sampler that passes through only samples that satisfy the
|
39
36
|
# predicate given as block
|
@@ -41,7 +38,7 @@ module Quick
|
|
41
38
|
# a sampled value to be tested
|
42
39
|
# @yieldreturn [Boolean]
|
43
40
|
# `true` to pass the value through
|
44
|
-
def such_that &predicate
|
41
|
+
def such_that max_iterations: 1000, &predicate
|
45
42
|
spawn(unwrap.take(max_iterations).select(&predicate))
|
46
43
|
end
|
47
44
|
|
@@ -45,9 +45,6 @@ module Quick
|
|
45
45
|
|
46
46
|
# Sampler of uniform arrays
|
47
47
|
#
|
48
|
-
# This sampler honors `upper_bound` config variable and samples Arrays of up to
|
49
|
-
# that many elements.
|
50
|
-
#
|
51
48
|
# **Rosetta stone** the single argument version corresponds to QuickCheck's `listOf`.
|
52
49
|
# Passing `non_empty: true` turns it into QuickCheck's `listOf1`.
|
53
50
|
#
|
@@ -55,25 +52,22 @@ module Quick
|
|
55
52
|
# a sampler that produces arrays of values sampled from its argument
|
56
53
|
# @param [Quick::Sampler] sampler
|
57
54
|
# a sampler to sample array elements from
|
58
|
-
|
59
|
-
|
60
|
-
def list_of sampler, non_empty: false
|
61
|
-
lower_bound = non_empty ? 1 : 0
|
62
|
-
feed { sampler.first(rand(lower_bound..upper_bound)) }
|
55
|
+
def list_of sampler, size: 1..10
|
56
|
+
send_to(sampler, :first, size)
|
63
57
|
end
|
64
58
|
|
65
|
-
# Sampler of uniform fixed
|
59
|
+
# Sampler of uniform fixed size arrays
|
66
60
|
#
|
67
61
|
# **Rosetta stone** this sampler corresponds to QuickCheck's `vectorOf`.
|
68
62
|
#
|
69
63
|
# @return [Quick::Sampler]
|
70
|
-
# a sampler that produces arrays of `
|
71
|
-
# @param [Integer]
|
72
|
-
# sample array
|
64
|
+
# a sampler that produces arrays of `size` of values sampled from `sampler`
|
65
|
+
# @param [Integer] size
|
66
|
+
# sample array size
|
73
67
|
# @param [Quick::Sampler] sampler
|
74
68
|
# a sampler to sample array elements from
|
75
|
-
def vector_of
|
76
|
-
feed { sampler.take(
|
69
|
+
def vector_of size, sampler
|
70
|
+
feed { sampler.take(size).force }
|
77
71
|
end
|
78
72
|
|
79
73
|
# Sampler of arbitrary nested structures made up of `Array`s, `Hash`es, `Quick::Sampler`s and
|
@@ -85,7 +85,7 @@ module Quick
|
|
85
85
|
# @todo document character classes
|
86
86
|
# @param [Integer, Range, Quick::Sampler<Integer>] size:
|
87
87
|
# Length of the string to generate
|
88
|
-
def string *classes, size:
|
88
|
+
def string *classes, size: 1..10
|
89
89
|
classes = [:printable] if classes.empty?
|
90
90
|
repertoire = DSL::CharacterClass.expand(*classes)
|
91
91
|
size = pick_from(size) if Range === size
|
data/lib/quick/sampler/dsl.rb
CHANGED
@@ -13,7 +13,6 @@ module Quick
|
|
13
13
|
# (Incidentally, {Fluidiom} instances for deeper nested sub-samplers get
|
14
14
|
# leaked from compile at the moment)
|
15
15
|
class DSL
|
16
|
-
include Quick::Sampler::Config
|
17
16
|
include SimpleValues
|
18
17
|
include SimpleCombinators
|
19
18
|
|
@@ -44,7 +43,7 @@ module Quick
|
|
44
43
|
# @yieldreturn [<Sample>] a sampled value
|
45
44
|
#
|
46
45
|
def feed &block
|
47
|
-
Fluidiom.new(Base.new(block)
|
46
|
+
Fluidiom.new(Base.new(block))
|
48
47
|
end
|
49
48
|
|
50
49
|
private
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: quick-sampler
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Artem Baguinski
|
@@ -112,7 +112,6 @@ files:
|
|
112
112
|
- lib/quick/sampler.rb
|
113
113
|
- lib/quick/sampler/autoload.rb
|
114
114
|
- lib/quick/sampler/base.rb
|
115
|
-
- lib/quick/sampler/config.rb
|
116
115
|
- lib/quick/sampler/dsl.rb
|
117
116
|
- lib/quick/sampler/dsl/character_class.rb
|
118
117
|
- lib/quick/sampler/dsl/fluidiom.rb
|
data/lib/quick/sampler/config.rb
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
require "active_support/configurable"
|
2
|
-
require "active_support/concern"
|
3
|
-
|
4
|
-
module Quick
|
5
|
-
module Sampler
|
6
|
-
module Config
|
7
|
-
extend ActiveSupport::Concern
|
8
|
-
include ActiveSupport::Configurable
|
9
|
-
|
10
|
-
included do
|
11
|
-
config_accessor(:max_iterations) { 1000 }
|
12
|
-
config_accessor(:upper_bound) { 25 }
|
13
|
-
end
|
14
|
-
|
15
|
-
def config options = :none_given
|
16
|
-
if options == :none_given
|
17
|
-
super()
|
18
|
-
else
|
19
|
-
config.merge!(options)
|
20
|
-
self
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|