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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe PDF::Core::Text do
let(:mock) do
text_mock_class =
Class.new do
include PDF::Core::Text
attr_reader :text
def add_content(str)
@text ||= +''
@text << str
end
def font
@font ||= Class.new do
def encode_text(text, _options)
[nil, text]
end
def add_to_current_page(_subset); end
def identifier_for(_subset)
:Font
end
end.new
end
def font_size
12
end
end
text_mock_class.new
end
describe '#text_rendering_mode' do
describe 'called without argument' do
let(:result) { mock.text_rendering_mode }
it 'functions as accessor' do
expect(result).to eq(:fill)
end
end
describe 'called with argument' do
context 'when the block does not raise an error' do
before do
mock.text_rendering_mode(:fill_stroke) do
mock.add_content('TEST')
end
end
it 'resets text_rendering_mode to original value' do
expect(mock.text_rendering_mode).to eq(:fill)
end
it 'outputs correct PDF content' do
expect(mock.text).to eq("\n2 TrTEST\n0 Tr")
end
end
context 'when the block raises an error' do
let(:error_message) { SecureRandom.hex(5) }
# rubocop:disable RSpec/ExpectInHook
before do
expect {
mock.text_rendering_mode(:fill_stroke) do
raise StandardError, error_message
end
}.to raise_error(StandardError, error_message)
end
# rubocop:enable RSpec/ExpectInHook
it 'resets text_rendering_mode to original value' do
expect(mock.text_rendering_mode).to eq(:fill)
end
it 'outputs correct PDF content' do
expect(mock.text).to eq("\n2 Tr\n0 Tr")
end
end
end
end
describe '#horizontal_text_scaling' do
describe 'called without argument' do
let(:result) { mock.horizontal_text_scaling }
it 'functions as accessor' do
expect(result).to eq(100)
end
end
describe 'called with argument' do
context 'when the block does not raise an error' do
before do
mock.horizontal_text_scaling(110) do
mock.add_content('TEST')
end
end
it 'resets horizontal_text_scaling to original value' do
expect(mock.horizontal_text_scaling).to eq(100)
end
it 'outputs correct PDF content' do
expect(mock.text).to eq("\n110.0 TzTEST\n100.0 Tz")
end
end
context 'when the block raises an error' do
let(:error_message) { SecureRandom.hex(5) }
# rubocop:disable RSpec/ExpectInHook
before do
expect {
mock.horizontal_text_scaling(110) do
raise StandardError, error_message
end
}.to raise_error(StandardError, error_message)
end
# rubocop:enable RSpec/ExpectInHook
it 'resets horizontal_text_scaling to original value' do
expect(mock.horizontal_text_scaling).to eq(100)
end
it 'outputs correct PDF content' do
expect(mock.text).to eq("\n110.0 Tz\n100.0 Tz")
end
end
end
end
describe '#character_spacing' do
describe 'called without argument' do
let(:result) { mock.character_spacing }
it 'functions as accessor' do
expect(result).to eq(0)
end
end
describe 'called with argument' do
context 'when the block does not raise an error' do
before do
mock.character_spacing(10) do
mock.add_content('TEST')
end
end
it 'resets character_spacing to original value' do
expect(mock.character_spacing).to eq(0)
end
it 'outputs correct PDF content' do
expect(mock.text).to eq("\n10.0 TcTEST\n0.0 Tc")
end
end
context 'when the block raises an error' do
let(:error_message) { SecureRandom.hex(5) }
# rubocop:disable RSpec/ExpectInHook
before do
expect {
mock.character_spacing(10) do
raise StandardError, error_message
end
}.to raise_error(StandardError, error_message)
end
# rubocop:enable RSpec/ExpectInHook
it 'resets character_spacing to original value' do
expect(mock.character_spacing).to eq(0)
end
it 'outputs correct PDF content' do
expect(mock.text).to eq("\n10.0 Tc\n0.0 Tc")
end
end
end
end
describe '#word_spacing' do
describe 'called without argument' do
let(:result) { mock.word_spacing }
it 'functions as accessor' do
expect(result).to eq(0)
end
end
describe 'called with argument' do
context 'when the block does not raise an error' do
before do
mock.word_spacing(10) do
mock.add_content('TEST')
end
end
it 'resets word_spacing to original value' do
expect(mock.word_spacing).to eq(0)
end
it 'outputs correct PDF content' do
expect(mock.text).to eq("\n10.0 TwTEST\n0.0 Tw")
end
end
context 'when the block raises an error' do
let(:error_message) { SecureRandom.hex(5) }
# rubocop:disable RSpec/ExpectInHook
before do
expect {
mock.word_spacing(10) do
raise StandardError, error_message
end
}.to raise_error(StandardError, error_message)
end
# rubocop:enable RSpec/ExpectInHook
it 'resets word_spacing to original value' do
expect(mock.word_spacing).to eq(0)
end
it 'outputs correct PDF content' do
expect(mock.text).to eq("\n10.0 Tw\n0.0 Tw")
end
end
end
end
describe '#rise' do
describe 'called without argument' do
let(:result) { mock.rise }
it 'functions as accessor' do
expect(result).to eq(0)
end
end
describe 'called with argument' do
context 'when the block does not raise an error' do
before do
mock.rise(2.3) do
mock.add_content('TEST')
end
end
it 'resets rise to original value' do
expect(mock.rise).to eq(0)
end
it 'outputs correct PDF content' do
expect(mock.text).to eq("\n2.3 TsTEST\n0.0 Ts")
end
end
context 'when the block raises an error' do
let(:error_message) { SecureRandom.hex(5) }
# rubocop:disable RSpec/ExpectInHook
before do
expect {
mock.rise(5) do
raise StandardError, error_message
end
}.to raise_error(StandardError, error_message)
end
# rubocop:enable RSpec/ExpectInHook
it 'resets rise to original value' do
expect(mock.rise).to eq(0)
end
it 'outputs correct PDF content' do
expect(mock.text).to eq("\n5.0 Ts\n0.0 Ts")
end
end
end
end
describe '#add_text_content' do
it 'handles frozen strings' do
expect { mock.add_text_content('text', 0, 0, {}) }
.to_not raise_error
end
end
end
|