rspec-junklet 2.0.1 → 2.1.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.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZDRhMTA4YzAzZmE4ZDZkMzY3NmYzNjU3NzQzNTQ3YzJiOGIzODA3Nw==
4
+ NTBkNmE5YzhiNjBkZTE5MTE4OWM5YzAxMzI1NzIwMmUxMjEzNTljOA==
5
5
  data.tar.gz: !binary |-
6
- ODZjMjRiZDQ0MGNhNDI0YzMzNWMxNWIzMTViODY2ZGMwMDY3OWJmYQ==
6
+ ZWIxZjZkNTg2NmVhMmVlYjYzYTdkMWJhMDdhYTc4YTIwZmY5MzhkNg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZjFkZTY3NjY0MmU4MTFhOWFhYmViMDI5YjZkODEwZTM0MTEzYjI4NzI1YzI1
10
- NWI3MmViMWJkMTExMWZjOWVjNjIzMTliMDk0NjlkYzc5ZDQwZGM2ZGFhMTgx
11
- MmFjYjA2YzE3NTUwYjQxMmRlY2NkMzMxNTFjMWQ3MTc4MWIwYTA=
9
+ NmEzM2E4OWJiMWUyOGY4MDM0YWNjN2ZhODlhN2ZkOWM4NjEyOThhMjFkMjM3
10
+ MmYyOWVkMzU4ZDc2ODIzYTZhYzVmZTdhZTM3YjkyNzE0MmRkNjhmYzAxOTBh
11
+ OTg1ZjI0ZTk5ODE4NWRmOTA1ZmY1NDA3ODE1Zjg5ZDc2YWEyNWU=
12
12
  data.tar.gz: !binary |-
13
- YzM4MzhlMzMxNWI0NDE4MmViZTQyNTk0YzJlZDY4ZDUwNTIxNGY4ZDI2NTk5
14
- NjBkYzI4Zjg3ZTY1OTAxMTc5M2RkNjRlMGJkNmY4ZmY4MTcxMzA4ZWUzOWYz
15
- NDAwM2I4YTUxNmI2Y2JlYjNjMzZlZWM0ZmE5MjIwYWU5N2M4YzE=
13
+ YjVhMjU5ODEwNzYzZTg3MTk5YzZhODA4MWU1NTA3NjY5NjRmMGM0ZDdjMjc5
14
+ NzhiMzJjYjgwYTEyMTcxNzlhYTEyMTA4ODRlOTg0YWE5MWZhOTVlZDBjYWE2
15
+ OTg3MzdkMmUxZGY2OGE0NjEwZGNlYjg0Y2Y1YWE0ZTllMTBlZmU=
data/README.md CHANGED
@@ -7,7 +7,8 @@ or `junk 4`.
7
7
  Junklet data is fixture data that:
8
8
 
9
9
  * We essentially don't care about,
10
- * But we might want to test for equality somewhere later,
10
+ * But we might need it to conform to a certain format,
11
+ * And we might want to test for equality somewhere later,
11
12
  * And we might need to be unique between runs in case a spec crashes and
12
13
  SQLServer fails to clean up the test database
13
14
 
@@ -230,6 +231,21 @@ let(:otherside) { junk :bool, exclude: coinflip } # Look I never said
230
231
  *VERY IMPORTANT CAVEAT* If you exclude all of the possibilities from the random
231
232
  key space, junk will cheerfully go into an infinite loop.
232
233
 
234
+ ### Size
235
+
236
+ Size constrains the size of the output. It works differently depending on the
237
+ type of junk:
238
+
239
+ * Number of Digits: as documented above, `junk :int, size: 4` will return a
240
+ 4-digit number beginning with 1-9.
241
+ * Number of Repetitions: For Enumerable and Array junk, where the value of the
242
+ junk is obtained by picking an element at random, size will make junk return an
243
+ array containing that many random selections. The same goes for proc, only it
244
+ will be called that number of times. Note that excluders and formatters will be
245
+ applied to each element of the array as well.
246
+
247
+
248
+
233
249
  ### Format
234
250
 
235
251
  A format can be applied to junk data after generation. This lets you change the
@@ -52,11 +52,16 @@ module RSpec
52
52
  # FIXME: Raise Argument error unless *args.size is 0-2
