spicycode-micronaut-rails 0.0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/LICENSE ADDED
@@ -0,0 +1,45 @@
1
+ Copyright (c) 2008 Chad Humphries
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
22
+
23
+ The matchers and expectations are licensed:
24
+ (The MIT License)
25
+
26
+ Copyright (c) 2005-2008 The RSpec Development Team
27
+
28
+ Permission is hereby granted, free of charge, to any person obtaining
29
+ a copy of this software and associated documentation files (the
30
+ "Software"), to deal in the Software without restriction, including
31
+ without limitation the rights to use, copy, modify, merge, publish,
32
+ distribute, sublicense, and/or sell copies of the Software, and to
33
+ permit persons to whom the Software is furnished to do so, subject to
34
+ the following conditions:
35
+
36
+ The above copyright notice and this permission notice shall be
37
+ included in all copies or substantial portions of the Software.
38
+
39
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
40
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
41
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
42
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
43
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
44
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
45
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,17 @@
1
+ = Micronaut Rails
2
+
3
+ * http://github.com/spicycode/micronaut-rails
4
+
5
+ == DESCRIPTION:
6
+
7
+ Micronaut Rails is a set of extension to deal with rails mode testing. For the moment think of it is a prototype of
8
+ RSpec lite. Definitely pre alpha at the moment.
9
+
10
+ == REQUIREMENTS:
11
+
12
+ + Ruby 1.8
13
+ + Micronaut (via spicycode-micronaut on GitHub) -- see Micronaut::Rails::Version::MICRONAUT_REQUIRED_VERSION for the required version of Micronaut for this version of micronaut-rails
14
+
15
+ == CREDITS:
16
+
17
+ * RSpec for the great ideas and matchers
data/RSPEC-LICENSE ADDED
@@ -0,0 +1,23 @@
1
+ (The MIT License)
2
+
3
+ Copyright (c) 2005-2008 The RSpec Development Team
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+
data/Rakefile ADDED
@@ -0,0 +1,92 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+ require 'rubygems/specification'
4
+ require './lib/micronaut/rails/version'
5
+
6
+ GEM = "micronaut-rails"
7
+ GEM_VERSION = Micronaut::Rails::Version::STRING
8
+ AUTHOR = "Chad Humphries"
9
+ EMAIL = "chad@spicycode.com"
10
+ HOMEPAGE = "http://spicycode.com"
11
+ SUMMARY = "An excellent replacement for the wheel..."
12
+
13
+ spec = Gem::Specification.new do |s|
14
+ s.name = GEM
15
+ s.version = GEM_VERSION
16
+ s.platform = Gem::Platform::RUBY
17
+ s.has_rdoc = true
18
+ s.extra_rdoc_files = ["README", "LICENSE", "RSPEC-LICENSE"]
19
+ s.summary = SUMMARY
20
+ s.description = s.summary
21
+ s.author = AUTHOR
22
+ s.email = EMAIL
23
+ s.homepage = HOMEPAGE
24
+ s.bindir = 'bin'
25
+ s.require_path = 'lib'
26
+ s.autorequire = GEM
27
+ s.add_dependency "actionpack", '>= 2.2.2'
28
+ s.add_dependency "spicycode-micronaut", Micronaut::Rails::Version::MICRONAUT_REQUIRED_VERSION
29
+ s.files = %w(LICENSE README RSPEC-LICENSE Rakefile) + Dir.glob("{lib,examples}/**/*")
30
+ end
31
+
32
+ Rake::GemPackageTask.new(spec) do |pkg|
33
+ pkg.gem_spec = spec
34
+ end
35
+
36
+ desc "install the gem locally"
37
+ task :install => [:package] do
38
+ sh %{sudo gem install pkg/#{GEM}-#{GEM_VERSION}}
39
+ end
40
+
41
+ desc "create a gemspec file"
42
+ task :make_gemspec do
43
+ File.open("#{GEM}.gemspec", "w") do |file|
44
+ file.puts spec.to_ruby
45
+ end
46
+ end
47
+
48
+ desc 'Run all examples'
49
+ task :examples do
50
+ examples = Dir["examples/**/*_example.rb"].map { |g| Dir.glob(g) }.flatten
51
+ ruby examples.join(" ")
52
+ end
53
+
54
+ namespace :examples do
55
+
56
+ desc "List files that don't have examples"
57
+ task :untested do
58
+ code = Dir["lib/**/*.rb"].map { |g| Dir.glob(g) }.flatten
59
+ examples = Dir["examples/**/*_example.rb"].map { |g| Dir.glob(g) }.flatten
60
+ examples.map! { |f| f =~ /examples\/(.*)_example/; "#{$1}.rb" }
61
+ missing_examples = (code - examples)
62
+ puts
63
+ puts "The following files seem to be missing their examples:"
64
+ missing_examples.each do |missing|
65
+ puts " #{missing}"
66
+ end
67
+ end
68
+
69
+ desc "Run all examples using rcov"
70
+ task :coverage do
71
+ examples = Dir["examples/**/*_example.rb"].map { |g| Dir.glob(g) }.flatten
72
+ result = system "rcov --exclude \"examples/*,gems/*,db/*,/Library/Ruby/*,config/*\" --text-summary --sort coverage --no-validator-links #{examples.join(' ')}"
73
+ fail_build unless result
74
+ end
75
+
76
+ def fail_build
77
+ puts
78
+ puts "-" * 79
79
+ puts "Build Failed"
80
+ puts "-" * 79
81
+ abort
82
+ end
83
+
84
+ desc "Delete coverage artifacts"
85
+ task :clean_coverage do
86
+ rm_rf Dir["coverage/**/*"]
87
+ end
88
+
89
+ end
90
+
91
+ task :default => 'examples:coverage'
92
+ task :clobber_package => 'examples:clean_coverage'
@@ -0,0 +1,44 @@
1
+ lib_path = File.expand_path(File.dirname(__FILE__) + "/../lib")
2
+ $LOAD_PATH.unshift lib_path unless $LOAD_PATH.include?(lib_path)
3
+
4
+ require 'micronaut/rails/version'
5
+ gem "spicycode-micronaut", Micronaut::Rails::Version::MICRONAUT_REQUIRED_VERSION
6
+ require 'micronaut'
7
+ require 'micronaut-rails'
8
+ require 'rubygems'
9
+ gem :mocha
10
+
11
+ module Micronaut
12
+ module Matchers
13
+ def fail
14
+ raise_error(::Micronaut::Expectations::ExpectationNotMetError)
15
+ end
16
+
17
+ def fail_with(message)
18
+ raise_error(::Micronaut::Expectations::ExpectationNotMetError, message)
19
+ end
20
+ end
21
+ end
22
+
23
+ def remove_last_describe_from_world
24
+ Micronaut.world.behaviours.pop
25
+ end
26
+
27
+ class DummyFormatter < Micronaut::Formatters::BaseTextFormatter; end
28
+
29
+ def dummy_reporter
30
+ DummyFormatter.new({}, StringIO.new)
31
+ end
32
+
33
+ def use_color?
34
+ !ENV.has_key?('TM_MODE')
35
+ end
36
+
37
+ Micronaut.configure do |config|
38
+ config.mock_with :mocha
39
+ config.color_enabled = use_color?
40
+ config.formatter = :documentation
41
+ config.profile_examples = false
42
+ config.filter_run :focused => true
43
+ config.autorun!
44
+ end
@@ -0,0 +1,6 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../example_helper")
2
+
3
+ describe Micronaut::Rails do
4
+
5
+
6
+ end
@@ -0,0 +1,17 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../../../example_helper")
2
+
3
+ describe Micronaut::Rails::Configuration do
4
+
5
+ example "loading micronaut rails include it in the micronaut configuration class" do
6
+ Micronaut::Configuration.included_modules.should include(Micronaut::Rails::Configuration)
7
+ end
8
+
9
+ it "should add a #rails method" do
10
+ Micronaut.configuration.should respond_to(:rails)
11
+ end
12
+
13
+ it "should add an #enable_active_record_transactional_support method" do
14
+ Micronaut.configuration.should respond_to(:enable_active_record_transactional_support)
15
+ end
16
+
17
+ end
@@ -0,0 +1,3 @@
1
+ Autotest.add_discovery do
2
+ "micronaut_rails" if File.directory?('examples')
3
+ end
@@ -0,0 +1,48 @@
1
+ require 'autotest'
2
+
3
+ Autotest.add_hook :initialize do |at|
4
+ at.clear_mappings
5
+ # watch out: Ruby bug (1.8.6):
6
+ # %r(/) != /\//
7
+ at.add_mapping(%r%^examples/.*_example.rb$%) { |filename, _|
8
+ filename
9
+ }
10
+ at.add_mapping(%r%^lib/(.*)\.rb$%) { |filename, m|
11
+ ["examples/lib/#{m[1]}_example.rb"]
12
+ }
13
+ at.add_mapping(%r%^examples/(example_helper|shared/.*)\.rb$%) {
14
+ at.files_matching %r%^examples/.*_example\.rb$%
15
+ }
16
+ end
17
+
18
+ class MicronautCommandError < StandardError; end
19
+
20
+ class Autotest::Micronaut < Autotest
21
+
22
+ def initialize
23
+ super
24
+ self.failed_results_re = /^\d+\)\n(?:\e\[\d*m)?(?:.*?Error in )?'([^\n]*)'(?: FAILED)?(?:\e\[\d*m)?\n(.*?)\n\n/m
25
+ self.completed_re = /\n(?:\e\[\d*m)?\d* examples?/m
26
+ end
27
+
28
+ def consolidate_failures(failed)
29
+ filters = new_hash_of_arrays
30
+ failed.each do |spec, trace|
31
+ if trace =~ /\n(\.\/)?(.*example\.rb):[\d]+:\Z?/
32
+ filters[$2] << spec
33
+ end
34
+ end
35
+ return filters
36
+ end
37
+
38
+ def make_test_cmd(files_to_test)
39
+ return '' if files_to_test.empty?
40
+
41
+ examples = files_to_test.keys.flatten
42
+
43
+ examples.map! {|f| %Q(require "#{f}")}
44
+
45
+ return "#{ruby} -e '#{examples.join("; ")}'"
46
+ end
47
+
48
+ end
@@ -0,0 +1,48 @@
1
+ require 'autotest'
2
+
3
+ Autotest.add_hook :initialize do |at|
4
+ at.clear_mappings
5
+ # watch out: Ruby bug (1.8.6):
6
+ # %r(/) != /\//
7
+ at.add_mapping(%r%^examples/.*_example.rb$%) { |filename, _|
8
+ filename
9
+ }
10
+ at.add_mapping(%r%^lib/(.*)\.rb$%) { |filename, m|
11
+ ["examples/lib/#{m[1]}_example.rb"]
12
+ }
13
+ at.add_mapping(%r%^examples/(example_helper|shared/.*)\.rb$%) {
14
+ at.files_matching %r%^examples/.*_example\.rb$%
15
+ }
16
+ end
17
+
18
+ class MicronautCommandError < StandardError; end
19
+
20
+ class Autotest::MicronautRails < Autotest
21
+
22
+ def initialize
23
+ super
24
+ self.failed_results_re = /^\d+\)\n(?:\e\[\d*m)?(?:.*?Error in )?'([^\n]*)'(?: FAILED)?(?:\e\[\d*m)?\n(.*?)\n\n/m
25
+ self.completed_re = /\n(?:\e\[\d*m)?\d* examples?/m
26
+ end
27
+
28
+ def consolidate_failures(failed)
29
+ filters = new_hash_of_arrays
30
+ failed.each do |spec, trace|
31
+ if trace =~ /\n(\.\/)?(.*example\.rb):[\d]+:\Z?/
32
+ filters[$2] << spec
33
+ end
34
+ end
35
+ return filters
36
+ end
37
+
38
+ def make_test_cmd(files_to_test)
39
+ return '' if files_to_test.empty?
40
+
41
+ examples = files_to_test.keys.flatten
42
+
43
+ examples.map! {|f| %Q(require "#{f}")}
44
+
45
+ return "#{ruby} -e '#{examples.join("; ")}'"
46
+ end
47
+
48
+ end
@@ -0,0 +1,27 @@
1
+ require 'action_controller'
2
+ require 'action_controller/test_process'
3
+ require 'micronaut/rails/configuration'
4
+ require 'micronaut/rails/matchers/controllers/redirect_to'
5
+ require 'micronaut/rails/matchers/controllers/render_template'
6
+ require 'micronaut/rails/transactional_database_support'
7
+ require 'micronaut/rails/version'
8
+ require 'micronaut/rails/helpers'
9
+ require 'micronaut/rails/controllers'
10
+ require 'micronaut/rails/extensions/active_record'
11
+
12
+ module Micronaut
13
+ module Rails
14
+ class IllegalDataAccessException < StandardError; end
15
+
16
+ def self.include_in(config)
17
+ if config.mock_framework.to_s =~ /mocha/i
18
+ require 'micronaut/rails/mocking/with_mocha'
19
+ Micronaut::Behaviour.send(:include, Micronaut::Rails::Mocking::WithMocha)
20
+ end
21
+ config.extend(Micronaut::Rails::TransactionalDatabaseSupport, :behaviour => { :describes => lambda { |dt| dt < ActiveRecord::Base } })
22
+ config.extend(Micronaut::Rails::Helpers, :behaviour => { :describes => lambda { |dt| dt.to_s.ends_with?('Helper') } })
23
+ config.extend(Micronaut::Rails::Controllers, :behaviour => { :describes => lambda { |dt| dt < ActionController::Base } })
24
+ end
25
+ end
26
+
27
+ end
@@ -0,0 +1,57 @@
1
+ module Micronaut
2
+ module Rails
3
+
4
+ module Configuration
5
+
6
+ def rails
7
+ self
8
+ end
9
+
10
+ # :behaviour => { :describes => lambda { |dt| dt < ActiveRecord::Base }
11
+ def enable_active_record_transactional_support(filter_options={})
12
+ if filter_options.empty?
13
+ ::Micronaut::Behaviour.send(:extend, ::Micronaut::Rails::TransactionalDatabaseSupport)
14
+ else
15
+ ::Micronaut.config.extend(::Micronaut::Rails::TransactionalDatabaseSupport, filter_options)
16
+ end
17
+ end
18
+
19
+ # :behaviour => { :describes => lambda { |dt| dt.to_s.ends_with?('Helper') }
20
+ def enable_helper_support(filter_options={})
21
+ if filter_options.empty?
22
+ ::Micronaut::Behaviour.send(:extend, ::Micronaut::Rails::Helpers)
23
+ else
24
+ ::Micronaut.config.extend(::Micronaut::Rails::Helpers, filter_options)
25
+ end
26
+ end
27
+
28
+ # :behaviour => { :describes => lambda { |dt| dt < ActionController::Base }
29
+ def enable_controller_support(filter_options={})
30
+ if filter_options.empty?
31
+ ::Micronaut::Behaviour.send(:extend, ::Micronaut::Rails::Controllers)
32
+ else
33
+ ::Micronaut.config.extend(::Micronaut::Rails::Controllers, filter_options)
34
+ end
35
+ end
36
+
37
+ def enable_rails_specific_mocking_extensions
38
+ case ::Micronaut.config.mock_framework.to_s
39
+ when /mocha/i
40
+ require 'micronaut/rails/mocking/with_mocha'
41
+ Micronaut::Behaviour.send(:include, Micronaut::Rails::Mocking::WithMocha)
42
+ end
43
+ end
44
+
45
+ def enable_reasonable_defaults!
46
+ enable_active_record_transactional_support :describes => lambda { |dt| dt < ::ActiveRecord::Base }
47
+ enable_helper_support :describes => lambda { |dt| dt.to_s.ends_with?('Helper') }
48
+ enable_controller_support :describes => lambda { |dt| dt < ::ActionController::Base }
49
+ enable_rails_specific_mocking_extensions
50
+ end
51
+
52
+ end
53
+
54
+ end
55
+ end
56
+
57
+ ::Micronaut::Configuration.send(:include, ::Micronaut::Rails::Configuration)
@@ -0,0 +1,106 @@
1
+ module Micronaut
2
+ module Rails
3
+ module Controllers
4
+
5
+ module InstanceMethods
6
+ attr_reader :request, :response, :controller
7
+
8
+ def assert_routing(path, options, defaults={}, extras={}, message=nil)
9
+ method = options[:method] || :get
10
+ route_for(params_from(method, path)).should == path
11
+ end
12
+
13
+ def route_for(options)
14
+ ActionController::Routing::Routes.reload if ActionController::Routing::Routes.empty?
15
+ ActionController::Routing::Routes.generate(options)
16
+ end
17
+
18
+ def params_from(method, path)
19
+ ActionController::Routing::Routes.reload if ActionController::Routing::Routes.empty?
20
+ ActionController::Routing::Routes.recognize_path(path, :method => method)
21
+ end
22
+
23
+ end
24
+
25
+ module RenderOverrides
26
+
27
+ def render_views!
28
+ @render_views = true
29
+ end
30
+
31
+ def render_views?
32
+ @render_views
33
+ end
34
+
35
+ def render(options=nil, deprecated_status_or_extra_options=nil, &block)
36
+ if ::Rails::VERSION::STRING >= '2.0.0' && deprecated_status_or_extra_options.nil?
37
+ deprecated_status_or_extra_options = {}
38
+ end
39
+ # puts "\n[render(views => #{render_views?.inspect})] options => #{options.inspect}, caller => #{caller(0)[1]}"
40
+ response.headers['Status'] = interpret_status((options && options[:status]) || ::ActionController::Base::DEFAULT_RENDER_STATUS_CODE)
41
+
42
+ unless block_given?
43
+ unless render_views?
44
+ if @template.respond_to?(:finder)
45
+ (class << @template.finder; self; end).class_eval do
46
+ define_method :file_exists? do; true; end
47
+ end
48
+ else
49
+ (class << @template; self; end).class_eval do
50
+ define_method :file_exists? do; true; end
51
+ end
52
+ end
53
+ (class << @template; self; end).class_eval do
54
+
55
+ define_method :render_file do |*args|
56
+ @first_render ||= args[0] unless args[0] =~ /^layouts/
57
+ @_first_render ||= args[0] unless args[0] =~ /^layouts/
58
+ end
59
+
60
+ define_method :_pick_template do |*args|
61
+ @_first_render ||= args[0] unless args[0] =~ /^layouts/
62
+ PickedTemplate.new
63
+ end
64
+
65
+ end
66
+
67
+ end
68
+ end
69
+
70
+ super(options, deprecated_status_or_extra_options, &block)
71
+ end
72
+
73
+ end
74
+
75
+ # Returned by _pick_template when running controller examples in isolation mode.
76
+ class PickedTemplate
77
+ # Do nothing when running controller examples in isolation mode.
78
+ def render_template(*ignore_args); end
79
+ # Do nothing when running controller examples in isolation mode.
80
+ def render_partial(*ignore_args); end
81
+ end
82
+
83
+ def self.extended(kls)
84
+ kls.send(:include, ActionController::TestProcess)
85
+ kls.send(:include, InstanceMethods)
86
+ kls.send(:include, Micronaut::Rails::Matchers::Controllers)
87
+
88
+ kls.before do
89
+ @controller.class.send :include, RenderOverrides
90
+ @controller.class.send :include, ActionController::TestCase::RaiseActionExceptions
91
+ @controller = self.class.describes.new
92
+ @request = ActionController::TestRequest.new
93
+ @controller.request = @request
94
+ @response = ActionController::TestResponse.new
95
+ @controller.params = {}
96
+ @controller.send(:initialize_current_url)
97
+ @response.session = @request.session
98
+ end
99
+
100
+
101
+
102
+ end
103
+
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,19 @@
1
+ module Micronaut
2
+ module Rails
3
+ module Extensions
4
+
5
+ module ActiveRecord
6
+ def errors_on(attribute)
7
+ self.valid?
8
+ [self.errors.on(attribute)].flatten.compact
9
+ end
10
+ alias :error_on :errors_on
11
+ end
12
+
13
+ end
14
+ end
15
+ end
16
+
17
+ if defined?(::ActiveRecord::Base)
18
+ ::ActiveRecord::Base.send(:include, Micronaut::Rails::Extensions::ActiveRecord)
19
+ end
@@ -0,0 +1,99 @@
1
+ module Micronaut
2
+ module Rails
3
+ module Helpers
4
+
5
+ class HelperController < ActionController::Base; end
6
+
7
+ module InstanceMethods
8
+
9
+ class HelperObject < ActionView::Base
10
+ def protect_against_forgery?
11
+ false
12
+ end
13
+
14
+ def session=(session)
15
+ @session = session
16
+ end
17
+
18
+ def request=(request)
19
+ @request = request
20
+ end
21
+
22
+ def flash=(flash)
23
+ @flash = flash
24
+ end
25
+
26
+ def params=(params)
27
+ @params = params
28
+ end
29
+
30
+ def controller=(controller)
31
+ @controller = controller
32
+ end
33
+
34
+ private
35
+ attr_reader :session, :request, :flash, :params, :controller
36
+ end
37
+
38
+ def helper
39
+ @helper_object ||= returning HelperObject.new do |helper_object|
40
+ if self.class.describes.class == Module
41
+ helper_object.extend self.class.describes
42
+ end
43
+ end
44
+ end
45
+
46
+ def params
47
+ request.parameters
48
+ end
49
+
50
+ def flash
51
+ response.flash
52
+ end
53
+
54
+ def session
55
+ response.session
56
+ end
57
+
58
+ def method_missing(sym, *args)
59
+ if helper.respond_to?(sym)
60
+ helper.send(sym, *args)
61
+ else
62
+ super
63
+ end
64
+ end
65
+
66
+ end
67
+
68
+ def self.extended(kls)
69
+ kls.send(:include, InstanceMethods)
70
+
71
+ kls.send(:attr_reader, :request, :response)
72
+
73
+ ActionView::Base.included_modules.reverse.each do |mod|
74
+ kls.send(:include, mod) if mod.parents.include?(ActionView::Helpers)
75
+ end
76
+
77
+ kls.before do
78
+ @controller = HelperController.new
79
+ @request = ActionController::TestRequest.new
80
+ @response = ActionController::TestResponse.new
81
+ @response.session = @request.session
82
+ @controller.request = @request
83
+ @flash = ActionController::Flash::FlashHash.new
84
+ @response.session['flash'] = @flash
85
+
86
+ ActionView::Helpers::AssetTagHelper::reset_javascript_include_default
87
+
88
+ helper.session = @response.session
89
+ helper.request = @request
90
+ helper.flash = @flash
91
+ helper.params = params
92
+ helper.controller = @controller
93
+ end
94
+
95
+ end
96
+
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,115 @@
1
+ module Micronaut
2
+ module Rails
3
+ module Matchers
4
+ module Controllers
5
+
6
+ class RedirectTo #:nodoc:
7
+
8
+ def initialize(request, expected)
9
+ @expected = expected
10
+ @request = request
11
+ end
12
+
13
+ def matches?(response)
14
+ @redirected = response.redirect?
15
+ @actual = response.redirect_url
16
+ return false unless @redirected
17
+ if @expected.instance_of? Hash
18
+ return false unless @actual =~ %r{^\w+://#{@request.host}}
19
+ return false unless actual_redirect_to_valid_route
20
+ return actual_hash == expected_hash
21
+ else
22
+ return @actual == expected_url
23
+ end
24
+ end
25
+
26
+ def actual_hash
27
+ hash_from_url @actual
28
+ end
29
+
30
+ def expected_hash
31
+ hash_from_url expected_url
32
+ end
33
+
34
+ def actual_redirect_to_valid_route
35
+ actual_hash
36
+ end
37
+
38
+ def hash_from_url(url)
39
+ query_hash(url).merge(path_hash(url)).with_indifferent_access
40
+ end
41
+
42
+ def path_hash(url)
43
+ path = url.sub(%r{^\w+://#{@request.host}(?::\d+)?}, "").split("?", 2)[0]
44
+ ActionController::Routing::Routes.recognize_path path
45
+ end
46
+
47
+ def query_hash(url)
48
+ query = url.split("?", 2)[1] || ""
49
+ QueryParameterParser.parse_query_parameters(query, @request)
50
+ end
51
+
52
+ def expected_url
53
+ case @expected
54
+ when Hash
55
+ return ActionController::UrlRewriter.new(@request, {}).rewrite(@expected)
56
+ when :back
57
+ return @request.env['HTTP_REFERER']
58
+ when %r{^\w+://.*}
59
+ return @expected
60
+ else
61
+ return "http://#{@request.host}" + (@expected.split('')[0] == '/' ? '' : '/') + @expected
62
+ end
63
+ end
64
+
65
+ def failure_message
66
+ if @redirected
67
+ return %Q{expected redirect to #{@expected.inspect}, got redirect to #{@actual.inspect}}
68
+ else
69
+ return %Q{expected redirect to #{@expected.inspect}, got no redirect}
70
+ end
71
+ end
72
+
73
+ def negative_failure_message
74
+ return %Q{expected not to be redirected to #{@expected.inspect}, but was} if @redirected
75
+ end
76
+
77
+ def description
78
+ "redirect to #{@actual.inspect}"
79
+ end
80
+
81
+ class QueryParameterParser
82
+ def self.parse_query_parameters(query, request)
83
+ if defined?(CGIMethods)
84
+ CGIMethods.parse_query_parameters(query)
85
+ else
86
+ request.class.parse_query_parameters(query)
87
+ end
88
+ end
89
+ end
90
+ end
91
+
92
+ # :call-seq:
93
+ # response.should redirect_to(url)
94
+ # response.should redirect_to(:action => action_name)
95
+ # response.should redirect_to(:controller => controller_name, :action => action_name)
96
+ # response.should_not redirect_to(url)
97
+ # response.should_not redirect_to(:action => action_name)
98
+ # response.should_not redirect_to(:controller => controller_name, :action => action_name)
99
+ #
100
+ # Passes if the response is a redirect to the url, action or controller/action.
101
+ # Useful in controller specs (integration or isolation mode).
102
+ #
103
+ # == Examples
104
+ #
105
+ # response.should redirect_to("path/to/action")
106
+ # response.should redirect_to("http://test.host/path/to/action")
107
+ # response.should redirect_to(:action => 'list')
108
+ def redirect_to(opts)
109
+ RedirectTo.new(request, opts)
110
+ end
111
+
112
+ end
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,101 @@
1
+ module Micronaut
2
+ module Rails
3
+ module Matchers
4
+ module Controllers
5
+
6
+ class RenderTemplate #:nodoc:
7
+
8
+ def initialize(expected, controller)
9
+ @controller = controller
10
+ @expected = expected
11
+ end
12
+
13
+ def matches?(response)
14
+ if response.respond_to?(:rendered_file)
15
+ @actual = response.rendered_file
16
+ else
17
+ @actual = response.rendered_template.to_s
18
+ end
19
+ return false if @actual.blank?
20
+ given_controller_path, given_file = path_and_file(@actual)
21
+ expected_controller_path, expected_file = path_and_file(@expected)
22
+ given_controller_path == expected_controller_path && given_file.match(expected_file)
23
+ end
24
+
25
+ def failure_message
26
+ "expected #{@expected.inspect}, got #{@actual.inspect}"
27
+ end
28
+
29
+ def negative_failure_message
30
+ "expected not to render #{@expected.inspect}, but did"
31
+ end
32
+
33
+ def description
34
+ "render template #{@expected.inspect}"
35
+ end
36
+
37
+ private
38
+ def path_and_file(path)
39
+ parts = path.split('/')
40
+ file = parts.pop
41
+ controller = parts.empty? ? current_controller_path : parts.join('/')
42
+ return controller, file
43
+ end
44
+
45
+ def controller_path_from(path)
46
+ parts = path.split('/')
47
+ parts.pop
48
+ parts.join('/')
49
+ end
50
+
51
+ def current_controller_path
52
+ @controller.class.to_s.underscore.gsub(/_controller$/,'')
53
+ end
54
+
55
+ end
56
+
57
+ # :call-seq:
58
+ # response.should render_template(template)
59
+ # response.should_not render_template(template)
60
+ #
61
+ # For use in controller code examples (integration or isolation mode).
62
+ #
63
+ # Passes if the specified template (view file) is rendered by the
64
+ # response. This file can be any view file, including a partial. However
65
+ # if it is a partial it must be rendered directly i.e. you can't detect
66
+ # that a partial has been rendered as part of a view using
67
+ # render_template. For that you should use a message expectation
68
+ # (mock) instead:
69
+ #
70
+ # controller.should_receive(:render).with(:partial => 'path/to/partial')
71
+ #
72
+ # <code>template</code> can include the controller path. It can also
73
+ # include an optional extension, which you only need to use when there
74
+ # is ambiguity.
75
+ #
76
+ # Note that partials must be spelled with the preceding underscore.
77
+ #
78
+ # == Examples
79
+ #
80
+ # response.should render_template('list')
81
+ # response.should render_template('same_controller/list')
82
+ # response.should render_template('other_controller/list')
83
+ #
84
+ # # with extensions
85
+ # response.should render_template('list.rjs')
86
+ # response.should render_template('list.haml')
87
+ # response.should render_template('same_controller/list.rjs')
88
+ # response.should render_template('other_controller/list.rjs')
89
+ #
90
+ # # partials
91
+ # response.should render_template('_a_partial')
92
+ # response.should render_template('same_controller/_a_partial')
93
+ # response.should render_template('other_controller/_a_partial')
94
+ def render_template(path)
95
+ RenderTemplate.new(path.to_s, @controller)
96
+ end
97
+
98
+ end
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,89 @@
1
+ module Micronaut
2
+ module Rails
3
+ module Mocking
4
+ module WithMocha
5
+
6
+ module ModelStubber
7
+
8
+ def connection
9
+ raise MicronautRails::IllegalDataAccessException.new("stubbed models are not allowed to access the database")
10
+ end
11
+
12
+ def new_record?
13
+ id.nil?
14
+ end
15
+
16
+ def as_new_record
17
+ self.id = nil
18
+ self
19
+ end
20
+
21
+ end
22
+
23
+ def stub_model(model_class, params = {})
24
+ params = params.dup
25
+ model = model_class.new
26
+ model.id = params.delete(:id) || next_id
27
+
28
+ model.extend ModelStubber
29
+ params.keys.each do |prop|
30
+ model[prop] = params.delete(prop) if model.has_attribute?(prop)
31
+ end
32
+ add_stubs(model, params)
33
+
34
+ yield model if block_given?
35
+ model
36
+ end
37
+
38
+ # Stubs methods on +object+ (if +object+ is a symbol or string a new mock
39
+ # with that name will be created). +stubs+ is a Hash of +method=>value+
40
+ def add_stubs(object, params) # :nodoc:
41
+ m = [String, Symbol].include?(object.class) ? mock(object.to_s) : object
42
+ params.each { |prop, value| m.stubs(prop).returns(value) }
43
+ m
44
+ end
45
+
46
+ def mock_model(model_class, params = {})
47
+ id = params[:id] || next_id
48
+ model = stub("#{model_class.name}_#{id}", {
49
+ :id => id,
50
+ :to_param => id.to_s,
51
+ :new_record? => false,
52
+ :errors => stub("errors", :count => 0)
53
+ }.update(params))
54
+
55
+ model.instance_eval <<-CODE
56
+ def as_new_record
57
+ self.stubs(:id).returns(nil)
58
+ self.stubs(:to_param).returns(nil)
59
+ self.stubs(:new_record?).returns(true)
60
+ self
61
+ end
62
+ def is_a?(other)
63
+ #{model_class}.ancestors.include?(other)
64
+ end
65
+ def kind_of?(other)
66
+ #{model_class}.ancestors.include?(other)
67
+ end
68
+ def instance_of?(other)
69
+ other == #{model_class}
70
+ end
71
+ def class
72
+ #{model_class}
73
+ end
74
+ CODE
75
+
76
+ yield model if block_given?
77
+ return model
78
+ end
79
+
80
+ private
81
+ @@model_id = 1000
82
+ def next_id
83
+ @@model_id += 1
84
+ end
85
+
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,39 @@
1
+ module Micronaut
2
+ module Rails
3
+ module TransactionalDatabaseSupport
4
+
5
+ module InstanceMethods
6
+
7
+ def active_record_configured?
8
+ defined?(ActiveRecord) && !ActiveRecord::Base.configurations.blank?
9
+ end
10
+
11
+ def transactional_protection_start
12
+ return unless active_record_configured?
13
+
14
+ ActiveRecord::Base.connection.increment_open_transactions
15
+ ActiveRecord::Base.connection.begin_db_transaction
16
+ end
17
+
18
+ def transactional_protection_cleanup
19
+ return unless active_record_configured?
20
+
21
+ if ActiveRecord::Base.connection.open_transactions != 0
22
+ ActiveRecord::Base.connection.rollback_db_transaction
23
+ ActiveRecord::Base.connection.decrement_open_transactions
24
+ end
25
+
26
+ ActiveRecord::Base.clear_active_connections!
27
+ end
28
+
29
+ end
30
+
31
+ def self.extended(kls)
32
+ kls.send(:include, InstanceMethods)
33
+ kls.before { transactional_protection_start }
34
+ kls.after { transactional_protection_cleanup }
35
+ end
36
+
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,14 @@
1
+ module Micronaut
2
+ module Rails
3
+ module Version
4
+ MAJOR = 0
5
+ MINOR = 0
6
+ TINY = 6
7
+ MINISCULE = 1
8
+
9
+ STRING = [MAJOR, MINOR, TINY, MINISCULE].join('.')
10
+
11
+ MICRONAUT_REQUIRED_VERSION = ">= 0.1.6"
12
+ end
13
+ end
14
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spicycode-micronaut-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.6.1
5
+ platform: ruby
6
+ authors:
7
+ - Chad Humphries
8
+ autorequire: micronaut-rails
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-12-27 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: actionpack
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 2.2.2
23
+ version:
24
+ - !ruby/object:Gem::Dependency
25
+ name: spicycode-micronaut
26
+ version_requirement:
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: 0.1.6
32
+ version:
33
+ description: An excellent replacement for the wheel...
34
+ email: chad@spicycode.com
35
+ executables: []
36
+
37
+ extensions: []
38
+
39
+ extra_rdoc_files:
40
+ - README
41
+ - LICENSE
42
+ - RSPEC-LICENSE
43
+ files:
44
+ - LICENSE
45
+ - README
46
+ - RSPEC-LICENSE
47
+ - Rakefile
48
+ - lib/autotest
49
+ - lib/autotest/discover.rb
50
+ - lib/autotest/micronaut-rails.rb
51
+ - lib/autotest/micronaut_rails.rb
52
+ - lib/micronaut
53
+ - lib/micronaut/rails
54
+ - lib/micronaut/rails/configuration.rb
55
+ - lib/micronaut/rails/controllers.rb
56
+ - lib/micronaut/rails/extensions
57
+ - lib/micronaut/rails/extensions/active_record.rb
58
+ - lib/micronaut/rails/helpers.rb
59
+ - lib/micronaut/rails/matchers
60
+ - lib/micronaut/rails/matchers/controllers
61
+ - lib/micronaut/rails/matchers/controllers/redirect_to.rb
62
+ - lib/micronaut/rails/matchers/controllers/render_template.rb
63
+ - lib/micronaut/rails/mocking
64
+ - lib/micronaut/rails/mocking/with_mocha.rb
65
+ - lib/micronaut/rails/transactional_database_support.rb
66
+ - lib/micronaut/rails/version.rb
67
+ - lib/micronaut-rails.rb
68
+ - examples/example_helper.rb
69
+ - examples/lib
70
+ - examples/lib/micronaut
71
+ - examples/lib/micronaut/rails
72
+ - examples/lib/micronaut/rails/configuration_example.rb
73
+ - examples/lib/micronaut-rails_example.rb
74
+ has_rdoc: true
75
+ homepage: http://spicycode.com
76
+ post_install_message:
77
+ rdoc_options: []
78
+
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: "0"
86
+ version:
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: "0"
92
+ version:
93
+ requirements: []
94
+
95
+ rubyforge_project:
96
+ rubygems_version: 1.2.0
97
+ signing_key:
98
+ specification_version: 2
99
+ summary: An excellent replacement for the wheel...
100
+ test_files: []
101
+