Organizando plugins e gems.

This commit is contained in:
2009-07-16 11:51:47 -03:00
parent 4e22c87074
commit dcfc38eb09
506 changed files with 10538 additions and 45562 deletions

View File

@@ -0,0 +1,18 @@
require File.join(File.dirname(__FILE__), '..', 'test_helper')
class AutoloadMacroTest < Test::Unit::TestCase # :nodoc:
context "The macro auto-loader" do
should "load macros from the plugins" do
assert self.class.respond_to?('plugin_macro')
end
should "load macros from the gems" do
assert self.class.respond_to?('gem_macro')
end
should "load custom macros from ROOT/test/shoulda_macros" do
assert self.class.respond_to?('custom_macro')
end
end
end

View File

@@ -0,0 +1,145 @@
require File.join(File.dirname(__FILE__), '..', 'test_helper')
class ContextTest < Test::Unit::TestCase # :nodoc:
def self.context_macro(&blk)
context "with a subcontext made by a macro" do
setup { @context_macro = :foo }
merge_block &blk
end
end
# def self.context_macro(&blk)
# context "with a subcontext made by a macro" do
# setup { @context_macro = :foo }
# yield # <- this doesn't work.
# end
# end
context "context with setup block" do
setup do
@blah = "blah"
end
should "run the setup block" do
assert_equal "blah", @blah
end
should "have name set right" do
assert_match(/^test: context with setup block/, self.to_s)
end
context "and a subcontext" do
setup do
@blah = "#{@blah} twice"
end
should "be named correctly" do
assert_match(/^test: context with setup block and a subcontext should be named correctly/, self.to_s)
end
should "run the setup blocks in order" do
assert_equal @blah, "blah twice"
end
end
context_macro do
should "have name set right" do
assert_match(/^test: context with setup block with a subcontext made by a macro should have name set right/, self.to_s)
end
should "run the setup block of that context macro" do
assert_equal :foo, @context_macro
end
should "run the setup block of the main context" do
assert_equal "blah", @blah
end
end
end
context "another context with setup block" do
setup do
@blah = "foo"
end
should "have @blah == 'foo'" do
assert_equal "foo", @blah
end
should "have name set right" do
assert_match(/^test: another context with setup block/, self.to_s)
end
end
context "context with method definition" do
setup do
def hello; "hi"; end
end
should "be able to read that method" do
assert_equal "hi", hello
end
should "have name set right" do
assert_match(/^test: context with method definition/, self.to_s)
end
end
context "another context" do
should "not define @blah" do
assert_nil @blah
end
end
context "context with multiple setups and/or teardowns" do
cleanup_count = 0
2.times do |i|
setup { cleanup_count += 1 }
teardown { cleanup_count -= 1 }
end
2.times do |i|
should "call all setups and all teardowns (check ##{i + 1})" do
assert_equal 2, cleanup_count
end
end
context "subcontexts" do
2.times do |i|
setup { cleanup_count += 1 }
teardown { cleanup_count -= 1 }
end
2.times do |i|
should "also call all setups and all teardowns in parent and subcontext (check ##{i + 1})" do
assert_equal 4, cleanup_count
end
end
end
end
should_eventually "pass, since it's unimplemented" do
flunk "what?"
end
should_eventually "not require a block when using should_eventually"
should "pass without a block, as that causes it to piggyback to should_eventually"
context "context for testing should piggybacking" do
should "call should_eventually as we are not passing a block"
end
context "context" do
context "with nested subcontexts" do
should_eventually "only print this statement once for a should_eventually"
end
end
end

View File

@@ -0,0 +1,63 @@
require 'test/unit'
class ConvertToShouldSyntaxTest < Test::Unit::TestCase # :nodoc:
BEFORE_FIXTURE = <<-EOS
class DummyTest < Test::Unit::TestCase
should "Not change this_word_with_underscores" do
end
def test_should_be_working
assert true
end
def test_some_cool_stuff
assert true
end
def non_test_method
end
end
EOS
AFTER_FIXTURE = <<-EOS
class DummyTest < Test::Unit::TestCase
should "Not change this_word_with_underscores" do
end
should "be working" do
assert true
end
should "RENAME ME: test some cool stuff" do
assert true
end
def non_test_method
end
end
EOS
FIXTURE_PATH = "./convert_to_should_syntax_fixture.dat"
RUBY = ENV['RUBY'] || 'ruby'
def test_convert_to_should_syntax
File.open(FIXTURE_PATH, "w") {|f| f.write(BEFORE_FIXTURE)}
cmd = "#{RUBY} #{File.join(File.dirname(__FILE__), '../../bin/convert_to_should_syntax')} #{FIXTURE_PATH}"
output = `#{cmd}`
File.unlink($1) if output.match(/has been stored in '([^']+)/)
assert_match(/has been converted/, output)
result = IO.read(FIXTURE_PATH)
assert_equal result, AFTER_FIXTURE
end
def teardown
File.unlink(FIXTURE_PATH)
end
end

