# Author:: Nicolas Pouillard . # Copyright:: Copyright (c) 2004, 2005 Uttk team. All rights reserved. # License:: LGPL # Revision:: $Id: /w/fey/uttk/trunk/lib/uttk/strategies/Block.rb 22117 2006-02-22T08:56:22.146625Z pouillar $ module Uttk module Strategies # I can evaluate a Ruby block. The test fails if block raise an # excption or return a false/nil value. class Block < IOBased include Concrete def self.create ( *a, &block ) raise ArgumentError, 'need a block' if block.nil? strategy = new(*a) strategy.test = block strategy end def test=(block1=nil, &block2) if block2 @test = block2 elsif not block1.nil? @test = block1 else raise ArgumentError, 'no block given' end end def prologue super if @test.is_a?(String) str = @test.dup @test = lambda { eval str.do_symtbl_gsub(@symtbl) } end @result, @raised_exception = nil, nil end protected :prologue def run_impl begin @result = @test[*@args] rescue Status => ex raise ex rescue Exception => ex @raised_exception = ex end end protected :run_impl def assertion fail @raised_exception if @raised_exception pass if @result fail @result end protected :assertion attribute :test, 'a Proc to call with args', :mandatory, :invisible attribute :args, 'arguments for the `test\' proc', [] end # closs Block end # module Strategies end # module Uttk #FIXME: adapt me to a real unit test suite # if defined? TEST_MODE or __FILE__ == $0 # include Uttk # include Strategies # $x = 0 # suite = Suite.new # suite.strategyclass = Block # suite.name = 'Test the Block strategy' # suite.symtbl = 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