quick-sampler 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a9b033fc8a97060c6aa4c6a747cbd064c54bf703
4
- data.tar.gz: 1565195eb76277ead9807e0999ed688218001855
3
+ metadata.gz: 62a7ce51aa6d063591fd221700c752755c531b3a
4
+ data.tar.gz: af103d57ff121544eca0b37fd9a1d10925323404
5
5
  SHA512:
6
- metadata.gz: 984ffcf0dbeb3f9b97c982d8437cc0a4d3a03580efad83cddb68f9b0eace999df5cb015d5635472681d80445108d600c58287356e4e0e6461d30f623dc45a3c3
7
- data.tar.gz: e9158e1858ab646cbe56e898e033d7d62ac76b6785299470003055be605e2164999a4c44cce5342d71906b5d6343db828d8508ad46016fd9199b8ca7a9cd03af
6
+ metadata.gz: eee6f10bf27b120548fc945bbd6f923c2f390c28a2d0d665f895fbdcf1d21d202cb649d1c9ad5e23d8f197ba417a93d26af58ee8a4e9cec9c62f518ed37c6944
7
+ data.tar.gz: ce4903abb8581d3801a4b513d9a1c60a8bf6934827b379e743d71135354e3190a25a6470aa0352a624594a3750f3b0117160ca4781c897544a9024fb1af258ea
@@ -87,6 +87,15 @@ module Quick
87
87
  feed { args.map { |arg| recursive_sample(arg) } }
88
88
  end
89
89
 
90
+ # Sampler of hashe's that in turn may contain `Array`s, `Hash`es and `Quick::Sampler`s.
91
+ # @param [Hash] pattern
92
+ # a hash that will be used as a template for samples
93
+ # @return [Quick::Sampler<Hash>]
94
+ # a recursive sampler of hashes
95
+ def hash_like template
96
+ send_to list_like(template), :first
97
+ end
98
+
90
99
  # Sampler of arbitrary message send ("method call") results
91
100
  #
92
101
  # @overload send_to recipient, message, *args
@@ -75,19 +75,21 @@ module Quick
75
75
  pick_from([true, false])
76
76
  end
77
77
 
78
- # This sampler honors `upper_bound` config variable.
79
- #
80
- # The sampler will produce strings of random (between 0 and `upper_bound`) length
81
- # made up of characters belonging to supplied named classes.
78
+ # The sampler will produce strings of whose length is controlled by
79
+ # `size:` argument made up of characters belonging to supplied named
80
+ # classes.
82
81
  #
83
82
  # @returns [Quick::Sampler<String>] random `String` sampler
84
- # @param [Array<Symbol>] classes
83
+ # @param [Array<Symbol>] *classes
85
84
  # Character classes to pick from.
86
85
  # @todo document character classes
87
- def string *classes
86
+ # @param [Integer, Range, Quick::Sampler<Integer>] size:
87
+ # Length of the string to generate
88
+ def string *classes, size: pick_from(1..10)
88
89
  classes = [:printable] if classes.empty?
89
90
  repertoire = DSL::CharacterClass.expand(*classes)
90
- feed { repertoire.sample(rand(upper_bound)).join }
91
+ size = pick_from(size) if Range === size
92
+ send_to( send_to(repertoire, :sample, size), :join )
91
93
  end
92
94
 
93
95
  end
@@ -1,5 +1,5 @@
1
1
  module Quick
2
2
  module Sampler
3
- VERSION = "0.0.3"
3
+ VERSION = "0.1.0"
4
4
  end
5
5
  end
@@ -0,0 +1,40 @@
1
+ describe Quick::Sampler::DSL do
2
+ it { is_expected.to have_method :hash_like }
3
+
4
+ describe "#hash_like" do
5
+ let(:dsl) { described_class.new }
6
+ let(:sampled_hashes) {
7
+ dsl.hash_like(template).first(5)
8
+ }
9
+ let(:positive_integer) { dsl.positive_integer }
10
+ let(:negative_integer) { dsl.negative_integer }
11
+
12
+ context "given only constants" do
13
+ let(:template) { { air: 1, water: 2 } }
14
+ it "repeats args as is" do
15
+ expect(sampled_hashes).to all eq template
16
+ end
17
+ end
18
+
19
+ context "given a hash with samplers" do
20
+ let(:template) { {plus: positive_integer, minus: negative_integer } }
21
+ it "samples from each sampler" do
22
+ expect(sampled_hashes).to all match plus: a_value > 0, minus: a_value < 0
23
+ end
24
+ end
25
+
26
+ context "given a combination of samplers and constants" do
27
+ let(:template) { {
28
+ string: "text",
29
+ plus: positive_integer,
30
+ minus: negative_integer,
31
+ pi: 3.14159
32
+ } }
33
+ it "keeps constants as is, but samples the samplers" do
34
+ expect(sampled_hashes).to all match string: "text",
35
+ plus: a_value > 0, minus: a_value < 0, pi: 3.14159
36
+ end
37
+ end
38
+
39
+ end
40
+ end
@@ -6,4 +6,30 @@ describe Quick::Sampler::DSL do
6
6
  subject(:samples) { dsl.const(Math::PI).first(5) }
7
7
  it { is_expected.to all be == Math::PI }
8
8
  end
9
+
10
+ describe "#string" do
11
+ subject(:samples) { dsl.string(*args).first(5) }
12
+ context "without arguments" do
13
+ let(:args) { [] }
14
+ it { is_expected.to all be_a String }
15
+ it "has length between 1 and 10" do
16
+ expect(samples).to all satisfy { |s| s.length >= 1 && s.length <= 10 }
17
+ end
18
+ end
19
+
20
+ context "with fixed size" do
21
+ let(:args) { [size: 10] }
22
+ it { is_expected.to all have_attributes(length: 10) }
23
+ end
24
+
25
+ context "with range size" do
26
+ let(:args) { [size: 5..15] }
27
+ it { is_expected.to all satisfy { |s| s.length >= 5 && s.length <= 15 } }
28
+ end
29
+
30
+ context "with a character class" do
31
+ let(:args) { [:lower] }
32
+ it { is_expected.to all match /^[a-z]{1,10}$/ }
33
+ end
34
+ end
9
35
  end
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.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Artem Baguinski
@@ -124,6 +124,7 @@ files:
124
124
  - lib/quick/sampler/version.rb
125
125
  - quick-sampler.gemspec
126
126
  - spec/quick/sampler/compile_spec.rb
127
+ - spec/quick/sampler/dsl/hash_like_spec.rb
127
128
  - spec/quick/sampler/dsl/list_like_spec.rb
128
129
  - spec/quick/sampler/dsl/send_to_spec.rb
129
130
  - spec/quick/sampler/dsl/simple_values_spec.rb
@@ -154,6 +155,7 @@ specification_version: 4
154
155
  summary: Composable samplers of random data
155
156
  test_files:
156
157
  - spec/quick/sampler/compile_spec.rb
158
+ - spec/quick/sampler/dsl/hash_like_spec.rb
157
159
  - spec/quick/sampler/dsl/list_like_spec.rb
158
160
  - spec/quick/sampler/dsl/send_to_spec.rb
159
161
  - spec/quick/sampler/dsl/simple_values_spec.rb