Created
June 24, 2009 11:52
-
-
Save trans/135201 to your computer and use it in GitHub Desktop.
TestCase Shortcut
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Create test/unit test with testcase("xxx") { test("yyy") { ... } ... } notation. | |
| # Out-dated considering things like Shoulda. | |
| # (c)2003 Christian Neukirchen | |
| # | |
| # Has one small issue: | |
| # There is no way to close off blocks from the outside scope. | |
| # This can cause interference between separate tests. | |
| # So while this works for simple cases, it may well run into | |
| # problems with anything sizable. | |
| require 'test/unit' | |
| $TCGEN = "000000" | |
| $TGEN = "000000" | |
| #$TS = [] | |
| $TC = [nil] | |
| def testcase(name=nil, &block) | |
| tc = Class.new(Test::Unit::TestCase) | |
| #self.class.const_set("TC_#{(name||$TCGEN.succ!)}", tc) | |
| $TC << tc | |
| tc.class_eval &block | |
| end | |
| class Test::Unit::TestCase | |
| def self.gen_test_name | |
| @gen_test_name ||= "000000" | |
| @gen_test_name.succ! | |
| return "#{@gen_test_name}" | |
| end | |
| def self.test(name=nil, &block) | |
| __send__(:define_method, "test_#{(name||gen_test_name)}", &block) | |
| end | |
| end | |
| def test(name=nil, &block) | |
| $TC[0] ||= Class.new(Test::Unit::TestCase) | |
| self.class.const_set("TC#{$TCGEN.succ!}", $TC[0]) | |
| $TC[0].__send__(:define_method, "test_#{(name||$TGEN.succ!)}", &block) | |
| end | |
| #Test::Unit.run = !$DEBUG |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment