Skip to content

Commit

Permalink
fix spec and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
le0pard committed Mar 13, 2013
1 parent 2dbf69a commit 516cce8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
16 changes: 10 additions & 6 deletions lib/webp/webp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ def webp_size(data)

# encode
def encode(input_file, output_file, options = {})
options_obj = Options.new options
res = C.webp_encode(input_file, output_file, options_obj.encode_pointer)
raise EncoderError, ENCODER_ERRORS[res - 1] unless 0 == res
unless 0 == (res = C.webp_encode(input_file, output_file, init_options_object(options).encode_pointer))
raise EncoderError, ENCODER_ERRORS[res - 1]
end
return true
end

# decode
def decode(input_file, output_file, options = {})
options_obj = Options.new options
res = C.webp_decode(input_file, output_file, options_obj.decode_pointer)
raise DecoderError, DECODER_ERRORS[res - 1] unless 0 == res
unless 0 == (res = C.webp_decode(input_file, output_file, init_options_object(options).decode_pointer))
raise DecoderError, DECODER_ERRORS[res - 1]
end
return true
end

Expand All @@ -53,5 +53,9 @@ def get_pointers_for_webp_size(data)
pointers
end

def init_options_object(options)
Options.new options
end

end
end
10 changes: 5 additions & 5 deletions spec/webp_ffi_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
it "#{image}.png image" do
in_filename = File.expand_path(File.join(File.dirname(__FILE__), "factories/#{image}.png"))
out_filename = File.expand_path(File.join(@out_dir, "#{image}.invpng.webp"))
expect { WebP.encode(in_filename, out_filename, crop_w: 30000) }.to raise_error WebP::EncoderError
expect { WebP.encode(in_filename, out_filename, crop_w: 30000) }.to raise_error WebP::EncoderError, /crop/i
end
end
end
Expand Down Expand Up @@ -161,16 +161,16 @@
it "#{image}.png image" do
in_filename = File.expand_path(File.join(File.dirname(__FILE__), "factories/#{image}.png"))
out_filename = File.expand_path(File.join(@out_dir, "#{image}.invpng.webp"))
expect { WebP.decode(in_filename, out_filename) }.to raise_error WebP::DecoderError
expect { WebP.decode(in_filename, out_filename) }.to raise_error WebP::DecoderError, /invalid webp/i
end
end
end
context "raise DecoderError on invalid options" do
factories[:png].each do |image|
it "#{image}.png image" do
in_filename = File.expand_path(File.join(File.dirname(__FILE__), "factories/#{image}.png"))
out_filename = File.expand_path(File.join(@out_dir, "#{image}.invpng.webp"))
expect { WebP.decode(in_filename, out_filename, crop_w: 30000) }.to raise_error WebP::DecoderError
in_filename = File.expand_path(File.join(File.dirname(__FILE__), "factories/#{image}.webp"))
out_filename = File.expand_path(File.join(@out_dir, "#{image}.png"))
expect { WebP.decode(in_filename, out_filename, crop_w: 30000) }.to raise_error WebP::DecoderError, /decoding failed/i
end
end
end
Expand Down

0 comments on commit 516cce8

Please sign in to comment.