Doa 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *~
2
+ *.bak
3
+ doc/api
4
+ doc/spec.html
5
+ pkg/*
data/Doa.gemspec ADDED
@@ -0,0 +1,52 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{Doa}
8
+ s.version = "0.1.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Nolan Eakins <sneakin@semanticgap.com>"]
12
+ s.date = %q{2009-10-21}
13
+ s.description = %q{rSpec controller macros}
14
+ s.email = %q{suppert@semanticgap.com}
15
+ s.extra_rdoc_files = [
16
+ "README.rdoc"
17
+ ]
18
+ s.files = [
19
+ ".gitignore",
20
+ "Doa.gemspec",
21
+ "README.rdoc",
22
+ "Rakefile",
23
+ "VERSION",
24
+ "lib/doa.rb",
25
+ "lib/doa/class_methods.rb",
26
+ "lib/doa/instance_methods.rb",
27
+ "spec/application.rb",
28
+ "spec/doa_spec.rb",
29
+ "spec/spec.opts",
30
+ "spec/spec_helper.rb"
31
+ ]
32
+ s.homepage = %q{http://github.com/sneakin/Doa}
33
+ s.rdoc_options = ["--charset=UTF-8"]
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = %q{1.3.5}
36
+ s.summary = %q{The Tester}
37
+ s.test_files = [
38
+ "spec/application.rb",
39
+ "spec/doa_spec.rb",
40
+ "spec/spec_helper.rb"
41
+ ]
42
+
43
+ if s.respond_to? :specification_version then
44
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
45
+ s.specification_version = 3
46
+
47
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
48
+ else
49
+ end
50
+ else
51
+ end
52
+ end
data/README.rdoc ADDED
@@ -0,0 +1,69 @@
1
+ = Doa (the tester)
2
+
3
+ == Introduction
4
+
5
+ Doa provides macros that makes Rails' controller specs more understandable and
6
+ drier. It provides methods to provide the context for a controller's action,
7
+ the params to be used in a given context, and the means to easily call the
8
+ action using those params.
9
+
10
+
11
+ == Usage
12
+
13
+ Use `rake rdoc` to generate the RDocs which provides a complete example in the
14
+ Doa module. You can run the specs, which are by example, with `rake spec`.
15
+
16
+
17
+ == Example
18
+
19
+ require 'doa'
20
+
21
+ Doa.install!
22
+ Doa.default_url_params = { :format => 'xml' }
23
+
24
+ describe Controller do
25
+ action :show do
26
+ params do
27
+ { :id => 123 }
28
+ end
29
+
30
+ it "does something" do
31
+ doa(:page => 1)
32
+ end
33
+ end
34
+ end
35
+
36
+
37
+ == Legal
38
+
39
+ === Copyright
40
+
41
+ Copyright (C) 2009 SemanticGap(R)
42
+
43
+ Permission is hereby granted, free of charge, to any person
44
+ obtaining a copy of this software and associated documentation
45
+ files (the "Software"), to deal in the Software without
46
+ restriction, including without limitation the rights to use,
47
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
48
+ copies of the Software, and to permit persons to whom the
49
+ Software is furnished to do so, subject to the following
50
+ conditions:
51
+
52
+ The above copyright notice and this permission notice shall be
53
+ included in all copies or substantial portions of the Software.
54
+
55
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
56
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
57
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
58
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
59
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
60
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
61
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
62
+ OTHER DEALINGS IN THE SOFTWARE.
63
+
64
+
65
+ === Trademarks
66
+
67
+ SemanticGap is a registered trademark of Nolan Eakins. All
68
+ rights reserved.
69
+
data/Rakefile ADDED
@@ -0,0 +1,34 @@
1
+ require 'spec/rake/spectask'
2
+
3
+ task :default => [ :spec, :rdoc ]
4
+
5
+ desc "Run all specs in the spec directory"
6
+ Spec::Rake::SpecTask.new(:spec) do |t|
7
+ t.spec_opts = [ '--options', File.join(File.dirname(__FILE__), "spec", "spec.opts") ]
8
+ t.spec_files = FileList['spec/**/*_spec.rb']
9
+ end
10
+
11
+ require 'rake/rdoctask'
12
+
13
+ Rake::RDocTask.new do |rdoc|
14
+ rdoc.rdoc_files.add("README.rdoc", "lib/**/*.rb")
15
+ rdoc.main = 'README.rdoc'
16
+ rdoc.title = 'Doa'
17
+ rdoc.rdoc_dir = 'doc/api'
18
+ rdoc.options << '--line-numbers' << '--inline-source'
19
+ end
20
+
21
+ begin
22
+ require 'jeweler'
23
+ Jeweler::Tasks.new do |gemspec|
24
+ gemspec.name = "Doa"
25
+ gemspec.summary = "The Tester"
26
+ gemspec.description = "rSpec controller macros"
27
+ gemspec.email = "suppert@semanticgap.com"
28
+ gemspec.homepage = "http://github.com/sneakin/Doa"
29
+ gemspec.authors = [ "Nolan Eakins <sneakin@semanticgap.com>" ]
30
+ end
31
+ Jeweler::GemcutterTasks.new
32
+ rescue LoadError
33
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
34
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.1
@@ -0,0 +1,78 @@
1
+ # Copyright (C) 2009 SemanticGap(R)
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person
4
+ # obtaining a copy of this software and associated documentation
5
+ # files (the "Software"), to deal in the Software without
6
+ # restriction, including without limitation the rights to use,
7
+ # copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ # copies of the Software, and to permit persons to whom the
9
+ # Software is furnished to do so, subject to the following
10
+ # conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ # OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ module Doa
25
+ # Methods available at the context level.
26
+ module ClassMethods
27
+ # Initialize the parameters to be used within a context inheriting
28
+ # those of the surrounding contexts. Takes a block which must
29
+ # return a Hash.
30
+ #
31
+ # Usage:
32
+ # params do
33
+ # { :id => @user.id }
34
+ # end
35
+ def params(&block)
36
+ self.default_params = block
37
+ end
38
+
39
+ # Setup a context to test a controller's action using #do_action.
40
+ # It takes the action's name and an optional HTTP method,
41
+ # evaluating the supplied block in the created context.
42
+ def action(name, method = nil, &block)
43
+ d = describe "\##{name}" do
44
+ do_action name, method
45
+ end
46
+
47
+ d.instance_eval(&block)
48
+ d
49
+ end
50
+
51
+ # Infrastructure method used by #action to generate the methods
52
+ # available within a test case.
53
+ def do_action(action_name, method = nil, params = Hash.new, &block)
54
+ # Locking in the arguments with closures
55
+ define_method("action_method") do
56
+ method || infer_method(action_name)
57
+ end
58
+
59
+ define_method("action_name") do
60
+ action_name
61
+ end
62
+
63
+ define_method("shared_params") do
64
+ return instance_eval(&block) if block
65
+ return Hash.new
66
+ end
67
+
68
+ define_method("action_params") do
69
+ params
70
+ end
71
+
72
+ # Now the methods we really want to use
73
+ class_eval do
74
+ include Doa::InstanceMethods
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,70 @@
1
+ # Copyright (C) 2009 SemanticGap(R)
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person
4
+ # obtaining a copy of this software and associated documentation
5
+ # files (the "Software"), to deal in the Software without
6
+ # restriction, including without limitation the rights to use,
7
+ # copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ # copies of the Software, and to permit persons to whom the
9
+ # Software is furnished to do so, subject to the following
10
+ # conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ # OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ module Doa
25
+ # Methods available inside individual cases.
26
+ module InstanceMethods
27
+ # Returns the parameters to be used in a given test case.
28
+ # The parameters are the result of merging the parameters from
29
+ # the top most context down to the current one.
30
+ def params
31
+ return @_params if @_params
32
+
33
+ p = Doa.default_params.try(:clone) ||
34
+ HashWithIndifferentAccess.new
35
+
36
+ p.recursive_merge!(super_params)
37
+ p.recursive_merge!(action_params)
38
+ p.recursive_merge!(self.instance_eval(&default_params)) if default_params
39
+ p.recursive_merge!(shared_params)
40
+
41
+ @_params = HashWithIndifferentAccess.new(p)
42
+ end
43
+
44
+ # Perform the action using the parameters that result from
45
+ # merging the context's parameters with those that were
46
+ # passed as an argument.
47
+ def doa(per_call_params = Hash.new)
48
+ send(action_method, action_name,
49
+ self.params.recursive_merge(per_call_params))
50
+ end
51
+
52
+ # Alias for #doa
53
+ def do_action(*args)
54
+ doa(*args)
55
+ end
56
+
57
+ private
58
+
59
+ # Merges in defaults from each parent class
60
+ def super_params
61
+ supers.reverse.inject(Hash.new) do |acc, klass|
62
+ next acc unless klass.respond_to?(:default_params)
63
+
64
+ dp = klass.default_params
65
+ acc.recursive_merge!(self.instance_eval(&dp) || Hash.new) if dp
66
+ acc
67
+ end
68
+ end
69
+ end
70
+ end
data/lib/doa.rb ADDED
@@ -0,0 +1,95 @@
1
+ # Copyright (C) 2009 SemanticGap(R)
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person
4
+ # obtaining a copy of this software and associated documentation
5
+ # files (the "Software"), to deal in the Software without
6
+ # restriction, including without limitation the rights to use,
7
+ # copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ # copies of the Software, and to permit persons to whom the
9
+ # Software is furnished to do so, subject to the following
10
+ # conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ # OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ require 'facets/hash'
25
+ require 'doa/class_methods'
26
+ require 'doa/instance_methods'
27
+
28
+ # =Setup
29
+ #
30
+ # To use this, you should place the following in your "spec/spec_helper.rb":
31
+ #
32
+ # require 'doa'
33
+ # Doa.install!
34
+ # Doa.default_params = { :css => 'desktop', :format => 'html' }
35
+ #
36
+ # =Usage
37
+ #
38
+ # And your specs can then resemble:
39
+ #
40
+ # describe SomeController do
41
+ # action :update do
42
+ # before(:each) do
43
+ # @person = people(:bob)
44
+ # end
45
+ #
46
+ # params do
47
+ # { :id => @person, :name => 'Robert' }
48
+ # end
49
+ #
50
+ # it "updates bob" do
51
+ # Person.should_receive(:find).with(params[:id]).and_return(@person)
52
+ # lambda { doa }.should change(@person)
53
+ # end
54
+ # end
55
+ # end
56
+ #
57
+ module Doa
58
+ mattr_accessor :default_params
59
+
60
+ def self.included(base)
61
+ base.class_inheritable_accessor :default_params
62
+ base.extend(ClassMethods)
63
+ end
64
+
65
+ # Install Doa into RSpec's ControllerExampleGroup.
66
+ def self.install!
67
+ Spec::Rails::Example::ControllerExampleGroup.class_eval do
68
+ include Doa
69
+ end
70
+ end
71
+
72
+ private
73
+
74
+ # Given an action name such as :create, it returns the expected
75
+ # HTTP method.
76
+ def infer_method(action)
77
+ case action
78
+ when 'update' then :put
79
+ when 'create' then :post
80
+ else :get
81
+ end
82
+ end
83
+
84
+ # Returns the superclasses of an object
85
+ def supers
86
+ klass = self.class
87
+ supers = Array.new
88
+ while(klass)
89
+ supers << klass
90
+ klass = klass.superclass
91
+ end
92
+
93
+ supers
94
+ end
95
+ end
@@ -0,0 +1,7 @@
1
+ # Placeholder
2
+
3
+ class ApplicationController < ActionController::Base
4
+ def update
5
+ @params = params
6
+ end
7
+ end
data/spec/doa_spec.rb ADDED
@@ -0,0 +1,64 @@
1
+ require File.join(File.dirname(__FILE__), "spec_helper")
2
+
3
+ describe Doa, :type => :controller do
4
+ ActionController::Routing::Routes.draw do |map|
5
+ map.resources :test, :controller => 'application'
6
+ end
7
+
8
+ controller_name :application
9
+
10
+ action :update do
11
+ before(:each) do
12
+ @person = 'person'
13
+ end
14
+
15
+ params do
16
+ { :id => @person, :name => 'Robert' }
17
+ end
18
+
19
+ context 'no extra params' do
20
+ it "assigns the params" do
21
+ doa
22
+
23
+ assigns[:params][:id].should == @person
24
+ assigns[:params][:name].should == 'Robert'
25
+ end
26
+ end
27
+
28
+ context 'per-call params' do
29
+ before(:each) do
30
+ doa(:name => 'Bob', :age => 21)
31
+ @params = assigns[:params]
32
+ end
33
+
34
+ it "includes the parent context's params" do
35
+ @params[:id].should == @person
36
+ end
37
+
38
+ it "includes the passed params" do
39
+ @params[:name].should == 'Bob'
40
+ @params[:age].should == '21'
41
+ end
42
+ end
43
+
44
+ context 'per-context params' do
45
+ params do
46
+ { 'name' => 'Bob', :age => 21 }
47
+ end
48
+
49
+ before(:each) do
50
+ do_action
51
+ @params = assigns[:params]
52
+ end
53
+
54
+ it "includes the parent context's params" do
55
+ @params[:id].should == @person
56
+ end
57
+
58
+ it "merges in the context's params with the parent's" do
59
+ @params[:name].should == 'Bob'
60
+ @params[:age].should == '21'
61
+ end
62
+ end
63
+ end
64
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,4 @@
1
+ --colour
2
+ --format progress
3
+ --format html:doc/spec.html
4
+ --require rubygems
@@ -0,0 +1,13 @@
1
+ $: << File.join(File.dirname(File.dirname(__FILE__)), 'lib')
2
+ $: << File.dirname(__FILE__)
3
+
4
+ require 'active_support'
5
+ require 'action_controller'
6
+ require 'action_controller/request'
7
+ require 'action_controller/rescue'
8
+ require 'initializer'
9
+
10
+ require 'spec/rails'
11
+ require 'doa'
12
+
13
+ Doa.install!
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: Doa
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Nolan Eakins <sneakin@semanticgap.com>
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-21 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: rSpec controller macros
17
+ email: suppert@semanticgap.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ files:
25
+ - .gitignore
26
+ - Doa.gemspec
27
+ - README.rdoc
28
+ - Rakefile
29
+ - VERSION
30
+ - lib/doa.rb
31
+ - lib/doa/class_methods.rb
32
+ - lib/doa/instance_methods.rb
33
+ - spec/application.rb
34
+ - spec/doa_spec.rb
35
+ - spec/spec.opts
36
+ - spec/spec_helper.rb
37
+ has_rdoc: true
38
+ homepage: http://github.com/sneakin/Doa
39
+ licenses: []
40
+
41
+ post_install_message:
42
+ rdoc_options:
43
+ - --charset=UTF-8
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ requirements: []
59
+
60
+ rubyforge_project:
61
+ rubygems_version: 1.3.5
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: The Tester
65
+ test_files:
66
+ - spec/application.rb
67
+ - spec/doa_spec.rb
68
+ - spec/spec_helper.rb