-
Notifications
You must be signed in to change notification settings - Fork 7
/
image_test.rb
executable file
·277 lines (237 loc) · 6.81 KB
/
image_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
require 'rubygems'
require 'test/unit'
require 'mocha'
require 'pathname'
require 'stringio'
require File.expand_path('../../lib/mini_magick', __FILE__)
#MiniMagick.processor = :gm
class ImageTest < Test::Unit::TestCase
include MiniMagick
CURRENT_DIR = File.dirname(File.expand_path(__FILE__)) + "/"
SIMPLE_IMAGE_PATH = CURRENT_DIR + "simple.gif"
MINUS_IMAGE_PATH = CURRENT_DIR + "simple-minus.gif"
TIFF_IMAGE_PATH = CURRENT_DIR + "leaves spaced.tiff"
NOT_AN_IMAGE_PATH = CURRENT_DIR + "not_an_image.php"
GIF_WITH_JPG_EXT = CURRENT_DIR + "actually_a_gif.jpg"
EXIF_IMAGE_PATH = CURRENT_DIR + "trogdor.jpg"
ANIMATION_PATH = CURRENT_DIR + "animation.gif"
def test_image_from_blob
File.open(SIMPLE_IMAGE_PATH, "rb") do |f|
image = Image.read(f.read)
assert image.valid?
image.destroy!
end
end
def test_image_open
image = Image.open(SIMPLE_IMAGE_PATH)
assert image.valid?
image.destroy!
end
def test_image_io_reading
buffer = StringIO.new(File.read(SIMPLE_IMAGE_PATH))
image = Image.read(buffer)
image.destroy!
end
def test_image_create
image = Image.create do |f|
f.write(File.read(SIMPLE_IMAGE_PATH))
end
image.destroy!
end
def test_image_new
image = Image.new(SIMPLE_IMAGE_PATH)
image.destroy!
end
def test_remote_image
image = Image.open("http://www.google.com/images/logos/logo.png")
image.valid?
image.destroy!
end
def test_image_write
output_path = "output.gif"
begin
image = Image.new(SIMPLE_IMAGE_PATH)
image.write output_path
assert File.exists?(output_path)
ensure
File.delete output_path
end
image.destroy!
end
def test_image_write_with_space_in_output_path
output_path = "test output.gif"
begin
image = Image.new(SIMPLE_IMAGE_PATH)
image.write output_path
assert File.exists?(output_path)
ensure
File.delete output_path
end
image.destroy!
end
def test_image_write_with_stream
stream = StringIO.new
image = Image.open(SIMPLE_IMAGE_PATH)
image.write("/tmp/foo.gif")
image.write(stream)
# assert Image.read(stream.string).valid?
image.destroy!
end
def test_not_an_image
image = Image.new(NOT_AN_IMAGE_PATH)
assert_equal false, image.valid?
image.destroy!
end
def test_throw_on_openining_not_an_image
assert_raise(MiniMagick::Invalid) do
image = Image.open(NOT_AN_IMAGE_PATH)
image.destroy
end
end
def test_image_meta_info
image = Image.new(SIMPLE_IMAGE_PATH)
assert_equal 150, image[:width]
assert_equal 55, image[:height]
assert_equal [150, 55], image[:dimensions]
assert_match(/^gif$/i, image[:format])
image.destroy!
end
def test_tiff
image = Image.new(TIFF_IMAGE_PATH)
assert_equal "tiff", image[:format].downcase
assert_equal 50, image[:width]
assert_equal 41, image[:height]
image.destroy!
end
def test_gif_with_jpg_format
image = Image.new(GIF_WITH_JPG_EXT)
assert_equal "gif", image[:format].downcase
image.destroy!
end
def test_image_resize
image = Image.open(SIMPLE_IMAGE_PATH)
image.resize "20x30!"
assert_equal 20, image[:width]
assert_equal 30, image[:height]
assert_match(/^gif$/i, image[:format])
image.destroy!
end
def test_image_resize_with_minimum
image = Image.open(SIMPLE_IMAGE_PATH)
original_width, original_height = image[:width], image[:height]
image.resize "#{original_width + 10}x#{original_height + 10}>"
assert_equal original_width, image[:width]
assert_equal original_height, image[:height]
image.destroy!
end
def test_image_combine_options_resize_blur
image = Image.open(SIMPLE_IMAGE_PATH)
image.combine_options do |c|
c.resize "20x30!"
c.blur "50"
end
assert_equal 20, image[:width]
assert_equal 30, image[:height]
assert_match(/^gif$/i, image[:format])
image.destroy!
end
def test_image_combine_options_with_filename_with_minusses_in_it
image = Image.open(SIMPLE_IMAGE_PATH)
background = "#000000"
assert_nothing_raised do
image.combine_options do |c|
c.draw "image Over 0,0 10,10 '#{MINUS_IMAGE_PATH}'"
c.thumbnail "300x500>"
c.background background
end
end
image.destroy!
end
def test_exif
image = Image.open(EXIF_IMAGE_PATH)
assert_equal('0220', image["exif:ExifVersion"])
image = Image.open(SIMPLE_IMAGE_PATH)
assert_equal('', image["EXIF:ExifVersion"])
image.destroy!
end
def test_original_at
image = Image.open(EXIF_IMAGE_PATH)
assert_equal(Time.local('2005', '2', '23', '23', '17', '24'), image[:original_at])
image = Image.open(SIMPLE_IMAGE_PATH)
assert_nil(image[:original_at])
image.destroy!
end
def test_tempfile_at_path
image = Image.open(TIFF_IMAGE_PATH)
assert_equal image.path, image.instance_eval("@tempfile.path")
image.destroy!
end
def test_tempfile_at_path_after_format
image = Image.open(TIFF_IMAGE_PATH)
image.format('png')
assert_equal image.path, image.instance_eval("@tempfile.path")
image.destroy!
end
def test_previous_tempfile_deleted_after_format
image = Image.open(TIFF_IMAGE_PATH)
before = image.path.dup
image.format('png')
assert !File.exist?(before)
image.destroy!
end
def test_bad_method_bug
image = Image.open(TIFF_IMAGE_PATH)
begin
image.to_blog
rescue NoMethodError
assert true
end
image.to_blob
assert true #we made it this far without error
image.destroy!
end
def test_simple_composite
image = Image.open(EXIF_IMAGE_PATH)
result = image.composite(Image.open(TIFF_IMAGE_PATH)) do |c|
c.gravity "center"
end
assert `diff -s #{result.path} test/composited.jpg`.include?("identical")
end
# http://github.com/probablycorey/mini_magick/issues#issue/8
def test_issue_8
image = Image.open(SIMPLE_IMAGE_PATH)
assert_nothing_raised do
image.combine_options do |c|
c.sample "50%"
c.rotate "-90>"
end
end
image.destroy!
end
# http://github.com/probablycorey/mini_magick/issues#issue/15
def test_issue_15
image = Image.open(Pathname.new(SIMPLE_IMAGE_PATH))
output = Pathname.new("test.gif")
image.write(output)
ensure
FileUtils.rm("test.gif")
end
def test_throw_format_error
image = Image.open(SIMPLE_IMAGE_PATH)
assert_raise MiniMagick::Error do
image.combine_options do |c|
c.format "png"
end
end
image.destroy!
end
# testing that if copying files formatted from an animation fails,
# it raises an appropriate error
def test_throw_animation_copy_after_format_error
image = Image.open(ANIMATION_PATH)
FileUtils.stubs(:copy_file).raises(Errno::ENOENT)
assert_raises MiniMagick::Error do
image.format('png')
end
end
end