Skip to content

Commit

Permalink
add format test matcher
Browse files Browse the repository at this point in the history
* Test format case insensitive.
* Use format test matcher in the test suite.
* Add format test matcher to readme file.
  • Loading branch information
yanivpr authored and Yaniv Preiss committed Dec 6, 2015
1 parent 49fdad1 commit a14b6ec
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,10 @@ describe MyUploader do
it "should make the image readable only to the owner and not executable" do
@uploader.should have_permissions(0600)
end

it "should be the correct format" do
@uploader.should be_format('png')
end
end
```

Expand Down
41 changes: 41 additions & 0 deletions lib/carrierwave/test/matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,39 @@ def be_no_taller_than(height)
BeNoTallerThan.new(height)
end

class BeFormat # :nodoc:
def initialize(expected)
@expected = expected
end

def matches?(actual)
@actual = actual
# Satisfy expectation here. Return false or raise an error if it's not met.
image = ImageLoader.load_image(@actual.current_path)
@actual_expected = image.format
!@expected.nil? && @actual_expected.casecmp(@expected).zero?
end

def failure_message
"expected #{@actual.current_path.inspect} to have #{@expected} format, but it was #{@actual_expected}."
end

def failure_message_when_negated
"expected #{@actual.current_path.inspect} not to have #{@expected} format, but it did."
end

def description
"have #{@expected} format"
end

# RSpec 2 compatibility:
alias_method :negative_failure_message, :failure_message_when_negated
end

def be_format(expected)
BeFormat.new(expected)
end

class ImageLoader # :nodoc:
def self.load_image(filename)
if defined? ::MiniMagick
Expand Down Expand Up @@ -330,6 +363,10 @@ def height
image.rows
end

def format
image.format
end

def initialize(filename)
@image = ::Magick::Image.read(filename).first
end
Expand All @@ -345,6 +382,10 @@ def height
image[:height]
end

def format
image[:format]
end

def initialize(filename)
@image = ::MiniMagick::Image.open(filename)
end
Expand Down
3 changes: 1 addition & 2 deletions spec/processing/mini_magick_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@
describe "#convert" do
it "should convert from one format to another" do
@instance.convert('png')
img = ::MiniMagick::Image.open(@instance.current_path)
expect(img['format']).to match(/PNG/)
expect(@instance.file.extension).to eq('png')
expect(@instance).to be_format('png')
end

it "should convert all pages when no page number is specified" do
Expand Down
2 changes: 1 addition & 1 deletion spec/processing/rmagick_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
describe '#convert' do
it "should convert the image to the given format" do
@instance.convert(:png)
expect(::Magick::Image.read(@instance.current_path).first.format).to eq('PNG')
expect(@instance.file.extension).to eq('png')
expect(@instance).to be_format('png')
end
end

Expand Down

0 comments on commit a14b6ec

Please sign in to comment.