summaryrefslogtreecommitdiffhomepage
path: root/test/drawing/tc_title.rb
blob: ee4d12c9f6330e855bad9d058e5a104c1a3f9f85 (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
# frozen_string_literal: true

require 'tc_helper'

class TestTitle < Test::Unit::TestCase
  def setup
    @p = Axlsx::Package.new
    ws = @p.workbook.add_worksheet
    @row = ws.add_row ["one", 1, Time.now]
    @title = Axlsx::Title.new
    @chart = ws.add_chart Axlsx::Bar3DChart
  end

  def teardown; end

  def test_initialization
    assert_equal("", @title.text)
    assert_nil(@title.cell)
  end

  def test_initialize_title_size
    title = Axlsx::Title.new 'bob', 90

    assert_equal "90", title.text_size
  end

  def test_text
    assert_raise(ArgumentError, "text must be a string") { @title.text = 123 }
    @title.cell = @row.cells.first
    @title.text = "bob"

    assert_nil(@title.cell, "setting title with text clears the cell")
  end

  def test_cell
    assert_raise(ArgumentError, "cell must be a Cell") { @title.cell = "123" }
    @title.cell = @row.cells.first

    assert_equal("one", @title.text)
  end

  def test_to_xml_string_text
    @chart.title.text = 'foo'
    doc = Nokogiri::XML(@chart.to_xml_string)

    assert_equal(1, doc.xpath('//c:rich').size)
    assert_equal(1, doc.xpath("//a:t[text()='foo']").size)
  end

  def test_to_xml_string_cell
    @chart.title.cell = @row.cells.first
    doc = Nokogiri::XML(@chart.to_xml_string)

    assert_equal("'Sheet1'!$A$1:$A$1", doc.xpath('//c:strRef/c:f').text)
    assert_equal(1, doc.xpath('//c:strCache').size)
    assert_equal('one', doc.xpath('//c:strCache/c:pt//c:v').text)
  end

  def test_to_xml_string_empty_cell
    @row.cells.first.value = ""
    @chart.title.cell = @row.cells.first
    doc = Nokogiri::XML(@chart.to_xml_string)

    assert_equal("'Sheet1'!$A$1:$A$1", doc.xpath('//c:strRef/c:f').text)
    assert_equal(1, doc.xpath('//c:strCache').size)
    assert_equal('', doc.xpath('//c:strCache/c:pt//c:v').text)
  end

  def test_to_xml_string_for_special_characters
    @chart.title.text = "&><'\""
    doc = Nokogiri::XML(@chart.to_xml_string)
    errors = doc.errors

    assert_empty(errors, "invalid xml: #{errors.map(&:to_s).join(', ')}")
  end

  def test_to_xml_string_for_special_characters_in_cell
    cell = @row.cells.first
    cell.value = "&><'\""

    @chart.title.cell = cell
    doc = Nokogiri::XML(@chart.to_xml_string)
    errors = doc.errors

    assert_empty(errors, "invalid xml: #{errors.map(&:to_s).join(', ')}")
  end
end