spec/unit/pathname_matcher_spec.rb in middleman-webp-1.0.1 vs spec/unit/pathname_matcher_spec.rb in middleman-webp-1.0.2

- old
+ new

@@ -12,11 +12,11 @@ ] patterns.each do |p| matcher = Middleman::WebP::PathnameMatcher.new(p) files.each do |f| - matcher.matches?(f).must_equal true, "Pattern: #{p.inspect}, "\ + value(matcher.matches?(f)).must_equal true, "Pattern: #{p.inspect}, "\ "file: #{f.inspect}" end end end @@ -29,11 +29,11 @@ ] patterns.each do |p| matcher = Middleman::WebP::PathnameMatcher.new(p) files.each do |f| - matcher.matches?(f).must_equal false, "Pattern: #{p.inspect}, "\ + value(matcher.matches?(f)).must_equal false, "Pattern: #{p.inspect}, "\ "file: #{f.inspect}" end end end @@ -44,14 +44,42 @@ 'a/b/c/d/e/f/g' ] matcher = Middleman::WebP::PathnameMatcher.new paths.each do |p| - matcher.matches?(p).must_equal true + value(matcher.matches?(p)).must_equal true end end it 'handles nil path gracefully and returns false' do Middleman::WebP::PathnameMatcher.new('*.jpg').matches? nil + end + end + + describe '#<=>' do + it 'sorts by pattern length' do + patterns = ['*.jpg', 'really_long_example_name.jpg', %r{^images/.*\.jpg$}] + matchers = patterns.map { |p| Middleman::WebP::PathnameMatcher.new(p) } + + sorted_matchers = matchers.sort + + value(sorted_matchers.map(&:pattern)).must_equal(patterns.sort_by { |p| p.to_s.length }) + end + + it 'sorts proc pattern smaller than string' do + patterns = ['*.jpg', Proc.new { |p| p == 'jpg' }, %r{^images/.*\.jpg$}] + matchers = patterns.map { |p| Middleman::WebP::PathnameMatcher.new(p) } + min_value = matchers.min + + value(min_value.pattern).must_equal(patterns[1]) + end + + it 'considers any procs equal' do + proc1 = Proc.new { |p| p == 'jpg' } + proc2 = Proc.new { |p| p == 'png' } + matcher1 = Middleman::WebP::PathnameMatcher.new(proc1) + matcher2 = Middleman::WebP::PathnameMatcher.new(proc2) + + value(matcher1 <=> matcher2).must_equal 0 end end end