View File

@@ -0,0 +1,241 @@
require File.join(File.dirname(__FILE__), '..', 'test_helper')
require 'action_mailer'
require 'mocha'
class HelpersTest < Test::Unit::TestCase # :nodoc:
context "given delivered emails" do
setup do
email1 = stub(:subject => "one", :to => ["none1@email.com"])
email2 = stub(:subject => "two", :to => ["none2@email.com"])
ActionMailer::Base.stubs(:deliveries).returns([email1, email2])
end
should "have sent an email" do
assert_sent_email
assert_raises(Test::Unit::AssertionFailedError) do
assert_did_not_send_email
end
end
should "find email one" do
assert_sent_email do |e|
e.subject =~ /one/
end
end
should "not find an email that doesn't exist" do
assert_raises(Test::Unit::AssertionFailedError) do
assert_sent_email do |e|
e.subject =~ /whatever/
end
end
end
end
context "when there are no emails" do
setup do
ActionMailer::Base.stubs(:deliveries).returns([])
end
should "not have sent an email" do
assert_did_not_send_email
assert_raises(Test::Unit::AssertionFailedError) do
assert_sent_email
end
end
end
context "an array of values" do
setup do
@a = ['abc', 'def', 3]
end
[/b/, 'abc', 3].each do |x|
should "contain #{x.inspect}" do
assert_raises(Test::Unit::AssertionFailedError) do
assert_does_not_contain @a, x
end
assert_contains @a, x
end
end
should "not contain 'wtf'" do
assert_raises(Test::Unit::AssertionFailedError) {assert_contains @a, 'wtf'}
assert_does_not_contain @a, 'wtf'
end
should "be the same as another array, ordered differently" do
assert_same_elements(@a, [3, "def", "abc"])
assert_raises(Test::Unit::AssertionFailedError) do
assert_same_elements(@a, [3, 3, "def", "abc"])
end
assert_raises(Test::Unit::AssertionFailedError) do
assert_same_elements([@a, "abc"].flatten, [3, 3, "def", "abc"])
end
end
end
context "an array of values" do
setup do
@a = [1, 2, "(3)"]
end
context "after adding another value" do
setup do
@a.push(4)
end
should_change "@a.length", :by => 1
should_change "@a.length", :from => 3
should_change "@a.length", :to => 4
should_change "@a[0]", :by => 0
should_not_change "@a[0]"
end
context "after replacing it with an array of strings" do
setup do
@a = %w(a b c d e f)
end
should_change "@a.length", :by => 3
should_change "@a.length", :from => 3, :to => 6, :by => 3
should_change "@a[0]"
should_change "@a[1]", :from => 2, :to => "b"
should_change "@a[2]", :from => /\d/, :to => /\w/
should_change "@a[3]", :to => String
end
end
context "assert_good_value" do
should "validate a good email address" do
assert_good_value User.new, :email, "good@example.com"
end
should "validate a good SSN with a custom message" do
assert_good_value User.new, :ssn, "xxxxxxxxx", /length/
end
should "fail to validate a bad email address" do
assert_raises Test::Unit::AssertionFailedError do
assert_good_value User.new, :email, "bad"
end
end
should "fail to validate a bad SSN that is too short" do
assert_raises Test::Unit::AssertionFailedError do
assert_good_value User.new, :ssn, "x", /length/
end
end
should "accept a class as the first argument" do
assert_good_value User, :email, "good@example.com"
end
context "with an instance variable" do
setup do
@product = Product.new(:tangible => true)
end
should "use that instance variable" do
assert_good_value Product, :price, "9999", /included/
end
end
end
context "assert_bad_value" do
should "invalidate a bad email address" do
assert_bad_value User.new, :email, "bad"
end
should "invalidate a bad SSN with a custom message" do
assert_bad_value User.new, :ssn, "x", /length/
end
should "fail to invalidate a good email address" do
assert_raises Test::Unit::AssertionFailedError do
assert_bad_value User.new, :email, "good@example.com"
end
end
should "fail to invalidate a good SSN" do
assert_raises Test::Unit::AssertionFailedError do
assert_bad_value User.new, :ssn, "xxxxxxxxx", /length/
end
end
should "accept a class as the first argument" do
assert_bad_value User, :email, "bad"
end
context "with an instance variable" do
setup do
@product = Product.new(:tangible => true)
end
should "use that instance variable" do
assert_bad_value Product, :price, "0", /included/
end
end
end
context "a matching matcher" do
setup do
@matcher = stub('matcher', :matches? => true,
:failure_message => 'bad failure message',
:negative_failure_message => 'big time failure')
end
should "pass when given to assert_accepts" do
assert_accepts @matcher, 'target'
end
context "when given to assert_rejects" do
setup do
begin
assert_rejects @matcher, 'target'
rescue Test::Unit::AssertionFailedError => @error
end
end
should "fail" do
assert_not_nil @error
end
should "use the error message from the matcher" do
assert_equal 'big time failure', @error.message
end
end
end
context "a non-matching matcher" do
setup do
@matcher = stub('matcher', :matches? => false,
:failure_message => 'big time failure',
:negative_failure_message => 'bad failure message')
end
should "pass when given to assert_rejects" do
assert_rejects @matcher, 'target'
end
context "when given to assert_accepts" do
setup do
begin
assert_accepts @matcher, 'target'
rescue Test::Unit::AssertionFailedError => @error
end
end
should "fail" do
assert_not_nil @error
end
should "use the error message from the matcher" do
assert_equal 'big time failure', @error.message
end
end
end
end