53
53
  # FIXME: If arg 1 is a hash, it's the options hash, raise
54
54
  # ArgumentError unless args.size == 1
55
+
56
+ # TODO: Nice-to-have feature: if args.second is an :int, assume size.
57
+
55
58
  # FIXME: If arg 2 present, Raise Argument error unless it's a
56
59
  # hash.
60
+
57
61
  # FIXME: Figure out what our valid options are and parse them;
58
62
  # raise errors if present.
59
63
 
64
+ repeat = 0
60
65
  junk_types = [Symbol, Array, Enumerable, Proc]
61
66
  if args.size > 0 && junk_types.any? {|klass| args.first.is_a?(klass) }
62
67
  type = args.shift
@@ -118,27 +123,42 @@ module RSpec
118
123
  when :bool
119
124
  generator = -> { [true, false].sample }
120
125
  when Array, Enumerable
126
+ repeat = opts[:size] if opts[:size]
121
127
  generator = -> { type.to_a.sample }
122
128
  when Proc
129
+ repeat = opts[:size] if opts[:size]
123
130
  generator = type
124
131
  else
125
132
  raise "Unrecognized junk type: '#{type}'"
126
133
  end
127
134
 
128
- begin
129
- val = formatter.call(generator.call)
130
- end while excluder.call(val)
135
+ val = if repeat.zero?
136
+ value(generator, formatter, excluder)
137
+ else
138
+ repeat.times.map {
139
+ value(generator, formatter, excluder)
140
+ }
141
+ end
131
142
  val
132
143
  else
133
144
  size = args.first.is_a?(Numeric) ? args.first : 32
134
145
  # hex returns size*2 digits, because it returns a 0..255 byte
135
- # as a hex pair. But when we want junt, we want *bytes* of
146
+ # as a hex pair. But when we want junk, we want *bytes* of
136
147
  # junk. Get (size+1)/2 chars, which will be correct for even
137
148
  # sizes and 1 char too many for odds, so trim off with
138
149
  # [0...size] (note three .'s to trim off final char)
139
150
  SecureRandom.hex((size+1)/2)[0...size]
140
151
  end
141
152
  end
153
+
154
+ private
155
+
156
+ def value(generator, formatter, excluder)
157
+ begin
158
+ v = formatter.call(generator.call)
159
+ end while excluder.call(v)
160
+ v
161
+ end
142
162
  end
143
163
  end
144
164
  end
@@ -1,5 +1,5 @@
1
1
  module RSpec
2
2
  module Junklet
3
- VERSION = "2.0.1"
3
+ VERSION = "2.1.0"
4
4
  end
5
5
  end
data/lib/rspec/junklet.rb CHANGED
@@ -2,10 +2,9 @@ require_relative "./junklet/version"
2
2
  require_relative "./junklet/junk"
3
3
  require_relative "./junklet/junklet"
4
4
 
5
- if RSpec.respond_to?(:configure)
6
- RSpec.configure do |config|
7
- config.extend(RSpec::Junklet::Junklet)
8
- config.extend(RSpec::Junklet::Junk) # when metaprogramming cases, you may need junk in ExampleGroups
9
- config.include(RSpec::Junklet::Junk)
10
- end
5
+ # Automatically hook into RSpec when you `include "rspec-junklet"`
6
+ RSpec.configure do |config|
7
+ config.extend(RSpec::Junklet::Junklet) # This lets us say junklet() in describes and contexts
8
+ config.extend(RSpec::Junklet::Junk) # This lets us say junk() in describes and contexts
9
+ config.include(RSpec::Junklet::Junk) # This lets us say junk() in lets
11
10
  end
@@ -1,13 +1,10 @@
1
1
  require 'spec_helper'
2
2
 
3
- # This spec file is a documentation/example spec showing how various ways to use
4
- # Junklet. It was the original spec before I extracted out the classes of
5
- # Junklet, but still serves as a functional, high-level spec plus it's a good
6
- # collection of examples so here you go.
3
+ # Because rspec-junklet extends RSpec, this spec file actually EXERCISES
4
+ # rspec-junklet instead of testing it directly. You can treat it like a list of
5
+ # examples or a cheatsheet for how to try things.
7
6
 
8
7
  describe RSpec::Junklet do
