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
|
require 'tmpdir'
dir = Dir.mktmpdir "image_science."
ENV['INLINEDIR'] = dir
require 'minitest'
Minitest.after_run do
require 'fileutils'
FileUtils.rm_rf dir
end
require 'rubygems'
require 'minitest/test'
require 'minitest/autorun' if $0 == __FILE__
require 'image_science'
class TestImageScience < Minitest::Test
def setup
@path = 'test/pix.png'
@tmppath = 'test/pix-tmp.png'
@h = @w = 50
end
def teardown
File.unlink @tmppath if File.exist? @tmppath
end
def test_class_with_image
ImageScience.with_image @path do |img|
assert_kind_of ImageScience, img
assert_equal @h, img.height
assert_equal @w, img.width
assert img.save(@tmppath)
end
assert File.exist?(@tmppath)
ImageScience.with_image @tmppath do |img|
assert_kind_of ImageScience, img
assert_equal @h, img.height
assert_equal @w, img.width
end
end
def test_class_with_image_missing
assert_raises TypeError do
ImageScience.with_image @path + "nope" do |img|
flunk
end
end
end
def test_class_with_image_missing_with_img_extension
assert_raises RuntimeError do
assert_nil ImageScience.with_image("nope#{@path}") do |img|
flunk
end
end
end
def test_class_with_image_from_memory
data = File.new(@path).binmode.read
ImageScience.with_image_from_memory data do |img|
assert_kind_of ImageScience, img
assert_equal @h, img.height
assert_equal @w, img.width
assert img.save(@tmppath)
end
assert File.exist?(@tmppath)
ImageScience.with_image @tmppath do |img|
assert_kind_of ImageScience, img
assert_equal @h, img.height
assert_equal @w, img.width
end
end
def test_class_with_image_from_memory_empty_string
assert_raises TypeError do
ImageScience.with_image_from_memory "" do |img|
flunk
end
end
end
def test_resize
ImageScience.with_image @path do |img|
img.resize(25, 25) do |thumb|
assert thumb.save(@tmppath)
end
end
assert File.exist?(@tmppath)
ImageScience.with_image @tmppath do |img|
assert_kind_of ImageScience, img
assert_equal 25, img.height
assert_equal 25, img.width
end
end
def test_resize_floats
ImageScience.with_image @path do |img|
img.resize(25.2, 25.7) do |thumb|
assert thumb.save(@tmppath)
end
end
assert File.exist?(@tmppath)
ImageScience.with_image @tmppath do |img|
assert_kind_of ImageScience, img
assert_equal 25, img.height
assert_equal 25, img.width
end
end
def test_resize_zero
assert_raises ArgumentError do
ImageScience.with_image @path do |img|
img.resize(0, 25) do |thumb|
assert thumb.save(@tmppath)
end
end
end
refute File.exist?(@tmppath)
assert_raises ArgumentError do
ImageScience.with_image @path do |img|
img.resize(25, 0) do |thumb|
assert thumb.save(@tmppath)
end
end
end
refute File.exist?(@tmppath)
end
def test_resize_negative
assert_raises ArgumentError do
ImageScience.with_image @path do |img|
img.resize(-25, 25) do |thumb|
assert thumb.save(@tmppath)
end
end
end
refute File.exist?(@tmppath)
assert_raises ArgumentError do
ImageScience.with_image @path do |img|
img.resize(25, -25) do |thumb|
assert thumb.save(@tmppath)
end
end
end
refute File.exist?(@tmppath)
end
def test_thumbnail
ImageScience.with_image @path do |img|
img.thumbnail(29) do |thumb|
assert thumb.save(@tmppath)
end
end
assert File.exist?(@tmppath)
ImageScience.with_image @tmppath do |img|
assert_kind_of ImageScience, img
assert_equal 29, img.height
assert_equal 29, img.width
end
end
def test_auto_rotate_from_file
ImageScience.with_image "test/portrait.jpg" do |img|
assert_equal 50, img.height
assert_equal 38, img.width
end
end
def test_auto_rotate_from_memory
data = File.new("test/portrait.jpg").binmode.read
ImageScience.with_image_from_memory data do |img|
assert_equal 50, img.height
assert_equal 38, img.width
end
end
def test_rotate
ImageScience.with_image "test/portrait.jpg" do |image|
image.rotate 90 do |img|
assert_equal 50, img.width
assert_equal 38, img.height
end
end
end
def test_rotate_not_45
e = assert_raises ArgumentError do
ImageScience.with_image "test/portrait.jpg" do |image|
image.rotate 44
end
end
assert_equal 'Angle must be 45 degree skew', e.message
end
end
|