Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 363 lines (285 sloc) 10.731 kb
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 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363

#! /usr/local/bin/ruby -w

require 'RMagick'
require 'test/unit'
require 'test/unit/ui/console/testrunner' if RUBY_VERSION != '1.9.1'


module Magick
  def self._tmpnam_
    @@_tmpnam_
  end
end

class Magick::AlphaChannelType
  def self.enumerators
    @@enumerators
  end
end

class Magick::AlignType
  def self.enumerators
    @@enumerators
  end
end

class Magick::AnchorType
  def self.enumerators
    @@enumerators
  end
end


class Magick_UT < Test::Unit::TestCase

    def test_colors
      res = nil
      assert_nothing_raised { res = Magick.colors }
      assert_instance_of(Array, res)
      res.each do |c|
        assert_instance_of(Magick::Color, c)
        assert_instance_of(String, c.name)
        assert_instance_of(Magick::ComplianceType, c.compliance)
        assert_instance_of(Magick::Pixel, c.color)
      end
      Magick.colors {|c| assert_instance_of(Magick::Color, c) }
    end

    # Test a few of the @@enumerator arrays in the Enum subclasses.
    # No need to test all of them.
    def test_enumerators
      ary = nil
      assert_nothing_raised do
        ary = Magick::AlphaChannelType.enumerators
      end
      assert_instance_of(Array, ary)
      assert_equal(10, ary.length)

      assert_nothing_raised do
        ary = Magick::AlignType.enumerators
      end
      assert_instance_of(Array, ary)
      assert_equal(4, ary.length)

      assert_nothing_raised do
        ary = Magick::AnchorType.enumerators
      end
      assert_instance_of(Array, ary)
      assert_equal(3, ary.length)
    end

    def test_fonts
      res = nil
      assert_nothing_raised { res = Magick.fonts }
      assert_instance_of(Array, res)
      res.each do |f|
        assert_instance_of(Magick::Font, f)
        assert_instance_of(String, f.name)
        assert_instance_of(String, f.description) unless f.description.nil?
        assert_instance_of(String, f.family)
        assert_instance_of(Magick::StyleType, f.style)
        assert_instance_of(Magick::StretchType, f.stretch)
        assert_instance_of(Fixnum, f.weight)
        assert_instance_of(String, f.encoding) unless f.encoding.nil?
        assert_instance_of(String, f.foundry) unless f.foundry.nil?
        assert_instance_of(String, f.format) unless f.format.nil?
      end
    Magick.fonts {|f| assert_instance_of(Magick::Font, f) }
    end

    def test_formats
      res = nil
      assert_nothing_raised { res = Magick.formats }
      assert_instance_of(Hash, res)
      res.each do |f, v|
        assert_instance_of(String, f)
        assert_instance_of(String, v)
      end
      Magick.formats.each { |f, v| assert_not_nil(f); assert_not_nil(v) }
    end

    def test_geometry
      g, gs, g2, gs2 = nil, nil, nil, nil
      assert_nothing_raised { g = Magick::Geometry.new() }
      assert_nothing_raised { gs = g.to_s }
      assert_equal("", gs)

      g = Magick::Geometry.new(40)
      gs = g.to_s
      assert_equal("40x", gs)

      assert_nothing_raised { g2 = Magick::Geometry.from_s(gs) }
      gs2 = g2.to_s
      assert_equal(gs, gs2)

      g = Magick::Geometry.new(40, 50)
      gs = g.to_s
      assert_equal("40x50", gs)

      assert_nothing_raised { g2 = Magick::Geometry.from_s(gs) }
      gs2 = g2.to_s
      assert_equal(gs, gs2)

      g = Magick::Geometry.new(40, 50, 10)
      gs = g.to_s
      assert_equal("40x50+10+0", gs)

      assert_nothing_raised { g2 = Magick::Geometry.from_s(gs) }
      gs2 = g2.to_s
      assert_equal(gs, gs2)

      g = Magick::Geometry.new(40, 50, 10, -15)
      gs = g.to_s
      assert_equal("40x50+10-15", gs)

      assert_nothing_raised { g2 = Magick::Geometry.from_s(gs) }
      gs2 = g2.to_s
      assert_equal(gs, gs2)

      g = Magick::Geometry.new(40, 50, 0, 0, Magick::AreaGeometry)
      gs = g.to_s
      assert_equal("40x50@", gs)

      assert_nothing_raised { g2 = Magick::Geometry.from_s(gs) }
      gs2 = g2.to_s
      assert_equal(gs, gs2)

      g = Magick::Geometry.new(40, 50, 0, 0, Magick::AspectGeometry)
      gs = g.to_s
      assert_equal("40x50!", gs)

      assert_nothing_raised { g2 = Magick::Geometry.from_s(gs) }
      gs2 = g2.to_s
      assert_equal(gs, gs2)

      g = Magick::Geometry.new(40, 50, 0, 0, Magick::LessGeometry)
      gs = g.to_s
      assert_equal("40x50<", gs)

      assert_nothing_raised { g2 = Magick::Geometry.from_s(gs) }
      gs2 = g2.to_s
      assert_equal(gs, gs2)

      g = Magick::Geometry.new(40, 50, 0, 0, Magick::GreaterGeometry)
      gs = g.to_s
      assert_equal("40x50>", gs)

      assert_nothing_raised { g2 = Magick::Geometry.from_s(gs) }
      gs2 = g2.to_s
      assert_equal(gs, gs2)

      g = Magick::Geometry.new(40, 50, 0, 0, Magick::MinimumGeometry)
      gs = g.to_s
      assert_equal("40x50^", gs)

      assert_nothing_raised { g2 = Magick::Geometry.from_s(gs) }
      gs2 = g2.to_s
      assert_equal(gs, gs2)

      g = Magick::Geometry.new(40, 0, 0, 0, Magick::PercentGeometry)
      gs = g.to_s
      assert_equal("40%", gs)

      assert_nothing_raised { g2 = Magick::Geometry.from_s(gs) }
      gs2 = g2.to_s
      assert_equal(gs, gs2)

      g = Magick::Geometry.new(40, 60, 0, 0, Magick::PercentGeometry)
      gs = g.to_s
      assert_equal("40%x60%", gs)

      assert_nothing_raised { g2 = Magick::Geometry.from_s(gs) }
      gs2 = g2.to_s
      assert_equal(gs, gs2)

      g = Magick::Geometry.new(40, 60, 10, 0, Magick::PercentGeometry)
      gs = g.to_s
      assert_equal("40%x60%+10+0", gs)

      assert_nothing_raised { g2 = Magick::Geometry.from_s(gs) }
      gs2 = g2.to_s
      assert_equal(gs, gs2)

      g = Magick::Geometry.new(40, 60, 10, 20, Magick::PercentGeometry)
      gs = g.to_s
      assert_equal("40%x60%+10+20", gs)

      assert_nothing_raised { g2 = Magick::Geometry.from_s(gs) }
      gs2 = g2.to_s
      assert_equal(gs, gs2)

      g = Magick::Geometry.new(40.5, 60.75)
      gs = g.to_s
      assert_equal("40.50x60.75", gs)

      assert_nothing_raised { g2 = Magick::Geometry.from_s(gs) }
      gs2 = g2.to_s
      assert_equal(gs, gs2)

      g = Magick::Geometry.new(40.5, 60.75, 0, 0, Magick::PercentGeometry)
      gs = g.to_s
      assert_equal("40.50%x60.75%", gs)

      assert_nothing_raised { g2 = Magick::Geometry.from_s(gs) }
      gs2 = g2.to_s
      assert_equal(gs, gs2)

      g = Magick::Geometry.new(0, 0, 10, 20)
      gs = g.to_s
      assert_equal("+10+20", gs)

      assert_nothing_raised { g2 = Magick::Geometry.from_s(gs) }
      gs2 = g2.to_s
      assert_equal(gs, gs2)

      g = Magick::Geometry.new(0, 0, 10)
      gs = g.to_s
      assert_equal("+10+0", gs)

      assert_nothing_raised { g2 = Magick::Geometry.from_s(gs) }
      gs2 = g2.to_s
      assert_equal(gs, gs2)

      # assert behavior with empty string argument
      assert_nothing_raised { g = Magick::Geometry.from_s("") }
      assert_equal("", g.to_s)

      assert_raise(ArgumentError) { Magick::Geometry.new(Magick::AreaGeometry) }
      assert_raise(ArgumentError) { Magick::Geometry.new(40, Magick::AreaGeometry) }
      assert_raise(ArgumentError) { Magick::Geometry.new(40, 20, Magick::AreaGeometry) }
      assert_raise(ArgumentError) { Magick::Geometry.new(40, 20, 10, Magick::AreaGeometry) }

    end

    def test_set_log_event_mask
      assert_nothing_raised { Magick.set_log_event_mask("Module,Coder") }
    end

    def test_set_log_format
      assert_nothing_raised { Magick.set_log_format("format %d%e%f") }
    end

    # put old limits back in place after testing
    def test_limit_resources
        cur = new = nil

        assert_nothing_raised {cur = Magick::limit_resource(:memory, 500)}
        assert_equal(789866496, cur)
        assert_nothing_raised {new = Magick::limit_resource("memory")}
        assert_equal(500, new)
        Magick::limit_resource(:memory, cur)

        assert_nothing_raised {cur = Magick::limit_resource(:map, 3500)}
        assert_equal(2106310656, cur)
        assert_nothing_raised {new = Magick::limit_resource("map")}
        assert_equal(3500, new)
        Magick::limit_resource(:map, cur)

        assert_nothing_raised {cur = Magick::limit_resource(:disk, 3*1024*1024*1024)}
        assert_equal(4294967295, cur)
        assert_nothing_raised {new = Magick::limit_resource("disk")}
        assert_equal(3221225472, new)
        Magick::limit_resource(:disk, cur)

        assert_nothing_raised {cur = Magick::limit_resource(:file, 500)}
        assert_equal(768, cur)
        assert_nothing_raised {new = Magick::limit_resource("file")}
        assert_equal(500, new)
        Magick::limit_resource(:file, cur)

        assert_raise(ArgumentError) { Magick::limit_resource(:xxx) }
        assert_raise(ArgumentError) { Magick::limit_resource("xxx") }
        assert_raise(ArgumentError) { Magick::limit_resource("map", 3500, 2) }
        assert_raise(ArgumentError) { Magick::limit_resource() }

    end

    # test the @@_tmpnam_ class variable
    # the count is incremented by Image::Info#texture=,
    # ImageList::Montage#texture=, and Draw.composite

    def test_tmpnam

      tmpfiles = Dir[ENV["HOME"] + "/tmp/magick*"].length

      texture = Magick::Image.read("granite:") {self.size = "20x20" }.first
      info = Magick::Image::Info.new

      # does not exist at first
      # assert_raise(NameError) { x = Magick._tmpnam_ }
      info.texture = texture

      # now it exists
      assert_nothing_raised { x = Magick._tmpnam_ }
      assert_equal(1, Magick._tmpnam_)

      info.texture = texture
      assert_equal(2, Magick._tmpnam_)

      mon = Magick::ImageList::Montage.new
      mon.texture = texture
      assert_equal(3, Magick._tmpnam_)

      mon.texture = texture
      assert_equal(4, Magick._tmpnam_)

      gc = Magick::Draw.new
      gc.composite(0, 0, 20, 20, texture)
      assert_equal(5, Magick._tmpnam_)

      gc.composite(0, 0, 20, 20, texture)
      assert_equal(6, Magick._tmpnam_)

      tmpfiles2 = Dir[ENV["HOME"] + "/tmp/magick*"].length

      # The 2nd montage texture deletes the first.
      # The 2nd info texture deletes the first.
      # Both composite images are still alive.
      # Therefore only 4 tmp files are left.
      #assert_equal(tmpfiles+4, tmpfiles2)
      # 6.4.1-5 - only 1 tmpfile?
      assert_equal(tmpfiles, tmpfiles2)

    end

    def test_trace_proc
      Magick.trace_proc = lambda do |which, description, id, method|
        assert(which == :c)
        assert_instance_of(String, description)
        assert_instance_of(String, id)
        assert_equal(:initialize, method)
      end
      begin
        img = Magick::Image.new(20,20)
      ensure
        Magick.trace_proc = nil
      end
    end
end

if __FILE__ == $0
Test::Unit::UI::Console::TestRunner.run(Magick_UT) if RUBY_VERSION != '1.9.1'
end
Something went wrong with that request. Please try again.