cranky 0.0.1
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.
- data/Gemfile +11 -0
- data/LICENSE +20 -0
- data/README.rdoc +0 -0
- data/init.rb +2 -0
- data/lib/cranky.rb +87 -0
- data/spec/cranky_spec.rb +78 -0
- data/spec/spec_helper.rb +68 -0
- metadata +70 -0
data/Gemfile
ADDED
data/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright (c) 2010 Stephen McGinty
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
+
a copy of this software and associated documentation files (the
|
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
+
the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be
|
|
12
|
+
included in all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
|
File without changes
|
data/init.rb
ADDED
data/lib/cranky.rb
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
class Cranky
|
|
2
|
+
|
|
3
|
+
VERSION = "0.0.1"
|
|
4
|
+
|
|
5
|
+
# Dir.glob("#{File.expand_path(File.dirname(__FILE__))}/*.rb").each do |file|
|
|
6
|
+
# require file
|
|
7
|
+
# # Auto include all modules found in the directory
|
|
8
|
+
# file =~ /.*[\/\\](.*)\.rb/
|
|
9
|
+
# begin
|
|
10
|
+
# include $1.to_s.camelcase.constantize
|
|
11
|
+
# rescue
|
|
12
|
+
# end
|
|
13
|
+
# end
|
|
14
|
+
|
|
15
|
+
attr_accessor :debug
|
|
16
|
+
|
|
17
|
+
def initialize
|
|
18
|
+
@what = []
|
|
19
|
+
@attrs = []
|
|
20
|
+
@n = 0
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def build(what, attrs={})
|
|
24
|
+
crank_it(what, false, attrs)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def create(what, attrs={})
|
|
28
|
+
crank_it(what, true, attrs)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def reset
|
|
32
|
+
self.instance_variables.each do |var|
|
|
33
|
+
instance_variable_set(var, nil)
|
|
34
|
+
end
|
|
35
|
+
initialize
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def attributes_for(what, attrs={})
|
|
39
|
+
build(what, attrs).attributes
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
private
|
|
43
|
+
|
|
44
|
+
def n
|
|
45
|
+
@n += 1
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def inherit(what, attrs={})
|
|
49
|
+
build(what, attrs.merge(options))
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def crank_it(what, save, attrs)
|
|
53
|
+
@attrs << attrs; @what << what
|
|
54
|
+
item = self.send(what)
|
|
55
|
+
@attrs.pop; @what.pop
|
|
56
|
+
if @debug && !item.valid?
|
|
57
|
+
raise "Oops, the #{what} created by the Factory has the following errors: #{item.errors}"
|
|
58
|
+
end
|
|
59
|
+
item.save if save
|
|
60
|
+
item
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def define(attrs={})
|
|
64
|
+
final_attrs = attrs.merge(@attrs.last)
|
|
65
|
+
#item = (attrs[:class] ? attrs[:class] : @what.last).to_s.camelcase.constantize.new
|
|
66
|
+
item = get_constant(attrs[:class] ? attrs[:class] : @what.last).new
|
|
67
|
+
final_attrs.delete(:class)
|
|
68
|
+
final_attrs.each do |attr, value|
|
|
69
|
+
item.send("#{attr}=", value)
|
|
70
|
+
end
|
|
71
|
+
item
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# Nicked from here: http://gist.github.com/301173
|
|
75
|
+
def get_constant(name_sym)
|
|
76
|
+
name = name_sym.to_s.split('_').collect {|s| s.capitalize }.join('')
|
|
77
|
+
Object.const_defined?(name) ? Object.const_get(name) : Object.const_missing(name)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def options
|
|
81
|
+
@attrs.last
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
Factory = Cranky.new unless defined?(Factory)
|
|
87
|
+
|
data/spec/cranky_spec.rb
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
require "spec_helper"
|
|
2
|
+
|
|
3
|
+
describe Cranky do
|
|
4
|
+
|
|
5
|
+
before(:each) do
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
it "should be alive" do
|
|
9
|
+
Factory.build(:user).class.should == User
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
it "should not save the item when generated via build" do
|
|
13
|
+
Factory.build(:user).saved?.should == false
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
it "should save the item when generated via create" do
|
|
17
|
+
Factory.create(:user).saved?.should == true
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
it "should allow all attributes to be overriden in the call" do
|
|
21
|
+
u = Factory.build(:user, :name => "Indy", :email => "indy@home.com", :role => :dog, :unique => :yep)
|
|
22
|
+
u.name.should == "Indy"
|
|
23
|
+
u.email.should == "indy@home.com"
|
|
24
|
+
u.role.should == :dog
|
|
25
|
+
u.unique.should == :yep
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
it "should return valid attributes rather than the model (rails only)" do
|
|
29
|
+
attrs = Factory.attributes_for(:user)
|
|
30
|
+
attrs.class.should == Array
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
it "should clear all instance variables when reset" do
|
|
34
|
+
Factory.some_instance_variable = true
|
|
35
|
+
Factory.some_instance_variable.should == true
|
|
36
|
+
Factory.reset
|
|
37
|
+
Factory.some_instance_variable.should == nil
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
it "should be able to create items using the define helper or manually" do
|
|
41
|
+
m = Factory.build(:user_manually)
|
|
42
|
+
d = Factory.build(:user_by_define)
|
|
43
|
+
m.name.should == d.name
|
|
44
|
+
m.email.should == d.email
|
|
45
|
+
m.role.should == d.role
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
it "should be able to build by inheriting and overriding from other methods" do
|
|
49
|
+
a = Factory.build(:admin_manually)
|
|
50
|
+
a.name.should == "Fred"
|
|
51
|
+
a.role.should == :admin
|
|
52
|
+
b = Factory.build(:admin_by_define)
|
|
53
|
+
b.name.should == "Fred"
|
|
54
|
+
b.role.should == :admin
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
it "should create unique values using the n method" do
|
|
58
|
+
a = Factory.build(:user)
|
|
59
|
+
b = Factory.build(:user)
|
|
60
|
+
c = Factory.build(:user)
|
|
61
|
+
a.unique.should_not == b.unique
|
|
62
|
+
a.unique.should_not == c.unique
|
|
63
|
+
b.unique.should_not == c.unique
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
it "should raise an error if the factory produces an invalid object when debug is enabled (rails only)" do
|
|
67
|
+
Factory.debug = true
|
|
68
|
+
error = false
|
|
69
|
+
begin
|
|
70
|
+
Factory.build(:user)
|
|
71
|
+
rescue
|
|
72
|
+
error = true
|
|
73
|
+
end
|
|
74
|
+
error.should == true
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
end
|
|
78
|
+
|
data/spec/spec_helper.rb
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
require 'rubygems'
|
|
2
|
+
require 'spec'
|
|
3
|
+
require 'cranky'
|
|
4
|
+
|
|
5
|
+
# A basic model to crank out
|
|
6
|
+
class User
|
|
7
|
+
attr_accessor :name
|
|
8
|
+
attr_accessor :role
|
|
9
|
+
attr_accessor :email
|
|
10
|
+
attr_accessor :unique
|
|
11
|
+
|
|
12
|
+
def save
|
|
13
|
+
@saved = true
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def saved?
|
|
17
|
+
!!@saved
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def valid?
|
|
21
|
+
false
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def errors
|
|
25
|
+
"some validation errors"
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def attributes
|
|
29
|
+
self.instance_variables
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Some basic factory methods
|
|
35
|
+
class Cranky
|
|
36
|
+
|
|
37
|
+
attr_accessor :some_instance_variable
|
|
38
|
+
|
|
39
|
+
def user_manually
|
|
40
|
+
u = User.new
|
|
41
|
+
u.name = "Fred"
|
|
42
|
+
u.role = options[:role] || :user
|
|
43
|
+
u.unique = "value#{n}"
|
|
44
|
+
u.email = "fred@home.com"
|
|
45
|
+
u
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def user_by_define
|
|
49
|
+
define :class => :user,
|
|
50
|
+
:name => "Fred",
|
|
51
|
+
:role => :user,
|
|
52
|
+
:unique => "value#{n}",
|
|
53
|
+
:email => "fred@home.com"
|
|
54
|
+
end
|
|
55
|
+
alias :user :user_by_define
|
|
56
|
+
|
|
57
|
+
def admin_manually
|
|
58
|
+
inherit(:user_manually, :role => :admin)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def admin_by_define
|
|
62
|
+
inherit(:user_by_define, :role => :admin)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
|
metadata
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: cranky
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
prerelease: false
|
|
5
|
+
segments:
|
|
6
|
+
- 0
|
|
7
|
+
- 0
|
|
8
|
+
- 1
|
|
9
|
+
version: 0.0.1
|
|
10
|
+
platform: ruby
|
|
11
|
+
authors:
|
|
12
|
+
- Stephen McGinty
|
|
13
|
+
autorequire:
|
|
14
|
+
bindir: bin
|
|
15
|
+
cert_chain: []
|
|
16
|
+
|
|
17
|
+
date: 2010-05-22 00:00:00 -05:00
|
|
18
|
+
default_executable:
|
|
19
|
+
dependencies: []
|
|
20
|
+
|
|
21
|
+
description: A simple yet powerful test factory framework that makes it very easy to define your own factories without a block in sight.
|
|
22
|
+
email: ginty@hyperdecade.com
|
|
23
|
+
executables: []
|
|
24
|
+
|
|
25
|
+
extensions: []
|
|
26
|
+
|
|
27
|
+
extra_rdoc_files: []
|
|
28
|
+
|
|
29
|
+
files:
|
|
30
|
+
- lib/cranky.rb
|
|
31
|
+
- spec/cranky_spec.rb
|
|
32
|
+
- spec/spec_helper.rb
|
|
33
|
+
- README.rdoc
|
|
34
|
+
- Gemfile
|
|
35
|
+
- LICENSE
|
|
36
|
+
- init.rb
|
|
37
|
+
has_rdoc: true
|
|
38
|
+
homepage: http://github.com/ginty/cranky
|
|
39
|
+
licenses: []
|
|
40
|
+
|
|
41
|
+
post_install_message:
|
|
42
|
+
rdoc_options: []
|
|
43
|
+
|
|
44
|
+
require_paths:
|
|
45
|
+
- lib
|
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
47
|
+
requirements:
|
|
48
|
+
- - ">="
|
|
49
|
+
- !ruby/object:Gem::Version
|
|
50
|
+
segments:
|
|
51
|
+
- 0
|
|
52
|
+
version: "0"
|
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
54
|
+
requirements:
|
|
55
|
+
- - ">="
|
|
56
|
+
- !ruby/object:Gem::Version
|
|
57
|
+
segments:
|
|
58
|
+
- 1
|
|
59
|
+
- 3
|
|
60
|
+
- 4
|
|
61
|
+
version: 1.3.4
|
|
62
|
+
requirements: []
|
|
63
|
+
|
|
64
|
+
rubyforge_project:
|
|
65
|
+
rubygems_version: 1.3.6
|
|
66
|
+
signing_key:
|
|
67
|
+
specification_version: 3
|
|
68
|
+
summary: A simple yet powerful test factory framework.
|
|
69
|
+
test_files: []
|
|
70
|
+
|