# Author:: Nicolas Pouillard . # Copyright:: Copyright (c) 2004, 2005 Uttk team. All rights reserved. # License:: LGPL # $Id: /w/fey/uttk/trunk/lib/uttk/strategies/IOBased.rb 21844 2006-02-17T17:26:59.771162Z pouillar $ module Uttk module Strategies class IOBased < Strategy include Abstract # # Constructor # def initialize ( *a, &b ) stream :input stream :output stream :error super end # # Methods # def prologue super make_streams unless @input.nil? if @input.ref.is_a? Pathname @input.my = @input.ref else @input.my.open('w') do |my| my.print @input.ref.to_s_for_uttk_log end end end end protected :prologue def assertion @streams.each do |stream| next if invalid_stream?(stream) fail("#{stream.name} is different") unless stream.compare_streams end pass end protected :assertion def epilogue @streams.each { |strm| strm.clean unless strm.nil? } super end protected :epilogue def pass_hook super print_streams() if @verbose_print end protected :pass_hook def failed_hook super @streams.each do |stream| next if invalid_stream?(stream) @log["#{stream.name}_status"] = stream.status end print_streams() unless @quiet_print end protected :failed_hook def print_streams @streams.each do |stream| next if invalid_stream?(stream) @log << stream end end private :print_streams def invalid_stream? ( stream ) stream.nil? or stream.ref.nil? or stream.my.nil? or stream.name == 'input' or (stream.my.is_a? Pathname and !stream.my.exist?) or (stream.ref.is_a? Pathname and !stream.ref.exist?) end private :invalid_stream? def stream ( name ) @streams ||= [] @streams_name ||= [] @streams_name << name end def make_streams @streams = @streams_name.map do |name| stream = nil if instance_variables.include? "@#{name}" val = instance_variable_get("@#{name}") if val.nil? stream = @stream_class.new(name) else val = val.do_symtbl_gsub(@symtbl) if val.is_a? String tmp = TempPath.new tmp.open('w') { |res| res.print val } val = tmp.open('r') end stream = @stream_class.new(name, val) end end instance_variable_set "@#{name}", stream stream end end protected :make_streams # # Accessors # def stream_class= ( aClass ) if aClass.is_a? Class @stream_class = aClass else @stream_class = Streams.const_get(aClass) end end # # Attributes # attribute :input, 'the input reference', :invisible, nil attribute :output, 'the output reference', :invisible, nil attribute :error, 'the error reference', :invisible, nil attribute :stream_class, 'the stream class (< Streams::Stream)', Class, Streams::Stream attribute :verbose_print, 'print even if the test pass', false, :invisible attribute :quiet_print, 'quiet even if the test fails', false, :invisible end # class IOBased end # module Strategies end # module Uttk