Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Ginty committed May 22, 2010
0 parents commit 03e61e8
Show file tree
Hide file tree
Showing 10 changed files with 311 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
*.swp
*.swo
.*
coverage/*
11 changes: 11 additions & 0 deletions Gemfile
@@ -0,0 +1,11 @@
source "http://gemcutter.org"

group :development do
gem "gemcutter"
end

group :test do
gem "rspec", "1.3.0"
gem "rcov"
end

20 changes: 20 additions & 0 deletions LICENSE
@@ -0,0 +1,20 @@
Copyright (c) 2010 Stephen McGinty

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Empty file added README.rdoc
Empty file.
18 changes: 18 additions & 0 deletions cranky.gemspec
@@ -0,0 +1,18 @@
require "lib/cranky"

Gem::Specification.new do |gem|
gem.name = 'cranky'
gem.version = Cranky::VERSION

gem.summary = "A simple yet powerful test factory framework."
gem.description = "A simple yet powerful test factory framework that makes it very easy to define your own factories without a block in sight."

gem.author = "Stephen McGinty"
gem.email = "ginty@hyperdecade.com"
gem.homepage = "http://github.com/ginty/cranky"

gem.files = Dir["{lib,spec}/**/*", "[A-Z]*", "init.rb"]
gem.require_path = "lib"

gem.required_rubygems_version = ">= 1.3.4"
end
2 changes: 2 additions & 0 deletions init.rb
@@ -0,0 +1,2 @@
require 'crank_it'

87 changes: 87 additions & 0 deletions lib/cranky.rb
@@ -0,0 +1,87 @@
class Cranky

VERSION = "0.0.1"

# Dir.glob("#{File.expand_path(File.dirname(__FILE__))}/*.rb").each do |file|
# require file
# # Auto include all modules found in the directory
# file =~ /.*[\/\\](.*)\.rb/
# begin
# include $1.to_s.camelcase.constantize
# rescue
# end
# end

attr_accessor :debug

def initialize
@what = []
@attrs = []
@n = 0
end

def build(what, attrs={})
crank_it(what, false, attrs)
end

def create(what, attrs={})
crank_it(what, true, attrs)
end

def reset
self.instance_variables.each do |var|
instance_variable_set(var, nil)
end
initialize
end

def attributes_for(what, attrs={})
build(what, attrs).attributes
end

private

def n
@n += 1
end

def inherit(what, attrs={})
build(what, attrs.merge(options))
end

def crank_it(what, save, attrs)
@attrs << attrs; @what << what
item = self.send(what)
@attrs.pop; @what.pop
if @debug && !item.valid?
raise "Oops, the #{what} created by the Factory has the following errors: #{item.errors}"
end
item.save if save
item
end

def define(attrs={})
final_attrs = attrs.merge(@attrs.last)
#item = (attrs[:class] ? attrs[:class] : @what.last).to_s.camelcase.constantize.new
item = get_constant(attrs[:class] ? attrs[:class] : @what.last).new
final_attrs.delete(:class)
final_attrs.each do |attr, value|
item.send("#{attr}=", value)
end
item
end

# Nicked from here: http://gist.github.com/301173
def get_constant(name_sym)
name = name_sym.to_s.split('_').collect {|s| s.capitalize }.join('')
Object.const_defined?(name) ? Object.const_get(name) : Object.const_missing(name)
end

def options
@attrs.last
end

end

Factory = Cranky.new unless defined?(Factory)

23 changes: 23 additions & 0 deletions rakefile
@@ -0,0 +1,23 @@
require 'lib/cranky'
require 'rubygems'
require 'spec/rake/spectask'

desc "Run specs"
Spec::Rake::SpecTask.new(:spec) do |t|
t.spec_files = Dir.glob("spec/**/*_spec.rb")
t.spec_opts = ["-c"]
t.rcov = true
end

task :default => :spec

task :build do
system "gem build cranky.gemspec"
end

desc "Release version #{Cranky::VERSION} to Rubygems.org"
task :release => :build do
system "gem push cranky-#{Cranky::VERSION}.gem"
end


78 changes: 78 additions & 0 deletions spec/cranky_spec.rb
@@ -0,0 +1,78 @@
require "spec_helper"

describe Cranky do

before(:each) do
end

it "should be alive" do
Factory.build(:user).class.should == User
end

it "should not save the item when generated via build" do
Factory.build(:user).saved?.should == false
end

it "should save the item when generated via create" do
Factory.create(:user).saved?.should == true
end

it "should allow all attributes to be overriden in the call" do
u = Factory.build(:user, :name => "Indy", :email => "indy@home.com", :role => :dog, :unique => :yep)
u.name.should == "Indy"
u.email.should == "indy@home.com"
u.role.should == :dog
u.unique.should == :yep
end

it "should return valid attributes rather than the model (rails only)" do
attrs = Factory.attributes_for(:user)
attrs.class.should == Array
end

it "should clear all instance variables when reset" do
Factory.some_instance_variable = true
Factory.some_instance_variable.should == true
Factory.reset
Factory.some_instance_variable.should == nil
end

it "should be able to create items using the define helper or manually" do
m = Factory.build(:user_manually)
d = Factory.build(:user_by_define)
m.name.should == d.name
m.email.should == d.email
m.role.should == d.role
end

it "should be able to build by inheriting and overriding from other methods" do
a = Factory.build(:admin_manually)
a.name.should == "Fred"
a.role.should == :admin
b = Factory.build(:admin_by_define)
b.name.should == "Fred"
b.role.should == :admin
end

it "should create unique values using the n method" do
a = Factory.build(:user)
b = Factory.build(:user)
c = Factory.build(:user)
a.unique.should_not == b.unique
a.unique.should_not == c.unique
b.unique.should_not == c.unique
end

it "should raise an error if the factory produces an invalid object when debug is enabled (rails only)" do
Factory.debug = true
error = false
begin
Factory.build(:user)
rescue
error = true
end
error.should == true
end

end

68 changes: 68 additions & 0 deletions spec/spec_helper.rb
@@ -0,0 +1,68 @@
require 'rubygems'
require 'spec'
require 'cranky'

# A basic model to crank out
class User
attr_accessor :name
attr_accessor :role
attr_accessor :email
attr_accessor :unique

def save
@saved = true
end

def saved?
!!@saved
end

def valid?
false
end

def errors
"some validation errors"
end

def attributes
self.instance_variables
end

end

# Some basic factory methods
class Cranky

attr_accessor :some_instance_variable

def user_manually
u = User.new
u.name = "Fred"
u.role = options[:role] || :user
u.unique = "value#{n}"
u.email = "fred@home.com"
u
end

def user_by_define
define :class => :user,
:name => "Fred",
:role => :user,
:unique => "value#{n}",
:email => "fred@home.com"
end
alias :user :user_by_define

def admin_manually
inherit(:user_manually, :role => :admin)
end

def admin_by_define
inherit(:user_by_define, :role => :admin)
end

end



0 comments on commit 03e61e8

Please sign in to comment.