magickwand 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,28 @@
1
+ # $Id: magickwand.rb 128 2009-05-31 13:12:51Z rmagick $
2
+ # Copyright (C) 2009 Timothy P. Hunter
3
+
4
+ require 'magickwand.so'
5
+
6
+ module MagickWand
7
+ class Wand
8
+
9
+ # options are :filter, :blur
10
+ def resize_to_fit(width, height=nil, options=nil)
11
+ height ||= width
12
+ new_width, new_height = get_resize_geometry("#{width}x#{height}")
13
+ resize(new_width, new_height, options)
14
+ end
15
+
16
+ # options are :filter, :blur, :gravity, :repage
17
+ def resize_to_fill(width, height=nil, options=nil)
18
+ height ||= width
19
+ options ||= Hash.new
20
+ new_width, new_height = get_resize_geometry("#{width}x#{height}^")
21
+ resize(new_width, new_height, options)
22
+ options[:gravity] ||= "center"
23
+ crop(width, height, options)
24
+ end
25
+
26
+ end
27
+ end
28
+
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,1100 @@
1
+ require 'test/unit'
2
+ require 'magickwand'
3
+
4
+ # The test images are in the same directory as this file.
5
+ BUTTON_IMAGES = Dir[File.dirname(__FILE__) + "/Button_*.gif"].freeze
6
+
7
+ # Determine what exception is raised when you try to modify a frozen object
8
+ begin
9
+ f = "string"
10
+ f.freeze
11
+ f[0] = "x"
12
+ rescue TypeError
13
+ FreezeError = TypeError
14
+ rescue RuntimeError
15
+ FreezeError = RuntimeError
16
+ end
17
+
18
+
19
+ class TestMagickWand < Test::Unit::TestCase
20
+
21
+ # Very basic tests
22
+ def test_aaaaaaaaaa
23
+ assert_not_nil(MagickWand::Wand)
24
+ assert_kind_of(Class, MagickWand::Wand)
25
+ assert_kind_of(Class, MagickWand::ImageMagickError)
26
+ assert_kind_of(Class, MagickWand::FatalImageMagickError)
27
+
28
+ assert_kind_of(String, MagickWand::VERSION)
29
+ assert MagickWand::VERSION.frozen?
30
+ assert_kind_of(Hash, MagickWand::IMAGEMAGICK_CONFIG)
31
+ assert MagickWand::IMAGEMAGICK_CONFIG.frozen?
32
+ assert_kind_of(String, MagickWand::IMAGEMAGICK_CONFIG['VERSION'])
33
+ assert MagickWand::IMAGEMAGICK_CONFIG['VERSION'].frozen?
34
+
35
+ wand = nil
36
+ assert_nothing_raised { wand = MagickWand::Wand.new }
37
+ assert_instance_of(MagickWand::Wand, wand)
38
+ assert_equal(0, wand.length) # length is a synonym for size
39
+ end
40
+
41
+ # tests wand.background, too
42
+ def test_add_canvas
43
+ wand = MagickWand::Wand.new
44
+ assert_nothing_raised { wand.add_canvas(100) }
45
+ assert_equal([100, 100], wand.dimensions)
46
+ assert_equal("#FFFFFFFFFFFF", wand.background)
47
+ wand.add_canvas(100, 200, "red")
48
+ assert_equal([100, 200], wand[1].dimensions)
49
+ assert_equal("#FFFF00000000", wand[1].background)
50
+ wand.add_canvas(100, 250, "rgb(0,255,0)")
51
+ assert_equal([100, 250], wand[2].dimensions)
52
+ assert_equal("#0000FFFF0000", wand[2].background)
53
+ end
54
+
55
+ # :xyzzy=>false suppresses the actual animation
56
+ # all we can do is ensure the options are handled
57
+ def test_animate
58
+ wand = MagickWand::Wand.new
59
+ BUTTON_IMAGES.sort.each { |file| wand.read file }
60
+ assert_nothing_raised { wand.animate :xyzzy=>false }
61
+ assert_nothing_raised { wand.animate :delay=>50, :xyzzy=>false }
62
+ assert_nothing_raised { wand.animate :delay=>[50], :xyzzy=>false }
63
+ assert_nothing_raised { wand.animate :delay=>[50,50], :xyzzy=>false }
64
+ assert_nothing_raised { wand.animate :iterations=>10, :xyzzy=>false }
65
+ end
66
+
67
+ def test_annotate
68
+ wand = MagickWand::Wand.new
69
+ wand.add_canvas(200, 200)
70
+ assert_nothing_raised { wand.annotate "hello!" }
71
+ assert_nothing_raised { wand.annotate "hello!", :angle=>90, :fill=>"red", :stroke=>"black", :pointsize=>24 }
72
+ assert_nothing_raised { wand.annotate "hello!", :undercolor=>"cyan", :decoration=>"underline", :strokewidth=>3 }
73
+ assert_nothing_raised { wand.annotate "hello!", :family=>"arial", :stretch=>"condensed", :fill_opacity=>0.5, :style=>"italic" }
74
+ assert_nothing_raised { wand.annotate "hello!", :stroke_opacity=>0.05, :weight=>400, :antialias=>false, :undercolor=>"red" }
75
+ assert_nothing_raised { wand.annotate "hello!", :interword_spacing=>15, :kerning=>15 }
76
+ assert_nothing_raised { wand.annotate "hello!", :gravity=>"none", :x=>100, :y=>100, :align=>"left" }
77
+
78
+ # option errors - IM doesn't diagnose invalid colors, presumably because they
79
+ # could be pattern names. Don't test simple type errors. That's Ruby's job.
80
+ assert_raise(ArgumentError) { wand.annotate "hello", :align=>"xxx" }
81
+ #assert_raise(TypeError) { wand.annotate "hello", :angle=>"xxx" }
82
+ assert_raise(ArgumentError) { wand.annotate "hello", :decoration=>"xxx" }
83
+ assert_raise(ArgumentError) { wand.annotate "hello", :gravity=>"xxx" }
84
+ #assert_raise(ArgumentError) { wand.annotate "hello", :fill=>"xxx" }
85
+ #assert_raise(TypeError) { wand.annotate "hello", :fill_opacity=>"xxx" }
86
+ assert_raise(ArgumentError) { wand.annotate "hello", :gravity=>"xxx" }
87
+ #assert_raise(TypeError) { wand.annotate "hello", :interword_spacing=>"xxx" }
88
+ #assert_raise(TypeError) { wand.annotate "hello", :kerning=>"xxx" }
89
+ #assert_raise(TypeError) { wand.annotate "hello", :pointsize=>"xxx" }
90
+ assert_raise(ArgumentError) { wand.annotate "hello", :stretch=>"xxx" }
91
+ #assert_raise(ArgumentError) { wand.annotate "hello", :stroke=>"xxx" }
92
+ #assert_raise(TypeError) { wand.annotate "hello", :stroke_opacity=>"xxx" }
93
+ #assert_raise(TypeError) { wand.annotate "hello", :strokewidth=>"xxx" }
94
+ assert_raise(ArgumentError) { wand.annotate "hello", :style=>"xxx" }
95
+ #assert_raise(ArgumentError) { wand.annotate "hello", :undercolor=>"xxx" }
96
+ assert_raise(ArgumentError) { wand.annotate "hello", :weight=>"xxx" }
97
+ #assert_raise(TypeError) { wand.annotate "hello", :x=>"xxx" }
98
+ #assert_raise(TypeError) { wand.annotate "hello", :y=>"xxx" }
99
+
100
+ wand.freeze
101
+ assert_raise(FreezeError) { wand.annotate "hello" }
102
+ end
103
+
104
+ def test_aref
105
+ wand = MagickWand::Wand.new
106
+ BUTTON_IMAGES.sort.each { |file| wand.read file }
107
+ assert_equal(BUTTON_IMAGES.length, wand.size)
108
+
109
+ t = nil
110
+ assert_nothing_raised { t = wand[0]}
111
+ assert_instance_of(MagickWand::Wand, t)
112
+ assert_equal(1, t.size)
113
+ assert_not_same(t, wand, "subset wand is not a new wand!")
114
+ assert_match(/Button_0/, t.filename)
115
+
116
+ t = wand[1]
117
+ assert_equal(1, t.size);
118
+ assert_match(/Button_1/, t.filename)
119
+
120
+ t = wand[-1]
121
+ assert_equal(1, t.size);
122
+ assert_match(/Button_9/, t.filename)
123
+
124
+ t = wand[2, 3]
125
+ assert_equal(3, t.size);
126
+ assert_match(/Button_2/, t.filename)
127
+ assert_match(/Button_2/, t[0].filename)
128
+ assert_match(/Button_3/, t[1].filename)
129
+ assert_match(/Button_4/, t[2].filename)
130
+
131
+ t = wand[-3, 2]
132
+ assert_equal(2, t.size);
133
+ assert_match(/Button_7/, t.filename)
134
+ assert_match(/Button_8/, t[1].filename)
135
+
136
+ t = wand[0..-1]
137
+ assert_equal(wand.size, t.size)
138
+ inspect_string = t.inspect
139
+ t.size.times do |n|
140
+ assert_match(/Button_#{n}/, inspect_string)
141
+ end
142
+
143
+ # out-of-range returns empty object
144
+ assert_nothing_raised { t = wand[10]}
145
+ assert_instance_of(MagickWand::Wand, t)
146
+ assert_equal(0, t.size)
147
+ assert_not_same(t, wand, "subset wand is not a new wand!")
148
+ assert_equal("()", t.inspect)
149
+
150
+ assert_nothing_raised { t = wand[10..12]}
151
+ assert_instance_of(MagickWand::Wand, t)
152
+ assert_equal(0, t.size)
153
+ assert_not_same(t, wand, "subset wand is not a new wand!")
154
+ assert_equal("()", t.inspect)
155
+
156
+ assert_nothing_raised { t = wand[-13..-1]}
157
+ assert_instance_of(MagickWand::Wand, t)
158
+ assert_equal(0, t.size)
159
+ assert_not_same(t, wand, "subset wand is not a new wand!")
160
+ assert_equal("()", t.inspect)
161
+
162
+ assert_raise(TypeError) { wand[10.2] }
163
+ end
164
+
165
+ def test_aset
166
+ wand = MagickWand::Wand.new
167
+ wand2 = MagickWand::Wand.new
168
+ # wand gets images 0-4
169
+ # wand2 gets images 5-9
170
+ sorted_images = BUTTON_IMAGES.sort
171
+ 5.times {|n| wand.read sorted_images.sort[n]}
172
+ 5.times {|n| wand2.read sorted_images.sort[n+5]}
173
+
174
+ assert_nothing_raised { wand[0] = wand2[0] }
175
+ assert_match(/Button_5.gif/, wand[0].filename)
176
+
177
+ # replace wand[0] with all images in wand2
178
+ wand = MagickWand::Wand.new
179
+ 5.times {|n| wand.read sorted_images[n]}
180
+ assert_nothing_raised { wand[0] = wand2 }
181
+ assert_equal(9, wand.length)
182
+ [5, 6, 7, 8, 9, 1, 2, 3, 4].each_with_index do |m, n|
183
+ assert_match(/Button_#{m}.gif/, wand[n].filename)
184
+ end
185
+
186
+ # replace wand[1] with all images in wand2
187
+ wand = MagickWand::Wand.new
188
+ 5.times {|n| wand.read sorted_images[n]}
189
+ assert_nothing_raised { wand[1] = wand2 }
190
+ assert_equal(9, wand.length)
191
+ [0, 5, 6, 7, 8, 9, 2, 3, 4].each_with_index do |m, n|
192
+ assert_match(/Button_#{m}.gif/, wand[n].filename)
193
+ end
194
+
195
+ # replace sorted_images 1 and 2 in wand with all images in wand2
196
+ wand = MagickWand::Wand.new
197
+ 5.times {|n| wand.read sorted_images[n]}
198
+ assert_nothing_raised { wand[1, 2] = wand2 }
199
+ assert_equal(8, wand.length)
200
+ [0, 5, 6, 7, 8, 9, 3, 4].each_with_index do |m, n|
201
+ assert_match(/Button_#{m}.gif/, wand[n].filename)
202
+ end
203
+
204
+ # replace wand[0] with an empty wand - deletes the first image
205
+ empty = MagickWand::Wand.new
206
+ wand = MagickWand::Wand.new
207
+ 5.times {|n| wand.read sorted_images[n]}
208
+ assert_nothing_raised { wand[0] = empty }
209
+ assert_equal(4, wand.length)
210
+ [1, 2, 3, 4].each_with_index do |m, n|
211
+ assert_match(/Button_#{m}.gif/, wand[n].filename)
212
+ end
213
+
214
+ # reduce the length to the number of images remaining in the wand
215
+ # extend the length of the receiving wand
216
+ wand = MagickWand::Wand.new
217
+ 5.times {|n| wand.read sorted_images[n]}
218
+ assert_nothing_raised { wand[1,5] = wand2 }
219
+ assert_equal(6, wand.length)
220
+ [0, 5, 6, 7, 8, 9].each_with_index do |m, n|
221
+ assert_match(/Button_#{m}.gif/, wand[n].filename)
222
+ end
223
+
224
+ # An index equal to the #images is okay. It appends wand2 to wand
225
+ wand = MagickWand::Wand.new
226
+ 5.times {|n| wand.read sorted_images[n]}
227
+ assert_nothing_raised { wand[wand.length,1] = wand2 }
228
+ assert_equal(10, wand.length)
229
+ 10.times do |n|
230
+ assert_match(/Button_#{n}.gif/, wand[n].filename)
231
+ end
232
+
233
+ # Same as previous test but with negative index.
234
+ # _Replaces_ the 0th image with the images in wand2 (same as String[-5,1])
235
+ wand = MagickWand::Wand.new
236
+ 5.times {|n| wand.read sorted_images[n]}
237
+ assert_nothing_raised { wand[-wand.length,1] = wand2 }
238
+ assert_equal(9, wand.length)
239
+ [5, 6, 7, 8, 9, 1, 2, 3, 4].each_with_index do |m, n|
240
+ assert_match(/Button_#{m}.gif/, wand[n].filename)
241
+ end
242
+
243
+ # Replace wand[-1] (i.e. the 4th image)
244
+ wand = MagickWand::Wand.new
245
+ 5.times {|n| wand.read sorted_images[n]}
246
+ assert_nothing_raised { wand[-1] = wand2 }
247
+ [0, 1, 2, 3, 5, 6, 7, 8, 9].each_with_index do |m, n|
248
+ assert_match(/Button_#{m}.gif/, wand[n].filename)
249
+ end
250
+
251
+ # Replace 2 images at -3 (i.e. the 2nd and 3rd image)
252
+ wand = MagickWand::Wand.new
253
+ 5.times {|n| wand.read sorted_images[n]}
254
+ assert_nothing_raised { wand[-3, 2] = wand2 }
255
+ [0, 1, 5, 6, 7, 8, 9, 4].each_with_index do |m, n|
256
+ assert_match(/Button_#{m}.gif/, wand[n].filename)
257
+ end
258
+
259
+ # An index > # of images raises IndexError
260
+ wand = MagickWand::Wand.new
261
+ 5.times {|n| wand.read sorted_images[n]}
262
+ assert_raise(IndexError) { wand[wand.length+1,1] = wand2 }
263
+
264
+ # Same as previous test but with negative index.
265
+ assert_raise(IndexError) { wand[-(wand.length+1),1] = wand2 }
266
+
267
+ # Frozen?
268
+ wand.freeze
269
+ assert_raise(FreezeError) { wand[0] = wand2 }
270
+ end
271
+
272
+ def test_blur
273
+ wand = MagickWand::Wand.new
274
+ wand.read BUTTON_IMAGES[0]
275
+ assert_nothing_raised {wand.blur 0,3}
276
+ assert_nothing_raised {wand.blur 0,3, :channel=>"red"}
277
+ assert_raise(ArgumentError) { wand.blur 0,3, :channel=>"xxx" }
278
+
279
+ wand.freeze
280
+ assert_raise(FreezeError) {wand.blur 0,3}
281
+ end
282
+
283
+ def test_border
284
+ wand = MagickWand::Wand.new
285
+ wand.add_canvas(100, 100)
286
+ assert_nothing_raised { wand.border("black") }
287
+ # default width and height is 1
288
+ assert_equal([102, 102], wand.dimensions)
289
+ # default height = width
290
+ assert_nothing_raised { wand.border("red", 5) }
291
+ assert_equal([112, 112], wand.dimensions)
292
+ # no defaults
293
+ assert_nothing_raised { wand.border("red", 5, 10) }
294
+ assert_equal([122, 132], wand.dimensions)
295
+
296
+ wand.freeze
297
+ assert_raise(FreezeError) { wand.border("black") }
298
+ end
299
+
300
+ def test_clone
301
+ wand = MagickWand::Wand.new
302
+ wand.read BUTTON_IMAGES[0]
303
+ wand2 = nil
304
+ assert_nothing_raised { wand2 = wand.clone }
305
+ assert_not_same(wand, wand2)
306
+ assert_equal(wand.length, wand2.length)
307
+ assert_equal(wand.signature, wand2.signature)
308
+
309
+ # Identical objects but not the same MagickWand
310
+ wand.comment = "Comment A"
311
+ wand2.comment = "Comment B"
312
+ assert_equal("Comment A", wand.comment)
313
+
314
+ assert_nothing_raised { wand2 = wand.dup }
315
+ assert_not_same(wand, wand2)
316
+ assert_equal(wand.length, wand2.length)
317
+ assert_equal(wand.signature, wand2.signature)
318
+ end
319
+
320
+ def test_colors
321
+ wand = MagickWand::Wand.new
322
+ assert_nil(wand.colors)
323
+ wand.read BUTTON_IMAGES[0]
324
+ assert_equal(135, wand.colors)
325
+ end
326
+
327
+ # test #signature too
328
+ def test_cmp
329
+ awand = MagickWand::Wand.new
330
+ awand.read BUTTON_IMAGES[0]
331
+ bwand = MagickWand::Wand.new
332
+ bwand.read BUTTON_IMAGES[1]
333
+
334
+ asig = nil
335
+ assert_nothing_raised { asig = awand.signature }
336
+ assert_instance_of(String, asig)
337
+ assert_equal(64, asig.length)
338
+
339
+ bsig = bwand.signature
340
+ assert_not_equal(asig, bsig)
341
+ assert_equal(asig <=> bsig, awand <=> bwand)
342
+ assert_equal(bsig <=> asig, bwand <=> awand)
343
+
344
+ assert_instance_of(Fixnum, awand <=> bwand)
345
+
346
+ # equal
347
+ assert_equal(0, awand <=> awand)
348
+ # not equal
349
+ assert_not_equal(0, awand <=> bwand)
350
+ # different
351
+ assert_not_equal(awand <=> bwand, bwand <=> awand)
352
+ end
353
+
354
+ def test_comment
355
+ wand = MagickWand::Wand.new
356
+ wand.read BUTTON_IMAGES.first
357
+ assert_nothing_raised { wand.comment = "A comment" }
358
+ assert_equal("A comment", wand.comment)
359
+
360
+ # Comment must be a string!
361
+ assert_raise(TypeError) { wand.comment = 123 }
362
+
363
+ wand.freeze
364
+ assert_raise(FreezeError) { wand.comment = "A comment" }
365
+ end
366
+
367
+ def test_compare
368
+ awand = MagickWand::Wand.new
369
+ awand.read BUTTON_IMAGES[0]
370
+ bwand = MagickWand::Wand.new
371
+ bwand.read BUTTON_IMAGES[1]
372
+
373
+ #compare a to a should be equal
374
+ diff, dist = nil
375
+ assert_nothing_raised { diff, dist = awand.compare(awand) }
376
+ assert_instance_of(MagickWand::Wand, diff)
377
+ assert_equal(0.0, dist)
378
+
379
+ # compare a to be should not be equal
380
+ _, dist = awand.compare(bwand)
381
+ assert_not_equal(0.0, dist)
382
+
383
+ dist2 = nil
384
+ assert_nothing_raised { _, dist2 = awand.compare(bwand, :metric=>"rmse") }
385
+ # different metric
386
+ assert_not_equal(dist, dist2)
387
+
388
+ assert_nothing_raised { _, dist = awand.compare(bwand, :channel=>"red", :metric=>"rmse") }
389
+ _, dist2 = awand.compare(bwand, :channel=>"green", :metric=>"rmse")
390
+ # different channel
391
+ assert_not_equal(dist, dist2)
392
+
393
+ _, dist = awand.compare(bwand)
394
+ assert_nothing_raised { _, dist2 = awand.compare(bwand, :fuzz=>"10%") }
395
+ # different fuzz
396
+ assert_not_equal(dist, dist2)
397
+
398
+ # fuzz can be a string as well as a number
399
+ assert_nothing_raised { _, dist2 = awand.compare(bwand, :fuzz=>50) }
400
+
401
+ end
402
+
403
+ def test_composite
404
+ wand = MagickWand::Wand.new
405
+ wand.add_canvas(200, 200)
406
+ comp_wand = MagickWand::Wand.new
407
+ comp_wand.add_canvas(50, 50, "blue")
408
+ assert_nothing_raised { wand.composite comp_wand }
409
+ assert_equal(1, wand.length)
410
+ assert_equal([200, 200], wand.dimensions)
411
+
412
+ # test multiple images
413
+ wand.add_canvas(300, 300)
414
+ comp_wand.add_canvas(50, 50, "blue")
415
+ assert_nothing_raised { wand.composite comp_wand }
416
+ assert_equal(2, wand.length)
417
+ assert_equal([300, 300], wand[1].dimensions)
418
+
419
+ # test unequal numbers of images
420
+ wand.add_canvas(200, 200)
421
+ assert_nothing_raised { wand.composite comp_wand }
422
+ assert_equal(3, wand.length)
423
+
424
+ comp_wand.add_canvas(50, 50, "blue")
425
+ comp_wand.add_canvas(50, 50, "blue")
426
+ assert_nothing_raised { wand.composite comp_wand }
427
+ assert_equal(3, wand.length)
428
+
429
+ comp_wand.add_canvas(50, 50, "blue")
430
+ assert_nothing_raised { wand.composite comp_wand }
431
+ assert_equal(3, wand.length)
432
+
433
+ # test optional arguments
434
+ wand = MagickWand::Wand.new
435
+ wand.add_canvas(200, 200)
436
+ comp_wand = MagickWand::Wand.new
437
+ comp_wand.add_canvas(50, 50, "blue")
438
+ assert_nothing_raised { wand.composite comp_wand, :gravity => "north" }
439
+ assert_nothing_raised { wand.composite comp_wand, :x => 5, :y => 5 }
440
+ assert_nothing_raised { wand.composite comp_wand, :x=> 5, :y => 5, :gravity => "north" }
441
+ assert_raise(ArgumentError) { wand.composite comp_wand, :gravity=>"xyzzy" }
442
+
443
+ wand.freeze
444
+ assert_raise(FreezeError) { wand.composite comp_wand }
445
+ end
446
+
447
+ # concat is an alias for <<
448
+ def test_concat
449
+ wand = MagickWand::Wand.new
450
+ wand2 = MagickWand::Wand.new
451
+ 5.times {|n| wand.read BUTTON_IMAGES[n]}
452
+ 5.times {|n| wand2.read BUTTON_IMAGES[5+n]}
453
+ assert_equal(5, wand.size)
454
+ assert_equal(5, wand2.size)
455
+ assert_nothing_raised { wand << wand2 }
456
+ assert_equal(10, wand.size)
457
+
458
+ wand = MagickWand::Wand.new
459
+ wand2 = MagickWand::Wand.new
460
+ 5.times {|n| wand.read BUTTON_IMAGES[n]}
461
+ 5.times {|n| wand2.read BUTTON_IMAGES[5+n]}
462
+ assert_nothing_raised { wand.concat wand2 }
463
+ assert_equal(10, wand.size)
464
+
465
+ wand.freeze
466
+ assert_raise(FreezeError) { wand.concat wand2 }
467
+ end
468
+
469
+ def test_crop
470
+ wand = MagickWand::Wand.new
471
+ wand.read BUTTON_IMAGES[0]
472
+ wand2 = wand[0]
473
+ assert_nothing_raised { wand2.crop(50) }
474
+ assert_equal([50, 50], wand2.dimensions)
475
+
476
+ wand2 = wand[0]
477
+ assert_nothing_raised { wand2.crop(50, 35) }
478
+ assert_equal([50, 35], wand2.dimensions)
479
+
480
+ wand2 = wand[0]
481
+ assert_nothing_raised { wand2.crop(50, 35, :x=>5, :y=>5, :repage=>false) }
482
+ assert_equal([50, 35], wand2.dimensions)
483
+ assert_match(/\+5\+5 /, wand2.inspect)
484
+
485
+ wand2 = wand[0]
486
+ assert_nothing_raised { wand2.crop(50, 35, :gravity=>"center", :repage=>false) }
487
+ assert_equal([50, 35], wand2.dimensions)
488
+ assert_match(/\+38\+43 /, wand2.inspect)
489
+
490
+ # test all possible gravities
491
+ %w[northwest north northeast
492
+ west center east
493
+ southwest south southeast].each do |gravity|
494
+ wand2 = wand[0]
495
+ assert_nothing_raised { wand2.crop(50, 35, :gravity=>gravity) }
496
+ end
497
+ end
498
+
499
+ def test_dimensions
500
+ wand = MagickWand::Wand.new
501
+ assert_nil(wand.dimensions)
502
+
503
+ wand.read BUTTON_IMAGES[0]
504
+ dim = nil
505
+ assert_nothing_raised { dim = wand.dimensions }
506
+ assert_equal([127, 120], dim)
507
+ end
508
+
509
+ def test_depth
510
+ wand = MagickWand::Wand.new
511
+ assert_nil(wand.depth)
512
+ wand.read BUTTON_IMAGES[0]
513
+ assert_equal(8, wand.depth)
514
+ end
515
+
516
+ def test_each
517
+ wand = MagickWand::Wand.new
518
+ BUTTON_IMAGES.sort.each { |f| wand.read f }
519
+ wand.each_with_index do |w, n|
520
+ assert_match(/Button_#{n}/, w.filename)
521
+ end
522
+
523
+ # Handle empty wands
524
+ wand = MagickWand::Wand.new
525
+ assert_nothing_raised { wand.each {|w| } }
526
+
527
+ # In >= 1.8.7 return an Enumerator if no block
528
+ # In 1.8.6 return self
529
+ if RUBY_VERSION == "1.8.7" || RUBY_VERSION == "1.9.1"
530
+ enum = nil
531
+ assert_nothing_raised { enum = wand.each }
532
+ assert_instance_of(Enumerable::Enumerator, enum) if RUBY_VERSION == "1.8.7"
533
+ assert_instance_of(Enumerator, enum) if RUBY_VERSION == "1.9.1"
534
+ else
535
+ enum = wand.each
536
+ assert_same(wand, enum)
537
+ end
538
+ end
539
+
540
+ def test_empty?
541
+ wand = MagickWand::Wand.new
542
+ assert_nothing_raised { wand.empty? }
543
+ assert( wand.empty? )
544
+ wand.read BUTTON_IMAGES[0]
545
+ assert( !wand.empty? )
546
+ end
547
+
548
+ def test_export_pixels
549
+ wand = MagickWand::Wand.new
550
+ wand.read BUTTON_IMAGES[0]
551
+ cols, rows = wand.dimensions
552
+ pixels = nil
553
+ assert_nothing_raised { pixels = wand.export_pixels(0, 0, cols, rows) }
554
+ assert_equal(cols*rows*3, pixels.size)
555
+ assert_nothing_raised { pixels = wand.export_pixels(0, 0, cols, rows, :map=>"I") }
556
+ assert_equal(cols*rows*1, pixels.size)
557
+ assert_nothing_raised { pixels = wand.export_pixels(0, 0, cols, rows, :map=>"I", :storage_type=>"char") }
558
+ assert_equal(cols*rows*1*1, pixels.size)
559
+ assert_nothing_raised { pixels = wand.export_pixels(0, 0, cols, rows, :map=>"I", :storage_type=>"short") }
560
+ assert_equal(cols*rows*1*2, pixels.size)
561
+ assert_nothing_raised { pixels = wand.export_pixels(0, 0, cols, rows, :map=>"I", :storage_type=>"integer") }
562
+ assert_equal(cols*rows*1*4, pixels.size)
563
+ assert_nothing_raised { pixels = wand.export_pixels(0, 0, cols, rows, :map=>"I", :storage_type=>"long") }
564
+ assert_equal(cols*rows*1*4, pixels.size)
565
+ assert_nothing_raised { pixels = wand.export_pixels(0, 0, cols, rows, :map=>"I", :storage_type=>"float") }
566
+ assert_equal(cols*rows*1*4, pixels.size)
567
+ assert_nothing_raised { pixels = wand.export_pixels(0, 0, cols, rows, :map=>"I", :storage_type=>"double") }
568
+ assert_equal(cols*rows*1*8, pixels.size)
569
+ assert_nothing_raised { pixels = wand.export_pixels(0, 0, cols, rows, :storage_type=>"quantum") }
570
+ assert_equal(cols*rows*3*MagickWand::QUANTUM_SIZE, pixels.size)
571
+
572
+ assert_raise(ArgumentError) { pixels = wand.export_pixels(0, 0, cols, rows, :map=>"I", :storage_type=>"int") }
573
+
574
+ end
575
+
576
+ def test_filename
577
+ wand = MagickWand::Wand.new
578
+ assert_nil(wand.filename)
579
+ wand.read BUTTON_IMAGES[0]
580
+ assert_nothing_raised { wand.filename }
581
+ assert_equal(BUTTON_IMAGES[0], wand.filename)
582
+ end
583
+
584
+ def test_format
585
+ wand = MagickWand::Wand.new
586
+ assert_nil(wand.format)
587
+ wand.read BUTTON_IMAGES[0]
588
+ assert_nothing_raised { wand.format }
589
+ assert_equal("GIF", wand.format)
590
+ end
591
+
592
+ # test eql? too
593
+ def test_hash
594
+ wand = MagickWand::Wand.new
595
+ wand2 = MagickWand::Wand.new
596
+ wand3 = MagickWand::Wand.new
597
+ wand.read BUTTON_IMAGES[0]
598
+ wand2.read BUTTON_IMAGES[1] # different than wand
599
+ wand3.read BUTTON_IMAGES[0] # same as wand
600
+
601
+ assert_nothing_raised { wand.hash }
602
+ assert_equal(wand.hash, wand3.hash)
603
+ assert_not_equal(wand.hash, wand2.hash)
604
+
605
+ assert_nothing_raised { wand.eql?(wand2) }
606
+ assert(!wand.eql?(wand2))
607
+ assert(wand.eql?(wand3))
608
+ end
609
+
610
+ def test_import_pixels
611
+ wand = MagickWand::Wand.new
612
+ wand.read BUTTON_IMAGES[0]
613
+ cols, rows = wand.dimensions
614
+ pixels = wand.export_pixels(0, 0, cols, rows)
615
+ wand2 = MagickWand::Wand.new
616
+ wand2.add_canvas(cols, rows)
617
+ assert_nothing_raised { wand2.import_pixels(0, 0, cols, rows, pixels) }
618
+ _, dist = wand.compare(wand2)
619
+ assert_in_delta(0, dist, 10e-6)
620
+
621
+ # pixel array too small
622
+ wand2 = MagickWand::Wand.new
623
+ wand2.add_canvas(cols, rows)
624
+ assert_raise(ArgumentError) { wand2.import_pixels(0, 0, cols, rows, pixels[0..-2]) }
625
+
626
+ wand2.freeze
627
+ assert_raise(FreezeError) { wand2.import_pixels(0, 0, cols, rows, pixels) }
628
+ end
629
+
630
+ def test_insert
631
+ wand = MagickWand::Wand.new
632
+ wand2 = MagickWand::Wand.new
633
+
634
+ images = BUTTON_IMAGES.sort
635
+
636
+ 5.times { |n| wand.read images[n] }
637
+ wand2.read images[9]
638
+
639
+ assert_nothing_raised { wand.insert(0, wand2) }
640
+ [9, 0, 1, 2, 3, 4].each_with_index do |m, n|
641
+ assert_match(/Button_#{m}.gif/, wand[n].filename)
642
+ end
643
+
644
+ wand = MagickWand::Wand.new
645
+ 5.times { |n| wand.read images[n] }
646
+ assert_nothing_raised { wand.insert(3, wand2) }
647
+ [0, 1, 2, 9, 3, 4].each_with_index do |m, n|
648
+ assert_match(/Button_#{m}.gif/, wand[n].filename)
649
+ end
650
+
651
+ wand = MagickWand::Wand.new
652
+ 5.times { |n| wand.read images[n] }
653
+ assert_nothing_raised { wand.insert(5, wand2) }
654
+ [0, 1, 2, 3, 4, 9].each_with_index do |m, n|
655
+ assert_match(/Button_#{m}.gif/, wand[n].filename)
656
+ end
657
+
658
+ wand = MagickWand::Wand.new
659
+ 5.times { |n| wand.read images[n] }
660
+ assert_nothing_raised { wand.insert(-1, wand2) }
661
+ [0, 1, 2, 3, 4, 9].each_with_index do |m, n|
662
+ assert_match(/Button_#{m}.gif/, wand[n].filename)
663
+ end
664
+
665
+ wand = MagickWand::Wand.new
666
+ 5.times { |n| wand.read images[n] }
667
+ assert_nothing_raised { wand.insert(-3, wand2) }
668
+ [0, 1, 2, 9, 3, 4].each_with_index do |m, n|
669
+ assert_match(/Button_#{m}.gif/, wand[n].filename)
670
+ end
671
+
672
+ wand2.read images[8]
673
+ wand = MagickWand::Wand.new
674
+ 5.times { |n| wand.read images[n] }
675
+ assert_nothing_raised { wand.insert(-3, wand2) }
676
+ [0, 1, 2, 9, 8, 3, 4].each_with_index do |m, n|
677
+ assert_match(/Button_#{m}.gif/, wand[n].filename)
678
+ end
679
+
680
+ wand.freeze
681
+ assert_raise(FreezeError) { wand.insert(-3, wand2) }
682
+ end
683
+
684
+ # need more
685
+ def test_inspect
686
+ wand = MagickWand::Wand.new
687
+ wand.add_canvas(200, 200)
688
+ wand.add_canvas(300, 300)
689
+ assert_match(/200x200/, wand[0].inspect)
690
+ assert_match(/300x300/, wand[1].inspect)
691
+ assert_match(/DirectClass/, wand[0].inspect)
692
+
693
+ wand = MagickWand::Wand.new
694
+ wand.read "test/Button_0.gif"
695
+ assert_match(/test\/Button_0.gif/, wand.inspect)
696
+ assert_match(/ GIF /, wand.inspect)
697
+ assert_match(/ 127x120\+0\+0 /, wand.inspect)
698
+ assert_match(/ 8-bit PaletteType PseudoClass /, wand.inspect)
699
+ assert_match(/ 256c /, wand.inspect)
700
+ end
701
+
702
+ def test_list_fonts
703
+ fonts = nil
704
+ assert_nothing_raised { fonts = MagickWand.list_fonts }
705
+ assert_instance_of(Array, fonts)
706
+ assert(fonts.size > 1)
707
+ fonts.each do |font|
708
+ assert_instance_of(String, font)
709
+ end
710
+ end
711
+
712
+ def test_modulate
713
+ wand = MagickWand::Wand.new
714
+ assert_nothing_raised { wand.modulate(50) }
715
+ assert_nothing_raised { wand.modulate(50, 50) }
716
+ assert_nothing_raised { wand.modulate(50, 50, 50) }
717
+ wand.freeze
718
+ assert_raise(FreezeError) { wand.modulate(50) }
719
+ end
720
+
721
+ # option_values returns a hash. The keys are symbols and the values are arrays.
722
+ # Each element in the array is a string.
723
+ def test_option_values
724
+ values = nil
725
+ assert_nothing_raised { values = MagickWand.option_values }
726
+ assert_instance_of(Hash, values)
727
+ assert(values.frozen?)
728
+
729
+ values.each_pair do |key, value|
730
+ assert_instance_of(Symbol, key)
731
+ assert_instance_of(Array, value)
732
+ assert(value.frozen?)
733
+ value.each do |v|
734
+ assert_instance_of(String, v)
735
+ assert(v.frozen?)
736
+ end
737
+ end
738
+
739
+ assert(values.has_key?(:channel))
740
+ assert(values.has_key?(:colorspace))
741
+ assert(values.has_key?(:compose))
742
+ assert(values.has_key?(:gravity))
743
+ assert(values.has_key?(:compress))
744
+ assert(values.has_key?(:filter))
745
+
746
+ end
747
+
748
+ def test_page
749
+ wand = MagickWand::Wand.new
750
+ wand.read BUTTON_IMAGES[0]
751
+ wand.crop(20, 20, :x=>5, :y=>7, :repage=>false)
752
+ assert_equal([127, 120, 5, 7], wand.page)
753
+ end
754
+
755
+ # ping and read share the same code so there's not much to do here
756
+ def test_ping
757
+ wand = MagickWand::Wand.new
758
+ wand.read BUTTON_IMAGES[0]
759
+
760
+ wand2 = MagickWand::Wand.new
761
+ assert_nothing_raised { wand2.ping BUTTON_IMAGES[0] }
762
+
763
+ assert_equal(wand.dimensions, wand2.dimensions)
764
+ assert_equal(wand.filename, wand2.filename)
765
+
766
+ wand2.freeze
767
+ assert_raise(FreezeError) { wand2.ping BUTTON_IMAGES[0] }
768
+ end
769
+
770
+ def test_ping_string
771
+ wand = MagickWand::Wand.new
772
+ wand.read BUTTON_IMAGES[0]
773
+ image = wand.write_string
774
+
775
+ wand = MagickWand::Wand.new
776
+ wand2 = MagickWand::Wand.new
777
+ wand.read_string image
778
+ wand2.ping_string image
779
+
780
+ assert_equal(wand.dimensions, wand2.dimensions)
781
+ assert_equal(wand.filename, wand2.filename)
782
+
783
+ wand2.freeze
784
+ assert_raise(FreezeError) { wand2.ping_string image }
785
+ end
786
+
787
+ def test_plus
788
+ wand = MagickWand::Wand.new
789
+ wand2 = MagickWand::Wand.new
790
+ 5.times {|n| wand.read BUTTON_IMAGES[n]}
791
+ 5.times {|n| wand2.read BUTTON_IMAGES[5+n]}
792
+ assert_equal(5, wand.size)
793
+ assert_equal(5, wand2.size)
794
+ wand3 = nil
795
+ assert_nothing_raised { wand3 = wand + wand2 }
796
+ assert_equal(10, wand3.size)
797
+ assert_not_same(wand, wand3)
798
+ assert_not_same(wand2, wand3)
799
+ end
800
+
801
+ # Each read adds the new image(s) to the end of the list.
802
+ def test_read
803
+ wand = MagickWand::Wand.new
804
+
805
+ BUTTON_IMAGES.each_with_index do |file, n|
806
+ assert_nothing_raised { wand.read file }
807
+ assert_equal(1+n, wand.size)
808
+ end
809
+
810
+ # :size option
811
+ wand = MagickWand::Wand.new
812
+ assert_nothing_raised { wand.read("xc:white", :size=>'100x100') }
813
+ assert_equal([100, 100], wand.dimensions)
814
+
815
+ wand.freeze
816
+ assert_raise(FreezeError) { wand.read BUTTON_IMAGES[0] }
817
+ end
818
+
819
+ def test_read_errors
820
+ wand = MagickWand::Wand.new
821
+
822
+ # unknown option is NOT diagnosed
823
+ assert_nothing_raised { wand.read(BUTTON_IMAGES[0], :xyzzy => 1) }
824
+ # file not found
825
+ assert_raise(MagickWand::ImageMagickError) { wand.read("does_not_exist.jpg") }
826
+ # invalid value for :size
827
+ assert_raise(ArgumentError) { wand.read("xc:white", :size=>'x') }
828
+ end
829
+
830
+ def test_resize
831
+ wand = MagickWand::Wand.new
832
+ wand.read BUTTON_IMAGES[0]
833
+
834
+ # implicit clone
835
+ wand2 = wand[0]
836
+ assert_nothing_raised { wand2.resize(254, 240) }
837
+ assert_equal([254, 240], wand2.dimensions)
838
+
839
+ # default height = width
840
+ wand2 = wand[0]
841
+ assert_nothing_raised { wand2.resize(254) }
842
+ assert_equal([254, 254], wand2.dimensions)
843
+
844
+ # legal options
845
+ wand2 = wand[0]
846
+ assert_nothing_raised { wand2.resize(254, 240, :filter=>"hanning", :blur => 1.5) }
847
+ # illegal option is ignored!
848
+ assert_nothing_raised { wand2.resize(254, 240, :xyzzy => 1) }
849
+
850
+ # known filters
851
+ wand = MagickWand::Wand.new
852
+ wand.add_canvas(20)
853
+ %w[bartlett bessel blackman bohman box catrom cubic gaussian hamming hanning hermite
854
+ kaiser lagrange lanczos mitchell parzen point quadratic sinc triangle welsh]. each do |filter|
855
+ assert_nothing_raised { wand.resize(20, 20, :filter => filter) }
856
+ end
857
+ # unknown filter
858
+ assert_raise(ArgumentError) { wand.resize(20, 20, :filter=>"xyzzy") }
859
+
860
+ wand.freeze
861
+ assert_raise(FreezeError) { wand.resize(20) }
862
+ end
863
+
864
+ def test_resize_to_fill
865
+ wand = MagickWand::Wand.new
866
+ wand.add_canvas(200, 200)
867
+ assert_nothing_raised { wand.resize_to_fill(200, 200) }
868
+ assert_equal([200, 200], wand.dimensions)
869
+
870
+ # height defaults to width
871
+ wand.resize_to_fill(100)
872
+ assert_equal([100, 100], wand.dimensions)
873
+
874
+ wand = MagickWand::Wand.new.add_canvas(200, 250)
875
+ wand.resize_to_fill(300, 100)
876
+ assert_equal([300, 100], wand.dimensions)
877
+
878
+ wand = MagickWand::Wand.new.add_canvas(200, 250)
879
+ wand.resize_to_fill(100, 300)
880
+ assert_equal([100, 300], wand.dimensions)
881
+
882
+ wand = MagickWand::Wand.new.add_canvas(200, 250)
883
+ wand.resize_to_fill(20, 400)
884
+ assert_equal([20, 400], wand.dimensions)
885
+
886
+ wand = MagickWand::Wand.new.add_canvas(200, 250)
887
+ wand.resize_to_fill(1000, 400)
888
+ assert_equal([1000, 400], wand.dimensions)
889
+ end
890
+
891
+ def test_resize_to_fit
892
+ wand = MagickWand::Wand.new
893
+ wand.add_canvas(40, 30)
894
+ # default height = width
895
+ assert_nothing_raised { wand.resize_to_fit(20) }
896
+ assert_equal([20, 15], wand.dimensions)
897
+
898
+ wand = MagickWand::Wand.new
899
+ wand.add_canvas(40, 30)
900
+ # explicit height
901
+ assert_nothing_raised { wand.resize_to_fit(80, 30) }
902
+ assert_equal([40, 30], wand.dimensions)
903
+
904
+ # Ensure options get passed by passing an unknown filter
905
+ assert_raise(ArgumentError) { wand.resize_to_fit(80, 30, :filter=>"xyzzy") }
906
+ end
907
+
908
+ def test_resize_many
909
+ wand = MagickWand::Wand.new
910
+ BUTTON_IMAGES.each {|f| wand.read f}
911
+ assert_nothing_raised { wand.resize(254, 240) }
912
+ assert_equal(BUTTON_IMAGES.length, wand.length)
913
+ wand.each do |w|
914
+ assert_equal([254, 240], wand.dimensions)
915
+ end
916
+ end
917
+
918
+ def test_resolution
919
+ wand = MagickWand::Wand.new
920
+ wand.read BUTTON_IMAGES[0]
921
+ assert_equal([72, 72], wand.resolution)
922
+ end
923
+
924
+ def test_rotate
925
+ wand = MagickWand::Wand.new
926
+ BUTTON_IMAGES.sort.each {|f| wand. read f }
927
+ assert_nothing_raised { wand.rotate(30) }
928
+ wand.each do |w|
929
+ assert_equal([169, 168], wand.dimensions)
930
+ end
931
+
932
+ # ensure it accepts a background color argument
933
+ wand = MagickWand::Wand.new
934
+ wand.read BUTTON_IMAGES[0]
935
+ assert_nothing_raised { wand.rotate(30, "black") }
936
+
937
+ wand.freeze
938
+ assert_raise(FreezeError) { wand.rotate(10) }
939
+ end
940
+
941
+
942
+ def test_sharpen
943
+ wand = MagickWand::Wand.new
944
+ wand.read BUTTON_IMAGES[0]
945
+ assert_nothing_raised {wand.sharpen 0, 0.5}
946
+ assert_nothing_raised {wand.sharpen 1, 2, :channel=>"red"}
947
+ assert_raise(ArgumentError) { wand.sharpen 1, 2, :channel=>"xxx" }
948
+
949
+ wand.freeze
950
+ assert_raise(FreezeError) {wand.sharpen 0, 0.5}
951
+ end
952
+
953
+ # slice is a synonym for []
954
+ def test_slice
955
+ wand = MagickWand::Wand.new
956
+ wand2 = nil
957
+
958
+ BUTTON_IMAGES.sort.each {|f| wand. read f }
959
+ assert_nothing_raised { wand2 = wand.slice(0) }
960
+ assert_instance_of(MagickWand::Wand, wand2)
961
+ assert_not_same(wand, wand2)
962
+ assert_equal(1, wand2.length)
963
+ assert_match(/Button_0.gif/, wand2.filename)
964
+
965
+ assert_nothing_raised { wand2 = wand.slice(1..3) }
966
+ assert_equal(3, wand2.length)
967
+ [1, 2, 3].each_with_index do |m, n|
968
+ assert_match(/Button_#{m}.gif/, wand2[n].filename)
969
+ end
970
+ end
971
+
972
+ def test_slice!
973
+ wand = MagickWand::Wand.new
974
+ BUTTON_IMAGES.sort.each {|f| wand.read f }
975
+ initial_length = wand.length
976
+
977
+ wand2 = nil
978
+ assert_nothing_raised { wand2 = wand.slice!(2) }
979
+ assert_instance_of(MagickWand::Wand, wand2)
980
+ assert_not_same(wand, wand2)
981
+ assert_equal(1, wand2.length)
982
+ assert_equal(initial_length-1, wand.length)
983
+
984
+ # image #2 in wand2
985
+ assert_match(/Button_2.gif/, wand2.filename)
986
+ # image #2 gone from wand
987
+ assert_no_match(/Button_2.gif/, wand.filename)
988
+
989
+ wand = MagickWand::Wand.new
990
+ BUTTON_IMAGES.sort.each {|f| wand.read f }
991
+ initial_length = wand.length
992
+
993
+ wand2 = nil
994
+ assert_nothing_raised { wand2 = wand.slice!(3..6) }
995
+ assert_not_same(wand, wand2)
996
+ assert_equal(4, wand2.length)
997
+ assert_equal(initial_length-4, wand.length)
998
+
999
+ # images moved to wand2
1000
+ [3, 4, 5, 6].each_with_index do |m, n|
1001
+ assert_match(/Button_#{m}.gif/, wand2[n].filename)
1002
+ end
1003
+ # images no longer present in wand
1004
+ [3, 4, 5, 6].each_with_index do |m, n|
1005
+ assert_no_match(/Button_#{m}.gif/, wand.filename)
1006
+ end
1007
+
1008
+ wand.freeze
1009
+ assert_raise(FreezeError) { wand.slice!(0) }
1010
+ end
1011
+
1012
+ def test_strip
1013
+ wand = MagickWand::Wand.new
1014
+ wand.add_canvas(20, 20)
1015
+ assert_nothing_raised { wand.strip }
1016
+ wand.freeze
1017
+ assert_raise(FreezeError) { wand.strip }
1018
+ end
1019
+
1020
+ def test_type
1021
+ wand = MagickWand::Wand.new
1022
+ assert_nil(wand.type)
1023
+ wand.read BUTTON_IMAGES[0]
1024
+ assert_equal("PaletteType", wand.type)
1025
+ if RUBY_VERSION != '1.9.1'
1026
+ assert_respond_to(wand, :__type__)
1027
+ # this assertion produces an unsuppressable warning message.
1028
+ #assert_equal("MagickWand::Wand", wand.__type__)
1029
+ end
1030
+ end
1031
+
1032
+ def test_write
1033
+ begin
1034
+ wand = MagickWand::Wand.new
1035
+ wand.read BUTTON_IMAGES[0]
1036
+ assert_nothing_raised { wand.write "#{ENV['TMP']}/test_write.jpg" }
1037
+ File.delete "#{ENV['TMP']}/test_write.jpg"
1038
+
1039
+ wand = MagickWand::Wand.new
1040
+ BUTTON_IMAGES.each {|f| wand.read f}
1041
+ length = wand.length
1042
+ assert_nothing_raised { wand.write("#{ENV['TMP']}/test_write.gif", :adjoin=>true) }
1043
+
1044
+ wand = MagickWand::Wand.new
1045
+ wand.read "#{ENV['TMP']}/test_write.gif"
1046
+ assert_equal(length, wand.length)
1047
+ File.delete "#{ENV['TMP']}/test_write.gif"
1048
+
1049
+ # Write 10 separate files
1050
+ assert_nothing_raised { wand.write "#{ENV['TMP']}/test_write%d.gif", :adjoin=>false }
1051
+ files = Dir["#{ENV['TMP']}/test_write*.gif"]
1052
+ assert_equal(length, files.length)
1053
+
1054
+ ensure
1055
+ File.delete "#{ENV['TMP']}/test_write.gif" rescue nil
1056
+ File.delete(*files) rescue nil
1057
+ end
1058
+ end
1059
+
1060
+ def test_write_errors
1061
+ wand = MagickWand::Wand.new
1062
+ wand.read BUTTON_IMAGES[0]
1063
+
1064
+ # unknown option NOT diagnosed
1065
+ assert_nothing_raised { wand.write "tmp/test_write_errors.gif", :xyzzy=>1 }
1066
+
1067
+ # Write to R/O directory
1068
+ assert_raise(MagickWand::ImageMagickError) { wand.write "/usr/test.jpg" }
1069
+ # bad compression
1070
+ assert_raise(ArgumentError) { wand.write "test.gif", :compress=>"zzz" }
1071
+ # can't convert nil into string
1072
+ assert_raise(TypeError) { wand.write nil }
1073
+ # ensure :type option is accepted and validated
1074
+ assert_raise(ArgumentError) { wand.write "test.gif", :type=>"zzzz" }
1075
+ end
1076
+
1077
+ # test read_string too
1078
+ def test_write_string
1079
+ wand = MagickWand::Wand.new
1080
+ BUTTON_IMAGES.each {|f| wand.read f }
1081
+ s = nil
1082
+ assert_nothing_raised { s = wand.write_string }
1083
+ assert_equal(46278, s.length)
1084
+
1085
+ wand2 = MagickWand::Wand.new
1086
+ assert_nothing_raised { wand2.read_string s }
1087
+ assert_equal(wand.length, wand2.length)
1088
+ wand.length.times do |n|
1089
+ assert_equal(wand[n].format, wand2[n].format)
1090
+ assert_equal(wand[n].dimensions, wand2[n].dimensions)
1091
+ assert_equal(wand[n].page, wand2[n].page)
1092
+ end
1093
+
1094
+ assert_nothing_raised { wand.write_string :format=>"miff", :compress=>"lzw", :quality=>80 }
1095
+
1096
+ # unknown format
1097
+ assert_raise(ArgumentError) { wand.write_string s, :format=>"zzz" }
1098
+ end
1099
+
1100
+ end