File: parser_spec.rb

package info (click to toggle)
ruby-prawn-icon 3.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 3,228 kB
  • sloc: ruby: 1,370; makefile: 5
file content (223 lines) | stat: -rw-r--r-- 6,702 bytes parent folder | download | duplicates (2)
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# encoding: utf-8
#
# Copyright October 2014, Jesse Doyle. All rights reserved.
#
# This is free software. Please see the LICENSE and COPYING files for details.

require 'spec_helper'

describe Prawn::Icon::Parser do
  let(:pdf) { create_pdf }

  describe '::format' do
    it 'should return a raw prawn-formatted string on valid input' do
      string = '<icon>far-address-book</icon>'
      formatted = Prawn::Icon::Parser.format(pdf, string)
      match = '<font name="far"></font>'

      expect(formatted).to eq(match)
    end

    it 'should return unchanged if no icon tags are found' do
      string = <<-EOS
      <link href='#'>test</link>
      Here's some sample text.
      <i>more text</i>
      EOS
      formatted = Prawn::Icon::Parser.format(pdf, string)

      expect(formatted).to eq(string)
    end

    it 'should return the empty string if given the empty string' do
      string = ''
      formatted = Prawn::Icon::Parser.format(pdf, string)

      expect(formatted).to be_empty
    end

    it 'should raise an error when an icon key is invalid' do
      string = '<icon>an invalid key</icon>'

      expect { Prawn::Icon::Parser.format(pdf, string) }.to raise_error(
        Prawn::Errors::UnknownFont
      )
    end

    it 'should raise an error when an icon is not found for valid set' do
      string = '<icon>far-__INVALID__</icon>'

      expect { Prawn::Icon::Parser.format(pdf, string) }.to raise_error(
        Prawn::Icon::Errors::IconNotFound
      )
    end
  end

  describe '::config_from_tokens' do
    it 'should handle attrs with double quotes' do
      string = '<icon size="20">far-address-book</icon>'
      tokens = tokenize_string(string)
      config = Prawn::Icon::Parser.config_from_tokens(tokens)
      inner = config.first

      expect(inner[:size]).to eq(20.0)
    end

    it 'should handle attrs with single quotes' do
      string = '<icon size="20">far-address-book</icon>'
      tokens = tokenize_string(string)
      config = Prawn::Icon::Parser.config_from_tokens(tokens)
      inner = config.first

      expect(inner[:size]).to eq(20.0)
    end

    it 'should handle both single/double quotes in same string' do
      string = '<icon color="0099FF" size=\'20\'>far-address-book</icon>'
      tokens = tokenize_string(string)
      config = Prawn::Icon::Parser.config_from_tokens(tokens)
      inner = config.first

      expect(inner[:size]).to eq(20.0)
      expect(inner[:color]).to eq('0099FF')
    end

    it 'should return an array containing only an empty hash' do
      string = '<icon>far-address-book</icon>'
      tokens = tokenize_string(string)
      config = Prawn::Icon::Parser.config_from_tokens(tokens)
      inner = config.first

      expect(config.size).to eq(1)
      expect(inner).to be_empty
    end

    it 'should return an array containing a single hash of attrs' do
      string = '<icon size="12" color="CCCCCC">far-address-book</icon>'
      tokens = tokenize_string(string)
      config = Prawn::Icon::Parser.config_from_tokens(tokens)
      inner = config.first

      expect(config.size).to eq(1)
      expect(inner[:size]).to eq(12.0)
      expect(inner[:color]).to eq('CCCCCC')
    end

    it 'should return an array containing as many hashes as icons' do
      string = <<-EOS
      <icon>far-address-book</icon>
      <icon>far-address-book</icon>
      <icon>far-address-book</icon>
      EOS
      tokens = tokenize_string(string)
      config = Prawn::Icon::Parser.config_from_tokens(tokens)

      expect(config.size).to eq(3)
    end
  end

  describe '::keys_to_unicode' do
    it 'should return an empty array for empty input' do
      string = ''
      config = []
      content = contentize_string(string)
      icons = Prawn::Icon::Parser.keys_to_unicode(pdf, content, config)

      expect(icons).to be_empty
    end

    it 'should return an array with unicode content' do
      string = '<icon>far-address-book</icon>'
      tokens = tokenize_string(string)
      content = contentize_string(string)
      config = Prawn::Icon::Parser.config_from_tokens(tokens)
      icons = Prawn::Icon::Parser.keys_to_unicode(pdf, content, config)
      icon = icons.first[:content]

      expect(valid_unicode?(icon)).to be true
    end

    it 'should return a single array containing attr hash of defaults' do
      # Hash must contain :set and :content by default
      string = '<icon>far-address-book</icon>'
      tokens = tokenize_string(string)
      content = contentize_string(string)
      config = Prawn::Icon::Parser.config_from_tokens(tokens)
      icons = Prawn::Icon::Parser.keys_to_unicode(pdf, content, config)
      icon = icons.first

      expect(icon[:set]).to eq(:far)
      expect(icon[:content]).not_to be_empty
    end

    it 'should handle strings with multiple icons/attrs combinations' do
      string = <<-EOS
      <icon size='20'>far-address-book</icon>
      some filler text
      <icon>far-address-book</icon>
      <icon size='8' color='0099FF'>far-address-book</icon>
      EOS
      tokens = tokenize_string(string)
      content = contentize_string(string)
      config = Prawn::Icon::Parser.config_from_tokens(tokens)
      icons = Prawn::Icon::Parser.keys_to_unicode(pdf, content, config)
      first = icons.first
      second = icons[1]
      third = icons[2]

      expect(icons.size).to eq(3)
      expect(first[:size]).to eq(20.0)
      expect(second[:size]).to be_nil
      expect(third[:size]).to eq(8.0)
      expect(third[:color]).to eq('0099FF')
    end
  end

  describe '::icon_tags' do
    let(:tags) { Prawn::Icon::Parser.icon_tags(icons) }

    context 'with color attribute' do
      let(:icons) do
        [
          { set: :far, color: 'CCCCCC', content: '' }
        ]
      end

      it 'should return valid input as prawn formatted text tags wrapping color tags' do
        match = '<font name="far"><color rgb="CCCCCC"></color></font>'

        expect(tags).to eq([match])
      end
    end

    context 'without the color attribute' do
      let(:icons) do
        [
          { set: :far, content: '' }
        ]
      end

      it 'should return valid input as prawn formatted text tags without color' do
        match = '<font name="far"></font>'

        expect(tags).to eq([match])
      end
    end

    context 'with multiple icon fonts' do
      let(:icons) do
        [
          { set: :far, content: '' },
          { set: :fi, content: '\uf001' }
        ]
      end

      it 'should be capable of handling multiple icon fonts' do
        match1 = '<font name="far"></font>'
        match2 = '<font name="fi">\uf001</font>'

        expect(tags).to eq([match1, match2])
      end
    end
  end
end