summaryrefslogtreecommitdiffhomepage
path: root/test/workbook/worksheet/tc_worksheet_hyperlink.rb
blob: e4e82e88f38e6a72357d31f62e16c582cfc716bd (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
# frozen_string_literal: true

require 'tc_helper'

class TestWorksheetHyperlink < Test::Unit::TestCase
  def setup
    p = Axlsx::Package.new
    wb = p.workbook
    @ws = wb.add_worksheet
    @options = { location: 'https://github.com/randym/axlsx?foo=1&bar=2', tooltip: 'axlsx', ref: 'A1', display: 'AXSLX', target: :internal }
    @a = @ws.add_hyperlink @options
  end

  def test_initailize
    assert_raise(ArgumentError) { Axlsx::WorksheetHyperlink.new }
  end

  def test_location
    assert_equal(@options[:location], @a.location)
  end

  def test_tooltip
    assert_equal(@options[:tooltip], @a.tooltip)
  end

  def test_target
    assert_equal(@options[:target], Axlsx.instance_values_for(@a)['target'])
  end

  def test_display
    assert_equal(@options[:display], @a.display)
  end

  def test_ref
    assert_equal(@options[:ref], @a.ref)
  end

  def test_to_xml_string_with_non_external
    doc = Nokogiri::XML(@ws.to_xml_string)

    assert_equal(1, doc.xpath("//xmlns:hyperlink[@ref='#{@a.ref}']").size)
    assert_equal(1, doc.xpath("//xmlns:hyperlink[@tooltip='#{@a.tooltip}']").size)
    assert_equal(1, doc.xpath("//xmlns:hyperlink[@location='#{@a.location}']").size)
    assert_equal(1, doc.xpath("//xmlns:hyperlink[@display='#{@a.display}']").size)
    assert_equal(0, doc.xpath("//xmlns:hyperlink[@r:id]").size)
  end

  def test_to_xml_stirng_with_external
    @a.target = :external
    doc = Nokogiri::XML(@ws.to_xml_string)

    assert_equal(1, doc.xpath("//xmlns:hyperlink[@ref='#{@a.ref}']").size)
    assert_equal(1, doc.xpath("//xmlns:hyperlink[@tooltip='#{@a.tooltip}']").size)
    assert_equal(1, doc.xpath("//xmlns:hyperlink[@display='#{@a.display}']").size)
    assert_equal(0, doc.xpath("//xmlns:hyperlink[@location='#{@a.location}']").size)
    assert_equal(1, doc.xpath("//xmlns:hyperlink[@r:id='#{@a.relationship.Id}']").size)
  end
end