9
- specify { expect(subject).to be }
10
-
11
8
  let(:hex_regex) { /[\da-f]{32}/ }
12
9
 
13
10
  describe '.junklet' do
@@ -26,6 +23,10 @@ describe RSpec::Junklet do
26
23
  context "with multiple args" do
27
24
  junklet :trash, :toss, :crud, :crap
28
25
 
26
+ # FIXME: if we were to stub out junk here, these examples
27
+ # could be changed to read
28
+ # expect(trash).to eq("trash_1234567809etc")
29
+ # which would document the output MUCH more clearly
29
30
  specify { expect(trash).to match /^trash_/ }
30
31
  specify { expect(trash).to match hex_regex }
31
32
  specify { expect(toss).to match /^toss_/ }
@@ -77,32 +78,61 @@ describe RSpec::Junklet do
77
78
  end
78
79
  end
79
80
 
81
+ shared_examples_for "repeatable junk" do
82
+ it "returns an array" do
83
+ expect(subject).to be_a(Array)
84
+ end
85
+
86
+ it "returns an array of that size" do
87
+ expect(subject.size).to eq(3)
88
+ end
89
+
90
+ it "each element of the array is junk" do
91
+ expect(subject.all? {|i| [0,1,2].include? i}).to be_truthy
92
+ end
93
+ end
94
+
80
95
  context "with type: array" do
81
- let(:junk_ray) { junk [:a, :b, :c] }
96
+ subject { junk [:a, :b, :c] }
82
97
  it "returns a random element of the array" do
83
- expect([:a, :b, :c]).to include(junk_ray)
98
+ expect([:a, :b, :c]).to include(subject)
84
99
  end
85
100
 
86
101
  context "with excludes" do
87
- let(:junk_ray) { junk [:a, :b, :c], exclude: [:a, :b] }
88
- specify { expect(junk_ray).to eq(:c) }
102
+ let(:subject) { junk [:a, :b, :c], exclude: [:a, :b] }
103
+ it "does not return excluded values" do
104
+ expect(subject).to eq(:c)
105
+ end
106
+ end
107
+
108
+ context "with size" do
109
+ subject { junk [0, 1, 2], size: 3 }
110
+ it_behaves_like "repeatable junk"
89
111
  end
90
112
  end
91
113
 
92
114
  context "with type: Proc" do
93
- let(:junk_proc) { junk(->{ rand(3) }) }
94
- specify { expect([0,1,2]).to include(junk_proc) }
115
+ subject { junk(->{ rand(3) }) }
116
+ it "calls the proc" do
117
+ expect([0, 1, 2]).to include(subject)
118
+ end
95
119
 
96
- context "with excludes" do
97
- let(:junk_proc) { junk(->{ rand(3) }, exclude: [0,2]) }
98
- specify { expect(junk_proc).to eq(1) }
120
+ context "with size" do
121
+ subject { junk(->{ rand(3) }, size: 3 ) }
122
+ it_behaves_like "repeatable junk"
99
123
  end
100
124
  end
101
125
 
102
126
  context "with type: enumerable" do
103
- let(:junk_list) { junk (0..3) }
104
- it "returns a random element of the array" do
105
- expect([0,1,2,3]).to include(junk_list)
127
+ subject { junk 0..2 }
128
+
129
+ it "takes a sample from the enumerable" do
130
+ expect([0,1,2]).to include(subject)
131
+ end
132
+
133
+ context "with size" do
134
+ subject { junk 0..2, size: 3 }
135
+ it_behaves_like "repeatable junk"
106
136
  end
107
137
  end
108
138
 
@@ -170,6 +170,11 @@ describe JunkSpy do
170
170
  end
171
171
 
172
172
 
173
+ # BUG: this special case of format does not work:
174
+ # doubled_string = junk 6, format: ->(x) { x * 2 })
175
+ # expect(doubled_string.size).to eq(12) # nope, it's 6
176
+ # junk 6, format: ->(x) { x.upcase } # also returns x unmodified
177
+
173
178
  context "when format is a Proc" do
174
179
  let(:junk) { subject.junk [3], format: ->(x) { x * 3 } }
175
180
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-junklet
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Brady
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-26 00:00:00.000000000 Z
11
+ date: 2016-08-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler