summaryrefslogtreecommitdiffhomepage
path: root/test/workbook/worksheet/tc_rich_text_run.rb
blob: be05d818d4f850973a77e6529561d6bfeadf12c9 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# frozen_string_literal: true

require 'tc_helper'

class RichTextRun < Test::Unit::TestCase
  def setup
    @p = Axlsx::Package.new
    @ws = @p.workbook.add_worksheet name: "hmmmz"
    @p.workbook.styles.add_style sz: 20
    @rtr = Axlsx::RichTextRun.new('hihihi', b: true, i: false)
    @rtr2 = Axlsx::RichTextRun.new('hihi2hi2', b: false, i: true)
    @rt = Axlsx::RichText.new
    @rt.runs << @rtr
    @rt.runs << @rtr2
    @row = @ws.add_row [@rt]
    @c = @row.first
  end

  def test_initialize
    assert_equal('hihihi', @rtr.value)
    assert(@rtr.b)
    refute(@rtr.i)
  end

  def test_font_size_with_custom_style_and_no_sz
    @c.style = @c.row.worksheet.workbook.styles.add_style bg_color: 'FF00FF'
    sz = @rtr.send(:font_size)

    assert_equal(sz, @c.row.worksheet.workbook.styles.fonts.first.sz * 1.5)
    sz = @rtr2.send(:font_size)

    assert_equal(sz, @c.row.worksheet.workbook.styles.fonts.first.sz)
  end

  def test_font_size_with_bolding
    @c.style = @c.row.worksheet.workbook.styles.add_style b: true

    assert_equal(@c.row.worksheet.workbook.styles.fonts.first.sz * 1.5, @rtr.send(:font_size))
    assert_equal(@c.row.worksheet.workbook.styles.fonts.first.sz * 1.5, @rtr2.send(:font_size)) # is this the correct behaviour?
  end

  def test_font_size_with_custom_sz
    @c.style = @c.row.worksheet.workbook.styles.add_style sz: 52
    sz = @rtr.send(:font_size)

    assert_equal(52 * 1.5, sz)
    sz2 = @rtr2.send(:font_size)

    assert_equal(52, sz2)
  end

  def test_rtr_with_sz
    @rtr.sz = 25

    assert_equal(25, @rtr.send(:font_size))
  end

  def test_color
    assert_raise(ArgumentError) { @rtr.color = -1.1 }
    assert_nothing_raised { @rtr.color = "FF00FF00" }
    assert_equal("FF00FF00", @rtr.color.rgb)
  end

  def test_scheme
    assert_raise(ArgumentError) { @rtr.scheme = -1.1 }
    assert_nothing_raised { @rtr.scheme = :major }
    assert_equal(:major, @rtr.scheme)
  end

  def test_vertAlign
    assert_raise(ArgumentError) { @rtr.vertAlign = -1.1 }
    assert_nothing_raised { @rtr.vertAlign = :baseline }
    assert_equal(:baseline, @rtr.vertAlign)
  end

  def test_sz
    assert_raise(ArgumentError) { @rtr.sz = -1.1 }
    assert_nothing_raised { @rtr.sz = 12 }
    assert_equal(12, @rtr.sz)
  end

  def test_extend
    assert_raise(ArgumentError) { @rtr.extend = -1.1 }
    assert_nothing_raised { @rtr.extend = false }
    refute(@rtr.extend)
  end

  def test_condense
    assert_raise(ArgumentError) { @rtr.condense = -1.1 }
    assert_nothing_raised { @rtr.condense = false }
    refute(@rtr.condense)
  end

  def test_shadow
    assert_raise(ArgumentError) { @rtr.shadow = -1.1 }
    assert_nothing_raised { @rtr.shadow = false }
    refute(@rtr.shadow)
  end

  def test_outline
    assert_raise(ArgumentError) { @rtr.outline = -1.1 }
    assert_nothing_raised { @rtr.outline = false }
    refute(@rtr.outline)
  end

  def test_strike
    assert_raise(ArgumentError) { @rtr.strike = -1.1 }
    assert_nothing_raised { @rtr.strike = false }
    refute(@rtr.strike)
  end

  def test_u
    @c.type = :string
    assert_raise(ArgumentError) { @c.u = -1.1 }
    assert_nothing_raised { @c.u = :single }
    assert_equal(:single, @c.u)
    doc = Nokogiri::XML(@c.to_xml_string(1, 1))

    assert(doc.xpath('//u[@val="single"]'))
  end

  def test_i
    assert_raise(ArgumentError) { @c.i = -1.1 }
    assert_nothing_raised { @c.i = false }
    refute(@c.i)
  end

  def test_rFont
    assert_raise(ArgumentError) { @c.font_name = -1.1 }
    assert_nothing_raised { @c.font_name = "Arial" }
    assert_equal("Arial", @c.font_name)
  end

  def test_charset
    assert_raise(ArgumentError) { @c.charset = -1.1 }
    assert_nothing_raised { @c.charset = 1 }
    assert_equal(1, @c.charset)
  end

  def test_family
    assert_raise(ArgumentError) { @rtr.family = 0 }
    assert_nothing_raised { @rtr.family = 1 }
    assert_equal(1, @rtr.family)
  end

  def test_b
    assert_raise(ArgumentError) { @c.b = -1.1 }
    assert_nothing_raised { @c.b = false }
    refute(@c.b)
  end

  def test_multiline_autowidth
    wrap = @p.workbook.styles.add_style({ alignment: { wrap_text: true } })
    awtr = Axlsx::RichTextRun.new("I'm bold\n", b: true)
    rt = Axlsx::RichText.new
    rt.runs << awtr
    @ws.add_row [rt], style: wrap
    ar = [0]
    awtr.autowidth(ar)

    assert_equal(2, ar.length)
    assert_in_delta(13.2, ar[0])
    assert_equal(0, ar[1])
  end

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

    assert_empty(errors, "error free validation")

    assert(doc.xpath('//rPr/b[@val=1]'))
    assert(doc.xpath('//rPr/i[@val=0]'))
    assert(doc.xpath('//rPr/b[@val=0]'))
    assert(doc.xpath('//rPr/i[@val=1]'))
    assert(doc.xpath('//is//t[contains(text(), "hihihi")]'))
    assert(doc.xpath('//is//t[contains(text(), "hihi2hi2")]'))
  end
end