# frozen_string_literal: true
require 'spec_helper'
describe Prawn::Text::Formatted::Parser do
describe '#format' do
it 'handles sup' do
string = 'superscript'
array = described_class.format(string)
expect(array[0]).to eq(
text: 'superscript',
styles: [:superscript],
color: nil,
link: nil,
anchor: nil,
local: nil,
font: nil,
size: nil,
character_spacing: nil
)
end
it 'handles sub' do
string = 'subscript'
array = described_class.format(string)
expect(array[0]).to eq(
text: 'subscript',
styles: [:subscript],
color: nil,
link: nil,
anchor: nil,
local: nil,
font: nil,
size: nil,
character_spacing: nil
)
end
it 'handles rgb' do
string = "red text"
array = described_class.format(string)
expect(array[0]).to eq(
text: 'red text',
styles: [],
color: 'ff0000',
link: nil,
anchor: nil,
local: nil,
font: nil,
size: nil,
character_spacing: nil
)
end
it '# should be optional in rgb' do
string = "red text"
array = described_class.format(string)
expect(array[0]).to eq(
text: 'red text',
styles: [],
color: 'ff0000',
link: nil,
anchor: nil,
local: nil,
font: nil,
size: nil,
character_spacing: nil
)
end
it 'handles cmyk' do
string = "magenta text"
array = described_class.format(string)
expect(array[0]).to eq(
text: 'magenta text',
styles: [],
color: [0, 100, 0, 0],
link: nil,
anchor: nil,
local: nil,
font: nil,
size: nil,
character_spacing: nil
)
end
it 'handles fonts' do
string = "Courier text"
array = described_class.format(string)
expect(array[0]).to eq(
text: 'Courier text',
styles: [],
color: nil,
link: nil,
anchor: nil,
local: nil,
font: 'Courier',
size: nil,
character_spacing: nil
)
end
it 'handles size' do
string = "14 point text"
array = described_class.format(string)
expect(array[0]).to eq(
text: '14 point text',
styles: [],
color: nil,
link: nil,
anchor: nil,
local: nil,
font: nil,
size: 14,
character_spacing: nil
)
end
it 'handles character_spacing' do
string = "extra character spacing"
array = described_class.format(string)
expect(array[0]).to eq(
text: 'extra character spacing',
styles: [],
color: nil,
link: nil,
anchor: nil,
local: nil,
font: nil,
size: nil,
character_spacing: 2.5
)
end
it 'handles links' do
string = "external link"
array = described_class.format(string)
expect(array[0]).to eq(
text: 'external link',
styles: [],
color: nil,
link: 'http://example.com',
anchor: nil,
local: nil,
font: nil,
size: nil,
character_spacing: nil
)
end
it 'handles local links' do
string = "local link"
array = described_class.format(string)
expect(array[0]).to eq(
text: 'local link',
styles: [],
color: nil,
link: nil,
anchor: nil,
local: '/home/example/foo.bar',
font: nil,
size: nil,
character_spacing: nil
)
end
it 'handles anchors' do
string = "internal link"
array = described_class.format(string)
expect(array[0]).to eq(
text: 'internal link',
styles: [],
color: nil,
link: nil,
anchor: 'ToC',
local: nil,
font: nil,
size: nil,
character_spacing: nil
)
end
it 'handles higher order characters properly' do
string = "©\n©"
array = described_class.format(string)
expect(array[0]).to eq(
text: '©',
styles: [:bold],
color: nil,
link: nil,
anchor: nil,
local: nil,
font: nil,
size: nil,
character_spacing: nil
)
expect(array[1]).to eq(
text: "\n",
styles: [:bold],
color: nil,
link: nil,
anchor: nil,
local: nil,
font: nil,
size: nil,
character_spacing: nil
)
expect(array[2]).to eq(
text: '©',
styles: [:bold],
color: nil,
link: nil,
anchor: nil,
local: nil,
font: nil,
size: nil,
character_spacing: nil
)
end
it 'converts < >, and & to <, >, and &, respectively' do
string = 'hello <, >, and &'
array = described_class.format(string)
expect(array[1]).to eq(
text: '<, >, and &',
styles: [:bold],
color: nil,
link: nil,
anchor: nil,
local: nil,
font: nil,
size: nil,
character_spacing: nil
)
end
it 'handles double qoutes around tag attributes' do
string = 'some sized text'
array = described_class.format(string)
expect(array[1]).to eq(
text: 'sized',
styles: [],
color: nil,
link: nil,
anchor: nil,
local: nil,
font: nil,
size: 14,
character_spacing: nil
)
end
it 'handles single qoutes around tag attributes' do
string = "some sized text"
array = described_class.format(string)
expect(array[1]).to eq(
text: 'sized',
styles: [],
color: nil,
link: nil,
anchor: nil,
local: nil,
font: nil,
size: 14,
character_spacing: nil
)
end
it 'constructs a formatted text array from a string' do
string = "hello world\nhow are you?"
array = described_class.format(string)
expect(array[0]).to eq(
text: 'hello ',
styles: [],
color: nil,
link: nil,
anchor: nil,
local: nil,
font: nil,
size: nil,
character_spacing: nil
)
expect(array[1]).to eq(
text: 'world',
styles: [:bold],
color: nil,
link: nil,
anchor: nil,
local: nil,
font: nil,
size: nil,
character_spacing: nil
)
expect(array[2]).to eq(
text: "\n",
styles: [:bold],
color: nil,
link: nil,
anchor: nil,
local: nil,
font: nil,
size: nil,
character_spacing: nil
)
expect(array[3]).to eq(
text: 'how ',
styles: [:bold],
color: nil,
link: nil,
anchor: nil,
local: nil,
font: nil,
size: nil,
character_spacing: nil
)
expect(array[4]).to eq(
text: 'are',
styles: %i[bold italic],
color: nil,
link: nil,
anchor: nil,
local: nil,
font: nil,
size: nil,
character_spacing: nil
)
expect(array[5]).to eq(
text: ' you?',
styles: [],
color: nil,
link: nil,
anchor: nil,
local: nil,
font: nil,
size: nil,
character_spacing: nil
)
end
it 'accepts as an alternative to ' do
string = 'bold not bold'
array = described_class.format(string)
expect(array[0]).to eq(
text: 'bold',
styles: [:bold],
color: nil,
link: nil,
anchor: nil,
local: nil,
font: nil,
size: nil,
character_spacing: nil
)
expect(array[1]).to eq(
text: ' not bold',
styles: [],
color: nil,
link: nil,
anchor: nil,
local: nil,
font: nil,
size: nil,
character_spacing: nil
)
end
it 'accepts as an alternative to ' do
string = 'italic not italic'
array = described_class.format(string)
expect(array[0]).to eq(
text: 'italic',
styles: [:italic],
color: nil,
link: nil,
anchor: nil,
local: nil,
font: nil,
size: nil,
character_spacing: nil
)
expect(array[1]).to eq(
text: ' not italic',
styles: [],
color: nil,
link: nil,
anchor: nil,
local: nil,
font: nil,
size: nil,
character_spacing: nil
)
end
it 'accepts as an alternative to ' do
string = "link not a link"
array = described_class.format(string)
expect(array[0]).to eq(
text: 'link',
styles: [],
color: nil,
link: 'http://example.com',
anchor: nil,
local: nil,
font: nil,
size: nil,
character_spacing: nil
)
expect(array[1]).to eq(
text: ' not a link',
styles: [],
color: nil,
link: nil,
anchor: nil,
local: nil,
font: nil,
size: nil,
character_spacing: nil
)
end
it 'turns
,
into newline' do
array = described_class.format('hello
big
world')
expect(array.map { |frag| frag[:text] }.join).to eq("hello\nbig\nworld")
end
end
describe '#to_string' do
it 'handles sup' do
string = 'superscript'
array = [{
text: 'superscript',
styles: [:superscript],
color: nil,
link: nil,
anchor: nil,
local: nil,
font: nil,
size: nil,
character_spacing: nil
}]
expect(described_class.to_string(array)).to eq(string)
end
it 'handles sub' do
string = 'subscript'
array = [{
text: 'subscript',
styles: [:subscript],
color: nil,
link: nil,
anchor: nil,
local: nil,
font: nil,
size: nil,
character_spacing: nil
}]
expect(described_class.to_string(array)).to eq(string)
end
it 'handles rgb' do
string = "red text"
array = [{
text: 'red text',
styles: [],
color: 'ff0000',
link: nil,
anchor: nil,
local: nil,
font: nil,
size: nil,
character_spacing: nil
}]
expect(described_class.to_string(array)).to eq(string)
end
it 'handles cmyk' do
string = "magenta text"
array = [{
text: 'magenta text',
styles: [],
color: [0, 100, 0, 0],
link: nil,
anchor: nil,
local: nil,
font: nil,
size: nil,
character_spacing: nil
}]
expect(described_class.to_string(array)).to eq(string)
end
it 'handles fonts' do
string = "Courier text"
array = [{
text: 'Courier text',
styles: [],
color: nil,
link: nil,
anchor: nil,
local: nil,
font: 'Courier',
size: nil,
character_spacing: nil
}]
expect(described_class.to_string(array)).to eq(string)
end
it 'handles size' do
string = "14 point text"
array = [{
text: '14 point text',
styles: [],
color: nil,
link: nil,
anchor: nil,
local: nil,
font: nil,
size: 14,
character_spacing: nil
}]
expect(described_class.to_string(array)).to eq(string)
end
it 'handles character spacing' do
string =
"2.5 extra character spacing"
array = [{
text: '2.5 extra character spacing',
styles: [],
color: nil,
link: nil,
anchor: nil,
local: nil,
font: nil,
size: nil,
character_spacing: 2.5
}]
expect(described_class.to_string(array)).to eq(string)
end
it 'handles links' do
array = [{
text: 'external link',
styles: [],
color: nil,
link: 'http://example.com',
anchor: nil,
local: nil,
font: nil,
size: nil,
character_spacing: nil
}]
string = "external link"
expect(described_class.to_string(array)).to eq(string)
end
it 'handles anchors' do
array = [{
text: 'internal link',
styles: [],
color: nil,
link: nil,
anchor: 'ToC',
font: nil,
size: nil,
character_spacing: nil
}]
string = "internal link"
expect(described_class.to_string(array)).to eq(string)
end
it 'converts <, >, and & to < >, and &, respectively' do
array = [
{
text: 'hello ',
styles: [],
color: nil,
link: nil,
font: nil,
size: nil,
character_spacing: nil
},
{
text: '<, >, and &',
styles: [:bold],
color: nil,
link: nil,
font: nil,
size: nil,
character_spacing: nil
}
]
string = 'hello <, >, and &'
expect(described_class.to_string(array)).to eq(string)
end
it 'constructs an HTML-esque string from a formatted text array' do
array = [
{
text: 'hello ',
styles: [],
color: nil,
link: nil,
font: nil,
size: 14,
character_spacing: nil
},
{
text: 'world',
styles: [:bold],
color: nil,
link: nil,
font: nil,
size: nil,
character_spacing: nil
},
{
text: "\n",
styles: [:bold],
color: nil,
link: nil,
font: nil,
size: nil,
character_spacing: nil
},
{
text: 'how ',
styles: [:bold],
color: nil,
link: nil,
font: nil,
size: nil,
character_spacing: nil
},
{
text: 'are',
styles: %i[bold italic],
color: nil,
link: nil,
font: nil,
size: nil,
character_spacing: nil
},
{
text: ' you?',
styles: [],
color: nil,
link: nil,
font: nil,
size: nil,
character_spacing: nil
}
]
string = "hello world\n"\
'how are you?'
expect(described_class.to_string(array)).to eq(string)
end
end
describe '#array_paragraphs' do
it 'groups fragments separated by newlines' do
array = [
{ text: "\nhello\nworld" },
{ text: "\n\n" },
{ text: 'how' },
{ text: 'are' },
{ text: 'you' }
]
target = [
[{ text: "\n" }],
[{ text: 'hello' }],
[{ text: 'world' }],
[{ text: "\n" }],
[
{ text: 'how' },
{ text: 'are' },
{ text: 'you' }
]
]
expect(described_class.array_paragraphs(array)).to eq(target)
end
it 'works properly if ending in an empty paragraph' do
array = [{ text: "\nhello\nworld\n" }]
target = [
[{ text: "\n" }],
[{ text: 'hello' }],
[{ text: 'world' }]
]
expect(described_class.array_paragraphs(array)).to eq(target)
end
end
end