generative 0.2.0 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -1
- data/README.md +85 -0
- data/Rakefile +4 -4
- data/bin/generative +1 -1
- data/generative.gemspec +1 -1
- data/lib/generative.rb +21 -0
- data/lib/generative/dsl.rb +5 -0
- data/lib/generative/formatters.rb +1 -1
- data/lib/generative/generator_manager.rb +32 -0
- data/spec/generative/generator_manager_spec.rb +50 -0
- data/spec/generative_spec.rb +12 -0
- metadata +18 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 30facfd44bae285d5299ad418352db2d662b9fae
|
4
|
+
data.tar.gz: 10dd90eaf237f02a57c1087ce6287400a78427d6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: da448e92ce5aa862d8fe45cea577dd2682d3e9bf01cc5b169e0520a2bb8e90bdcd5a859a11fee4a14ea45de06ad68c481e1950057ccd8eaedd0395923f81c21e
|
7
|
+
data.tar.gz: 174c2dfd2ec20af6bfac7df2499afec77dbe3849ad3dd414f0837a18d9e7542220dcdbd112c20d40c2212d30a43415290c96c5cc215a39651f4680c3fc3a2b36
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -110,3 +110,88 @@ Given the examples above, running `rspec` will use the default "progress"
|
|
110
110
|
formatter. Requiring generative will modify this formatter to output blue dots
|
111
111
|
instead of green for generative tests. Generative also includes it's own
|
112
112
|
formatter, which will only display generative test names once, also in blue.
|
113
|
+
|
114
|
+
### Generators
|
115
|
+
|
116
|
+
Generative allows you to register your own custom generators for generative
|
117
|
+
tests. Registering a generator requires two thing, a name and an object that
|
118
|
+
responds to `#call`.
|
119
|
+
|
120
|
+
Whether you just want to use lambdas.
|
121
|
+
|
122
|
+
```rb
|
123
|
+
Generative.register_generator(:full_name) { "#{Generative.generate(:string)} #{Generative.generate(:string}" }
|
124
|
+
|
125
|
+
Generative.register_generator(:user) { FactoryGirl.build(:user, id: Generative.generate(:integer)) }
|
126
|
+
```
|
127
|
+
|
128
|
+
A class or module that responds to `#call`.
|
129
|
+
|
130
|
+
```rb
|
131
|
+
class LameGenerator
|
132
|
+
def self.call
|
133
|
+
random_string
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
Generative.register_generator(:lame, LameGenerator)
|
138
|
+
```
|
139
|
+
|
140
|
+
Or you want to use a fancy library.
|
141
|
+
|
142
|
+
```rb
|
143
|
+
FakeTypeMappingLibrary
|
144
|
+
generator_for Bar do
|
145
|
+
initializer Float, Fixnum
|
146
|
+
|
147
|
+
accessor :baz, Float
|
148
|
+
accessor :quz, NotherClass
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
Generative.register_generator(:bar, Bar }
|
153
|
+
```
|
154
|
+
|
155
|
+
You can then use your generators using the `generate` helper.
|
156
|
+
|
157
|
+
```rb
|
158
|
+
describe String do
|
159
|
+
let(:string) { "abc" }
|
160
|
+
|
161
|
+
describe "#reverse" do
|
162
|
+
generative do
|
163
|
+
data(:string) { generate(:my_fancy_string) }
|
164
|
+
|
165
|
+
it "maintains length" do
|
166
|
+
expect(string.reverse.length).to eq(string.length)
|
167
|
+
end
|
168
|
+
end
|
169
|
+
end
|
170
|
+
end
|
171
|
+
```
|
172
|
+
|
173
|
+
#### I heard you like generators so I put some generators in your generators...
|
174
|
+
|
175
|
+
If you want to use other generators in you registered generator do note
|
176
|
+
you have to use Generative's module level method `.generate` like so...
|
177
|
+
|
178
|
+
```rb
|
179
|
+
Generative.register_generator :user do
|
180
|
+
User.new( first_name: Generative.generate(:string, max: 100) )
|
181
|
+
end
|
182
|
+
```
|
183
|
+
|
184
|
+
|
185
|
+
|
186
|
+
### Pre-made Generators
|
187
|
+
|
188
|
+
**Note**: At this time Generative provides no pre-made generators. We
|
189
|
+
encourage you to go out and seek good ways of generating
|
190
|
+
data and maybe even package them up as your own gems. If you
|
191
|
+
do, let us know and we'll link to it here.
|
192
|
+
|
193
|
+
|
194
|
+
#### Degenerate
|
195
|
+
|
196
|
+
This library has a few generators for some basic primitives... A great start if
|
197
|
+
you're looking to move quickly.
|
data/Rakefile
CHANGED
@@ -13,10 +13,10 @@ task :acceptance do
|
|
13
13
|
ENV.delete('GENERATIVE_COUNT')
|
14
14
|
|
15
15
|
[
|
16
|
-
['rspec', '
|
17
|
-
['rake spec', '
|
18
|
-
['rake generative', '
|
19
|
-
['bin/generative', '
|
16
|
+
['rspec', '22'], # can we do this some other way
|
17
|
+
['rake spec', '22'], # sucks to bump as we add tests...
|
18
|
+
['rake generative', '40000'],
|
19
|
+
['bin/generative', '40000']
|
20
20
|
].each do |command, expected_example_count|
|
21
21
|
puts "Checking output of `#{command}`"
|
22
22
|
output = %x{#{command}}
|
data/bin/generative
CHANGED
@@ -13,7 +13,7 @@ command = %w[
|
|
13
13
|
--tag generative
|
14
14
|
].join(' ')
|
15
15
|
|
16
|
-
command = "GENERATIVE_COUNT=#{ENV['GENERATIVE_COUNT']} #{command}"
|
16
|
+
command = "export GENERATIVE_COUNT=#{ENV['GENERATIVE_COUNT']}; #{command}; unset GENERATIVE_COUNT"
|
17
17
|
|
18
18
|
puts command
|
19
19
|
system command
|
data/generative.gemspec
CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |gem|
|
6
6
|
gem.name = 'generative'
|
7
|
-
gem.version = '0.2.
|
7
|
+
gem.version = '0.2.2'
|
8
8
|
gem.authors = ["Justin Campbell", "Dan McClory"]
|
9
9
|
gem.email = ["justin@justincampbell.me", "danmcclory@gmail.com"]
|
10
10
|
gem.description = "Generative testing for RSpec"
|
data/lib/generative.rb
CHANGED
@@ -1,8 +1,28 @@
|
|
1
1
|
require 'rspec/core'
|
2
2
|
|
3
3
|
module Generative
|
4
|
+
|
4
5
|
DEFAULT_COUNT = '10_000'
|
5
6
|
|
7
|
+
class << self
|
8
|
+
extend Forwardable
|
9
|
+
def_delegators :manager, :register_generator, :find_and_call
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.generate(generator_name, *args)
|
13
|
+
find_and_call(generator_name, *args)
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.manager
|
17
|
+
@manager ||= GeneratorManager.new(preregistered_generators)
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.preregistered_generators
|
21
|
+
{
|
22
|
+
# nothing to see here yet
|
23
|
+
}
|
24
|
+
end
|
25
|
+
|
6
26
|
def self.running?
|
7
27
|
!!ENV['GENERATIVE_COUNT']
|
8
28
|
end
|
@@ -11,3 +31,4 @@ end
|
|
11
31
|
require 'generative/dsl'
|
12
32
|
require 'generative/formatters'
|
13
33
|
require 'generative/ordering'
|
34
|
+
require 'generative/generator_manager'
|
data/lib/generative/dsl.rb
CHANGED
@@ -0,0 +1,32 @@
|
|
1
|
+
module Generative
|
2
|
+
class InvalidGenerator < StandardError; end
|
3
|
+
class UnregisteredGenerator < StandardError; end
|
4
|
+
|
5
|
+
class GeneratorManager
|
6
|
+
|
7
|
+
attr_reader :generators
|
8
|
+
|
9
|
+
def initialize(preregistered_generators={})
|
10
|
+
@generators = Hash.new(preregistered_generators)
|
11
|
+
end
|
12
|
+
|
13
|
+
def register_generator(name, factory=nil, &generator)
|
14
|
+
registerable = block_given? ? generator : factory
|
15
|
+
msg = "#{registerable} must respond to :call"
|
16
|
+
raise InvalidGenerator, msg unless generator_valid?(registerable)
|
17
|
+
@generators.merge!(name => registerable)
|
18
|
+
end
|
19
|
+
|
20
|
+
def find_and_call(name, *args)
|
21
|
+
begin
|
22
|
+
generators[name].call(*args)
|
23
|
+
rescue NoMethodError
|
24
|
+
raise UnregisteredGenerator, "#{name} generator not registered"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def generator_valid?(generator)
|
29
|
+
generator.respond_to?(:call)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Generative::GeneratorManager do
|
4
|
+
|
5
|
+
subject { Generative::GeneratorManager.new }
|
6
|
+
|
7
|
+
describe "#register_generator_generator" do
|
8
|
+
it "throws an exception if a block or a factory object isn't given" do
|
9
|
+
expect { subject.register_generator(:test) }.to raise_error(Generative::InvalidGenerator)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "registers a new generator when given a generator block" do
|
13
|
+
name, generator = :test, lambda { "hi" }
|
14
|
+
expect(subject.generators).not_to include(name)
|
15
|
+
expect(subject.register_generator(name) { "hi" }).to include(name)
|
16
|
+
expect(subject.generators[name].call).to eq("hi")
|
17
|
+
end
|
18
|
+
|
19
|
+
it "registers a new generator when given a factory object" do
|
20
|
+
name, factory = :test, OpenStruct.new(call: true)
|
21
|
+
expect(subject.generators).not_to include(name)
|
22
|
+
expect(subject.register_generator(name, factory)).to include(name)
|
23
|
+
expect(subject.generators[name].call).to be_truthy
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "#find_and_call" do
|
28
|
+
it "calls a specific generator passing in it's arguments" do
|
29
|
+
name, generator = :test, lambda { |arg| arg }
|
30
|
+
subject.register_generator(name, &generator)
|
31
|
+
expect(subject.find_and_call(name, "hi")).to eq("hi")
|
32
|
+
end
|
33
|
+
|
34
|
+
it "raises an error fo ran unregistered generator" do
|
35
|
+
expect { subject.find_and_call(:nope) }
|
36
|
+
.to raise_error(Generative::UnregisteredGenerator)
|
37
|
+
end
|
38
|
+
|
39
|
+
describe "#generator_valid?" do
|
40
|
+
it "returns false if an object doesn't respond to :call or :build" do
|
41
|
+
expect(subject.generator_valid?(nil)).to be_falsey
|
42
|
+
end
|
43
|
+
|
44
|
+
it "returns true if an object doesn't respond to :call" do
|
45
|
+
generator = OpenStruct.new(call: true)
|
46
|
+
expect(subject.generator_valid?(generator)).to be_truthy
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
data/spec/generative_spec.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
require 'generative'
|
2
2
|
|
3
|
+
Generative.register_generator(:string) { "a" * rand(255) }
|
4
|
+
|
3
5
|
describe String do
|
4
6
|
let(:string) { "abc" }
|
5
7
|
|
@@ -17,6 +19,16 @@ describe String do
|
|
17
19
|
expect(string.length).to be >= 0
|
18
20
|
end
|
19
21
|
end
|
22
|
+
|
23
|
+
xit "uses registered generators"
|
24
|
+
|
25
|
+
generative do
|
26
|
+
data(:string) { generate(:string) }
|
27
|
+
|
28
|
+
it "is never negative" do
|
29
|
+
expect(string.length).to be >= 0
|
30
|
+
end
|
31
|
+
end
|
20
32
|
end
|
21
33
|
|
22
34
|
describe "#reverse" do
|
metadata
CHANGED
@@ -1,44 +1,44 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: generative
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Justin Campbell
|
8
8
|
- Dan McClory
|
9
|
-
autorequire:
|
9
|
+
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-08
|
12
|
+
date: 2014-12-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
|
16
|
+
version_requirements: !ruby/object:Gem::Requirement
|
17
17
|
requirements:
|
18
18
|
- - '>='
|
19
19
|
- !ruby/object:Gem::Version
|
20
20
|
version: '3.0'
|
21
|
-
|
22
|
-
prerelease: false
|
23
|
-
version_requirements: !ruby/object:Gem::Requirement
|
21
|
+
requirement: !ruby/object:Gem::Requirement
|
24
22
|
requirements:
|
25
23
|
- - '>='
|
26
24
|
- !ruby/object:Gem::Version
|
27
25
|
version: '3.0'
|
26
|
+
prerelease: false
|
27
|
+
type: :runtime
|
28
28
|
- !ruby/object:Gem::Dependency
|
29
29
|
name: rake
|
30
|
-
|
30
|
+
version_requirements: !ruby/object:Gem::Requirement
|
31
31
|
requirements:
|
32
32
|
- - '>='
|
33
33
|
- !ruby/object:Gem::Version
|
34
34
|
version: '0'
|
35
|
-
|
36
|
-
prerelease: false
|
37
|
-
version_requirements: !ruby/object:Gem::Requirement
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
38
36
|
requirements:
|
39
37
|
- - '>='
|
40
38
|
- !ruby/object:Gem::Version
|
41
39
|
version: '0'
|
40
|
+
prerelease: false
|
41
|
+
type: :development
|
42
42
|
description: Generative testing for RSpec
|
43
43
|
email:
|
44
44
|
- justin@justincampbell.me
|
@@ -61,9 +61,11 @@ files:
|
|
61
61
|
- lib/generative.rb
|
62
62
|
- lib/generative/dsl.rb
|
63
63
|
- lib/generative/formatters.rb
|
64
|
+
- lib/generative/generator_manager.rb
|
64
65
|
- lib/generative/ordering.rb
|
65
66
|
- lib/generative/rake_task.rb
|
66
67
|
- lib/generative/version.rb
|
68
|
+
- spec/generative/generator_manager_spec.rb
|
67
69
|
- spec/generative/ordering_spec.rb
|
68
70
|
- spec/generative/rake_task_spec.rb
|
69
71
|
- spec/generative_spec.rb
|
@@ -72,7 +74,7 @@ homepage: https://github.com/justincampbell/generative
|
|
72
74
|
licenses:
|
73
75
|
- MIT
|
74
76
|
metadata: {}
|
75
|
-
post_install_message:
|
77
|
+
post_install_message:
|
76
78
|
rdoc_options: []
|
77
79
|
require_paths:
|
78
80
|
- lib
|
@@ -87,14 +89,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
87
89
|
- !ruby/object:Gem::Version
|
88
90
|
version: '0'
|
89
91
|
requirements: []
|
90
|
-
rubyforge_project:
|
91
|
-
rubygems_version: 2.
|
92
|
-
signing_key:
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 2.1.9
|
94
|
+
signing_key:
|
93
95
|
specification_version: 4
|
94
96
|
summary: Generative testing for RSpec
|
95
97
|
test_files:
|
98
|
+
- spec/generative/generator_manager_spec.rb
|
96
99
|
- spec/generative/ordering_spec.rb
|
97
100
|
- spec/generative/rake_task_spec.rb
|
98
101
|
- spec/generative_spec.rb
|
99
102
|
- spec/spec_helper.rb
|
100
|
-
has_rdoc:
|