summaryrefslogtreecommitdiffhomepage
path: root/test/workbook/worksheet/tc_comment.rb
blob: cbd93f01570e0190a6d681ab3ae89a4c56e40098 (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
require 'tc_helper.rb'

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(@c1.author == 'author with special char <')
    assert(@c2.author == 'PO')
  end

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

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

  def test_visible
    assert_equal(false, @c1.visible)
    assert_equal(true, @c2.visible)
  end

  def test_ref
    assert(@c1.ref == 'A1')
    assert(@c2.ref == 'C3')
  end

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

  def test_to_xml_string
    doc = Nokogiri::XML(@c1.to_xml_string)
    assert_equal(doc.xpath("//comment[@ref='#{@c1.ref}']").size, 1)
    assert_equal(doc.xpath("//comment[@authorId='#{@c1.author_index.to_s}']").size, 1)
    assert_equal(doc.xpath("//t[text()='#{@c1.author}:\n']").size, 1)
    assert_equal(doc.xpath("//t[text()='#{@c1.text}']").size, 1)
  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