View File

@@ -0,0 +1,34 @@
require File.join(File.dirname(__FILE__), '..', 'test_helper')
class PrivateHelpersTest < Test::Unit::TestCase # :nodoc:
include Shoulda::Private
context "get_options!" do
should "remove opts from args" do
args = [:a, :b, {}]
get_options!(args)
assert_equal [:a, :b], args
end
should "return wanted opts in order" do
args = [{:one => 1, :two => 2}]
one, two = get_options!(args, :one, :two)
assert_equal 1, one
assert_equal 2, two
end
should "raise ArgumentError if given unwanted option" do
args = [{:one => 1, :two => 2}]
assert_raises ArgumentError do
get_options!(args, :one)
end
end
end
class ::SomeModel; end
context "model_class" do
should "sniff the class constant from the test class" do
self.expects(:name).returns("SomeModelTest")
assert_equal SomeModel, model_class
end
end
end

View File

@@ -0,0 +1,266 @@
require File.join(File.dirname(__FILE__), '..', 'test_helper')
class ShouldTest < Test::Unit::TestCase # :nodoc:
should "be able to define a should statement outside of a context" do
assert true
end
should "see the name of my class as ShouldTest" do
assert_equal "ShouldTest", self.class.name
end
def self.should_see_class_methods
should "be able to see class methods" do
assert true
end
end
def self.should_see_a_context_block_like_a_Test_Unit_class
should "see a context block as a Test::Unit class" do
assert_equal "ShouldTest", self.class.name
end
end
def self.should_see_blah
should "see @blah through a macro" do
assert @blah
end
end
def self.should_not_see_blah
should "not see @blah through a macro" do
assert_nil @blah
end
end
def self.should_be_able_to_make_context_macros(prefix = nil)
context "a macro" do
should "have the tests named correctly" do
assert_match(/^test: #{prefix}a macro should have the tests named correctly/, self.to_s)
end
end
end
context "Context" do
should_see_class_methods
should_see_a_context_block_like_a_Test_Unit_class
should_be_able_to_make_context_macros("Context ")
should "not define @blah" do
assert ! self.instance_variables.include?("@blah")
end
should_not_see_blah
should "be able to define a should statement" do
assert true
end
should "see the name of my class as ShouldTest" do
assert_equal "ShouldTest", self.class.name
end
context "with a subcontext" do
should_be_able_to_make_context_macros("Context with a subcontext ")
end
end
context "Context with setup block" do
setup do
@blah = "blah"
end
should "have @blah == 'blah'" do
assert_equal "blah", @blah
end
should_see_blah
should "have name set right" do
assert_match(/^test: Context with setup block/, self.to_s)
end
context "and a subcontext" do
setup do
@blah = "#{@blah} twice"
end
should "be named correctly" do
assert_match(/^test: Context with setup block and a subcontext should be named correctly/, self.to_s)
end
should "run the setup methods in order" do
assert_equal @blah, "blah twice"
end
should_see_blah
end
end
context "Another context with setup block" do
setup do
@blah = "foo"
end
should "have @blah == 'foo'" do
assert_equal "foo", @blah
end
should "have name set right" do
assert_match(/^test: Another context with setup block/, self.to_s)
end
should_see_blah
end
should_eventually "pass, since it's a should_eventually" do
flunk "what?"
end
# Context creation and naming
def test_should_create_a_new_context
assert_nothing_raised do
Shoulda::Context.new("context name", self) do; end
end
end
def test_should_create_a_nested_context
assert_nothing_raised do
parent = Shoulda::Context.new("Parent", self) do; end
child = Shoulda::Context.new("Child", parent) do; end
end
end
def test_should_name_a_contexts_correctly
parent = Shoulda::Context.new("Parent", self) do; end
child = Shoulda::Context.new("Child", parent) do; end
grandchild = Shoulda::Context.new("GrandChild", child) do; end
assert_equal "Parent", parent.full_name
assert_equal "Parent Child", child.full_name
assert_equal "Parent Child GrandChild", grandchild.full_name
end
# Should statements
def test_should_have_should_hashes_when_given_should_statements
context = Shoulda::Context.new("name", self) do
should "be good" do; end
should "another" do; end
end
names = context.shoulds.map {|s| s[:name]}
assert_equal ["another", "be good"], names.sort
end
# setup and teardown
def test_should_capture_setup_and_teardown_blocks
context = Shoulda::Context.new("name", self) do
setup do; "setup"; end
teardown do; "teardown"; end
end
assert_equal "setup", context.setup_blocks.first.call
assert_equal "teardown", context.teardown_blocks.first.call
end
# building
def test_should_create_shoulda_test_for_each_should_on_build
context = Shoulda::Context.new("name", self) do
should "one" do; end
should "two" do; end
end
context.expects(:create_test_from_should_hash).with(has_entry(:name => "one"))
context.expects(:create_test_from_should_hash).with(has_entry(:name => "two"))
context.build
end
def test_should_create_test_methods_on_build
tu_class = Test::Unit::TestCase
context = Shoulda::Context.new("A Context", tu_class) do
should "define the test" do; end
end
tu_class.expects(:define_method).with(:"test: A Context should define the test. ")
context.build
end
def test_should_create_test_methods_on_build_when_subcontext
tu_class = Test::Unit::TestCase
context = Shoulda::Context.new("A Context", tu_class) do
context "with a child" do
should "define the test" do; end
end
end
tu_class.expects(:define_method).with(:"test: A Context with a child should define the test. ")
context.build
end
# Test::Unit integration
def test_should_create_a_new_context_and_build_it_on_Test_Unit_context
c = mock("context")
c.expects(:build)
Shoulda::Context.expects(:new).with("foo", kind_of(Class)).returns(c)
self.class.context "foo" do; end
end
def test_should_create_a_one_off_context_and_build_it_on_Test_Unit_should
s = mock("test")
Shoulda::Context.any_instance.expects(:should).with("rock", {}).returns(s)
Shoulda::Context.any_instance.expects(:build)
self.class.should "rock" do; end
end
def test_should_create_a_one_off_context_and_build_it_on_Test_Unit_should_eventually
s = mock("test")
Shoulda::Context.any_instance.expects(:should_eventually).with("rock").returns(s)
Shoulda::Context.any_instance.expects(:build)
self.class.should_eventually "rock" do; end
end
should "run a :before proc", :before => lambda { @value = "before" } do
assert_equal "before", @value
end
context "A :before proc" do
setup do
assert_equal "before", @value
@value = "setup"
end
should "run before the current setup", :before => lambda { @value = "before" } do
assert_equal "setup", @value
end
end
context "a before statement" do
setup do
assert_equal "before", @value
@value = "setup"
end
before_should "run before the current setup" do
@value = "before"
end
end
context "A context" do
setup do
@value = "outer"
end
context "with a subcontext and a :before proc" do
before = lambda do
assert "outer", @value
@value = "before"
end
should "run after the parent setup", :before => before do
assert_equal "before", @value
end
end
end
end