# Author:: Nicolas Pouillard . # Copyright:: Copyright (c) 2004, 2005 TTK team. All rights reserved. # License:: LGPL # $Id: Loader.rb 575 2005-04-14 10:22:30Z polrop $ module TTK # TTK::Loaders are design to help you to make tests. # # The `testify' method is here to provide a usefull way # to convert anything to a test. # # For example Hash#testify instanciate a test from a hash: # # doc = { # :strategy => Cmd, # :command => 'echo -n foo', # :output => 'foo', # :symtbl => SYMTBL # } # # test = doc.testify # test.run # # You can also load a test from a string, an io or a file. # In this case the format can change (YAML, XML...). # To manage this you can use TTK::Loaders to add new loader easily. # # To implement a loader you just need to subclass the abstract # loader (TTK::Loaders::Loader), and implement the `load_doc' # method. # # The `load_doc' method take an IO as argument and return # something which can be testified. module Loaders def self.get_class ( aClassName ) return aClassName if aClassName.is_a? Class aClassName = "TTK::Strategies::#{aClassName}" unless aClassName =~ /::/ return eval(aClassName) if aClassName =~ /[\w:]+/ raise NameError, "not a valid class name #{aClassName}" end # FIXME: document me class Loader include Abstract # override me in subclasses def load_doc(io) io end def load ( anObject, &block ) anObject.testify(self, &block) end end # class Loader end # module Loaders end # module TTK class Pathname def testify ( aLoader, &block ) strategy = open { |f| f.testify(aLoader, &block) } strategy.symtbl[:pwd] = self.expand_path.dirname strategy end end class IO def testify ( aLoader, &block ) aLoader.load_doc(self).testify(aLoader, &block) end end class Class def testify ( aLoader, &block ) unless self < TTK::Strategies::Strategy raise ArgumentError, "Need a subclass of Strategy not #{name}" end new(&block) end end class Hash def testify ( aLoader=nil, &block ) if size == 1 and (tab = to_a[0])[1].is_a? Hash name, doc = tab doc[:name] = name return doc.testify(aLoader, &block) end unless has_key? :strategy raise ArgumentError, "no strategy for this test (#{inspect})" end TTK::Loaders.get_class(self[:strategy]).new do |t| block[t] if block_given? t.assign(self) end end end