Skip to content

Commit

Permalink
Merge pull request rails#7749 from blowmage/minitest
Browse files Browse the repository at this point in the history
Improve support for minitest's spec DSL
  • Loading branch information
tenderlove committed Sep 25, 2012
2 parents a05a079 + 64f254c commit c96b20f
Show file tree
Hide file tree
Showing 16 changed files with 726 additions and 11 deletions.
15 changes: 12 additions & 3 deletions actionmailer/lib/action_mailer/test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ def initialize(name)
end

class TestCase < ActiveSupport::TestCase

# Use AM::TestCase for the base class when describing a mailer
register_spec_type(self) do |desc|
Class === desc && desc < ActionMailer::Base
end
register_spec_type(/Mailer( ?Test)?\z/i, self)

module Behavior
extend ActiveSupport::Concern

Expand Down Expand Up @@ -42,9 +49,11 @@ def mailer_class
end

def determine_default_mailer(name)
name.sub(/Test$/, '').constantize
rescue NameError
raise NonInferrableMailerError.new(name)
mailer = determine_constant_from_test_name(name) do |constant|
Class === constant && constant < ActionMailer::Base
end
raise NonInferrableMailerError.new(name) if mailer.nil?
mailer
end
end

Expand Down
37 changes: 37 additions & 0 deletions actionmailer/test/spec_type_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require 'abstract_unit'

class NotificationMailer < ActionMailer::Base; end
class Notifications < ActionMailer::Base; end

class SpecTypeTest < ActiveSupport::TestCase
def assert_mailer actual
assert_equal ActionMailer::TestCase, actual
end

def refute_mailer actual
refute_equal ActionMailer::TestCase, actual
end

def test_spec_type_resolves_for_class_constants
assert_mailer MiniTest::Spec.spec_type(NotificationMailer)
assert_mailer MiniTest::Spec.spec_type(Notifications)
end

def test_spec_type_resolves_for_matching_strings
assert_mailer MiniTest::Spec.spec_type("WidgetMailer")
assert_mailer MiniTest::Spec.spec_type("WidgetMailerTest")
assert_mailer MiniTest::Spec.spec_type("Widget Mailer Test")
# And is not case sensitive
assert_mailer MiniTest::Spec.spec_type("widgetmailer")
assert_mailer MiniTest::Spec.spec_type("widgetmailertest")
assert_mailer MiniTest::Spec.spec_type("widget mailer test")
end

def test_spec_type_wont_match_non_space_characters
refute_mailer MiniTest::Spec.spec_type("Widget Mailer\tTest")
refute_mailer MiniTest::Spec.spec_type("Widget Mailer\rTest")
refute_mailer MiniTest::Spec.spec_type("Widget Mailer\nTest")
refute_mailer MiniTest::Spec.spec_type("Widget Mailer\fTest")
refute_mailer MiniTest::Spec.spec_type("Widget MailerXTest")
end
end
144 changes: 144 additions & 0 deletions actionmailer/test/test_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,147 @@ def test_set_mailer_class_manual_using_string
assert_equal TestTestMailer, self.class.mailer_class
end
end

describe TestTestMailer do
it "gets the mailer from the test name" do
assert_equal TestTestMailer, self.class.mailer_class
end
end

describe TestTestMailer, :action do
it "gets the mailer from the test name" do
assert_equal TestTestMailer, self.class.mailer_class
end
end

describe TestTestMailer do
describe "nested" do
it "gets the mailer from the test name" do
assert_equal TestTestMailer, self.class.mailer_class
end
end
end

describe TestTestMailer, :action do
describe "nested" do
it "gets the mailer from the test name" do
assert_equal TestTestMailer, self.class.mailer_class
end
end
end

describe "TestTestMailer" do
it "gets the mailer from the test name" do
assert_equal TestTestMailer, self.class.mailer_class
end
end

describe "TestTestMailerTest" do
it "gets the mailer from the test name" do
assert_equal TestTestMailer, self.class.mailer_class
end
end

describe "TestTestMailer" do
describe "nested" do
it "gets the mailer from the test name" do
assert_equal TestTestMailer, self.class.mailer_class
end
end
end

describe "TestTestMailerTest" do
describe "nested" do
it "gets the mailer from the test name" do
assert_equal TestTestMailer, self.class.mailer_class
end
end
end

describe "AnotherCrazySymbolNameMailerTest" do
tests :test_test_mailer

it "gets the mailer after setting it with a symbol" do
assert_equal TestTestMailer, self.class.mailer_class
end
end

describe "AnotherCrazyStringNameMailerTest" do
tests 'test_test_mailer'

it "gets the mailer after setting it with a string" do
assert_equal TestTestMailer, self.class.mailer_class
end
end

describe "Another Crazy Name Mailer Test" do
tests TestTestMailer

it "gets the mailer after setting it manually" do
assert_equal TestTestMailer, self.class.mailer_class
end
end

describe "Another Crazy Symbol Name Mailer Test" do
tests :test_test_mailer

it "gets the mailer after setting it with a symbol" do
assert_equal TestTestMailer, self.class.mailer_class
end
end

describe "Another Crazy String Name Mailer Test" do
tests 'test_test_mailer'

it "gets the mailer after setting it with a string" do
assert_equal TestTestMailer, self.class.mailer_class
end
end

describe "AnotherCrazySymbolNameMailerTest" do
tests :test_test_mailer

describe "nested" do
it "gets the mailer after setting it with a symbol" do
assert_equal TestTestMailer, self.class.mailer_class
end
end
end

describe "AnotherCrazyStringNameMailerTest" do
tests 'test_test_mailer'

describe "nested" do
it "gets the mailer after setting it with a string" do
assert_equal TestTestMailer, self.class.mailer_class
end
end
end

describe "Another Crazy Name Mailer Test" do
tests TestTestMailer

describe "nested" do
it "gets the mailer after setting it manually" do
assert_equal TestTestMailer, self.class.mailer_class
end
end
end

describe "Another Crazy Symbol Name Mailer Test" do
tests :test_test_mailer

describe "nested" do
it "gets the mailer after setting it with a symbol" do
assert_equal TestTestMailer, self.class.mailer_class
end
end
end

describe "Another Crazy String Name Mailer Test" do
tests 'test_test_mailer'

it "gets the mailer after setting it with a string" do
assert_equal TestTestMailer, self.class.mailer_class
end
end
9 changes: 6 additions & 3 deletions actionpack/lib/action_controller/test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -347,10 +347,11 @@ def exists?
# assert_redirected_to page_url(:title => 'foo')
class TestCase < ActiveSupport::TestCase

# Use AS::TestCase for the base class when describing a model
# Use AC::TestCase for the base class when describing a controller
register_spec_type(self) do |desc|
Class === desc && desc < ActionController::Base
Class === desc && desc < ActionController::Metal
end
register_spec_type(/Controller( ?Test)?\z/i, self)

module Behavior
extend ActiveSupport::Concern
Expand Down Expand Up @@ -391,7 +392,9 @@ def controller_class
end

def determine_default_controller_class(name)
name.sub(/Test$/, '').safe_constantize
determine_constant_from_test_name(name) do |constant|
Class === constant && constant < ActionController::Metal
end
end

def prepare_controller_class(new_class)
Expand Down
3 changes: 3 additions & 0 deletions actionpack/lib/action_dispatch/testing/integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,9 @@ class IntegrationTest < ActiveSupport::TestCase
include ActionController::TemplateAssertions
include ActionDispatch::Routing::UrlFor

# Use AD::IntegrationTest for acceptance tests
register_spec_type(/(Acceptance|Integration) ?Test\z/i, self)

@@app = nil

def self.app
Expand Down
10 changes: 6 additions & 4 deletions actionpack/lib/action_view/test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ def initialize
end
end

# Use AV::TestCase for the base class for helpers and views
register_spec_type(/(Helper|View)( ?Test)?\z/i, self)

module Behavior
extend ActiveSupport::Concern

Expand Down Expand Up @@ -58,10 +61,9 @@ def tests(helper_class)
end

def determine_default_helper_class(name)
mod = name.sub(/Test$/, '').constantize
mod.is_a?(Class) ? nil : mod
rescue NameError
nil
determine_constant_from_test_name(name) do |constant|
Module === constant && !(Class === constant)
end
end

def helper_method(*methods)
Expand Down
Loading

0 comments on commit c96b20f

Please sign in to comment.