Skip to content

Commit

Permalink
tests for adding traits
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermesilveira committed Oct 26, 2010
1 parent 7f32e5e commit 7232183
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 19 deletions.
9 changes: 9 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# A sample Gemfile
source :gemcutter
#
gem "rails", ">= 3.0.0"

group :test do
gem "rspec-rails", ">= 2.0.0.beta.19"
gem "rcov"
end
91 changes: 91 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
GEM
remote: http://rubygems.org/
specs:
abstract (1.0.0)
actionmailer (3.0.0)
actionpack (= 3.0.0)
mail (~> 2.2.5)
actionpack (3.0.0)
activemodel (= 3.0.0)
activesupport (= 3.0.0)
builder (~> 2.1.2)
erubis (~> 2.6.6)
i18n (~> 0.4.1)
rack (~> 1.2.1)
rack-mount (~> 0.6.12)
rack-test (~> 0.5.4)
tzinfo (~> 0.3.23)
activemodel (3.0.0)
activesupport (= 3.0.0)
builder (~> 2.1.2)
i18n (~> 0.4.1)
activerecord (3.0.0)
activemodel (= 3.0.0)
activesupport (= 3.0.0)
arel (~> 1.0.0)
tzinfo (~> 0.3.23)
activeresource (3.0.0)
activemodel (= 3.0.0)
activesupport (= 3.0.0)
activesupport (3.0.0)
arel (1.0.1)
activesupport (~> 3.0.0)
builder (2.1.2)
diff-lcs (1.1.2)
erubis (2.6.6)
abstract (>= 1.0.0)
i18n (0.4.1)
mail (2.2.5)
activesupport (>= 2.3.6)
mime-types
treetop (>= 1.4.5)
mime-types (1.16)
nokogiri (1.4.3.1)
polyglot (0.3.1)
rack (1.2.1)
rack-mount (0.6.13)
rack (>= 1.0.0)
rack-test (0.5.6)
rack (>= 1.0)
rails (3.0.0)
actionmailer (= 3.0.0)
actionpack (= 3.0.0)
activerecord (= 3.0.0)
activeresource (= 3.0.0)
activesupport (= 3.0.0)
bundler (~> 1.0.0)
railties (= 3.0.0)
railties (3.0.0)
actionpack (= 3.0.0)
activesupport (= 3.0.0)
rake (>= 0.8.4)
thor (~> 0.14.0)
rake (0.8.7)
rcov (0.9.8)
rspec (2.0.0.beta.19)
rspec-core (= 2.0.0.beta.19)
rspec-expectations (= 2.0.0.beta.19)
rspec-mocks (= 2.0.0.beta.19)
rspec-core (2.0.0.beta.19)
rspec-expectations (2.0.0.beta.19)
diff-lcs (>= 1.1.2)
rspec-mocks (2.0.0.beta.19)
rspec-rails (2.0.0.beta.19)
rspec (= 2.0.0.beta.19)
webrat (>= 0.7.2.beta.1)
thor (0.14.0)
treetop (1.4.8)
polyglot (>= 0.3.1)
tzinfo (0.3.23)
webrat (0.7.2.beta.1)
nokogiri (>= 1.2.0)
rack (>= 1.0)
rack-test (>= 0.5.3)

PLATFORMS
ruby

DEPENDENCIES
rails (>= 3.0.0)
rcov
rspec-rails (>= 2.0.0.beta.19)
16 changes: 6 additions & 10 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,12 @@ rescue LoadError
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
end

require 'spec/rake/spectask'
Spec::Rake::SpecTask.new(:spec) do |spec|
spec.libs << 'lib' << 'spec'
spec.spec_files = FileList['spec/**/*_spec.rb']
end

Spec::Rake::SpecTask.new(:rcov) do |spec|
spec.libs << 'lib' << 'spec'
spec.pattern = 'spec/**/*_spec.rb'
spec.rcov = true
require 'rspec'
require 'rspec/core'
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec) do |t|
# t.spec_files = FileList['spec_*.rb']
t.rspec_opts = ['--colour', '--format progress']
end

task :spec => :check_dependencies
Expand Down
15 changes: 15 additions & 0 deletions lib/respondie.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'action_controller'

module Respondie

def use_trait(&block)
Respondie::Builder.new("Respondie::Trait::$trait$", self).instance_eval(&block)
end

alias_method :use_traits, :use_trait

end

ActionController::Base.extend Respondie

require 'respondie/builder'
22 changes: 22 additions & 0 deletions lib/respondie/builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Respondie::Builder

def initialize(pattern, controller)
@pattern = pattern
custom = Class.new(ActionController::Responder)
controller.responder = custom
@responder = controller.responder
end

def method_missing(sym)
trait sym
end

private

def trait(sym)
type = @pattern.gsub(/\$trait\$/, sym.to_s.camelize)
@responder.send :include, type.constantize
self
end

end
24 changes: 24 additions & 0 deletions spec/respondie/builder_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

class ProductsController
attr_accessor :responder
end
module Respondie
module Trait
module Cute
def is_cute?
true
end
end
end
end

describe Respondie::Builder do

it "should include a type into a responder" do
controller = ProductsController.new
Respondie::Builder.new("Respondie::Trait::$trait$", controller).instance_eval { cute }
controller.responder.instance_method(:is_cute?).should_not be_nil
end

end
18 changes: 15 additions & 3 deletions spec/respondie_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')

describe "Respondie" do
it "fails" do
fail "hey buddy, you should probably rename this file and start specing for real"
class ApplicationController < ActionController::Base
end

class MyController < ApplicationController
end

describe Respondie do

it "should enhance ActionController with its cute method" do
MyController.should respond_to(:use_trait)
end

it "should enhance ActionController with its cute plural method" do
MyController.should respond_to(:use_traits)
end

end
8 changes: 2 additions & 6 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
$LOAD_PATH.unshift(File.dirname(__FILE__))
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
require 'respondie'
require 'spec'
require 'spec/autorun'

Spec::Runner.configure do |config|

end
require 'rspec'
require 'rspec/autorun'

0 comments on commit 7232183

Please sign in to comment.