summaryrefslogtreecommitdiffhomepage
path: root/test/drawing/tc_pic.rb
blob: 9e4d62e69888fe7485230d018867c2dd8c051c0a (plain)
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
require 'test/unit'
require 'axlsx.rb'

class TestPic < Test::Unit::TestCase

  def setup
    @p = Axlsx::Package.new
    ws = @p.workbook.add_worksheet
    @test_img =  File.dirname(__FILE__) + "/../../examples/image1.jpeg"
    @image = ws.add_image :image_src => @test_img
  end

  def teardown
  end

  def test_initialization
    assert_equal(@p.workbook.images.first, @image)
    assert_equal(@image.image_src, @test_img)
  end
  
  def test_hyperlink
    assert_equal(@image.hyperlink, nil)
    @image.hyperlink = "http://axlsx.blogspot.com"
    assert_equal(@image.hyperlink.href, "http://axlsx.blogspot.com")
  end

  def test_name
    assert_raise(ArgumentError) { @image.name = 49 }
    assert_nothing_raised { @image.name = "unknown" }
    assert_equal(@image.name, "unknown")
  end

  def test_start_at
    assert_raise(ArgumentError) { @image.start_at "a", 1 }
    assert_nothing_raised { @image.start_at 6, 7 }
    assert_equal(@image.anchor.from.col, 6)
    assert_equal(@image.anchor.from.row, 7)
  end

  def test_width
    assert_raise(ArgumentError) { @image.width = "a" }
    assert_nothing_raised { @image.width = 600 }
    assert_equal(@image.width, 600)   
  end

  def test_height
    assert_raise(ArgumentError) { @image.height = "a" }
    assert_nothing_raised { @image.height = 600 }
    assert_equal(600, @image.height)   
  end

  def test_image_src
    assert_raise(ArgumentError) { @image.image_src = 49 }
    assert_raise(ArgumentError) { @image.image_src = 'Unknown' }
    assert_raise(ArgumentError) { @image.image_src = __FILE__ }
    assert_nothing_raised { @image.image_src = @test_img }
    assert_equal(@image.image_src, @test_img)
  end

  def test_descr
    assert_raise(ArgumentError) { @image.descr = 49 }
    assert_nothing_raised { @image.descr = "test" }
    assert_equal(@image.descr, "test")
  end
 
  def test_to_xml
    schema = Nokogiri::XML::Schema(File.open(Axlsx::DRAWING_XSD))
    doc = Nokogiri::XML(@image.anchor.drawing.to_xml)
    errors = []
    schema.validate(doc).each do |error|
      errors.push error
      puts error.message
    end
    assert(errors.empty?, "error free validation")
  end  
  
end