ruby gem test-unit-1.2.3源码分析

165 篇文章 0 订阅
57 篇文章 0 订阅
ruby gem test-unit-1.2.3源码分析
先看lib/test/unit.rb
这里面注释太多了,用vim打开后,先把注释去掉
:g/^\s*#/d
去掉注释后,把代码露出来,如下:
module Test # :nodoc:
  module Unit
    def self.run=(flag)
      @run = flag
    end
    def self.run?
      @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.
module Test
  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"
      # Creates a new TestSuite with the given name.
      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
      # Adds the test to the suite.
      def <<(test)
        @tests << test
        self
      end
      def delete(test)
        @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
      # 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
这段代码基本上能看懂。好象是往一个数组中加测试用例,然后再批量执行。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值