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

require 'tc_helper'

class TestComment < Test::Unit::TestCase
  def setup
    p = Axlsx::Package.new
    wb = p.workbook
    @ws = wb.add_worksheet
    @c1 = @ws.add_comment ref: 'A1', text: 'text with special char <', author: 'author with special char <', visible: false
    @c2 = @ws.add_comment ref: 'C3', text: 'rust bucket', author: 'PO'
  end

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

  def test_author
    assert_equal('author with special char <', @c1.author)
    assert_equal('PO', @c2.author)
  end

  def test_text
    assert_equal('text with special char <', @c1.text)
    assert_equal('rust bucket', @c2.text)
  end

  def test_author_index
    assert_equal(1, @c1.author_index)
    assert_equal(0, @c2.author_index)
  end

  def test_visible
    refute(@c1.visible)
    assert(@c2.visible)
  end

  def test_ref
    assert_equal('A1', @c1.ref)
    assert_equal('C3', @c2.ref)
  end

  def test_vml_shape
    pos = Axlsx.name_to_indices(@c1.ref)

    assert(@c1.vml_shape.is_a?(Axlsx::VmlShape))
    assert_equal(@c1.vml_shape.column, pos[0])
    assert_equal(@c1.vml_shape.row, pos[1])
    assert_equal(@c1.vml_shape.row, pos[1])
    assert_equal(pos[0], @c1.vml_shape.left_column)
    assert_equal(@c1.vml_shape.top_row, pos[1])
    assert_equal(pos[0] + 2, @c1.vml_shape.right_column)
    assert_equal(@c1.vml_shape.bottom_row, pos[1] + 4)
  end

  def test_to_xml_string
    doc = Nokogiri::XML(@c1.to_xml_string)

    assert_equal(1, doc.xpath("//comment[@ref='#{@c1.ref}']").size)
    assert_equal(1, doc.xpath("//comment[@authorId='#{@c1.author_index}']").size)
    assert_equal(1, doc.xpath("//t[text()='#{@c1.author}:\n']").size)
    assert_equal(1, doc.xpath("//t[text()='#{@c1.text}']").size)
  end

  def test_comment_text_contain_author_and_text
    comment = @ws.add_comment ref: 'C4', text: 'some text', author: 'Bob'
    doc = Nokogiri::XML(comment.to_xml_string)

    assert_equal("Bob:\nsome text", doc.xpath("//comment/text").text)
  end

  def test_comment_text_does_not_contain_stray_colon_if_author_blank
    comment = @ws.add_comment ref: 'C5', text: 'some text', author: ''
    doc = Nokogiri::XML(comment.to_xml_string)

    assert_equal("some text", doc.xpath("//comment/text").text)
  end
end