# Author:: Nicolas Pouillard . # Copyright:: Copyright (c) 2004, 2005 TTK team. All rights reserved. # License:: LGPL # $Id: Block.rb 567 2005-04-13 08:00:06Z polrop $ module TTK module Strategies class Block < Strategy include Concrete def self.create ( *a, &block ) raise ArgumentError, 'need a block' unless block_given? strategy = new(*a) strategy.test = block strategy end def test=(block1=nil, &block2) if block_given? @test = block2 elsif !block1.nil? @test = block1 else raise ArgumentError, 'no block given' end if @test.is_a?(String) str = @test.dup @test = proc do eval str end end end def run_impl res = nil begin res = @test[*@args] rescue StatusException => ex raise ex rescue Exception => ex fail(ex) end pass if res fail(res) end attribute :test, 'a Proc to call with args', :mandatory, :invisible attribute :args, 'arguments for the `test\' proc', [] protected :run_impl, :epilogue end # closs Block end # module Strategies end # module TTK #FIXME: adapt me to a real unit test suite # if defined? TEST_MODE or __FILE__ == $0 # include TTK # include Strategies # $x = 0 # suite = Suite.new # suite.strategyclass = Block # suite.name = 'Test the Block strategy' # suite.symtbl = TTK::SymTbl.new # suite.symtbl[:loader] = Loaders::Yaml.new # t1 = Block.create { $x = $x.succ } # t1.name = 'test 1' # suite.create t1 # t2 = suite.create # t2.name = 'test 2' # t2.test = proc { $x = $x.succ } # t3 = suite.create # t3.name = 'test 3' # t3.test = '$x = $x.succ' # t4 = suite.create(Block.create { $x == 3 || fail("x != 3 (with x == #$x)") }) # t4.name = 'test 4 PASS' # t5 = suite.create(Block.create { fail("x != 3 (with x == #$x)") }) # t5.name = 'test 5 FAIL' # t5.weight = -1 # log = Logger.new(Dumpers::Yaml.new(STDOUT)) # suite.run(log) # end