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 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
|
# typed: false
# coding: utf-8
describe PDF::Reader::ObjectHash do
describe "mixins" do
it "has enumerable mixed in" do
filename = pdf_spec_file("cairo-unicode")
h = PDF::Reader::ObjectHash.new(filename)
expect(h.map { |ref, obj| obj.class }.size).to eql(57)
end
end
describe "initialisation" do
it "correctly loads a PDF from a StringIO object" do
filename = pdf_spec_file("cairo-unicode")
io = StringIO.new(binread(filename))
h = PDF::Reader::ObjectHash.new(io)
expect(h.map { |ref, obj| obj.class }.size).to eql(57)
end
it "correctly loads a PDF from a string containing a file path" do
filename = pdf_spec_file("cairo-unicode")
h = PDF::Reader::ObjectHash.new(filename)
expect(h.map { |ref, obj| obj.class }.size).to eql(57)
end
# This means PDF::Reader.open(URI.open("https://example.com/foo.pdf")) will work
it "correctly loads a PDF via a Tempfile object" do
filename = pdf_spec_file("cairo-unicode")
Tempfile.open('foo.pdf') do |tempfile|
tempfile.write binread(filename)
tempfile.rewind
h = PDF::Reader::ObjectHash.new(tempfile)
expect(h.map { |ref, obj| obj.class }.size).to eql(57)
end
end
it "raises an ArgumentError if passed a non filename and non IO" do
expect {PDF::Reader::ObjectHash.new(10)}.to raise_error(ArgumentError)
end
end
describe "#[]" do
it "returns nil for any invalid hash key" do
filename = pdf_spec_file("cairo-unicode")
h = PDF::Reader::ObjectHash.new(filename)
expect(h[-1]).to be_nil
expect(h[nil]).to be_nil
expect(h["James"]).to be_nil
end
it "returns nil for any hash key that doesn't exist" do
filename = pdf_spec_file("cairo-unicode")
h = PDF::Reader::ObjectHash.new(filename)
expect(h[10000]).to be_nil
end
it "correctly extracts an int object using int or string keys" do
filename = pdf_spec_file("cairo-unicode")
h = PDF::Reader::ObjectHash.new(filename)
expect(h[3]).to eql(3649)
expect(h["3"]).to eql(3649)
expect(h["3james"]).to eql(3649)
end
it "correctly extracts an int object using PDF::Reference as a key" do
filename = pdf_spec_file("cairo-unicode")
h = PDF::Reader::ObjectHash.new(filename)
ref = PDF::Reader::Reference.new(3,0)
expect(h[ref]).to eql(3649)
end
end
describe "#object" do
it "returns regular objects unchanged" do
filename = pdf_spec_file("cairo-unicode")
h = PDF::Reader::ObjectHash.new(filename)
expect(h.object(-1)).to eql(-1)
expect(h.object(nil)).to be_nil
expect(h.object("James")).to eql("James")
end
it "translates reference objects into an extracted PDF object" do
filename = pdf_spec_file("cairo-unicode")
h = PDF::Reader::ObjectHash.new(filename)
expect(h.object(PDF::Reader::Reference.new(3,0))).to eql(3649)
end
end
describe "#deref!" do
let(:hash) do
PDF::Reader::ObjectHash.new pdf_spec_file("cairo-unicode")
end
it "returns regular objects unchanged" do
expect(hash.deref!(-1)).to eql(-1)
expect(hash.deref!(nil)).to be_nil
expect(hash.deref!("James")).to eql("James")
end
it "translates reference objects into an extracted PDF object" do
expect(hash.deref!(PDF::Reader::Reference.new(3,0))).to eq 3649
end
it "recursively dereferences references within hashes" do
font_descriptor = hash.deref! PDF::Reader::Reference.new(17, 0)
expect(font_descriptor[:FontFile3]).to be_an_instance_of \
PDF::Reader::Stream
end
it "recursively dereferences references within stream hashes" do
font_file = hash.deref! PDF::Reader::Reference.new(15, 0)
expect(font_file.hash[:Length]).to eq 2103
end
it "recursively dereferences references within arrays" do
font = hash.deref! PDF::Reader::Reference.new(19, 0)
expect(font[:DescendantFonts][0][:Subtype]).to eq :CIDFontType0
end
it "returns a new Hash, not mutate the provided Hash" do
orig_collection = {}
new_collection = hash.deref!(orig_collection)
expect(orig_collection.object_id).not_to eq(new_collection.object_id)
end
it "returns a new Array, not mutate the provided Array" do
orig_collection = []
new_collection = hash.deref!(orig_collection)
expect(orig_collection.object_id).not_to eq(new_collection.object_id)
end
# a -> b -> c -> a
context "with nested Array's that reference themselves" do
let(:a) { [1, b] }
let(:b) { [2, c] }
let(:c) { [3] }
before do
c << a
end
it "returns a new Array, not mutate the provided Array" do
result = hash.deref!(c)
expect(result).to be_a(Array)
end
end
end
describe "#fetch" do
it "raises IndexError for any invalid hash key when no default is provided" do
filename = pdf_spec_file("cairo-unicode")
h = PDF::Reader::ObjectHash.new(filename)
expect { h.fetch(-1) }.to raise_error(IndexError)
expect { h.fetch(nil) }.to raise_error(IndexError)
expect { h.fetch("James") }.to raise_error(IndexError)
end
it "returns default for any hash key that doesn't exist when a default is provided" do
filename = pdf_spec_file("cairo-unicode")
h = PDF::Reader::ObjectHash.new(filename)
expect(h.fetch(10000, "default")).to eql("default")
end
it "correctly extracts an int object using int or string keys" do
filename = pdf_spec_file("cairo-unicode")
h = PDF::Reader::ObjectHash.new(filename)
expect(h.fetch(3)).to eql(3649)
expect(h.fetch("3")).to eql(3649)
expect(h.fetch("3james")).to eql(3649)
end
it "correctly extracts an int object using PDF::Reader::Reference keys" do
filename = pdf_spec_file("cairo-unicode")
h = PDF::Reader::ObjectHash.new(filename)
ref = PDF::Reader::Reference.new(3,0)
expect(h.fetch(ref)).to eql(3649)
end
end
describe "#each" do
it "iterates 57 times when using cairo-unicode PDF" do
filename = pdf_spec_file("cairo-unicode")
h = PDF::Reader::ObjectHash.new(filename)
count = 0
h.each do
count += 1
end
expect(count).to eql(57)
end
it "provides a PDF::Reader::Reference to each iteration" do
filename = pdf_spec_file("cairo-unicode")
h = PDF::Reader::ObjectHash.new(filename)
h.each do |id, obj|
expect(id).to be_a_kind_of(PDF::Reader::Reference)
expect(obj).not_to be_nil
end
end
end
describe "#each_key" do
it "iterates 57 times when using cairo-unicode PDF" do
filename = pdf_spec_file("cairo-unicode")
h = PDF::Reader::ObjectHash.new(filename)
count = 0
h.each_key do
count += 1
end
expect(count).to eql(57)
end
it "provides a PDF::Reader::Reference to each iteration" do
filename = pdf_spec_file("cairo-unicode")
h = PDF::Reader::ObjectHash.new(filename)
h.each_key do |ref|
expect(ref).to be_a_kind_of(PDF::Reader::Reference)
end
end
end
describe "#each_value" do
it "iterates 57 times when using cairo-unicode PDF" do
filename = pdf_spec_file("cairo-unicode")
h = PDF::Reader::ObjectHash.new(filename)
count = 0
h.each_value do
count += 1
end
expect(count).to eql(57)
end
end
describe "#size" do
it "returns 57 when using cairo-unicode PDF" do
filename = pdf_spec_file("cairo-unicode")
h = PDF::Reader::ObjectHash.new(filename)
expect(h.size).to eql(57)
end
end
describe "#empty?" do
it "returns false when using cairo-unicode PDF" do
filename = pdf_spec_file("cairo-unicode")
h = PDF::Reader::ObjectHash.new(filename)
expect(h.empty?).to be_falsey
end
end
describe "#has_key?" do
it "returns true when called with a valid ID" do
filename = pdf_spec_file("cairo-unicode")
h = PDF::Reader::ObjectHash.new(filename)
expect(h.has_key?(1)).to be_truthy
expect(h.has_key?(PDF::Reader::Reference.new(1,0))).to be_truthy
end
it "returns false when called with an invalid ID" do
filename = pdf_spec_file("cairo-unicode")
h = PDF::Reader::ObjectHash.new(filename)
expect(h.has_key?(-1)).to be_falsey
expect(h.has_key?(nil)).to be_falsey
expect(h.has_key?("James")).to be_falsey
expect(h.has_key?(PDF::Reader::Reference.new(10000,0))).to be_falsey
end
end
describe "#has_value?" do
it "returns true when called with a valid object" do
filename = pdf_spec_file("cairo-unicode")
h = PDF::Reader::ObjectHash.new(filename)
expect(h.has_value?(3649)).to be_truthy
end
it "returns false when called with an invalid object" do
filename = pdf_spec_file("cairo-unicode")
h = PDF::Reader::ObjectHash.new(filename)
expect(h.has_value?(-1)).to be_falsey
expect(h.has_value?(nil)).to be_falsey
expect(h.has_value?("James")).to be_falsey
end
end
describe "#keys" do
it "returns an array of keys" do
filename = pdf_spec_file("cairo-unicode")
h = PDF::Reader::ObjectHash.new(filename)
keys = h.keys
expect(keys.size).to eql(57)
keys.each { |k| expect(k).to be_a_kind_of(PDF::Reader::Reference) }
end
end
describe "#values" do
it "returns an array of object" do
filename = pdf_spec_file("cairo-unicode")
h = PDF::Reader::ObjectHash.new(filename)
values = h.values
expect(values.size).to eql(57)
values.each { |v| expect(v).not_to be_nil }
end
end
describe "#values_at" do
it "returns an array of object" do
filename = pdf_spec_file("cairo-unicode")
h = PDF::Reader::ObjectHash.new(filename)
ref3 = PDF::Reader::Reference.new(3,0)
ref6 = PDF::Reader::Reference.new(6,0)
expect(h.values_at(3,6)).to eql([3649,3287])
expect(h.values_at(ref3,ref6)).to eql([3649,3287])
end
end
describe "#to_a" do
it "returns an array of 57 arrays" do
filename = pdf_spec_file("cairo-unicode")
h = PDF::Reader::ObjectHash.new(filename)
arr = h.to_a
expect(arr.size).to eql(57)
arr.each { |a| expect(a).to be_a_kind_of(Array) }
end
end
describe "#trailer" do
it "returns the document trailer dictionary" do
filename = pdf_spec_file("cairo-unicode")
h = PDF::Reader::ObjectHash.new(filename)
expect(h.trailer[:Size]).to eql(58)
expect(h.trailer[:Root]).to eql(PDF::Reader::Reference.new(57,0))
expect(h.trailer[:Info]).to eql(PDF::Reader::Reference.new(56,0))
end
end
describe "#pdf_version" do
it "returns the document PDF version dictionary" do
filename = pdf_spec_file("cairo-unicode")
h = PDF::Reader::ObjectHash.new(filename)
expect(h.pdf_version).to eql(1.4)
end
end
describe "#page_references" do
it "returns an Array of PDF::Reader::Reference objs" do
filename = pdf_spec_file("cairo-unicode")
h = PDF::Reader::ObjectHash.new(filename)
expect(h.page_references).to be_a(Array)
h.page_references.each do |ref|
expect(ref).to be_a(PDF::Reader::Reference)
end
end
it "raises a MalformedPDFError if dereferenced value is not a dict" do
filename = pdf_spec_file("page_reference_is_not_a_dict")
h = PDF::Reader::ObjectHash.new(filename)
expect { h.page_references }.to raise_error(PDF::Reader::MalformedPDFError)
end
end
end
|