Browse files

Merge branch 'merb-test' of git://github.com/benburkert/merb-more

  • Loading branch information...
2 parents c2fc436 + 4907a07 commit ce0365f0ca5f96a6d4589799f37d1a5e0251fecf @ivey ivey committed Feb 13, 2008
View
62 merb-gen/app_generators/merb/templates/Rakefile
@@ -8,6 +8,7 @@ require 'rake/testtask'
require 'spec/rake/spectask'
require 'fileutils'
require 'merb-core'
+require 'rubigen'
$RAKE_ENV = true
@@ -33,67 +34,6 @@ task :uninstall => [:clean] do
sh %{sudo gem uninstall #{NAME}}
end
-desc 'Run unit tests'
-Rake::TestTask.new('test_unit') do |t|
- t.libs << 'test'
- t.pattern = 'test/unit/*_test.rb'
- t.verbose = true
-end
-
-desc 'Run functional tests'
-Rake::TestTask.new('test_functional') do |t|
- t.libs << 'test'
- t.pattern = 'test/functional/*_test.rb'
- t.verbose = true
-end
-
-desc 'Run all tests'
-Rake::TestTask.new('test') do |t|
- t.libs << 'test'
- t.pattern = 'test/**/*_test.rb'
- t.verbose = true
-end
-
-desc "Run all specs"
-Spec::Rake::SpecTask.new('specs') do |t|
- t.spec_opts = ["--format", "specdoc", "--colour"]
- t.spec_files = Dir['spec/**/*_spec.rb'].sort
-end
-
-desc "Run all model specs"
-Spec::Rake::SpecTask.new('model_specs') do |t|
- t.spec_opts = ["--format", "specdoc", "--colour"]
- t.spec_files = Dir['spec/models/**/*_spec.rb'].sort
-end
-
-desc "Run all controller specs"
-Spec::Rake::SpecTask.new('controller_specs') do |t|
- t.spec_opts = ["--format", "specdoc", "--colour"]
- t.spec_files = Dir['spec/controllers/**/*_spec.rb'].sort
-end
-
-desc "Run a specific spec with TASK=xxxx"
-Spec::Rake::SpecTask.new('spec') do |t|
- t.spec_opts = ["--format", "specdoc", "--colour"]
- t.libs = ['lib', 'server/lib' ]
- t.spec_files = ["spec/#{ENV['TASK']}_spec.rb"]
-end
-
-desc "Run all specs output html"
-Spec::Rake::SpecTask.new('specs_html') do |t|
- t.spec_opts = ["--format", "html"]
- t.libs = ['lib', 'server/lib' ]
- t.spec_files = Dir['spec/**/*_spec.rb'].sort
-end
-
-desc "RCov"
-Spec::Rake::SpecTask.new('rcov') do |t|
- t.spec_opts = ["--format", "specdoc", "--colour"]
- t.spec_files = Dir['spec/**/*_spec.rb'].sort
- t.libs = ['lib', 'server/lib' ]
- t.rcov = true
-end
-
desc 'Run all tests, specs and finish with rcov'
task :aok do
sh %{rake rcov}
View
20 merb-test/LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2008 YOUR NAME
+
+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.
View
4 merb-test/README
@@ -0,0 +1,4 @@
+merb-rspec
+=========
+
+A plugin for the Merb framework that provides ...
View
45 merb-test/Rakefile
@@ -0,0 +1,45 @@
+require 'rubygems'
+require 'rake/gempackagetask'
+
+PLUGIN = "merb-test"
+NAME = "merb-test"
+VERSION = "0.9.0"
+AUTHOR = "Yehuda Katz"
+EMAIL = "ykatz@engineyard.com"
+HOMEPAGE = "http://merb-plugins.rubyforge.org/merb-test/"
+SUMMARY = "Merb plugin that provides common test support"
+
+spec = Gem::Specification.new do |s|
+ s.name = NAME
+ s.version = VERSION
+ s.platform = Gem::Platform::RUBY
+ s.has_rdoc = true
+ s.extra_rdoc_files = ["README", "LICENSE", 'TODO']
+ s.summary = SUMMARY
+ s.description = s.summary
+ s.author = AUTHOR
+ s.email = EMAIL
+ s.homepage = HOMEPAGE
+ s.add_dependency('merb-core', '>= 0.9.0')
+ s.add_dependency('hpricot', '>= 0.6')
+ s.require_path = 'lib'
+ s.autorequire = PLUGIN
+ s.files = %w(LICENSE README Rakefile TODO) + Dir.glob("{lib,specs}/**/*")
+end
+
+Rake::GemPackageTask.new(spec) do |pkg|
+ pkg.gem_spec = spec
+end
+
+task :install => [:package] do
+ sh %{sudo gem install pkg/#{NAME}-#{VERSION}}
+end
+
+namespace :jruby do
+
+ desc "Run :package and install the resulting .gem with jruby"
+ task :install => :package do
+ sh %{#{SUDO} jruby -S gem install pkg/#{NAME}-#{Merb::VERSION}.gem --no-rdoc --no-ri}
+ end
+
+end
View
5 merb-test/TODO
@@ -0,0 +1,5 @@
+TODO:
+Fix LICENSE with your name
+Fix Rakefile with your name and contact info
+Add your code to lib/merb-rspec.rb
+Add your Merb rake tasks to lib/merb-rspec/merbtasks.rb
View
8 merb-test/lib/helpers/controller_helper.rb
@@ -0,0 +1,8 @@
+module Merb
+ module Test
+ module ControllerHelper
+ include Multipart
+ include RequestHelper
+ end
+ end
+end
View
76 merb-test/lib/helpers/hpricot_helper.rb
@@ -0,0 +1,76 @@
+module Merb
+ module Test
+ module HpricotHelper
+
+ # small utility class for working with
+ # the Hpricot parser class
+ class DocumentOutput
+ def initialize(response_body)
+ @parser = Hpricot.parse(response_body)
+ end
+
+ def content_for(css_query)
+ match = @parser.search(css_query).first
+ match.inner_text unless match.nil?
+ end
+
+ def content_for_all(css_query)
+ matches = @parser.search(css_query).collect{|ele| ele.inner_text}
+ end
+
+ def [](css_query)
+ @parser.search(css_query)
+ end
+ end
+
+ # returns the inner content of
+ # the first tag found by the css query
+ def tag(css_query, output = process_output)
+ output.content_for(css_query)
+ end
+
+ # returns an array of tag contents
+ # for all of the tags found by the
+ # css query
+ def tags(css_query, output = process_output)
+ output.content_for_all(css_query)
+ end
+
+ # returns a raw Hpricot::Elem object
+ # for the first result found by the query
+ def element(css_query, output = process_output)
+ output[css_query].first
+ end
+
+ # returns an array of Hpricot::Elem objects
+ # for the results found by the query
+ def elements(css_query, output = process_output)
+ Hpricot::Elements[*css_query.to_s.split(",").map{|s| s.strip}.map do |query|
+ output[query]
+ end.flatten]
+ end
+
+ def get_elements(css_query, text, output = nil)
+ els = elements(*[css_query, output].compact)
+ case text
+ when String then els.reject {|t| !t.contains?(text) }
+ when Regexp then els.reject {|t| !t.matches?(text) }
+ else []
+ end
+ end
+
+ protected
+ # creates a new DocumentOutput object from the response
+ # body if hasn't already been created. This is
+ # called automatically by the element and tag methods
+ def process_output
+ return @output unless @output.nil?
+ return @output = DocumentOutput.new(@response_output) unless @response_output.nil?
+
+ raise "The response output was not in it's usual places, please provide the output" if @controller.nil? || @controller.body.empty?
+ @response_output = @controller.body
+ @output = DocumentOutput.new(@controller.body)
+ end
+ end
+ end
+end
View
11 merb-test/lib/merb-test.rb
@@ -0,0 +1,11 @@
+require 'hpricot'
+
+require 'merb-core/test/fake_request'
+require 'merb-core/test/request_helper'
+require 'merb-core/test/multipart_helper'
+
+require File.join(File.dirname(__FILE__), "test_ext", "hpricot")
+require File.join(File.dirname(__FILE__), "test_ext", "object")
+
+require File.join(File.dirname(__FILE__), "helpers", "hpricot_helper")
+require File.join(File.dirname(__FILE__), "helpers", "controller_helper")
View
32 merb-test/lib/test_ext/hpricot.rb
@@ -0,0 +1,32 @@
+# http://yehudakatz.com/2007/01/27/a-better-assert_select-assert_elements/
+# based on assert_elements
+# Author: Yehuda Katz
+# Email: wycats @nospam@ gmail.com
+# Web: http://www.yehudakatz.com
+#
+# which was based on HpricotTestHelper
+# Author: Luke Redpath
+# Email: contact @nospam@ lukeredpath.co.uk
+# Web: www.lukeredpath.co.uk / opensource.agileevolved.com
+
+class Hpricot::Elem
+ def contain?(value)
+ self.inner_text.include?(value)
+ end
+
+ alias_method :contains?, :contain?
+
+ def match?(regex)
+ self.inner_text.match(regex)
+ end
+
+ alias_method :matches?, :match?
+
+ # courtesy of 'thomas' from the comments
+ # of _whys blog - get in touch if you want a better credit!
+ def inner_text
+ self.children.collect do |child|
+ child.is_a?(Hpricot::Text) ? child.content : ((child.respond_to?("inner_text") && child.inner_text) || "")
+ end.join.strip
+ end
+end
View
10 merb-test/lib/test_ext/object.rb
@@ -0,0 +1,10 @@
+class Object
+ # Checks that an object has assigned an instance variable of name
+ # :name
+ #
+ # ===Example in a spec
+ # @my_obj.assings(:my_value).should == @my_value
+ def assigns(attr)
+ self.instance_variable_get("@#{attr}")
+ end
+end
View
86 merb-test/spec/helpers/hpricot_helper_spec.rb
@@ -0,0 +1,86 @@
+require File.dirname(__FILE__) + '/../spec_helper'
+
+describe Merb::Test::HpricotHelper do
+ before(:each) do
+ @output = Merb::Test::HpricotHelper::DocumentOutput.new(test_response)
+ end
+
+ describe "#tag" do
+ it "should return the inner content of the first tag found by the css query" do
+ tag(:li).should == "item 1"
+ end
+ end
+
+ describe "#tags" do
+ it "should return an array of tag contents for all tags found by the css query" do
+ tags(:li).should include("item 1", "item 2", "item 3")
+ end
+ end
+
+ describe "#element" do
+ it "should return a raw Hpricot::Elem object for the first result found by the query" do
+ Hpricot::Elem.should === element(:body)
+ end
+ end
+
+ describe "#elements" do
+ it "should return an array of Hpricot::Elem objects for the results found by the query" do
+ elements("li, ul").each{|ele| Hpricot::Elem.should === ele}
+ end
+ end
+
+ describe "#get_elements" do
+ it "should return an array of Hpricot::Elem objects for the results found by the query containing the filter string" do
+ get_elements(:li, "item").size.should == 3
+ get_elements(:li, "item 2").size.should == 1
+ end
+
+ it "should return an array of Hpricot::Elem objects for the results found by the query matching the filter regexp" do
+ get_elements(:li, /^item \d$/).size.should == 3
+ get_elements(:li, /^item (1|2)$/).size.should == 2
+ end
+ end
+
+ it "should raise an error if the ouput is not specified and cannot be found" do
+ @output, @response_output, @controller = nil
+
+ lambda { tag("div") }.should raise_error("The response output was not in it's usual places, please provide the output")
+ end
+
+ it "should use @output if no output parameter is supplied" do
+ @output.should_receive(:content_for)
+
+ tag(:div)
+ end
+
+ it "should use @output_response if no output parameter is supplied and @output does not contain output" do
+ @output = nil
+ @response_output = test_response
+
+ tag(:div)
+ end
+
+ it "should use @controller.body if no output parameter is supplied and both @output and @response_output do not contain output" do
+ @output, @response_output = nil
+ @controller = mock(:controller)
+ @controller.should_receive(:nil?).and_return false
+ @controller.should_receive(:body).any_number_of_times.and_return test_response
+
+ tag(:div)
+ end
+
+ def test_response
+ <<-EOR
+<html>
+ <body>
+ <div>Hello, World!</div>
+ <ul>
+ <li>item 1</li>
+ <li>item 2</li>
+ <li>item 3</li>
+ </ul
+ </body>
+</html>
+EOR
+ end
+end
View
24 merb-test/spec/merb-test/fake_request_spec.rb
@@ -0,0 +1,24 @@
+require File.dirname(__FILE__) + '/../spec_helper'
+
+describe Merb::Test::FakeRequest, ".new(env = {}, req = StringIO.new)" do
+ it "should create request with default enviroment, minus rack.input" do
+ @mock = Merb::Test::FakeRequest.new
+ @mock.env.except('rack.input').should == Merb::Test::FakeRequest::DEFAULT_ENV
+ end
+
+ it "should override default env values passed in HTTP format" do
+ @mock = Merb::Test::FakeRequest.new('HTTP_ACCEPT' => 'nothing')
+ @mock.env['HTTP_ACCEPT'].should == 'nothing'
+ end
+
+ it "should override default env values passed in symbol format" do
+ @mock = Merb::Test::FakeRequest.new(:http_accept => 'nothing')
+ @mock.env['HTTP_ACCEPT'].should == 'nothing'
+ end
+
+ it "should set rack input to an empty StringIO" do
+ @mock = Merb::Test::FakeRequest.new
+ @mock.env['rack.input'].should be_kind_of(StringIO)
+ @mock.env['rack.input'].read.should == ''
+ end
+end
View
49 merb-test/spec/merb-test/multipart_spec.rb
@@ -0,0 +1,49 @@
+require File.dirname(__FILE__) + '/../spec_helper'
+
+describe Merb::Test::Multipart::Param, '.to_multipart' do
+ it "should represent the key and value correctly" do
+ param = Merb::Test::Multipart::Param.new('foo', 'bar')
+ param.to_multipart.should == %(Content-Disposition: form-data; name="foo"\r\n\r\nbar\r\n)
+ end
+end
+
+describe Merb::Test::Multipart::FileParam, '.to_multipart' do
+ it "should represent the key, filename and content correctly" do
+ param = Merb::Test::Multipart::FileParam.new('foo', '/bar.txt', 'baz')
+ param.to_multipart.should == %(Content-Disposition: form-data; name="foo"; filename="/bar.txt"\r\nContent-Type: text/plain\r\n\r\nbaz\r\n)
+ end
+end
+
+describe Merb::Test::Multipart::Post, '.push_params(params) param parsing' do
+ before(:each) do
+ @fake_return_param = mock('fake return_param')
+ end
+
+ it "should create Param from params when param doesn't respond to read" do
+ params = { 'normal' => 'normal_param' }
+ Merb::Test::Multipart::Param.should_receive(:new).with('normal', 'normal_param').and_return(@fake_return_param)
+ Merb::Test::Multipart::Post.new.push_params(params)
+ end
+
+ it "should create FileParam from params when param does response to read" do
+ file_param = mock('file param')
+ file_param.should_receive(:read).and_return('file contents')
+ file_param.should_receive(:path).and_return('file.txt')
+ params = { 'file' => file_param }
+ Merb::Test::Multipart::FileParam.should_receive(:new).with('file', 'file.txt', 'file contents').and_return(@fake_return_param)
+ Merb::Test::Multipart::Post.new.push_params(params)
+ end
+end
+
+describe Merb::Test::Multipart::Post, '.to_multipart' do
+ it "should create a multipart request from the params" do
+ file_param = mock('file param')
+ file_param.should_receive(:read).and_return('file contents')
+ file_param.should_receive(:path).and_return('file.txt')
+ params = { 'file' => file_param, 'normal' => 'normal_param' }
+ multipart = Merb::Test::Multipart::Post.new(params)
+ query, content_type = multipart.to_multipart
+ content_type.should == "multipart/form-data, boundary=----------0xKhTmLbOuNdArY"
+ query.should == "------------0xKhTmLbOuNdArY\r\nContent-Disposition: form-data; name=\"file\"; filename=\"file.txt\"\r\nContent-Type: text/plain\r\n\r\nfile contents\r\n------------0xKhTmLbOuNdArY\r\nContent-Disposition: form-data; name=\"normal\"\r\n\r\nnormal_param\r\n------------0xKhTmLbOuNdArY--"
+ end
+end
View
6 merb-test/spec/spec_helper.rb
@@ -0,0 +1,6 @@
+require 'rubygems'
+require "merb-core"
+
+Merb.start :environment => "test", :adapter => "runner"
+
+require File.join( File.dirname(__FILE__), "..", "lib", "merb-test" )

0 comments on commit ce0365f

Please sign in to comment.