seamusabshere-on_test_spec 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008, Fingertips
2
+ - Manfred Stienstra <manfred@fngtps.com>
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ of this software and associated documentation files (the "Software"), to deal
6
+ in the Software without restriction, including without limitation the rights
7
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the Software is
9
+ furnished to do so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in
12
+ all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,27 @@
1
+ = On Test Spec
2
+
3
+ Makes testing Rails apps with test/spec easier and more fun. Created with a half eye on the test_spec_on_rails plugin, thanks!
4
+
5
+ == Usage
6
+
7
+ In your test/test_helper.rb:
8
+
9
+ require 'test/spec'
10
+ require 'test/spec/rails'
11
+ require 'test/spec/share'
12
+
13
+ == Installation
14
+
15
+ === As a gem
16
+
17
+ Configure the gem in config/environments/test.rb: (__not__ in config/environment.rb)
18
+
19
+ config.gem 'seamusabshere-on_test_spec', :lib => 'test/spec', :source => 'http://gems.github.com'
20
+
21
+ Install them using Rails' rake task:
22
+
23
+ $ rake gems:install
24
+
25
+ === In your vendor directory:
26
+
27
+ script/install plugin git://github.com/seamusabshere/on_test_spec.git
@@ -0,0 +1,53 @@
1
+ require 'active_support'
2
+
3
+ class Class
4
+ def add_allow_switch(method, options={})
5
+ default = options[:default] || false
6
+
7
+ class_eval do
8
+ cattr_accessor "allow_#{method}"
9
+ self.send("allow_#{method}=", default)
10
+
11
+ alias_method "original_#{method}", method
12
+
13
+ eval %{
14
+ def #{method}(*args)
15
+ if allow_#{method}
16
+ original_#{method}(*args)
17
+ else
18
+ raise RuntimeError, "You're trying to call `#{method}' on `#{self}', which you probably don't want in a test."
19
+ end
20
+ end
21
+ }, binding, __FILE__, __LINE__
22
+ end
23
+ end
24
+ end
25
+
26
+ class Module
27
+ def add_allow_switch(method, options={})
28
+ default = options[:default] || false
29
+
30
+ mattr_accessor "allow_#{method}"
31
+ send("allow_#{method}=", default)
32
+
33
+ unless respond_to?(:__metaclass___)
34
+ def __metaclass__
35
+ class << self; self; end
36
+ end
37
+ end
38
+
39
+ __metaclass__.class_eval do
40
+ alias_method "original_#{method}", method
41
+
42
+ eval %{
43
+ def #{method}(*args)
44
+ if allow_#{method}
45
+ original_#{method}(*args)
46
+ else
47
+ raise RuntimeError, "You're trying to call `#{method}' on `#{self}', which you probably don't want in a test."
48
+ end
49
+ end
50
+ }, binding, __FILE__, __LINE__
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,37 @@
1
+ module Test
2
+ module Spec
3
+ module Rails
4
+ module Controller
5
+ module ClassMethods
6
+ # Sets up the test environment before every functional test
7
+ def tests(controller_class)
8
+ setups << lambda { setup_request_environment(controller_class) }
9
+ end
10
+ end
11
+ module InstanceMethods
12
+ include Assertions
13
+
14
+ attr_reader :controller
15
+
16
+ # Sets up the test environment for functional tests
17
+ def setup_request_environment(controller_class)
18
+ controller_class.class_eval do
19
+ def rescue_action(e)
20
+ raise e
21
+ end
22
+ end
23
+ @controller = controller_class.new
24
+ @controller.request = @request = ActionController::TestRequest.new
25
+ @response = ActionController::TestResponse.new
26
+
27
+ @controller.params = {}
28
+ @controller.send(:initialize_current_url)
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+ ActiveSupport::TestCase.send(:extend, Test::Spec::Rails::Controller::ClassMethods)
37
+ ActiveSupport::TestCase.send(:include, Test::Spec::Rails::Controller::InstanceMethods)
@@ -0,0 +1,99 @@
1
+ module Test
2
+ module Spec
3
+ module Rails
4
+ module ShouldExpectations
5
+ include Assertions
6
+
7
+ # Test that we were redirected somewhere:
8
+ # should.redirect
9
+ #
10
+ # Test that we were redirected to a specific url:
11
+ # should.redirect :controller => 'foo', :action => 'bar'
12
+ # or:
13
+ # should.redirect_to :controller => 'foo', :action => 'bar', :secure => true
14
+ def redirect(*args)
15
+ if args.empty?
16
+ assert_response @object.response.redirected_to, :redirect
17
+ elsif args.length == 1 and args.first.is_a?(String)
18
+ assert_equal args.first, @object.response.redirected_to
19
+ else
20
+ options = args.extract_options!
21
+ if secure = options.delete(:secure)
22
+ unless secure == true or secure == false
23
+ raise ArgumentError, ":secure option should be a boolean"
24
+ end
25
+ end
26
+
27
+ @object.instance_eval { assert_redirected_to *args }
28
+ if secure == true
29
+ assert @object.response.redirected_to.starts_with?('https:')
30
+ elsif secure == false
31
+ assert @object.response.redirected_to.starts_with?('http:')
32
+ end
33
+ end
34
+ end
35
+ alias :redirect_to :redirect
36
+
37
+ # Test that the object is valid
38
+ def validate
39
+ assert_valid @object
40
+ end
41
+
42
+ # Tests whether the evaluation of the expression changes.
43
+ #
44
+ # lambda { Norm.create }.should.differ('Norm.count')
45
+ # lambda { Norm.create; Norm.create }.should.differ('Norm.count', +2)
46
+ def differ(*args)
47
+ assert_difference(*args, &@object)
48
+ end
49
+ alias change differ
50
+
51
+ # Tests whether certain pages are cached.
52
+ #
53
+ # lambda { get :index }.should.cache_pages(posts_path)
54
+ # lambda { get :show, :id => post }.should.cache_pages(post_path(post), formatted_posts_path(:js, post))
55
+ def cache_pages(*pages, &block)
56
+ if block
57
+ block.call
58
+ else
59
+ @object.call
60
+ end
61
+ cache_dir = ActionController::Base.page_cache_directory
62
+ files = Dir.glob("#{cache_dir}/**/*").map do |filename|
63
+ filename[cache_dir.length..-1]
64
+ end
65
+ assert pages.all? { |page| files.include?(page) }
66
+ end
67
+
68
+ # Test two HTML strings for equivalency (e.g., identical up to reordering of attributes)
69
+ def dom_equal(expected)
70
+ assert_dom_equal expected, @object
71
+ end
72
+ end
73
+ module ShouldNotExpectations
74
+ include Assertions
75
+
76
+ # Test that an object is not valid
77
+ def validate
78
+ assert !@object.valid?
79
+ end
80
+
81
+ # Tests that the evaluation of the expression shouldn't change
82
+ #
83
+ # lambda { Norm.new }.should.not.differ('Norm.count')
84
+ def differ(*args)
85
+ assert_no_difference(*args, &@object)
86
+ end
87
+ alias change differ
88
+
89
+ # Test that two HTML strings are not equivalent
90
+ def dom_equal(expected)
91
+ assert_dom_not_equal expected, @object
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
97
+
98
+ Test::Spec::Should.send(:include, Test::Spec::Rails::ShouldExpectations)
99
+ Test::Spec::ShouldNot.send(:include, Test::Spec::Rails::ShouldNotExpectations)
@@ -0,0 +1,11 @@
1
+ module Test
2
+ module Spec
3
+ module Rails
4
+ module RequestHelpers
5
+ attr_reader :request
6
+ end
7
+ end
8
+ end
9
+ end
10
+
11
+ ActiveSupport::TestCase.send(:include, Test::Spec::Rails::RequestHelpers)
@@ -0,0 +1,42 @@
1
+ module Test
2
+ module Spec
3
+ module Rails
4
+ class Status < SpecResponder
5
+ def should_equal(status, message=nil)
6
+ @test_case.send(:assert_response, status, message)
7
+ end
8
+ end
9
+
10
+ class Template < SpecResponder
11
+ def should_equal(template, message=nil)
12
+ @test_case.send(:assert_template, template, message)
13
+ end
14
+ end
15
+
16
+ class Layout < SpecResponder
17
+ def should_equal(layout, message=nil)
18
+ rendered_layout = @test_case.response.layout.gsub(/layouts\//, '')
19
+ @test_case.send(:assert_equal, layout, rendered_layout, message)
20
+ end
21
+ end
22
+
23
+ module ResponseHelpers
24
+ attr_reader :response
25
+
26
+ def status
27
+ Test::Spec::Rails::Status.new(self)
28
+ end
29
+
30
+ def template
31
+ Test::Spec::Rails::Template.new(self)
32
+ end
33
+
34
+ def layout
35
+ Test::Spec::Rails::Layout.new(self)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ ActiveSupport::TestCase.send(:include, Test::Spec::Rails::ResponseHelpers)
@@ -0,0 +1,12 @@
1
+ module Test
2
+ module Spec
3
+ module Rails
4
+ class SpecResponder
5
+ attr_accessor :test_case
6
+ def initialize(test_case)
7
+ self.test_case = test_case
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,23 @@
1
+ class Test::Spec::Should
2
+ alias :_test_spec_equal :equal
3
+ def equal(*args)
4
+ @object.respond_to?(:should_equal) ? @object.should_equal(*args) : _test_spec_equal(*args)
5
+ end
6
+
7
+ alias :_test_spec_be :be
8
+ def be(*args)
9
+ @object.respond_to?(:should_equal) ? @object.should_equal(*args) : _test_spec_be(*args)
10
+ end
11
+ end
12
+
13
+ class Test::Spec::ShouldNot
14
+ alias :_test_spec_equal :equal
15
+ def equal(*args)
16
+ @object.respond_to?(:should_not_equal) ? @object.should_not_equal(*args) : _test_spec_equal(*args)
17
+ end
18
+
19
+ alias :_test_spec_be :be
20
+ def be(*args)
21
+ @object.respond_to?(:should_not_equal) ? @object.should_not_equal(*args) : _test_spec_be(*args)
22
+ end
23
+ end
@@ -0,0 +1,41 @@
1
+ require 'test/spec'
2
+
3
+ module Test
4
+ module Spec
5
+ module Rails
6
+ module Assertions
7
+ include ActiveSupport::Testing::Assertions
8
+ include ActionController::TestCase::Assertions
9
+ end
10
+ end
11
+
12
+ class Should
13
+ include Rails::Assertions
14
+ end
15
+
16
+ class ShouldNot
17
+ include Rails::Assertions
18
+ end
19
+ end
20
+ end
21
+
22
+ %w(test_spec_ext spec_responder expectations).each { |lib| require "test/spec/rails/#{lib}" }
23
+ Dir[File.dirname(__FILE__) + '/rails/**/*_helpers.rb'].each { |lib| require lib }
24
+
25
+ module Kernel
26
+ alias :context_before_on_test_spec :context
27
+ alias :xcontext_before_on_test_spec :xcontext
28
+
29
+ def context(name, superclass=ActiveSupport::TestCase, klass=Test::Spec::TestCase, &block)
30
+ context_before_on_test_spec(name, superclass, klass, &block)
31
+ end
32
+
33
+ def xcontext(name, superclass=ActiveSupport::TestCase, &block)
34
+ xcontext_before_on_test_spec(name, superclass, &block)
35
+ end
36
+
37
+ private :context, :xcontext
38
+
39
+ alias :describe :context
40
+ alias :xdescribe :xcontext
41
+ end
@@ -0,0 +1,48 @@
1
+ $shared_specs = {}
2
+
3
+ module Kernel
4
+ # Stores the passed in block for inclusion in test cases.
5
+ #
6
+ # share "User" do
7
+ # it "should authenticate" do
8
+ # "me".should == "me"
9
+ # end
10
+ # end
11
+ #
12
+ # describe "User, in general" do
13
+ # shared_specs_for 'User'
14
+ # end
15
+ #
16
+ # describe "User, in another case" do
17
+ # shared_specs_for 'User'
18
+ # end
19
+ #
20
+ # 2 tests, 2 assertions, 0 failures, 0 errors
21
+ def share(name, &specs_block)
22
+ $shared_specs[name] = specs_block
23
+ end
24
+ end
25
+
26
+ module SharedSpecsInclusionHelper
27
+ # Include the specified shared specs in this test case.
28
+ #
29
+ # share "User" do
30
+ # it "should authenticate" do
31
+ # "me".should == "me"
32
+ # end
33
+ # end
34
+ #
35
+ # describe "User, in general" do
36
+ # shared_specs_for 'User'
37
+ # end
38
+ #
39
+ # describe "User, in another case" do
40
+ # shared_specs_for 'User'
41
+ # end
42
+ #
43
+ # 2 tests, 2 assertions, 0 failures, 0 errors
44
+ def shared_specs_for(name)
45
+ self.class_eval &$shared_specs[name]
46
+ end
47
+ end
48
+ Test::Unit::TestCase.send(:extend, SharedSpecsInclusionHelper)
@@ -0,0 +1,24 @@
1
+ Gem::Specification.new do |spec|
2
+ spec.name = "on_test_spec"
3
+ spec.version = "1.0.1"
4
+
5
+ spec.authors = [ "Manfred Stienstra", "Eloy Duran" ]
6
+ spec.email = "manfred@fngtps.com"
7
+
8
+ spec.description = <<-EOF
9
+ Makes testing Rails apps with test/spec easier and more fun. Created with a half eye on the test_spec_on_rails plugin, thanks!
10
+ EOF
11
+ spec.summary = <<-EOF
12
+ Makes testing Rails apps with test/spec easier and more fun. Created with a half eye on the test_spec_on_rails plugin, thanks!
13
+ EOF
14
+ spec.homepage = "https://fngtps.com/svn/rails-plugins/trunk/on_test_spec"
15
+
16
+ # Dir['[a-zA-Z]*'] + Dir['lib/**/*'] + Dir['test/**/*']
17
+ spec.files = ["lib", "LICENSE", "on_test_spec.gemspec", "README", "test", "lib/test", "lib/test/spec", "lib/test/spec/add_allow_switch.rb", "lib/test/spec/rails", "lib/test/spec/rails/controller_helpers.rb", "lib/test/spec/rails/expectations.rb", "lib/test/spec/rails/request_helpers.rb", "lib/test/spec/rails/response_helpers.rb", "lib/test/spec/rails/spec_responder.rb", "lib/test/spec/rails/test_spec_ext.rb", "lib/test/spec/rails.rb", "lib/test/spec/share.rb", "test/add_allow_switch_test.rb", "test/share_test.rb"]
18
+ # Dir['test/**/*']
19
+ spec.test_files = ["test/add_allow_switch_test.rb", "test/share_test.rb"]
20
+
21
+ spec.has_rdoc = true
22
+ spec.extra_rdoc_files = ['README', 'LICENSE']
23
+ spec.rdoc_options << "--charset=utf-8"
24
+ end
@@ -0,0 +1,104 @@
1
+ require 'rubygems' rescue LoadError
2
+
3
+ require 'test/spec'
4
+ require 'mocha'
5
+
6
+ require File.expand_path('../../lib/test/spec/add_allow_switch', __FILE__)
7
+
8
+ module Factory
9
+ def self.run
10
+ true
11
+ end
12
+ end
13
+ Factory.add_allow_switch :run, :default => true
14
+
15
+ describe "Factory with an allow switch on run" do
16
+ it "should alias the original method" do
17
+ Factory.respond_to?(:original_run, include_private=true).should == true
18
+ end
19
+
20
+ it "should define a getter and setter" do
21
+ Factory.should.respond_to(:allow_run)
22
+ Factory.should.respond_to(:allow_run=)
23
+ end
24
+
25
+ it "should switch off" do
26
+ Factory.allow_run = false
27
+ lambda {
28
+ Factory.run
29
+ }.should.raise(RuntimeError)
30
+ end
31
+
32
+ it "should switch on" do
33
+ Factory.allow_run = true
34
+ lambda {
35
+ Factory.run.should == true
36
+ }.should.not.raise
37
+ end
38
+ end
39
+
40
+ class Bunny
41
+ def hop
42
+ 'Hop hop!'
43
+ end
44
+ end
45
+ Bunny.add_allow_switch :hop
46
+
47
+ describe "Bunny with an allow switch on hop" do
48
+ before do
49
+ @bunny = Bunny.new
50
+ end
51
+
52
+ it "should alias the original method" do
53
+ @bunny.respond_to?(:original_hop).should == true
54
+ end
55
+
56
+ it "should define a getter and setter" do
57
+ Bunny.should.respond_to(:allow_hop)
58
+ Bunny.should.respond_to(:allow_hop=)
59
+ end
60
+
61
+ it "should switch off" do
62
+ Bunny.allow_hop = false
63
+ lambda {
64
+ @bunny.hop
65
+ }.should.raise(RuntimeError)
66
+ end
67
+
68
+ it "should switch on" do
69
+ Bunny.allow_hop = true
70
+ lambda {
71
+ @bunny.hop.should == 'Hop hop!'
72
+ }.should.not.raise
73
+ end
74
+ end
75
+
76
+
77
+ Kernel.add_allow_switch :system
78
+
79
+ describe "Kernel with an allow switch on system" do
80
+ SILENT_COMMANT = 'ls > /dev/null'
81
+
82
+ it "should alias the original method" do
83
+ Kernel.respond_to?(:original_system, include_private=true).should == true
84
+ end
85
+
86
+ it "should define a getter and setter" do
87
+ Factory.should.respond_to(:allow_system)
88
+ Factory.should.respond_to(:allow_system=)
89
+ end
90
+
91
+ it "should switch off" do
92
+ Kernel.allow_system = false
93
+ lambda {
94
+ Kernel.system(SILENT_COMMANT)
95
+ }.should.raise(RuntimeError)
96
+ end
97
+
98
+ it "should switch on" do
99
+ Kernel.allow_system = true
100
+ lambda {
101
+ Kernel.system(SILENT_COMMANT)
102
+ }.should.not.raise
103
+ end
104
+ end
@@ -0,0 +1,52 @@
1
+ require 'rubygems' rescue LoadError
2
+ require 'test/spec'
3
+
4
+ require File.expand_path('../../lib/test/spec/share', __FILE__)
5
+
6
+ class DummyMock
7
+ extend SharedSpecsInclusionHelper
8
+
9
+ class << self
10
+ attr_reader :times_called
11
+ def it(name, &block)
12
+ @times_called ||= 0
13
+ @times_called += 1
14
+ end
15
+ end
16
+ end
17
+
18
+ share "Dummy" do
19
+ it("spec 1") {}
20
+ it("spec 2") {}
21
+ end
22
+
23
+ describe "Shared specs" do
24
+ it "should define a global variable that will hold all the shared specs" do
25
+ $shared_specs.should.be.instance_of Hash
26
+ end
27
+ end
28
+
29
+ describe "Kernel#share" do
30
+ it "should add the shared specs to the global shared modules variable" do
31
+ before = $shared_specs.length
32
+ share("Bar") {}
33
+ $shared_specs.length.should == before + 1
34
+ end
35
+
36
+ it "should have stored the proc that holds the specs" do
37
+ $shared_specs['Dummy'].should.be.instance_of Proc
38
+ end
39
+ end
40
+
41
+ describe "SharedSpecsInclusionHelper::shared_specs_for" do
42
+ it "should have extended Test::Unit::TestCase" do
43
+ Test::Unit::TestCase.should.respond_to :shared_specs_for
44
+ end
45
+
46
+ it "should return the specified module containing the shared specs" do
47
+ DummyMock.class_eval do
48
+ shared_specs_for 'Dummy'
49
+ end
50
+ DummyMock.times_called.should.be 2
51
+ end
52
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: seamusabshere-on_test_spec
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Manfred Stienstra
8
+ - Eloy Duran
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2009-01-11 00:00:00 -08:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description: Makes testing Rails apps with test/spec easier and more fun. Created with a half eye on the test_spec_on_rails plugin, thanks!
18
+ email: manfred@fngtps.com
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files:
24
+ - README
25
+ - LICENSE
26
+ files:
27
+ - lib
28
+ - LICENSE
29
+ - on_test_spec.gemspec
30
+ - README
31
+ - test
32
+ - lib/test
33
+ - lib/test/spec
34
+ - lib/test/spec/add_allow_switch.rb
35
+ - lib/test/spec/rails
36
+ - lib/test/spec/rails/controller_helpers.rb
37
+ - lib/test/spec/rails/expectations.rb
38
+ - lib/test/spec/rails/request_helpers.rb
39
+ - lib/test/spec/rails/response_helpers.rb
40
+ - lib/test/spec/rails/spec_responder.rb
41
+ - lib/test/spec/rails/test_spec_ext.rb
42
+ - lib/test/spec/rails.rb
43
+ - lib/test/spec/share.rb
44
+ - test/add_allow_switch_test.rb
45
+ - test/share_test.rb
46
+ has_rdoc: true
47
+ homepage: https://fngtps.com/svn/rails-plugins/trunk/on_test_spec
48
+ post_install_message:
49
+ rdoc_options:
50
+ - --charset=utf-8
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "0"
64
+ version:
65
+ requirements: []
66
+
67
+ rubyforge_project:
68
+ rubygems_version: 1.2.0
69
+ signing_key:
70
+ specification_version: 2
71
+ summary: Makes testing Rails apps with test/spec easier and more fun. Created with a half eye on the test_spec_on_rails plugin, thanks!
72
+ test_files:
73
+ - test/add_allow_switch_test.rb
74
+ - test/share_test.rb