ruby gem test-unit-1.2.3源码分析
先看lib/test/unit.rb
这里面注释太多了,用vim打开后,先把注释去掉
:g/^\s*#/d
去掉注释后,把代码露出来,如下:
module Test # :nodoc:
先看lib/test/unit.rb
这里面注释太多了,用vim打开后,先把注释去掉
:g/^\s*#/d
去掉注释后,把代码露出来,如下:
module Test # :nodoc:
module Unit
def self.run=(flag)
@run = flag
end
def self.run=(flag)
@run = flag
end
def self.run?
@run ||= false
end
end
end
@run ||= false
end
end
end
#at_exit do
# unless $! || Test::Unit.run?
# exit Test::Unit::AutoRunner.run
# end
#end
p Test::Unit.run?
Test::Unit.run=true
p Test::Unit.run?
我把那个at_exit注释掉,因为这个不知什么意思。然后打印方法的值,这个模块好象是实现一个单例模式,只要已经在跑test-unit,好象就不要再重复跑了。
我以为先看到这里。
接着分析下一个。
lib/test/unit/version.rb
这个很好理解,把版本号记下来。
lib/test/unit/testsuite.rb这个代码就有些意思了。
#--
#
# Author:: Nathaniel Talbott.
# Copyright:: Copyright (c) 2000-2003 Nathaniel Talbott. All rights reserved.
# License:: Ruby license.
# unless $! || Test::Unit.run?
# exit Test::Unit::AutoRunner.run
# end
#end
p Test::Unit.run?
Test::Unit.run=true
p Test::Unit.run?
我把那个at_exit注释掉,因为这个不知什么意思。然后打印方法的值,这个模块好象是实现一个单例模式,只要已经在跑test-unit,好象就不要再重复跑了。
我以为先看到这里。
接着分析下一个。
lib/test/unit/version.rb
这个很好理解,把版本号记下来。
lib/test/unit/testsuite.rb这个代码就有些意思了。
#--
#
# Author:: Nathaniel Talbott.
# Copyright:: Copyright (c) 2000-2003 Nathaniel Talbott. All rights reserved.
# License:: Ruby license.
module Test
module Unit
module Unit
# A collection of tests which can be #run.
#
# Note: It is easy to confuse a TestSuite instance with
# something that has a static suite method; I know because _I_
# have trouble keeping them straight. Think of something that
# has a suite method as simply providing a way to get a
# meaningful TestSuite instance.
class TestSuite
attr_reader :name, :tests
STARTED = name + "::STARTED"
FINISHED = name + "::FINISHED"
#
# Note: It is easy to confuse a TestSuite instance with
# something that has a static suite method; I know because _I_
# have trouble keeping them straight. Think of something that
# has a suite method as simply providing a way to get a
# meaningful TestSuite instance.
class TestSuite
attr_reader :name, :tests
STARTED = name + "::STARTED"
FINISHED = name + "::FINISHED"
# Creates a new TestSuite with the given name.
def initialize(name="Unnamed TestSuite")
@name = name
@tests = []
end
def initialize(name="Unnamed TestSuite")
@name = name
@tests = []
end
# Runs the tests and/or suites contained in this
# TestSuite.
def run(result, &progress_block)
yield(STARTED, name)
@tests.each do |test|
test.run(result, &progress_block)
end
yield(FINISHED, name)
end
# TestSuite.
def run(result, &progress_block)
yield(STARTED, name)
@tests.each do |test|
test.run(result, &progress_block)
end
yield(FINISHED, name)
end
# Adds the test to the suite.
def <<(test)
@tests << test
self
end
def <<(test)
@tests << test
self
end
def delete(test)
@tests.delete(test)
end
@tests.delete(test)
end
# Retuns the rolled up number of tests in this suite;
# i.e. if the suite contains other suites, it counts the
# tests within those suites, not the suites themselves.
def size
total_size = 0
@tests.each { |test| total_size += test.size }
total_size
end
def empty?
tests.empty?
end
# i.e. if the suite contains other suites, it counts the
# tests within those suites, not the suites themselves.
def size
total_size = 0
@tests.each { |test| total_size += test.size }
total_size
end
def empty?
tests.empty?
end
# Overridden to return the name given the suite at
# creation.
def to_s
@name
end
# It's handy to be able to compare TestSuite instances.
def ==(other)
return false unless(other.kind_of?(self.class))
return false unless(@name == other.name)
@tests == other.tests
end
end
end
end
这段代码基本上能看懂。好象是往一个数组中加测试用例,然后再批量执行。
# creation.
def to_s
@name
end
# It's handy to be able to compare TestSuite instances.
def ==(other)
return false unless(other.kind_of?(self.class))
return false unless(@name == other.name)
@tests == other.tests
end
end
end
end
这段代码基本上能看懂。好象是往一个数组中加测试用例,然后再批量执行。