lib/test/unit/testcase.rb in test-unit-2.0.5 vs lib/test/unit/testcase.rb in test-unit-2.0.6

- old
+ new

@@ -210,10 +210,43 @@ # Tests are sorted in defined order. def test_order=(order) @@test_order = order end + # Defines a test in declarative syntax. + # + # The following two test definitions are the same: + # + # description "register user" + # def test_register_user + # ... + # end + # + # test "register user" do + # ... + # end + def test(test_description, &block) + normalized_description = test_description.gsub(/[^a-zA-Z\d_]+/, '_') + method_name = "test_#{normalized_description}".to_sym + define_method(method_name, &block) + description(test_description, method_name) + end + + # Describes a test. + # + # The following example associates "register a + # normal user" description with "test_register" + # test. + # + # description "register a normal user" + # def test_register + # ... + # end + def description(value, target=nil) + attribute(:description, value, {}, target || []) + end + # :stopdoc: private def collect_test_names method_names = public_instance_methods(true).collect do |name| name.to_s @@ -370,9 +403,18 @@ # Returns a human-readable name for the specific test that # this instance of TestCase represents. def name "#{@method_name}(#{self.class.name})" + end + + # Returns a description for the test. A description + # will be associated by Test::Unit::TestCase.test or + # Test::Unit::TestCase.description. + # + # Returns a name for the test for no description test. + def description + self[:description] || name end # Overridden to return #name. def to_s name