Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 162 lines (145 sloc) 5.638 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
# -- encoding: utf-8 --
require 'digest/md5'
require 'fileutils'
require 'tempfile'
require 'helpers_for_test'

class TestWrite < TestCase

  def setup
    @temp_file = Tempfile.new('test')
    @temp_file.close
    @temp_filename = @temp_file.path
    @org_filename = File.dirname(__FILE__) + '/data/test.jpg'
    FileUtils.cp(@org_filename, @temp_filename)
    @mini_exiftool = MiniExiftool.new @temp_filename
    @mini_exiftool_num = MiniExiftool.new @temp_filename, :numerical => true
  end

  def teardown
    @temp_file.delete
  end

  def test_access_existing_tags
    assert_equal 'Horizontal (normal)', @mini_exiftool['Orientation']
    @mini_exiftool['Orientation'] = 'some string'
    assert_equal 'some string', @mini_exiftool['Orientation']
    assert_equal false, @mini_exiftool.changed?('Orientation')
    @mini_exiftool['Orientation'] = 2
    assert_equal 2, @mini_exiftool['Orientation']
    assert @mini_exiftool.changed_tags.include?('Orientation')
    @mini_exiftool.save
    assert_equal 'Mirror horizontal', @mini_exiftool['Orientation']
    @mini_exiftool_num.reload
    assert_equal 2, @mini_exiftool_num['Orientation']
  end

  def test_access_existing_tags_numerical
    assert_equal 1, @mini_exiftool_num['Orientation']
    @mini_exiftool_num['Orientation'] = 2
    assert_equal 2, @mini_exiftool_num['Orientation']
    assert_equal 2, @mini_exiftool_num.orientation
    @mini_exiftool_num.orientation = 3
    assert_equal 3, @mini_exiftool_num.orientation
    assert @mini_exiftool_num.changed_tags.include?('Orientation')
    @mini_exiftool_num.save
    assert_equal 3, @mini_exiftool_num['Orientation']
    @mini_exiftool.reload
    assert_equal 'Rotate 180', @mini_exiftool['Orientation']
  end

  def test_access_non_writable_tags
    @mini_exiftool_num['FileSize'] = 1
    assert_equal true, @mini_exiftool_num.changed?
    @mini_exiftool_num['SomeNonWritableName'] = 'test'
    assert_equal true, @mini_exiftool_num.changed?
  end

  # Catching rubyforge bug [#29596]
  # Thanks to Michael Grove for reporting
  # Part 1
  def test_quotes_in_values
    caption = "\"String in quotes\""
    @mini_exiftool.caption = caption
    assert_equal true, @mini_exiftool.save, 'Saving error'
    @mini_exiftool.reload
    assert_equal caption, @mini_exiftool.caption
  end

  # Catching rubyforge bug [#29596]
  # Thanks to Michael Grove for reporting
  # Part 2
  def test_quotes_and_apostrophe_in_values
    caption = caption = "\"Watch your step, it's slippery.\""
    @mini_exiftool.caption = caption
    assert_equal true, @mini_exiftool.save, 'Saving error'
    @mini_exiftool.reload
    assert_equal caption, @mini_exiftool.caption
  end

  def test_time_conversion
    t = Time.now
    @mini_exiftool_num['DateTimeOriginal'] = t
    assert_kind_of Time, @mini_exiftool_num['DateTimeOriginal']
    assert_equal true, @mini_exiftool_num.changed_tags.include?('DateTimeOriginal')
    @mini_exiftool_num.save
    assert_equal false, @mini_exiftool_num.changed?
    assert_kind_of Time, @mini_exiftool_num['DateTimeOriginal']
    assert_equal t.to_s, @mini_exiftool_num['DateTimeOriginal'].to_s
  end

  def test_float_conversion
    assert_kind_of Float, @mini_exiftool_num['BrightnessValue']
    new_time = @mini_exiftool_num['BrightnessValue'] + 1
    @mini_exiftool_num['BrightnessValue'] = new_time
    assert_equal new_time, @mini_exiftool_num['BrightnessValue']
    assert_equal true, @mini_exiftool_num.changed_tags.include?('BrightnessValue')
    @mini_exiftool_num.save
    assert_kind_of Float, @mini_exiftool_num['BrightnessValue']
    assert_equal new_time, @mini_exiftool_num['BrightnessValue']
  end

  def test_integer_conversion
    assert_kind_of Integer, @mini_exiftool_num['MeteringMode']
    new_mode = @mini_exiftool_num['MeteringMode'] - 1
    @mini_exiftool_num['MeteringMode'] = new_mode
    assert_equal new_mode, @mini_exiftool_num['MeteringMode']
    assert @mini_exiftool_num.changed_tags.include?('MeteringMode')
    @mini_exiftool_num.save
    assert_equal new_mode, @mini_exiftool_num['MeteringMode']
  end

  def test_rational_conversion
    new_exposure_time = Rational(1, 125)
    @mini_exiftool.exposure_time = new_exposure_time
    assert @mini_exiftool.changed?, 'No changing of value.'
    ok = @mini_exiftool.save
    assert ok, 'Saving failed.'
    @mini_exiftool.reload
    assert_equal new_exposure_time, @mini_exiftool.exposure_time
  end

  def test_list_conversion
    arr = ['a', 'b', 'c']
    @mini_exiftool['Keywords'] = arr
    ok = @mini_exiftool.save
    assert ok
    assert_equal arr, @mini_exiftool['Keywords']
    arr = ['text, with', 'commas, let us look']
    @mini_exiftool['Keywords'] = arr
    ok = @mini_exiftool.save
    assert ok
    if MiniExiftool.exiftool_version.to_f < 7.41
      assert_equal ['text', 'with', 'commas', 'let us look'], @mini_exiftool['Keywords']
    else
      assert_equal arr, @mini_exiftool['Keywords']
    end
  end

  def test_revert_one
    @mini_exiftool_num['Orientation'] = 2
    @mini_exiftool_num['ISO'] = 200
    res = @mini_exiftool_num.revert 'Orientation'
    assert_equal 1, @mini_exiftool_num['Orientation']
    assert_equal 200, @mini_exiftool_num['ISO']
    assert_equal true, res
    res = @mini_exiftool_num.revert 'Orientation'
    assert_equal false, res
  end

  def test_revert_all
    @mini_exiftool_num['Orientation'] = 2
    @mini_exiftool_num['ISO'] = 200
    res = @mini_exiftool_num.revert
    assert_equal 1, @mini_exiftool_num['Orientation']
    assert_equal 400, @mini_exiftool_num['ISO']
    assert_equal true, res
    res = @mini_exiftool_num.revert
    assert_equal false, res
  end

end
Something went wrong with that request. Please try again.