spec/unit/parser_spec.rb in prawn-icon-1.1.1 vs spec/unit/parser_spec.rb in prawn-icon-1.2.0
- old
+ new
@@ -5,14 +5,15 @@
# 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>fa-arrows</icon>'
- pdf = create_pdf
formatted = Prawn::Icon::Parser.format(pdf, string)
match = "<font name=\"fa\"></font>"
expect(formatted).to eq(match)
end
@@ -21,38 +22,34 @@
string = <<-EOS
<link href="#">test</link>
Here's some sample text.
<i>more text</i>
EOS
- pdf = create_pdf
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 = ''
- pdf = create_pdf
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>'
- pdf = create_pdf
proc = Proc.new { Prawn::Icon::Parser.format(pdf, string) }
expect(proc).to raise_error(Prawn::Errors::UnknownFont)
end
it 'should raise an error when an icon is not found for valid set' do
string = '<icon>fa-__INVALID__</icon>'
- pdf = create_pdf
proc = Proc.new { Prawn::Icon::Parser.format(pdf, string) }
- expect(proc).to raise_error(Prawn::Errors::IconNotFound)
+ expect(proc).to raise_error(Prawn::Icon::Errors::IconNotFound)
end
end
describe '::config_from_tokens' do
it 'should handle attrs with double quotes' do
@@ -120,22 +117,20 @@
describe '::keys_to_unicode' do
it 'should return an empty array for empty input' do
string = ''
config = []
content = contentize_string(string)
- pdf = create_pdf
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>fa-arrows</icon>'
tokens = tokenize_string(string)
content = contentize_string(string)
config = Prawn::Icon::Parser.config_from_tokens(tokens)
- pdf = create_pdf
icons = Prawn::Icon::Parser.keys_to_unicode(pdf, content, config)
icon = icons.first[:content]
expect(valid_unicode?(icon)).to be true
end
@@ -144,11 +139,10 @@
# Hash must contain :set and :content by default
string = '<icon>fa-arrows</icon>'
tokens = tokenize_string(string)
content = contentize_string(string)
config = Prawn::Icon::Parser.config_from_tokens(tokens)
- pdf = create_pdf
icons = Prawn::Icon::Parser.keys_to_unicode(pdf, content, config)
icon = icons.first
expect(icon[:set]).to eq(:fa)
expect(icon[:content]).not_to be_empty
@@ -162,11 +156,10 @@
<icon size="8" color="0099FF">fa-arrows</icon>
EOS
tokens = tokenize_string(string)
content = contentize_string(string)
config = Prawn::Icon::Parser.config_from_tokens(tokens)
- pdf = create_pdf
icons = Prawn::Icon::Parser.keys_to_unicode(pdf, content, config)
first = icons.first
second = icons[1]
third = icons[2]
@@ -177,42 +170,52 @@
expect(third[:color]).to eq('0099FF')
end
end
describe '::icon_tags' do
- it 'should return valid input as prawn formatted text tags wrapping color tags' do
- icons = [
- { set: :fa, color: 'CCCCCC', content: "\uf001" }
- ]
- tags = Prawn::Icon::Parser.icon_tags(icons)
- match = "<font name=\"fa\"><color rgb=\"CCCCCC\">\uf001</color></font>"
+ let(:tags) { Prawn::Icon::Parser.icon_tags(icons) }
- expect(tags.size).to eq(1)
- expect(tags.first).to eq(match)
+ context 'with color attribute' do
+ let(:icons) do
+ [
+ { set: :fa, color: 'CCCCCC', content: "\uf001" }
+ ]
+ end
+
+ it 'should return valid input as prawn formatted text tags wrapping color tags' do
+ match = "<font name=\"fa\"><color rgb=\"CCCCCC\">\uf001</color></font>"
+
+ expect(tags).to eq([match])
+ end
end
- it 'should return valid input as prawn formatted text tags without color' do
- icons = [
- { set: :fa, content: "\uf001" }
- ]
- tags = Prawn::Icon::Parser.icon_tags(icons)
- match = "<font name=\"fa\">\uf001</font>"
+ context 'without the color attribute' do
+ let(:icons) do
+ [
+ { set: :fa, content: "\uf001" }
+ ]
+ end
- expect(tags.size).to eq(1)
- expect(tags.first).to eq(match)
+ it 'should return valid input as prawn formatted text tags without color' do
+ match = "<font name=\"fa\">\uf001</font>"
+
+ expect(tags).to eq([match])
+ end
end
- it 'should be capable of handling multiple icon fonts' do
- icons = [
- { set: :fa, content: "\uf001" },
- { set: :fi, content: "\uf001" }
- ]
- tags = Prawn::Icon::Parser.icon_tags(icons)
- match1 = "<font name=\"fa\">\uf001</font>"
- match2 = "<font name=\"fi\">\uf001</font>"
+ context 'with multiple icon fonts' do
+ let(:icons) do
+ [
+ { set: :fa, content: "\uf001" },
+ { set: :fi, content: "\uf001" }
+ ]
+ end
- expect(tags.size).to eq(2)
- expect(tags[0]).to eq(match1)
- expect(tags[1]).to eq(match2)
+ it 'should be capable of handling multiple icon fonts' do
+ match1 = "<font name=\"fa\">\uf001</font>"
+ match2 = "<font name=\"fi\">\uf001</font>"
+
+ expect(tags).to eq([match1, match2])
+ end
end
end
end