# Author:: Nicolas Pouillard . # Copyright:: Copyright (c) 2004, 2005 TTK team. All rights reserved. # License:: LGPL # $Id: Collection.rb 586 2005-04-30 01:28:29Z ertai $ module TTK module Strategies class Collection < Composite include Abstract def initialize ( *a, &b ) @attributes = OHash.new super end # Create a new test, using the current loader, # and add it to the collection. def create ( anObject=strategyclass, &block ) super do |t| @log.debug{"assign collection attributes #{@attributes.size}"} @attributes.each do |k,v| @log.debug{"assign attribute: #{k}"} next if k == :contents t.assign_one(k, v, true) end if @attributes.has_key? :contents t.assign_one(:contents, @attributes[:contents], true) end block[t] if block_given? @attributes.each do |k,v| t.reject k if t.respond_to? k and t.send(k) == v end end end def prologue super # dump the attributes attr = @attributes.dup.delete_if do |k,v| v.nil? or v.respond_to?(:hidden) or k == :aliases end unless attr.empty? @log.new_node :attributes do attr.each { |k, v| @log[k] = v } end end end protected :prologue def internal_create ( doc, name=nil ) doc[:name] ||= name unless name.nil? doc[:strategy] ||= strategyclass create(doc) end private :internal_create #FIXME: Collection should create the tests in its contents during its # prologue as every other Composite do. def contents= ( other ) # :nodoc: if other.is_a? Hash raise ArgumentError, 'hash not ordered' unless other.ordered? other.each do |name, doc| if doc.is_a? Hash internal_create(doc, name) else create(doc) end end elsif other.is_a? Array other.each do |doc| if doc.is_a? Hash if doc.size == 1 and (tab = doc.to_a[0])[1].is_a? Hash internal_create(tab[1], tab[0]) else internal_create(doc) end elsif doc.is_a? String internal_create(:name => doc) else create(doc) end end else raise ArgumentError, 'bad contents type' end end def attributes=(other) # :nodoc: other.each { |k,v| @attributes[k] = v } end def strategyclass= ( aClass ) @attributes[:strategy] = aClass end def strategyclass @attributes[:strategy] end def testify_options super.merge(:strategy => strategyclass) end protected :testify_options def method_missing(key, val=nil, *a) # :nodoc: super unless a.empty? key = key.to_s if key =~ /^(.*)=$/ @attributes[$1] = val else @attributes[key] end end attribute :attributes, 'the common part of the suite', :invisible do OHash.new end end # class Collection end # module Strategies end # module TTK