Module: Bidi2pdf::TestHelpers::PDFTextSanitizer
- Defined in:
- lib/bidi2pdf/test_helpers/pdf_text_sanitizer.rb
Overview
rubocop: disable Metrics/ModuleLength
Class Method Summary collapse
- .clean(text) ⇒ Object
- .clean_pages(pdf) ⇒ Object
- .contains?(actual_pdf_thingy, expected, page_number = nil) ⇒ Boolean
- .content_mismatch_message(actual, expected) ⇒ Object
- .format_diff_output(diffs, expected, actual) ⇒ Object
- .match?(actual_pdf_thingy, expected_pdf_thingy) ⇒ Boolean
- .match_expected?(text, expected) ⇒ Boolean
- .normalize(text) ⇒ Object
- .page_difference_message(actual_page, expected_page, page_idx) ⇒ Object
- .report_content_mismatch(actual, expected) ⇒ Object
- .reporter ⇒ Object
Class Method Details
.clean(text) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/bidi2pdf/test_helpers/pdf_text_sanitizer.rb', line 12 def clean(text) text = UnicodeUtils.nfkd(text) text.gsub("\uFB01", "fi") .gsub("\uFB02", "fl") .gsub("-\n", "") .gsub('"', '"') .gsub("'", "'") .gsub("…", "...") .gsub("—", "--") .gsub("–", "-") .gsub(/\s+/, " ") .strip end |
.clean_pages(pdf) ⇒ Object
27 28 29 |
# File 'lib/bidi2pdf/test_helpers/pdf_text_sanitizer.rb', line 27 def clean_pages(pdf) Bidi2pdf::TestHelpers::PDFReaderUtils.pdf_text(pdf).map { |text| clean(text) } end |
.contains?(actual_pdf_thingy, expected, page_number = nil) ⇒ Boolean
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/bidi2pdf/test_helpers/pdf_text_sanitizer.rb', line 35 def contains?(actual_pdf_thingy, expected, page_number = nil) cleaned_pages = clean_pages(actual_pdf_thingy) return false if page_number && page_number > cleaned_pages.size if page_number text = cleaned_pages[page_number - 1] return match_expected?(text, expected) end cleaned_pages.any? { |page| match_expected?(page, expected) } end |
.content_mismatch_message(actual, expected) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/bidi2pdf/test_helpers/pdf_text_sanitizer.rb', line 71 def (actual, expected) sections = ["--- PDF content mismatch ---"] max_pages = [actual.length, expected.length].max (0...max_pages).each do |page_idx| actual_page = actual[page_idx] || "(missing page)" expected_page = expected[page_idx] || "(missing page)" = (actual_page, expected_page, page_idx) sections << if end sections.join("\n") end |
.format_diff_output(diffs, expected, actual) ⇒ Object
101 102 103 104 105 |
# File 'lib/bidi2pdf/test_helpers/pdf_text_sanitizer.rb', line 101 def format_diff_output(diffs, expected, actual) changes = group_changed_diffs(diffs) changes.flat_map { |change| format_change(expected, actual, change) }.join("\n") end |
.match?(actual_pdf_thingy, expected_pdf_thingy) ⇒ Boolean
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/bidi2pdf/test_helpers/pdf_text_sanitizer.rb', line 54 def match?(actual_pdf_thingy, expected_pdf_thingy) cleaned_actual = clean_pages(actual_pdf_thingy) cleaned_expected = clean_pages(expected_pdf_thingy) actual_for_comparison = cleaned_actual.map { |text| normalize(text) } expected_for_comparison = cleaned_expected.map { |text| normalize(text) } return true if actual_for_comparison == expected_for_comparison report_content_mismatch(cleaned_actual, cleaned_expected) false end |
.match_expected?(text, expected) ⇒ Boolean
48 49 50 51 52 |
# File 'lib/bidi2pdf/test_helpers/pdf_text_sanitizer.rb', line 48 def match_expected?(text, expected) return false unless text expected.is_a?(Regexp) ? text.match?(expected) : text.include?(expected.to_s) end |
.normalize(text) ⇒ Object
31 32 33 |
# File 'lib/bidi2pdf/test_helpers/pdf_text_sanitizer.rb', line 31 def normalize(text) clean(text).gsub(/\s+/, "") end |
.page_difference_message(actual_page, expected_page, page_idx) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/bidi2pdf/test_helpers/pdf_text_sanitizer.rb', line 86 def (actual_page, expected_page, page_idx) actual_no_space = normalize(actual_page.to_s) expected_no_space = normalize(expected_page.to_s) return nil if actual_no_space == expected_no_space diffs = Diff::LCS.sdiff(expected_page, actual_page) [ "", "Page #{page_idx + 1} differences (ignoring whitespace):", format_diff_output(diffs, expected_page, actual_page) ].join("\n") end |
.report_content_mismatch(actual, expected) ⇒ Object
67 68 69 |
# File 'lib/bidi2pdf/test_helpers/pdf_text_sanitizer.rb', line 67 def report_content_mismatch(actual, expected) reporter.((actual, expected)) end |
.reporter ⇒ Object
107 108 109 |
# File 'lib/bidi2pdf/test_helpers/pdf_text_sanitizer.rb', line 107 def reporter RSpec.configuration.reporter end |