summaryrefslogtreecommitdiffhomepage
path: root/test/drawing/tc_drawing.rb
blob: d48ae604e05630595235471fa753c4fb553e6c97 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# frozen_string_literal: true

require 'tc_helper'

class TestDrawing < Test::Unit::TestCase
  def setup
    p = Axlsx::Package.new
    @ws = p.workbook.add_worksheet
  end

  def test_initialization
    assert_empty(@ws.workbook.drawings)
  end

  def test_add_chart
    chart = @ws.add_chart(Axlsx::Pie3DChart, title: "bob", start_at: [0, 0], end_at: [1, 1])

    assert(chart.is_a?(Axlsx::Pie3DChart), "must create a chart")
    assert_equal(@ws.workbook.charts.last, chart, "must be added to workbook charts collection")
    assert_equal(@ws.drawing.anchors.last.object.chart, chart, "an anchor has been created and holds a reference to this chart")
    anchor = @ws.drawing.anchors.last

    assert_equal([0, 0], [anchor.from.row, anchor.from.col], "options for start at are applied")
    assert_equal([1, 1], [anchor.to.row, anchor.to.col], "options for start at are applied")
    assert_equal("bob", chart.title.text, "option for title is applied")
  end

  def test_add_image
    src = "#{File.dirname(__FILE__)}/../fixtures/image1.jpeg"
    image = @ws.add_image(image_src: src, start_at: [0, 0], width: 600, height: 400)

    assert(@ws.drawing.anchors.last.is_a?(Axlsx::OneCellAnchor))
    assert(image.is_a?(Axlsx::Pic))
    assert_equal(600, image.width)
    assert_equal(400, image.height)
  end

  def test_add_two_cell_anchor_image
    src = "#{File.dirname(__FILE__)}/../fixtures/image1.jpeg"
    image = @ws.add_image(image_src: src, start_at: [0, 0], end_at: [15, 0])

    assert(@ws.drawing.anchors.last.is_a?(Axlsx::TwoCellAnchor))
    assert(image.is_a?(Axlsx::Pic))
  end

  def test_charts
    chart = @ws.add_chart(Axlsx::Pie3DChart, title: "bob", start_at: [0, 0], end_at: [1, 1])

    assert_equal(@ws.drawing.charts.last, chart, "add chart is returned")
    chart = @ws.add_chart(Axlsx::Pie3DChart, title: "nancy", start_at: [1, 5], end_at: [5, 10])

    assert_equal(@ws.drawing.charts.last, chart, "add chart is returned")
  end

  def test_pn
    @ws.add_chart(Axlsx::Pie3DChart)

    assert_equal("drawings/drawing1.xml", @ws.drawing.pn)
  end

  def test_rels_pn
    @ws.add_chart(Axlsx::Pie3DChart)

    assert_equal("drawings/_rels/drawing1.xml.rels", @ws.drawing.rels_pn)
  end

  def test_index
    @ws.add_chart(Axlsx::Pie3DChart)

    assert_equal(@ws.drawing.index, @ws.workbook.drawings.index(@ws.drawing))
  end

  def test_relationships
    @ws.add_chart(Axlsx::Pie3DChart, title: "bob", start_at: [0, 0], end_at: [1, 1])

    assert_equal(1, @ws.drawing.relationships.size, "adding a chart adds a relationship")
    @ws.add_chart(Axlsx::Pie3DChart, title: "nancy", start_at: [1, 5], end_at: [5, 10])

    assert_equal(2, @ws.drawing.relationships.size, "adding a chart adds a relationship")
  end

  def test_to_xml
    schema = Nokogiri::XML::Schema(File.open(Axlsx::DRAWING_XSD))
    @ws.add_chart(Axlsx::Pie3DChart)
    doc = Nokogiri::XML(@ws.drawing.to_xml_string)
    errors = []
    schema.validate(doc).each do |error|
      errors.push error
      puts error.message
    end

    assert_empty(errors, "error free validation")
  end
end