spec/artwork/thumbnail_spec.rb in artwork-0.3.2 vs spec/artwork/thumbnail_spec.rb in artwork-0.4.0

- old
+ new

@@ -3,24 +3,27 @@ module Artwork describe Thumbnail do expected_defaults = {:compatible? => true} examples = { - :'320x' => {:width => 320, :retina? => false, :label => ''}, - :'320x_2x' => {:width => 320, :retina? => true, :label => ''}, - :'640x_2x' => {:width => 640, :retina? => true, :label => ''}, - :'640x' => {:width => 640, :retina? => false, :label => ''}, - :'1280x' => {:width => 1280, :retina? => false, :label => ''}, - :'1280x_2x' => {:width => 1280, :retina? => true, :label => ''}, - :'2000x' => {:width => 2000, :retina? => false, :label => ''}, - :'1500x_2x' => {:width => 1500, :retina? => true, :label => ''}, - :'320x_some_label' => {:width => 320, :retina? => false, :label => 'some_label'}, - :'320x_some_label_2x' => {:width => 320, :retina? => true, :label => 'some_label'}, - :'320x500' => {:width => 320, :retina? => false, :label => '500'}, - :'320x500_2x' => {:width => 320, :retina? => true, :label => '500'}, - :'320x500_crop' => {:width => 320, :retina? => false, :label => '500_crop'}, - :'320x500_crop_2x' => {:width => 320, :retina? => true, :label => '500_crop'}, + :'320x' => {:width => 320, :height => nil, :retina? => false, :label => nil}, + :'320x_2x' => {:width => 320, :height => nil, :retina? => true, :label => nil}, + :'640x_2x' => {:width => 640, :height => nil, :retina? => true, :label => nil}, + :'640x' => {:width => 640, :height => nil, :retina? => false, :label => nil}, + :'1280x' => {:width => 1280, :height => nil, :retina? => false, :label => nil}, + :'1280x_2x' => {:width => 1280, :height => nil, :retina? => true, :label => nil}, + :'2000x' => {:width => 2000, :height => nil, :retina? => false, :label => nil}, + :'1500x_2x' => {:width => 1500, :height => nil, :retina? => true, :label => nil}, + :'320x_some_label' => {:width => 320, :height => nil, :retina? => false, :label => 'some_label'}, + :'320x_some_label_2x' => {:width => 320, :height => nil, :retina? => true, :label => 'some_label'}, + :'320x500' => {:width => 320, :height => 500, :retina? => false, :label => nil}, + :'320x500_2x' => {:width => 320, :height => 500, :retina? => true, :label => nil}, + :'320x500_crop' => {:width => 320, :height => 500, :retina? => false, :label => 'crop'}, + :'320x500_crop_2x' => {:width => 320, :height => 500, :retina? => true, :label => 'crop'}, + :'320x_' => {:width => 320, :height => nil, :retina? => false, :label => ''}, + :'320x500_' => {:width => 320, :height => 500, :retina? => false, :label => ''}, + :'320x500__2x' => {:width => 320, :height => 500, :retina? => true, :label => ''}, :unsupported => {:compatible? => false}, 'unsupported_thumb' => {:compatible? => false}, } examples.each do |thumb_name, expected_properties| @@ -37,21 +40,57 @@ expect(Thumbnail.compatible?(thumb_name)).to eq compatible end end describe '#eq' do - it 'is true for different objects with the same width, label and retina flag' do + it 'is true for different objects with the same width, height, label and retina flag' do expect(Thumbnail.new(:'1200x500_crop_2x')).to eq Thumbnail.new('1200x500_crop_2x') expect(Thumbnail.new(:'1200x_2x')).to eq Thumbnail.new('1200x_2x') expect(Thumbnail.new(:'1200x')).to eq Thumbnail.new('1200x') expect(Thumbnail.new(:'1200x_black_and_white')).to eq Thumbnail.new('1200x_black_and_white') end - it 'is false for different objects if any of the width, label or retina flag differ' do + it 'is false for different objects if any of the width, height, label or retina flag differ' do expect(Thumbnail.new(:'1200x500_crop_2x')).not_to eq Thumbnail.new('1200x500_crop') expect(Thumbnail.new(:'1200x500_crop_2x')).not_to eq Thumbnail.new('1200x500crop_2x') expect(Thumbnail.new(:'1200x500_crop_2x')).not_to eq Thumbnail.new('1201x500_crop_2x') expect(Thumbnail.new(:'1200x500_crop_2x')).not_to eq Thumbnail.new('1500x') + expect(Thumbnail.new(:'1200x500_crop_2x')).not_to eq Thumbnail.new('1200x400_crop_2x') + end + end + + describe '#aspect_ratio' do + it 'is nil if height is not present or is zero' do + expect(Thumbnail.new('400x_label_2x').aspect_ratio).to be_nil + expect(Thumbnail.new('400x_2x').aspect_ratio).to be_nil + expect(Thumbnail.new('400x_').aspect_ratio).to be_nil + expect(Thumbnail.new('400x').aspect_ratio).to be_nil + expect(Thumbnail.new('400x0').aspect_ratio).to be_nil + end + + it 'is calculated as a decimal when both a width and a height are present' do + expect(Thumbnail.new('400x300').aspect_ratio).to be_within(0.1).of(1.33) + expect(Thumbnail.new('1600x900').aspect_ratio).to be_within(0.1).of(1.78) + expect(Thumbnail.new('1600x900_with_label').aspect_ratio).to be_within(0.1).of(1.78) + expect(Thumbnail.new('1600x900_with_label_2x').aspect_ratio).to be_within(0.1).of(1.78) + end + end + + describe '#same_aspect_ratio_with?' do + it 'returns nil if either thumbs have no clear height' do + expect(Thumbnail.new('400x300').same_aspect_ratio_with?(Thumbnail.new('400x'))).to be_nil + expect(Thumbnail.new('400x0').same_aspect_ratio_with?(Thumbnail.new('400x300'))).to be_nil + end + + it 'returns true if thumbs have aspect ratios within 0.1 of one another' do + expect(Thumbnail.new('400x300').same_aspect_ratio_with?(Thumbnail.new('400x300'))).to be true + expect(Thumbnail.new('400x300').same_aspect_ratio_with?(Thumbnail.new('1600x1200_with_label_2x'))).to be true + expect(Thumbnail.new('400x300').same_aspect_ratio_with?(Thumbnail.new('400x301'))).to be true + end + + it 'returns false if thumbs have different aspect ratios not within 0.1 of one another' do + expect(Thumbnail.new('400x300').same_aspect_ratio_with?(Thumbnail.new('400x200'))).to be false + expect(Thumbnail.new('400x300').same_aspect_ratio_with?(Thumbnail.new('5x10'))).to be false end end describe 'comparison' do it 'is based on the thumb width' do