Sha256: bd879d2465d1c3d57cb1e93fc05e0e6cb3b0155252add51b093837d3182d7f2e

Contents?: true

Size: 1.96 KB

Versions: 17

Compression:

Stored size: 1.96 KB

Contents

require 'set'

module IMWTest

  module CustomMatchers

    class PathsMatcher

      attr_accessor :given, :given_contents, :given_base, :to_match, :to_match_contents, :to_match_base

      def initialize given, options={}
        @given_base     = options[:given_base] || options[:relative_to]
        @to_match_base  = options[:to_match_base]
        @given          = given
        @given_contents = get_contents(given, given_base)
      end
      
      def matches? to_match
        @to_match          = to_match
        @to_match_contents = get_contents(to_match, to_match_base)
        to_match_contents == given_contents
      end

      def failure_message
        given_string    = given_contents.to_a.join("\n\t")
        to_match_string = to_match_contents.to_a.join("\n\t")          
        "expected contents to be identical.\n\ngiven #{given.inspect}:\n\t#{given_string}\n\nto match #{to_match}:\n\t#{to_match_string}"
      end

      def negative_failure_message
        "expected contents of #{given} and #{to_match} to be different"
      end

      protected
      def get_contents obj, base=nil
        if obj.is_a?(String) || obj.is_a?(Array)
          contents = [obj].flatten.map do |raw_path|
            path = File.expand_path(raw_path)
            if File.directory?(path)
              Dir[path + "/**/*"]
            else
              path
            end
          end.flatten
        else
          # obj is an IMW obj (archive or directory) so it has a
          # contents method
          contents = obj.send(obj.respond_to?(:all_contents) ? :all_contents : :contents)
        end
        if base
          contents.map do |path|
            new_path = path[base.length + 1..-1]
            new_path = nil if !new_path.nil? && new_path.size == 0
            new_path
          end.compact.to_set
        else
          contents.to_set
        end
      end
    end
    
    def contain_paths_like given, options={}
      PathsMatcher.new(given, options)
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
imw-0.2.18 spec/support/paths_matcher.rb
imw-0.2.17 spec/support/paths_matcher.rb
imw-0.2.16 spec/support/paths_matcher.rb
imw-0.2.15 spec/support/paths_matcher.rb
imw-0.2.14 spec/support/paths_matcher.rb
imw-0.2.13 spec/support/paths_matcher.rb
imw-0.2.12 spec/support/paths_matcher.rb
imw-0.2.11 spec/support/paths_matcher.rb
imw-0.2.10 spec/support/paths_matcher.rb
imw-0.2.9 spec/support/paths_matcher.rb
imw-0.2.8 spec/support/paths_matcher.rb
imw-0.2.7 spec/support/paths_matcher.rb
imw-0.2.6 spec/support/paths_matcher.rb
imw-0.2.5 spec/support/paths_matcher.rb
imw-0.2.4 spec/support/paths_matcher.rb
imw-0.2.3 spec/support/paths_matcher.rb
imw-0.2.2 spec/support/paths_matcher.rb