# Author:: Nicolas Pouillard . # Copyright:: Copyright (c) 2005 Uttk Team. All rights reserved. # License:: LGPL # Revision:: $Id: /w/fey/uttk/trunk/lib/uttk/filters/NodeCut.rb 21844 2006-02-17T17:26:59.771162Z pouillar $ module Uttk module Filters # FIXME Can be path based # This filter cut nodes which match `keep' but not `skip' # options: # keep: A regular expression (all keys which match this regexp are cut) # skip: Antoher regexp that skip keys which match # prune: Completly cut all sub nodes of a cut one class NodeCut < KeepSkipBased include Concrete default_options.merge! :prune => false # Don't prune by default def initialize ( *a, &b ) super @pruning_path = Set.new @pruning_path_node = nil end def new_node ( path, node ) if @pruning_path_node or keep? node.segment super elsif prune? @pruning_path << path.to_s @pruning_path_node = path.dup << node else @pruning_path << path.to_s end end protected :new_node def up ( path ) node = path.last path_s= path.to_s if @pruning_path.include? path_s @pruning_path_node = nil @pruning_path.delete path_s else super end end protected :up def notif ( msg, *args ) return super if args.first.nil? path = args.shift return if pruning_path? path copy = path.dup copy.delete_if do |x| skip? x.segment end super(msg, copy, *args) end protected :notif def prune? @options[:prune] end def pruning_path? ( path ) return false unless @pruning_path_node @pruning_path_node.each_with_index do |seg, i| return false if seg != path[i] end return true end module Assertions def assert_node_cut ( input, ref, options ) @mock_object.mock_clear @filter = NodeCut.new([@mock_object], options) input.each do |n| @filter.update(*Logger.make_notif(n)) end assert_mock Logger.make_notifs(ref) end end # module Assertions end # class NodeCut end # module Filters end # module Uttk