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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
|
# frozen_string_literal: true
require_relative 'ignore-gem-warnings' if $VERBOSE
case ENV['COVERAGE']
when 'deep'
ENV['DEEP_COVER'] = 'true'
require 'deep_cover'
when 'true'
require 'deep_cover/builtin_takeover'
require 'simplecov'
end
require 'asciidoctor/pdf'
require 'base64'
require 'chunky_png'
require 'fileutils' unless defined? FileUtils
require 'open3' unless defined? Open3
require 'pathname' unless defined? Pathname
require 'pdf/inspector'
require 'socket'
require 'tmpdir'
# NOTE: fix warning in Prawn::Font:TTF
Prawn::Font::TTF.prepend (Module.new do
def initialize *args
@italic_angle = nil
super
end
end)
# NOTE: fix warning in TTFunk::Table
TTFunk::Table.prepend (Module.new do
def initialize *args
@offset = nil
super
end
end)
PDF::Reader.prepend (Module.new do
def source
objects.instance_variable_get :@io
end
def catalog
root
end
def outlines
objects[catalog[:Outlines]]
end
end)
PDF::Inspector::Text.prepend (Module.new do
def page= page
@page_number = page.number
super
end
def move_text_position tx, ty
@positions << [tx, ty, @page_number]
end
end)
class EnhancedPDFTextInspector < PDF::Inspector
include ::RSpec::Matchers
attr_accessor :text
attr_accessor :pages
def initialize
@color = nil
@cursor = nil
@fonts = {}
@text = []
@pages = []
end
def find_text string, filter = {}
if ::Hash === string
filter = string.merge filter
else
filter[:string] = string
end
if ::Regexp === filter[:string]
string_rx = filter.delete :string
@text.select {|candidate| filter <= candidate && (string_rx.match? candidate[:string]) }
else
@text.select {|candidate| filter <= candidate }
end
end
def find_unique_text string, filter = {}
result = find_text string, filter
(expect result).to have_size 1 unless result.empty?
result[0]
end
def strings text = @text
text.map {|it| it[:string] }
end
def lines text = @text
prev = nil
text.each_with_object [] do |it, accum|
#if prev && (prev[:y] == it[:y] || (prev[:y] - it[:y]).abs < [it[:font_size], prev[:font_size]].min * 0.5)
if prev && (prev[:y] == it[:y] || (prev[:y] - it[:y]).abs < 6)
if it[:x] - prev[:x] > prev[:width] + 0.5
accum << %(#{accum.pop.rstrip} #{it[:string].lstrip})
else
accum << %(#{accum.pop}#{it[:string]})
end
else
accum << it[:string]
end
prev = it
end
end
def page pagenum
@pages[pagenum - 1]
end
def page= page
@pages << { size: (page.attributes[:MediaBox].slice 2, 2), text: [], raw_content: page.raw_content }
@page_number = page.number
@state = ::PDF::Reader::PageState.new page
page.fonts.each do |label, stream|
base_font = stream[:BaseFont].to_s
base_font = (base_font.partition '+')[-1] if base_font.include? '+'
@fonts[label] = base_font
end
end
def extract_graphic_states content
content = (content.delete_prefix %(q\n)).delete_suffix %(\nQ)
(content.scan %r/^q\n(.*?)\nQ$/m).map {|it| it[0].split ?\n }
end
# Tf
def set_text_font_and_size *params
@state.set_text_font_and_size(*params)
@font_settings = { name: @fonts[params[0]], size: params[1], color: @color }
end
# scn (used for font color in SVG)
def set_color_for_nonstroking_and_special *params
@color = params.size == 4 ? params.map {|it| it * 100 } : params.map {|it| sprintf '%02X', (it.to_f * 255).round }.join
end
# SCN
def set_color_for_stroking_and_special *params
@color = params.size == 4 ? params.map {|it| it * 100 } : params.map {|it| sprintf '%02X', (it.to_f * 255).round }.join
end
def move_text_position x, y
@cursor = { page_number: @page_number, x: x, y: y }
end
def show_text_with_positioning chunks
show_text chunks.reject {|candidate| ::Numeric === candidate }.join, true
end
def show_text text, kerned = false
# NOTE: this may be a rough approximation
text_width = (@state.current_font.unpack text).reduce 0 do |width, code|
width + (@state.current_font.glyph_width code) * @font_settings[:size] / 1000.0
end
string = @state.current_font.to_utf8 text
if @cursor
accum = @cursor
accum[:order] = @text.size + 1
accum[:font_name] = @font_settings[:name]
accum[:font_size] = @font_settings[:size]
accum[:font_color] = @font_settings[:color]
accum[:string] = string
accum[:width] = text_width
@text << accum
@pages[-1][:text] << accum
@cursor = nil
else
(accum = @text[-1])[:string] += string
accum[:width] += text_width
end
accum[:kerned] ||= kerned
end
end
class ImageInspector < PDF::Inspector
attr_reader :images
def initialize
@images = []
@x = @y = @width = @height = nil
@page_number = 0
end
def page= page
@page_number = page.number
@image_xobjects = page.xobjects.each_with_object({}) do |(name, xobject), accum|
accum[name] = xobject if xobject.hash[:Subtype] == :Image
end
end
def page_count
@page_number
end
def concatenate_matrix width, _p2, _p3, height, x, y
@width = width
@height = height
@x = x
@y = y + height
end
def invoke_xobject name
return unless @image_xobjects.key? name
image_info = (image = @image_xobjects[name]).hash
@images << { name: name, page_number: @page_number, x: @x, y: @y, width: @width, height: @height, intrinsic_height: image_info[:Height], intrinsic_width: image_info[:Width], data: image.data }
end
end
class LineInspector < PDF::Inspector
attr_accessor :lines
def initialize
@lines = []
@from = nil
@color = nil
@graphic_states = {}
@page_number = 1
@width = nil
@dash = nil
end
def append_curved_segment *args
x, y = args.pop 2
@from = { x: x, y: y }
end
def append_line x, y
style = @dash && @width ? (@dash[0] > @width ? :dashed : :dotted) : :solid
@lines << { page_number: @page_number, from: @from, to: { x: x, y: y }, color: @color, width: @width, style: style } unless @color.nil? && @width.nil?
@from = { x: x, y: y }
end
def begin_new_subpath x, y
@from = { x: x, y: y }
end
def close_subpath
@from = nil
end
def page= page
@page_number = page.number
@graphic_states = page.graphic_states
end
# SCN
def set_color_for_stroking_and_special *params
@color = params.size == 4 ? params.map {|it| it * 100 } : params.map {|it| sprintf '%02X', (it.to_f * 255).round }.join
end
# gs
def set_graphics_state_parameters ref
if (opacity = @graphic_states[ref][:ca])
@color += (sprintf '%02X', (opacity * 255).round)
end
end
# d
# NOTE: dash is often set before line width, so we must defer resolving until line is appended
def set_line_dash a, _b
@dash = a.empty? ? nil : a
end
# w
def set_line_width line_width
@width = line_width
end
# Q
def restore_graphics_state
@width = nil
end
end
module TareFirstPageContentStreamNoop
def tare_first_page_content_stream
yield
end
end
RSpec.configure do |config|
config.before :suite do
FileUtils.rm_r output_dir, force: true, secure: true
FileUtils.mkdir output_dir
end
config.after :suite do
FileUtils.rm_r output_dir, force: true, secure: true unless (ENV.key? 'DEBUG') || config.reporter.failed_examples.any? {|it| it.metadata[:visual] }
end
def bin_script name, opts = {}
bin_path = Gem.bin_path (opts.fetch :gem, 'asciidoctor-pdf'), name
if (defined? DeepCover) && !(DeepCover.const_defined? :TAKEOVER_IS_ON)
[Gem.ruby, '-rdeep_cover', bin_path]
elsif windows?
[Gem.ruby, bin_path]
else
bin_path
end
end
def asciidoctor_bin
bin_script 'asciidoctor', gem: 'asciidoctor'
end
def asciidoctor_pdf_bin
bin_script 'asciidoctor-pdf'
end
def asciidoctor_pdf_optimize_bin
bin_script 'asciidoctor-pdf-optimize'
end
def run_command cmd, *args
Dir.chdir __dir__ do
if Array === cmd
args.unshift(*cmd)
cmd = args.shift
end
kw_args = Hash === args[-1] ? args.pop : {}
env_override = kw_args[:env] || {}
unless kw_args[:use_bundler]
env_override['RUBYOPT'] = nil
end
if (out = kw_args[:out])
Open3.pipeline_w([env_override, cmd, *args, out: out]) {} # rubocop:disable Lint/EmptyBlock
else
Open3.capture3 env_override, cmd, *args
end
end
end
def docs_dir
File.join __dir__, '..', 'docs'
end
def doc_file path
File.absolute_path path, docs_dir
end
def examples_dir
File.join __dir__, '..', 'examples'
end
def example_file path
File.join examples_dir, path
end
def fixtures_dir
File.join __dir__, 'fixtures'
end
def fixture_file path, relative: false
if relative
(((Pathname.new fixtures_dir) / path).relative_path_from Pathname.new Dir.pwd).to_s
else
File.join fixtures_dir, path
end
end
def output_dir
File.join __dir__, 'output'
end
def output_file path
File.join output_dir, path
end
def create_class super_class, &block
klass = Class.new super_class, &block
Object.const_set %(AnonymousClass#{klass.object_id}).to_sym, klass
klass
end
(PDF_INSPECTOR_CLASS = {
image: ImageInspector,
line: LineInspector,
page: PDF::Inspector::Page,
rect: PDF::Inspector::Graphics::Rectangle,
text: EnhancedPDFTextInspector,
}).default = EnhancedPDFTextInspector
def to_pdf input, opts = {}
analyze = opts.delete :analyze
if (debug = opts.delete :debug) && ENV['CI']
raise ArgumentError, 'debug flag not permitted in CI'
end
enable_footer = opts.delete :enable_footer
safe_mode = opts.fetch :safe, :safe
opts[:attributes] = { 'imagesdir' => fixtures_dir } unless opts.key? :attributes
opts[:attributes]['nofooter'] = '' unless enable_footer
if (attribute_overrides = opts.delete :attribute_overrides)
(opts[:attributes] ||= {}).update attribute_overrides
end
opts = opts.merge backend: 'pdf' unless opts.key? :backend
if Hash === (pdf_theme = opts[:pdf_theme])
pdf_theme_extends = (pdf_theme = pdf_theme.dup).delete :extends if pdf_theme.key? :extends
opts[:pdf_theme] = build_pdf_theme pdf_theme, pdf_theme_extends
end
if Pathname === input
opts[:to_dir] = output_dir unless opts.key? :to_dir
doc = Asciidoctor.convert_file input, (opts.merge safe: safe_mode)
return doc.converter if analyze == :document
pdf_io = Pathname.new doc.attr 'outfile'
elsif analyze == :document
return Asciidoctor.convert input, (opts.merge safe: safe_mode, standalone: true)
else
Asciidoctor.convert input, (opts.merge safe: safe_mode, to_file: (pdf_io = StringIO.new), standalone: true)
end
File.write (File.join Dir.tmpdir, 'debug.pdf'), pdf_io.string if debug
analyze ? (PDF_INSPECTOR_CLASS[analyze].analyze pdf_io) : (PDF::Reader.new pdf_io)
end
def to_pdf_file input, output_filename, opts = {}
opts[:to_file] = (to_file = File.join output_dir, output_filename)
enable_footer = opts.delete :enable_footer
opts[:attributes] = { 'imagesdir' => fixtures_dir } unless opts.key? :attributes
opts[:attributes]['nofooter'] = '' unless enable_footer
if (attribute_overrides = opts.delete :attribute_overrides)
(opts[:attributes] ||= {}).update attribute_overrides
end
if Hash === (pdf_theme = opts[:pdf_theme])
pdf_theme_extends = (pdf_theme = pdf_theme.dup).delete :extends if pdf_theme.key? :extends
opts[:pdf_theme] = build_pdf_theme pdf_theme, pdf_theme_extends
end
opts = opts.merge backend: 'pdf' unless opts.key? :backend
if Pathname === input
Asciidoctor.convert_file input, (opts.merge safe: :safe)
else
Asciidoctor.convert input, (opts.merge safe: :safe, standalone: true)
end
to_file
end
def build_pdf_theme overrides = {}, extends = nil
(Asciidoctor::PDF::ThemeLoader.load_theme extends).tap {|theme| overrides.each {|k, v| theme[k] = v } }
end
def with_tmp_file ext, contents = nil
Tempfile.create %W(asciidoctor-pdf- #{ext}), encoding: 'UTF-8', newline: :universal do |tmp_file|
if contents
tmp_file.write contents
tmp_file.close
end
yield tmp_file
end
end
def with_content_spacer width, height, units = 'pt'
contents = <<~EOS
<svg width="#{width}#{units}" height="#{height}#{units}" viewBox="0 0 #{width} #{height}" version="1.0" xmlns="http://www.w3.org/2000/svg">
<g>
<rect style="fill:#999999" width="#{width}" height="#{height}" x="0" y="0"></rect>
</g>
</svg>
EOS
with_tmp_file '.svg', contents do |spacer_file|
yield spacer_file.path
end
end
def with_pdf_theme_file data
with_tmp_file '-theme.yml', data do |theme_file|
yield theme_file.path
end
end
def extract_outline pdf, list = pdf.outlines
result = []
objects = pdf.objects
pages = pdf.pages
labels = get_page_labels pdf
entry = list[:First] if list
while entry
entry = objects[entry]
title = (((title = entry[:Title]).slice 2, title.size).unpack 'n*').pack 'U*'
dest = entry[:Dest]
dest_page_object = objects[dest[0]]
dest_page = pages.find {|candidate| candidate.page_object == dest_page_object }
top = dest_page.attributes[:MediaBox][3] == dest[3]
if (count = entry[:Count]) == 0
closed = true
children = []
else
closed = count < 0
children = extract_outline pdf, entry
end
result << { title: title, dest: { pagenum: dest_page.number, label: labels[dest_page.number - 1], x: dest[2], y: dest[3], top: top }, closed: closed, children: children }
entry = entry[:Next]
end
result
end
def get_names pdf
if (names = pdf.catalog[:Names])
objects = pdf.objects
Hash[*objects[objects[names][:Dests]][:Names]]
else
{}
end
end
def get_dest pdf, name
if (name_ref = (get_names pdf)[name]) && (dest = pdf.objects[name_ref])
{ page: pdf.objects[(page_ref = dest[0])], page_number: (get_page_number pdf, page_ref), x: dest[2], y: dest[3] }
end
end
def get_page_labels pdf
objects = pdf.objects
Hash[*objects[pdf.catalog[:PageLabels]][:Nums]].each_with_object([]) {|(idx, val), accum| accum[idx] = val[:P] }
end
def get_annotations pdf, page_num = nil
objects = pdf.objects
if page_num
(pdf.page page_num).attributes[:Annots].to_a.map {|ref| objects[ref] }
else
pdf.pages.each_with_object([]) {|page, accum| page.attributes[:Annots].to_a.each {|ref| accum << objects[ref] } }
end
end
def get_images pdf, page_num = nil
if page_num
(pdf.page page_num).xobjects.select {|_, candidate| candidate.hash[:Subtype] == :Image }.values
else
pdf.pages.each_with_object([]) {|page, accum| page.xobjects.each {|_, candidate| candidate.hash[:Subtype] == :Image ? (accum << candidate) : accum } }
end
end
def get_page_size pdf, page_num = 1
if PDF::Reader === pdf
(pdf.page page_num).attributes[:MediaBox].slice 2, 2
else
pdf.pages[page_num - 1][:size]
end
end
def get_page_number pdf, page
page = pdf.objects[page] if PDF::Reader::Reference === page
pdf.pages.find {|candidate| candidate.page_object == page }&.number
end
def lorem_ipsum id
(@lorem_ipsum_data ||= (YAML.load_file fixture_file 'lorem-ipsum.yml'))[id]
end
def windows?
Gem.win_platform?
end
def jruby?
RUBY_ENGINE == 'jruby'
end
def gem_available? gem_name
Gem.loaded_specs.key? gem_name
end
def home_dir
windows? ? (Dir.home.tr ?\\, '/') : Dir.home
end
def with_memory_logger level = nil
old_logger, logger = Asciidoctor::LoggerManager.logger, Asciidoctor::MemoryLogger.new
logger.level = level if level
Asciidoctor::LoggerManager.logger = logger
yield logger
ensure
Asciidoctor::LoggerManager.logger = old_logger
end
def with_local_webserver host = resolve_localhost, port = 9876
base_dir = fixtures_dir
server = TCPServer.new host, port
server_thread = Thread.start do
Thread.current[:requests] = requests = []
while (session = server.accept)
requests << (request = session.gets)
if %r/^GET (\S+) HTTP\/1\.1$/ =~ request.chomp
resource = (resource = $1) == '' ? '.' : resource
else
session.print %(HTTP/1.1 405 Method Not Allowed\r\nContent-Type: text/plain\r\n\r\n)
session.print %(405 - Method not allowed\r\n)
session.close
next
end
resource, _query_string = resource.split '?', 2 if resource.include? '?'
if File.file? (resource_file = (File.join base_dir, resource))
if (ext = (File.extname resource_file)[1..-1])
mimetype = ext == 'adoc' ? 'text/plain' : %(image/#{ext})
else
mimetype = 'text/plain'
end
session.print %(HTTP/1.1 200 OK\r\nContent-Type: #{mimetype}\r\n\r\n)
File.open resource_file, 'rb:utf-8:utf-8' do |fd|
session.write fd.read 256 until fd.eof?
end
else
session.print %(HTTP/1.1 404 Not Found\r\nContent-Type: text/plain\r\n\r\n)
session.print %(404 - Resource not found.\r\n)
end
session.close
end
end
begin
yield %(http://#{host}:#{port}), server_thread
ensure
server_thread.exit
server_thread.value
server.close
end
end
def resolve_localhost
Socket.ip_address_list.find(&:ipv4?).ip_address
end
def compute_image_differences reference, actual, difference = nil
diff = []
if reference
reference_image = ChunkyPNG::Image.from_file reference
if actual
actual_image = ChunkyPNG::Image.from_file actual
else
actual_image = ChunkyPNG::Image.new reference_image.width, reference_image.height
end
else
actual_image = ChunkyPNG::Image.from_file actual
reference_image = ChunkyPNG::Image.new actual_image.width, actual_image.height
end
actual_image.height.times do |y|
actual_image.row(y).each_with_index do |pixel, x|
diff << [x, y] unless pixel == reference_image[x, y]
end
end
if !diff.empty? && difference
x = diff.map {|xy| xy[0] }
y = diff.map {|xy| xy[1] }
actual_image.rect x.min, y.min, x.max, y.max, (ChunkyPNG::Color.rgb 0, 255, 0)
actual_image.save difference
end
diff.length
end
end
RSpec::Matchers.define_negated_matcher :not_raise_exception, :raise_exception
RSpec::Matchers.define :have_size do |expected|
match {|actual| actual.size == expected }
failure_message {|actual| %(expected #{actual} to have size #{expected}, but was #{actual.size}) }
end
RSpec::Matchers.define :have_background do |expected|
match do |actual|
color = ((expected[:color].scan %r/../).map {|it| ((it.to_i 16) / 255.0).round 5 }.join ' ') + ' scn'
# FIXME: shave off lines before this line
(expect actual).to include color
x1, y1 = expected[:top_left]
x2, y2 = expected[:bottom_right]
(expect actual).to include %(#{x2} #{y1} #{x2} #{y1} #{x2} #{y1} c)
(expect actual).to include %(#{x1} #{y2} #{x1} #{y2} #{x1} #{y2} c)
end
failure_message {|actual| %(expected #{actual} to have background #{expected}, but was \n#{actual.join ?\n}) }
end
RSpec::Matchers.define :annotate do |text|
match do |subject|
left, bottom, right, top = subject[:Rect]
left == text[:x] && ((text[:x] + text[:width]) - right).abs < 0.25 && bottom < text[:y] && top > (text[:y] + text[:font_size])
end
failure_message {|subject| %(expected #{subject} to annotate #{text}) }
end
RSpec::Matchers.define :have_message do |expected|
actual = nil
match do |logger|
result = false
if (message = logger.messages[expected[:index] || 0])
if message[:severity] == expected[:severity]
message_text = Hash === (message_data = message[:message]) ? message_data[:text] : message_data
if Regexp === (expected_message = expected[:message])
result = true if expected_message.match? message_text
elsif expected_message.start_with? '~'
result = true if message_text.include? expected_message[1..-1]
elsif message_text === expected_message
result = true
end
result = false if (lineno = expected[:lineno]) && !(Hash === message_data && lineno == message_data[:source_location].lineno)
end
actual = message
end
result
end
failure_message do
%(expected #{expected[:severity]} message#{expected[:message].to_s.chr == '~' ? ' containing ' : ' matching '}`#{expected[:message]}' to have been logged) + (actual ? %(, but got #{actual[:severity]}: #{actual[:message]}) : '')
end
end
RSpec::Matchers.define :log_message do |expected|
match notify_expectation_failures: true do |actual|
if expected
log_level_override = expected.delete :using_log_level
expected = nil if expected.empty?
end
with_memory_logger log_level_override do |logger|
actual.call
if expected
(expect logger).to have_message expected
else
(expect logger).not_to be_empty
end
true
end
end
#match_when_negated notify_expectation_failures: true do |actual|
# with_memory_logger expected.to_h[:using_log_level] do |logger|
# actual.call
# logger ? logger.empty? : true
# end
#end
supports_block_expectations
end
RSpec::Matchers.define :log_messages do |expecteds|
match notify_expectation_failures: true do |actual|
with_memory_logger do |logger|
actual.call
expecteds.each_with_index do |it, idx|
(expect logger).to have_message (it.merge index: idx)
end if logger
true
end
end
supports_block_expectations
end
# define matcher to replace `.not_to log_message` until notify_expectation_failures is supported for negated match
# see https://github.com/rspec/rspec-expectations/issues/1124
RSpec::Matchers.define :not_log_message do |expected|
match notify_expectation_failures: true do |actual|
with_memory_logger expected.to_h[:using_log_level] do |logger|
actual.call
logger ? logger.empty? : true
end
end
supports_block_expectations
end
RSpec::Matchers.define :visually_match do |reference_filename|
reference_path = (Pathname.new reference_filename).absolute? ? reference_filename : (File.join __dir__, 'reference', reference_filename)
match do |actual_path|
warn %(#{RSpec.current_example.location} uses visual comparison but is not tagged with visual: true) unless RSpec.current_example.metadata[:visual]
return false unless File.exist? reference_path
images_output_dir = output_file 'visual-comparison-workdir'
Dir.mkdir images_output_dir unless Dir.exist? images_output_dir
output_basename = File.join images_output_dir, (File.basename actual_path, '.pdf')
pdftocairo_result = system 'pdftocairo', '-png', actual_path, %(#{output_basename}-actual)
raise Errno::ENOENT, 'pdftocairo' if pdftocairo_result.nil?
system 'pdftocairo', '-png', reference_path, %(#{output_basename}-reference)
pixels = 0
tmp_files = [actual_path]
files = Dir[%(#{output_basename}-{actual,reference}-*.png)].map {|filename| (/-(?:actual|reference)-(\d+)\.png$/.match filename)[1] }.sort.uniq
return false if files.empty?
files.each do |idx|
reference_page_filename = %(#{output_basename}-reference-#{idx}.png)
reference_page_filename = nil unless File.exist? reference_page_filename
tmp_files << reference_page_filename if reference_page_filename
actual_page_filename = %(#{output_basename}-actual-#{idx}.png)
actual_page_filename = nil unless File.exist? actual_page_filename
tmp_files << actual_page_filename if actual_page_filename
next if reference_page_filename && actual_page_filename && (FileUtils.compare_file reference_page_filename, actual_page_filename)
pixels += compute_image_differences reference_page_filename, actual_page_filename, %(#{output_basename}-diff-#{idx}.png)
end
if pixels > 0
false
else
tmp_files.each {|it| File.unlink it } unless ENV.key? 'DEBUG'
true
end
end
failure_message {|actual_path| %(expected #{actual_path} to be visually identical to #{reference_path}) }
end
|