# Author:: Nicolas Pouillard . # Copyright:: Copyright (c) 2004, 2005 Uttk team. All rights reserved. # License:: LGPL # $Id: /w/fey/uttk/trunk/lib/uttk/strategies/Collection.rb 22102 2006-02-21T23:03:39.538964Z pouillar $ module Uttk module Strategies # I do the same job as the _Composite_ strategy, plus I provide the # _attributes_ attribute which allow to share common attributes of the # strategies I compose. class Collection < Composite include Abstract attr_reader :internal_contents protected :internal_contents def initialize ( *a, &b ) @attributes = OHash.new @internal_contents = [] 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 @attributes.each do |k,v| t.reject k if t.respond_to? k and t.send(k) == v end end end def prologue super # Create sub tests if @internal_contents.is_a? Hash unless @internal_contents.ordered? raise ArgumentError, 'hash not ordered' end @internal_contents.each do |name, doc| if doc.is_a? Hash internal_create(doc, name) else create(doc) end end elsif @internal_contents.is_a? Array @internal_contents.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_error 'bad contents type' end # 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 def contents= ( other ) # :nodoc: @internal_contents = other end def attributes(other=nil, &block) if other.nil? if block.nil? @attributes else self.attributes = HashEval.new(&block).hash end else self.attributes = other 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.update(: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 Uttk