From 03e61e8c0a693be7a7b7f14e67c1831ae5aad297 Mon Sep 17 00:00:00 2001 From: Ginty Date: Sat, 22 May 2010 16:23:24 -0500 Subject: [PATCH] Initial commit --- .gitignore | 4 +++ Gemfile | 11 ++++++ LICENSE | 20 +++++++++++ README.rdoc | 0 cranky.gemspec | 18 ++++++++++ init.rb | 2 ++ lib/cranky.rb | 87 +++++++++++++++++++++++++++++++++++++++++++++ rakefile | 23 ++++++++++++ spec/cranky_spec.rb | 78 ++++++++++++++++++++++++++++++++++++++++ spec/spec_helper.rb | 68 +++++++++++++++++++++++++++++++++++ 10 files changed, 311 insertions(+) create mode 100644 .gitignore create mode 100644 Gemfile create mode 100644 LICENSE create mode 100644 README.rdoc create mode 100644 cranky.gemspec create mode 100644 init.rb create mode 100644 lib/cranky.rb create mode 100644 rakefile create mode 100644 spec/cranky_spec.rb create mode 100644 spec/spec_helper.rb diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cbb9f7c --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.swp +*.swo +.* +coverage/* diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..937e61f --- /dev/null +++ b/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 + diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..4c391d1 --- /dev/null +++ b/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. diff --git a/README.rdoc b/README.rdoc new file mode 100644 index 0000000..e69de29 diff --git a/cranky.gemspec b/cranky.gemspec new file mode 100644 index 0000000..2b9aace --- /dev/null +++ b/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 diff --git a/init.rb b/init.rb new file mode 100644 index 0000000..5f2d2ba --- /dev/null +++ b/init.rb @@ -0,0 +1,2 @@ +require 'crank_it' + diff --git a/lib/cranky.rb b/lib/cranky.rb new file mode 100644 index 0000000..8f882cc --- /dev/null +++ b/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) + diff --git a/rakefile b/rakefile new file mode 100644 index 0000000..ae5f714 --- /dev/null +++ b/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 + + diff --git a/spec/cranky_spec.rb b/spec/cranky_spec.rb new file mode 100644 index 0000000..846aade --- /dev/null +++ b/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 + diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..fba922e --- /dev/null +++ b/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 + + +