test/helper.rb in assert-2.0.0.rc.1 vs test/helper.rb in assert-2.0.0

- old
+ new

@@ -1,76 +1,99 @@ -# this file is automatically required in when you require 'assert' -# put test helpers here +# this file is automatically required when you run `assert` +# put any test helpers here -require 'stringio' - -# test/.. (root dir for gem) +# add the root dir to the load path $LOAD_PATH.unshift(File.expand_path("../..", __FILE__)) -# This is the suite intended to be used in the tests, this is seperate from Assert.suite which is -# the actual suite being used to run the tests, confused? Don't use Assert.suite in your tests, -# use TEST_ASSERT_SUITE +# Force tests to run without halting on failures (needed so all tests will run +# properly). For halt on fail behavior testing, the context of those tests +# configures Assert temporarily as needed. + +class Assert::Context + def setup + Assert.config.halt_on_fail false + # Note: don't mess with the `show_output` setting in this setup block. Doing + # so will break the capture output tests. If you really need to set it one + # way or the other, do so in the `.assert.rb` local settings file. + end + + # a context for use in testing all the different context singleton methods + class ContextSingletonTests < Assert::Context + desc "Assert context singleton" + setup do + @orig_assert_suite = Assert.suite + Assert.config.suite TEST_ASSERT_SUITE + @test = Factory.test + @context_class = @test.context_class + end + teardown do + TEST_ASSERT_SUITE.tests.clear + Assert.config.suite @orig_assert_suite + end + subject{ @context_class } + + end + +end + + +# A Suite for use in the tests. It is seperate from `Assert.suite` +# (which is the actual suite being used to run the tests). Don't use +# `Assert.suite` in your tests, use TEST_ASSERT_SUITE + TEST_ASSERT_SUITE = Assert::Suite.new -# This is the test context intended to be used in the tests, and is also used in the context_class -# factory by default. This will ensure any contexts you define in your tests will not be shoved -# onto the the suite running the tests. +# A context for use in the tests and also in the `context_class` factory. This +# will ensure any contexts defined as part of the tests will add their methods +# to `TEST_ASSERT_SUITE` + class TestContext < Assert::Context def self.method_added(meth) if meth.to_s =~ Assert::Suite::TEST_METHOD_REGEX ci = Assert::Suite::ContextInfo.new(self, Factory.context_info_called_from) TEST_ASSERT_SUITE.tests << Assert::Test.new(meth.to_s, ci, meth) end end end -# force tests to run without halting on fail (needed for tests to run) -# anywhere we test halt on fail behavior, we take care of it in the specific context -class Assert::Context - def setup - Assert.config.halt_on_fail false - # Note: don't mess with `Assert.config.output` in this setup block - it will - # break the capture output tests. If you really need to set it one way or - # another, do it in the `.assert.rb` local settings file. +module Factory + + def self.context_info_called_from + "/path/to_file.rb:1234" end -end -module Factory - class << self + def self.context_info(context_class) + Assert::Suite::ContextInfo.new(context_class, context_info_called_from) + end - def context_info_called_from - "/path/to_file.rb:1234" - end + # Generate an anonymous `Context` inherited from `TestContext` by default. + # This provides a common interface for all contexts used in testing. - def context_info(context_class) - Assert::Suite::ContextInfo.new(context_class, context_info_called_from) + def self.context_class(inherit_from=nil, &block) + inherit_from ||= TestContext + klass = Class.new(inherit_from, &block) + default = (const_name = "FactoryAssertContext").dup + + while(Object.const_defined?(const_name)) do + const_name = "FactoryAssertContext#{rand(Time.now.to_i)}" end - # Generates an anonymous class inherited from whatever you pass or TextContext by default. This - # provides a common interface for all context classes to be generated in the tests. - def context_class(inherit_from = nil, &block) - inherit_from ||= TestContext - klass = Class.new(inherit_from, &block) - default = (const_name = "FactoryAssertContext").dup - while(Object.const_defined?(const_name)) do - const_name = "FactoryAssertContext#{rand(Time.now.to_i)}" - end - Object.const_set(const_name, klass) - klass - end + Object.const_set(const_name, klass) + klass + end - # Common interface for generating a new test, takes args and a block, will default everything - # if you need a no-op test. - def test(*args, &block) - name = (args[0] || "a test").to_s - context_info = args[1] || self.context_info(self.context_class) - test_block = (block || args[2] || ::Proc.new{}) + # Generate a no-op test for use in testing. - Assert::Test.new(name, context_info, &test_block) - end + def self.test(*args, &block) + name = (args[0] || "a test").to_s + context_info = args[1] || self.context_info(self.context_class) + test_block = (block || args[2] || ::Proc.new{}) - # Common interface for generating a new skip result - def skip_result(name, exception) - Assert::Result::Skip.new(Factory.test(name), exception) - end + Assert::Test.new(name, context_info, &test_block) + end + # Generate a skip result for use in testing. + + def self.skip_result(name, exception) + Assert::Result::Skip.new(Factory.test(name), exception) end + end