Top Level Namespace
Defined Under Namespace
Modules: Bidi2pdf
Constant Summary collapse
- ChromedriverTestcontainer =
alias the long class name
Bidi2pdf::TestHelpers::Testcontainers::ChromedriverContainer
Instance Method Summary collapse
- #chromedriver_tests_present? ⇒ Boolean
-
#contains_pdf_text ⇒ Boolean
Custom RSpec matcher for checking whether a PDF document contains specific text.
- #dump_container_logs(container) ⇒ Object
-
#have_pdf_page_count ⇒ RSpec::Matchers::Matcher
RSpec matcher to assert the number of pages in a PDF document.
-
#match_pdf_text ⇒ RSpec::Matchers::Matcher
Custom RSpec matcher to compare the sanitized text content of two PDF files.
- #reporter ⇒ Object
- #start_chromedriver_container(build_dir:, mounts:, shared_network:) ⇒ Object
- #stop_container(container) ⇒ Object
- #stop_running_container(container) ⇒ Object
- #test_of_kind_present?(type) ⇒ Boolean
Instance Method Details
#chromedriver_tests_present? ⇒ Boolean
95 96 97 |
# File 'lib/bidi2pdf/test_helpers/testcontainers/chromedriver_test_helper.rb', line 95 def chromedriver_tests_present? test_of_kind_present? :chromedriver end |
#contains_pdf_text ⇒ Boolean
Custom RSpec matcher for checking whether a PDF document contains specific text.
This matcher allows you to assert that a certain string or regular expression is present in the sanitized text of a PDF document.
It supports chaining with .at_page(n) to limit the search to a specific page.
Examples
expect(pdf_data).to contains_pdf_text("Total: 123.45")
expect(pdf_data).to contains_pdf_text(/Invoice #\d+/).at_page(2)
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 |
# File 'lib/bidi2pdf/test_helpers/matchers/contains_pdf_text.rb', line 20 RSpec::Matchers.define :contains_pdf_text do |expected| chain :at_page do |page_number| @page_number = page_number end match do |actual| Bidi2pdf::TestHelpers::PDFTextSanitizer.contains?(actual, expected, @page_number) end do |actual| pages = Bidi2pdf::TestHelpers::PDFTextSanitizer.clean_pages(actual) return "Document does not contain page #{@page_number}" if @page_number && !(@page_number && @page_number <= pages.size) " PDF text did not contain expected content.\n\n --- Expected (\#{expected.inspect}) ---\n On page \#{@page_number || \"any\"}:\n\n --- Actual ---\n \#{pages.each_with_index.map { |text, i| \"Page \#{i + 1}:\\n\#{text}\" }.join(\"\\n\\n\")}\n MSG\n end\n\n description do\n desc = \"contain \#{expected.inspect} in PDF\"\n desc += \" on page \#{@page_number}\" if @page_number\n desc\n end\nend\n" |
#dump_container_logs(container) ⇒ Object
75 76 77 78 79 80 81 82 |
# File 'lib/bidi2pdf/test_helpers/testcontainers/chromedriver_test_helper.rb', line 75 def dump_container_logs(container) return unless ENV["SHOW_CONTAINER_LOGS"] stdout, stderr = container.logs reporter.("Container logs:") reporter.(stderr.to_s) unless stderr.to_s.empty? reporter.(stdout.to_s) unless stdout.to_s.empty? end |
#have_pdf_page_count ⇒ RSpec::Matchers::Matcher
This matcher depends on Bidi2pdf::TestHelpers::PDFReaderUtils.pdf_reader_for
to extract the page count. Make sure it supports all your intended input formats.
RSpec matcher to assert the number of pages in a PDF document.
This matcher is useful for verifying the structural integrity of generated or uploaded PDFs, especially in tests for reporting, invoice generation, or document exports.
It supports a variety of input types:
- Raw PDF data as a
String - File paths (
String) StringIOorFileobjects- Even Base64-encoded strings, if your
pdf_reader_formethod handles it
Example
expect(pdf_data).to have_pdf_page_count(5)
expect(StringIO.new(pdf_data)).to have_pdf_page_count(3)
If the PDF is malformed, the matcher will gracefully fail and show the error message.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/bidi2pdf/test_helpers/matchers/have_pdf_page_count.rb', line 29 RSpec::Matchers.define :have_pdf_page_count do |expected_count| match do |pdf_data| reader = Bidi2pdf::TestHelpers::PDFReaderUtils.pdf_reader_for(pdf_data) @actual_count = reader.page_count @actual_count == expected_count rescue PDF::Reader::MalformedPDFError => e = e. false end do |_pdf_data| if "Expected a valid PDF with #{expected_count} pages, but encountered an error: #{@error_message}" else "Expected PDF to have #{expected_count} pages, but it has #{@actual_count} pages" end end description do "have #{expected_count} PDF pages" end end |
#match_pdf_text ⇒ RSpec::Matchers::Matcher
Ensure PDFTextSanitizer.match? and PDFTextSanitizer.clean_pages are implemented
to handle your specific PDF processing logic.
Custom RSpec matcher to compare the sanitized text content of two PDF files.
This matcher is useful for comparing PDF documents where formatting and metadata may differ,
but the actual visible text content should be the same. It uses PDFTextSanitizer internally
to normalize and clean the text before comparison.
Example
expect(actual_pdf).to match_pdf_text(expected_pdf)
If the texts don’t match, it prints a diff-friendly message showing cleaned text content.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/bidi2pdf/test_helpers/matchers/match_pdf_text.rb', line 22 RSpec::Matchers.define :match_pdf_text do |expected| match do |actual| Bidi2pdf::TestHelpers::PDFTextSanitizer.match?(actual, expected) end do |actual| cleaned_actual = Bidi2pdf::TestHelpers::PDFTextSanitizer.clean_pages(actual) cleaned_expected = Bidi2pdf::TestHelpers::PDFTextSanitizer.clean_pages(expected) " PDF text did not match.\n\n --- Expected ---\n \#{cleaned_expected.join(\"\\n\")}\n\n --- Actual ---\n \#{cleaned_actual.join(\"\\n\")}\n MSG\n end\n\n description do\n \"match sanitized PDF text content\"\n end\nend\n" |
#reporter ⇒ Object
23 24 25 |
# File 'lib/bidi2pdf/test_helpers/testcontainers/shared_docker_network.rb', line 23 def reporter RSpec.configuration.reporter end |
#start_chromedriver_container(build_dir:, mounts:, shared_network:) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/bidi2pdf/test_helpers/testcontainers/chromedriver_test_helper.rb', line 106 def start_chromedriver_container(build_dir:, mounts:, shared_network:) container = ChromedriverTestcontainer.new(ChromedriverTestcontainer::DEFAULT_IMAGE, build_dir: build_dir, docker_file: "docker/Dockerfile.chromedriver") .with_network(shared_network) .with_network_aliases("remote-chrome") container.with_filesystem_binds(mounts) if mounts&.any? container.start container end |
#stop_container(container) ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/bidi2pdf/test_helpers/testcontainers/chromedriver_test_helper.rb', line 67 def stop_container(container) return unless container dump_container_logs(container) if container.running? stop_running_container(container) container.remove end |
#stop_running_container(container) ⇒ Object
84 85 86 87 88 89 |
# File 'lib/bidi2pdf/test_helpers/testcontainers/chromedriver_test_helper.rb', line 84 def stop_running_container(container) return unless container.running? reporter.("🧹 #{container.image} stopping container...") container.stop end |
#test_of_kind_present?(type) ⇒ Boolean
99 100 101 |
# File 'lib/bidi2pdf/test_helpers/testcontainers/chromedriver_test_helper.rb', line 99 def test_of_kind_present?(type) RSpec.world.filtered_examples.values.flatten.any? { |example| example.[type] } end |