Add builder pattern for paragraph, lists, tables
This commit is contained in:
74
test/document_test.rb
Normal file
74
test/document_test.rb
Normal file
@@ -0,0 +1,74 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "test_helper"
|
||||
|
||||
class DocumentTest < Minitest::Test
|
||||
include EzdocTestHelpers
|
||||
|
||||
def test_creates_valid_docx_structure
|
||||
Tempfile.create(["test", ".docx"]) do |file|
|
||||
Ezdoc::Document.create(file.path) do |doc|
|
||||
doc.p "Test"
|
||||
end
|
||||
|
||||
assert File.exist?(file.path)
|
||||
assert File.size(file.path).positive?
|
||||
|
||||
Zip::File.open(file.path) do |zip|
|
||||
assert zip.find_entry("[Content_Types].xml"), "Missing [Content_Types].xml"
|
||||
assert zip.find_entry("_rels/.rels"), "Missing _rels/.rels"
|
||||
assert zip.find_entry("word/document.xml"), "Missing word/document.xml"
|
||||
assert zip.find_entry("word/_rels/document.xml.rels"), "Missing word/_rels/document.xml.rels"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_document_xml_has_correct_namespaces
|
||||
xml = create_doc_and_read_xml { |doc| doc.p "Test" }
|
||||
|
||||
assert_includes xml, 'xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"'
|
||||
assert_includes xml, "<w:document"
|
||||
assert_includes xml, "<w:body>"
|
||||
end
|
||||
|
||||
def test_empty_document
|
||||
Tempfile.create(["test", ".docx"]) do |file|
|
||||
Ezdoc::Document.create(file.path) { |_doc| } # rubocop:disable Lint/EmptyBlock
|
||||
|
||||
assert File.exist?(file.path)
|
||||
Zip::File.open(file.path) do |zip|
|
||||
assert zip.find_entry("word/document.xml")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_document_create_returns_document
|
||||
result = nil
|
||||
Tempfile.create(["test", ".docx"]) do |file|
|
||||
result = Ezdoc::Document.create(file.path) do |doc|
|
||||
doc.p "Test"
|
||||
end
|
||||
end
|
||||
|
||||
assert_instance_of Ezdoc::Document, result
|
||||
end
|
||||
|
||||
def test_document_has_nodes
|
||||
doc = Ezdoc::Document.new
|
||||
doc.p "Test"
|
||||
|
||||
assert_equal 1, doc.nodes.count
|
||||
assert_instance_of Ezdoc::Nodes::Paragraph, doc.nodes.first
|
||||
end
|
||||
|
||||
def test_document_lists_helper
|
||||
doc = Ezdoc::Document.new
|
||||
doc.p "Paragraph"
|
||||
doc.ul { doc.li "Bullet" }
|
||||
doc.ol { doc.li "Number" }
|
||||
doc.table { doc.tr { doc.td "Cell" } }
|
||||
|
||||
assert_equal 2, doc.lists.count
|
||||
assert(doc.lists.all? { |l| l.is_a?(Ezdoc::Nodes::List) })
|
||||
end
|
||||
end
|
||||
@@ -1,28 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "test_helper"
|
||||
require "tempfile"
|
||||
require "zip"
|
||||
|
||||
class EzdocTest < Minitest::Test
|
||||
def test_creates_valid_docx_with_hello_world
|
||||
Tempfile.create(["test", ".docx"]) do |file|
|
||||
Ezdoc::Document.create(file.path) do |doc|
|
||||
doc.text "Hello World"
|
||||
end
|
||||
|
||||
assert File.exist?(file.path)
|
||||
assert File.size(file.path).positive?
|
||||
|
||||
Zip::File.open(file.path) do |zip|
|
||||
assert zip.find_entry("[Content_Types].xml")
|
||||
assert zip.find_entry("_rels/.rels")
|
||||
assert zip.find_entry("word/document.xml")
|
||||
assert zip.find_entry("word/_rels/document.xml.rels")
|
||||
|
||||
document_xml = zip.read("word/document.xml")
|
||||
assert_includes document_xml, "Hello World"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
136
test/formatting_test.rb
Normal file
136
test/formatting_test.rb
Normal file
@@ -0,0 +1,136 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "test_helper"
|
||||
|
||||
class FormattingTest < Minitest::Test
|
||||
include EzdocTestHelpers
|
||||
|
||||
def test_bold_text
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.p do
|
||||
doc.b { doc.text "bold text" }
|
||||
end
|
||||
end
|
||||
|
||||
assert_includes xml, "<w:b/>"
|
||||
assert_includes xml, "bold text"
|
||||
end
|
||||
|
||||
def test_italic_text
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.p do
|
||||
doc.i { doc.text "italic text" }
|
||||
end
|
||||
end
|
||||
|
||||
assert_includes xml, "<w:i/>"
|
||||
assert_includes xml, "italic text"
|
||||
end
|
||||
|
||||
def test_underline_text
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.p do
|
||||
doc.u { doc.text "underlined text" }
|
||||
end
|
||||
end
|
||||
|
||||
assert_includes xml, '<w:u w:val="single"/>'
|
||||
assert_includes xml, "underlined text"
|
||||
end
|
||||
|
||||
def test_bold_and_italic_nested
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.p do
|
||||
doc.b do
|
||||
doc.i { doc.text "bold and italic" }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
assert_includes xml, "<w:b/>"
|
||||
assert_includes xml, "<w:i/>"
|
||||
assert_includes xml, "bold and italic"
|
||||
# Both should be in the same rPr element
|
||||
assert_match(%r{<w:rPr>.*<w:b/>.*<w:i/>.*</w:rPr>}m, xml)
|
||||
end
|
||||
|
||||
def test_italic_and_bold_nested_reverse_order
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.p do
|
||||
doc.i do
|
||||
doc.b { doc.text "italic and bold" }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
assert_includes xml, "<w:b/>"
|
||||
assert_includes xml, "<w:i/>"
|
||||
end
|
||||
|
||||
def test_all_three_formatting_options
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.p do
|
||||
doc.b do
|
||||
doc.i do
|
||||
doc.u { doc.text "bold italic underline" }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
assert_includes xml, "<w:b/>"
|
||||
assert_includes xml, "<w:i/>"
|
||||
assert_includes xml, '<w:u w:val="single"/>'
|
||||
assert_includes xml, "bold italic underline"
|
||||
end
|
||||
|
||||
def test_mixed_formatting_in_paragraph
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.p do
|
||||
doc.text "Normal "
|
||||
doc.b { doc.text "bold" }
|
||||
doc.text " normal "
|
||||
doc.i { doc.text "italic" }
|
||||
doc.text " normal "
|
||||
doc.u { doc.text "underline" }
|
||||
doc.text " end"
|
||||
end
|
||||
end
|
||||
|
||||
assert_includes xml, "Normal "
|
||||
assert_includes xml, "bold"
|
||||
assert_includes xml, "italic"
|
||||
assert_includes xml, "underline"
|
||||
assert_equal 7, xml.scan("<w:r>").count
|
||||
end
|
||||
|
||||
def test_formatting_does_not_leak_between_blocks
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.p do
|
||||
doc.b { doc.text "bold" }
|
||||
doc.text "not bold"
|
||||
end
|
||||
end
|
||||
|
||||
# Count rPr elements - should only be one (for the bold text)
|
||||
assert_equal 1, xml.scan("<w:rPr>").count
|
||||
end
|
||||
|
||||
def test_deeply_nested_formatting
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.p do
|
||||
doc.b do
|
||||
doc.text "bold "
|
||||
doc.i do
|
||||
doc.text "bold+italic "
|
||||
doc.u { doc.text "all three" }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
assert_includes xml, "bold "
|
||||
assert_includes xml, "bold+italic "
|
||||
assert_includes xml, "all three"
|
||||
end
|
||||
end
|
||||
158
test/integration_test.rb
Normal file
158
test/integration_test.rb
Normal file
@@ -0,0 +1,158 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "test_helper"
|
||||
|
||||
class IntegrationTest < Minitest::Test
|
||||
include EzdocTestHelpers
|
||||
|
||||
def test_complex_document_with_all_features
|
||||
xml_files = create_doc_and_read_all_xml do |doc|
|
||||
doc.p "Introduction paragraph"
|
||||
|
||||
doc.p do
|
||||
doc.text "This has "
|
||||
doc.b { doc.text "bold" }
|
||||
doc.text " and "
|
||||
doc.i { doc.text "italic" }
|
||||
doc.text " text."
|
||||
end
|
||||
|
||||
doc.ul do
|
||||
doc.li "Bullet point 1"
|
||||
doc.li { doc.b { doc.text "Bold bullet" } }
|
||||
end
|
||||
|
||||
doc.ol do
|
||||
doc.li "Numbered item 1"
|
||||
doc.li "Numbered item 2"
|
||||
end
|
||||
|
||||
doc.table do
|
||||
doc.tr do
|
||||
doc.td "Header 1"
|
||||
doc.td "Header 2"
|
||||
end
|
||||
doc.tr do
|
||||
doc.td { doc.i { doc.text "Italic cell" } }
|
||||
doc.td "Normal cell"
|
||||
end
|
||||
end
|
||||
|
||||
doc.p "Conclusion paragraph"
|
||||
end
|
||||
|
||||
doc_xml = xml_files["word/document.xml"]
|
||||
|
||||
# Verify all content types are present
|
||||
assert_includes doc_xml, "Introduction paragraph"
|
||||
assert_includes doc_xml, "Conclusion paragraph"
|
||||
assert_includes doc_xml, "<w:b/>"
|
||||
assert_includes doc_xml, "<w:i/>"
|
||||
assert_includes doc_xml, "<w:tbl>"
|
||||
assert_includes doc_xml, "<w:numId"
|
||||
|
||||
# Verify numbering exists
|
||||
assert xml_files.key?("word/numbering.xml")
|
||||
end
|
||||
|
||||
def test_paragraph_between_lists
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.ul { doc.li "Before" }
|
||||
doc.p "Middle paragraph"
|
||||
doc.ol { doc.li "After" }
|
||||
end
|
||||
|
||||
# Verify order by checking relative positions
|
||||
before_pos = xml.index("Before")
|
||||
middle_pos = xml.index("Middle paragraph")
|
||||
after_pos = xml.index("After")
|
||||
|
||||
assert before_pos < middle_pos, "Before should come before Middle"
|
||||
assert middle_pos < after_pos, "Middle should come before After"
|
||||
end
|
||||
|
||||
def test_table_between_paragraphs
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.p "Before table"
|
||||
doc.table do
|
||||
doc.tr { doc.td "In table" }
|
||||
end
|
||||
doc.p "After table"
|
||||
end
|
||||
|
||||
before_pos = xml.index("Before table")
|
||||
table_pos = xml.index("<w:tbl>")
|
||||
after_pos = xml.index("After table")
|
||||
|
||||
assert before_pos < table_pos
|
||||
assert table_pos < after_pos
|
||||
end
|
||||
|
||||
def test_formatting_in_list_items
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.ul do
|
||||
doc.li do
|
||||
doc.b do
|
||||
doc.i { doc.text "Bold and italic in list" }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
assert_includes xml, "<w:b/>"
|
||||
assert_includes xml, "<w:i/>"
|
||||
assert_includes xml, "Bold and italic in list"
|
||||
end
|
||||
|
||||
def test_formatting_in_table_cells
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.table do
|
||||
doc.tr do
|
||||
doc.td do
|
||||
doc.b do
|
||||
doc.i { doc.text "Bold and italic in table" }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
assert_includes xml, "<w:b/>"
|
||||
assert_includes xml, "<w:i/>"
|
||||
assert_includes xml, "Bold and italic in table"
|
||||
end
|
||||
|
||||
def test_multiple_features_interleaved
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.p "Para 1"
|
||||
doc.ul { doc.li "Bullet" }
|
||||
doc.p "Para 2"
|
||||
doc.table { doc.tr { doc.td "Cell" } }
|
||||
doc.p "Para 3"
|
||||
doc.ol { doc.li "Number" }
|
||||
doc.p "Para 4"
|
||||
end
|
||||
|
||||
# Verify all elements exist
|
||||
assert_includes xml, "Para 1"
|
||||
assert_includes xml, "Para 4"
|
||||
assert_includes xml, "Bullet"
|
||||
assert_includes xml, "Cell"
|
||||
assert_includes xml, "Number"
|
||||
|
||||
# Verify correct ordering
|
||||
positions = [
|
||||
xml.index("Para 1"),
|
||||
xml.index("Bullet"),
|
||||
xml.index("Para 2"),
|
||||
xml.index("<w:tbl>"),
|
||||
xml.index("Para 3"),
|
||||
xml.index("Number"),
|
||||
xml.index("Para 4")
|
||||
]
|
||||
|
||||
positions.each_cons(2) do |a, b|
|
||||
assert a < b, "Elements should be in correct order"
|
||||
end
|
||||
end
|
||||
end
|
||||
147
test/list_test.rb
Normal file
147
test/list_test.rb
Normal file
@@ -0,0 +1,147 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "test_helper"
|
||||
|
||||
class ListTest < Minitest::Test
|
||||
include EzdocTestHelpers
|
||||
|
||||
#
|
||||
# Bullet List Tests
|
||||
#
|
||||
|
||||
def test_bullet_list_basic
|
||||
xml_files = create_doc_and_read_all_xml do |doc|
|
||||
doc.ul do
|
||||
doc.li "Item 1"
|
||||
doc.li "Item 2"
|
||||
end
|
||||
end
|
||||
|
||||
doc_xml = xml_files["word/document.xml"]
|
||||
assert_includes doc_xml, "Item 1"
|
||||
assert_includes doc_xml, "Item 2"
|
||||
assert_includes doc_xml, "<w:numId"
|
||||
|
||||
numbering_xml = xml_files["word/numbering.xml"]
|
||||
assert numbering_xml, "numbering.xml should exist"
|
||||
assert_includes numbering_xml, "bullet"
|
||||
end
|
||||
|
||||
def test_bullet_list_with_many_items
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.ul do
|
||||
10.times { |i| doc.li "Item #{i + 1}" }
|
||||
end
|
||||
end
|
||||
|
||||
10.times do |i|
|
||||
assert_includes xml, "Item #{i + 1}"
|
||||
end
|
||||
end
|
||||
|
||||
def test_bullet_list_with_formatted_items
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.ul do
|
||||
doc.li { doc.b { doc.text "Bold item" } }
|
||||
doc.li { doc.i { doc.text "Italic item" } }
|
||||
doc.li do
|
||||
doc.text "Mixed: "
|
||||
doc.b { doc.text "bold" }
|
||||
doc.text " and "
|
||||
doc.i { doc.text "italic" }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
assert_includes xml, "Bold item"
|
||||
assert_includes xml, "Italic item"
|
||||
assert_includes xml, "<w:b/>"
|
||||
assert_includes xml, "<w:i/>"
|
||||
end
|
||||
|
||||
#
|
||||
# Numbered List Tests
|
||||
#
|
||||
|
||||
def test_numbered_list_basic
|
||||
xml_files = create_doc_and_read_all_xml do |doc|
|
||||
doc.ol do
|
||||
doc.li "First"
|
||||
doc.li "Second"
|
||||
doc.li "Third"
|
||||
end
|
||||
end
|
||||
|
||||
doc_xml = xml_files["word/document.xml"]
|
||||
assert_includes doc_xml, "First"
|
||||
assert_includes doc_xml, "Second"
|
||||
assert_includes doc_xml, "Third"
|
||||
|
||||
numbering_xml = xml_files["word/numbering.xml"]
|
||||
assert_includes numbering_xml, "decimal"
|
||||
end
|
||||
|
||||
def test_numbered_list_with_formatted_items
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.ol do
|
||||
doc.li { doc.b { doc.text "Bold numbered" } }
|
||||
doc.li "Plain numbered"
|
||||
end
|
||||
end
|
||||
|
||||
assert_includes xml, "Bold numbered"
|
||||
assert_includes xml, "Plain numbered"
|
||||
assert_includes xml, "<w:b/>"
|
||||
end
|
||||
|
||||
#
|
||||
# Multiple Lists Tests
|
||||
#
|
||||
|
||||
def test_multiple_bullet_lists
|
||||
xml_files = create_doc_and_read_all_xml do |doc|
|
||||
doc.ul do
|
||||
doc.li "List 1 Item 1"
|
||||
doc.li "List 1 Item 2"
|
||||
end
|
||||
doc.p "Separator paragraph"
|
||||
doc.ul do
|
||||
doc.li "List 2 Item 1"
|
||||
doc.li "List 2 Item 2"
|
||||
end
|
||||
end
|
||||
|
||||
doc_xml = xml_files["word/document.xml"]
|
||||
assert_includes doc_xml, "List 1 Item 1"
|
||||
assert_includes doc_xml, "List 2 Item 1"
|
||||
assert_includes doc_xml, "Separator paragraph"
|
||||
end
|
||||
|
||||
def test_mixed_list_types
|
||||
xml_files = create_doc_and_read_all_xml do |doc|
|
||||
doc.ul do
|
||||
doc.li "Bullet 1"
|
||||
doc.li "Bullet 2"
|
||||
end
|
||||
doc.ol do
|
||||
doc.li "Number 1"
|
||||
doc.li "Number 2"
|
||||
end
|
||||
end
|
||||
|
||||
numbering_xml = xml_files["word/numbering.xml"]
|
||||
assert_includes numbering_xml, "bullet"
|
||||
assert_includes numbering_xml, "decimal"
|
||||
end
|
||||
|
||||
def test_document_without_lists_has_no_numbering_xml
|
||||
xml_files = create_doc_and_read_all_xml do |doc|
|
||||
doc.p "Just a paragraph"
|
||||
doc.table do
|
||||
doc.tr { doc.td "Cell" }
|
||||
end
|
||||
end
|
||||
|
||||
refute xml_files.key?("word/numbering.xml"), "numbering.xml should not exist without lists"
|
||||
end
|
||||
end
|
||||
114
test/paragraph_test.rb
Normal file
114
test/paragraph_test.rb
Normal file
@@ -0,0 +1,114 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "test_helper"
|
||||
|
||||
class ParagraphTest < Minitest::Test
|
||||
include EzdocTestHelpers
|
||||
|
||||
def test_simple_paragraph
|
||||
xml = create_doc_and_read_xml { |doc| doc.p "Hello World" }
|
||||
|
||||
assert_includes xml, "<w:p>"
|
||||
assert_includes xml, "<w:r>"
|
||||
assert_includes xml, "<w:t"
|
||||
assert_includes xml, "Hello World"
|
||||
end
|
||||
|
||||
def test_multiple_paragraphs
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.p "First paragraph"
|
||||
doc.p "Second paragraph"
|
||||
doc.p "Third paragraph"
|
||||
end
|
||||
|
||||
assert_includes xml, "First paragraph"
|
||||
assert_includes xml, "Second paragraph"
|
||||
assert_includes xml, "Third paragraph"
|
||||
assert_equal 3, xml.scan("<w:p>").count
|
||||
end
|
||||
|
||||
def test_paragraph_with_block_and_multiple_text_runs
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.p do
|
||||
doc.text "Part one "
|
||||
doc.text "Part two "
|
||||
doc.text "Part three"
|
||||
end
|
||||
end
|
||||
|
||||
assert_includes xml, "Part one "
|
||||
assert_includes xml, "Part two "
|
||||
assert_includes xml, "Part three"
|
||||
assert_equal 3, xml.scan("<w:r>").count
|
||||
end
|
||||
|
||||
def test_paragraph_preserves_whitespace
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.p do
|
||||
doc.text " leading spaces"
|
||||
doc.text "trailing spaces "
|
||||
doc.text " both "
|
||||
end
|
||||
end
|
||||
|
||||
assert_includes xml, 'xml:space="preserve"'
|
||||
assert_includes xml, " leading spaces"
|
||||
assert_includes xml, "trailing spaces "
|
||||
assert_includes xml, " both "
|
||||
end
|
||||
|
||||
def test_paragraph_with_special_characters
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.p "Special chars: <>&\"'"
|
||||
doc.p "Unicode: café, naïve, 日本語"
|
||||
end
|
||||
|
||||
# XML should escape special characters
|
||||
assert_includes xml, "<"
|
||||
assert_includes xml, ">"
|
||||
assert_includes xml, "&"
|
||||
assert_includes xml, "café"
|
||||
assert_includes xml, "日本語"
|
||||
end
|
||||
|
||||
def test_empty_paragraph_with_block
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.p {} # rubocop:disable Lint/EmptyBlock
|
||||
end
|
||||
|
||||
# Empty paragraph may be self-closing or have opening tag
|
||||
assert(xml.include?("<w:p>") || xml.include?("<w:p/>"), "Should contain paragraph element")
|
||||
end
|
||||
|
||||
def test_long_text_content
|
||||
long_text = "x" * 10_000
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.p long_text
|
||||
end
|
||||
|
||||
assert_includes xml, long_text
|
||||
end
|
||||
|
||||
def test_unicode_content
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.p "Emoji: 🎉🚀💻"
|
||||
doc.p "Chinese: 你好世界"
|
||||
doc.p "Arabic: مرحبا بالعالم"
|
||||
doc.p "Russian: Привет мир"
|
||||
end
|
||||
|
||||
assert_includes xml, "🎉🚀💻"
|
||||
assert_includes xml, "你好世界"
|
||||
assert_includes xml, "مرحبا بالعالم"
|
||||
assert_includes xml, "Привет мир"
|
||||
end
|
||||
|
||||
def test_newlines_in_text
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.p "Line 1\nLine 2\nLine 3"
|
||||
end
|
||||
|
||||
# Newlines should be preserved in the text
|
||||
assert_includes xml, "Line 1\nLine 2\nLine 3"
|
||||
end
|
||||
end
|
||||
137
test/table_test.rb
Normal file
137
test/table_test.rb
Normal file
@@ -0,0 +1,137 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "test_helper"
|
||||
|
||||
class TableTest < Minitest::Test
|
||||
include EzdocTestHelpers
|
||||
|
||||
def test_simple_table
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.table do
|
||||
doc.tr do
|
||||
doc.td "Cell 1"
|
||||
doc.td "Cell 2"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
assert_includes xml, "<w:tbl>"
|
||||
assert_includes xml, "<w:tr>"
|
||||
assert_includes xml, "<w:tc>"
|
||||
assert_includes xml, "Cell 1"
|
||||
assert_includes xml, "Cell 2"
|
||||
end
|
||||
|
||||
def test_table_has_borders
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.table do
|
||||
doc.tr { doc.td "Cell" }
|
||||
end
|
||||
end
|
||||
|
||||
assert_includes xml, "<w:tblBorders>"
|
||||
assert_includes xml, "<w:top"
|
||||
assert_includes xml, "<w:bottom"
|
||||
assert_includes xml, "<w:left"
|
||||
assert_includes xml, "<w:right"
|
||||
assert_includes xml, "<w:insideH"
|
||||
assert_includes xml, "<w:insideV"
|
||||
end
|
||||
|
||||
def test_table_multiple_rows
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.table do
|
||||
doc.tr do
|
||||
doc.td "R1C1"
|
||||
doc.td "R1C2"
|
||||
end
|
||||
doc.tr do
|
||||
doc.td "R2C1"
|
||||
doc.td "R2C2"
|
||||
end
|
||||
doc.tr do
|
||||
doc.td "R3C1"
|
||||
doc.td "R3C2"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
assert_equal 3, xml.scan("<w:tr>").count
|
||||
assert_equal 6, xml.scan("<w:tc>").count
|
||||
assert_includes xml, "R1C1"
|
||||
assert_includes xml, "R3C2"
|
||||
end
|
||||
|
||||
def test_table_with_formatted_cells
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.table do
|
||||
doc.tr do
|
||||
doc.td { doc.b { doc.text "Bold" } }
|
||||
doc.td { doc.i { doc.text "Italic" } }
|
||||
doc.td { doc.u { doc.text "Underline" } }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
assert_includes xml, "Bold"
|
||||
assert_includes xml, "Italic"
|
||||
assert_includes xml, "Underline"
|
||||
assert_includes xml, "<w:b/>"
|
||||
assert_includes xml, "<w:i/>"
|
||||
assert_includes xml, '<w:u w:val="single"/>'
|
||||
end
|
||||
|
||||
def test_table_with_mixed_content_cells
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.table do
|
||||
doc.tr do
|
||||
doc.td do
|
||||
doc.text "Normal "
|
||||
doc.b { doc.text "bold" }
|
||||
doc.text " normal"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
assert_includes xml, "Normal "
|
||||
assert_includes xml, "bold"
|
||||
assert_includes xml, " normal"
|
||||
end
|
||||
|
||||
def test_multiple_tables
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.table do
|
||||
doc.tr { doc.td "Table 1" }
|
||||
end
|
||||
doc.p "Between tables"
|
||||
doc.table do
|
||||
doc.tr { doc.td "Table 2" }
|
||||
end
|
||||
end
|
||||
|
||||
assert_equal 2, xml.scan("<w:tbl>").count
|
||||
assert_includes xml, "Table 1"
|
||||
assert_includes xml, "Table 2"
|
||||
assert_includes xml, "Between tables"
|
||||
end
|
||||
|
||||
def test_large_table
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.table do
|
||||
5.times do |row|
|
||||
doc.tr do
|
||||
5.times do |col|
|
||||
doc.td "R#{row}C#{col}"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
assert_equal 5, xml.scan("<w:tr>").count
|
||||
assert_equal 25, xml.scan("<w:tc>").count
|
||||
assert_includes xml, "R0C0"
|
||||
assert_includes xml, "R4C4"
|
||||
end
|
||||
end
|
||||
@@ -3,3 +3,33 @@
|
||||
$LOAD_PATH.unshift File.expand_path("../lib", __dir__)
|
||||
require "ezdoc"
|
||||
require "minitest/autorun"
|
||||
require "tempfile"
|
||||
require "zip"
|
||||
|
||||
module EzdocTestHelpers
|
||||
# Helper to create a document and return the document.xml content
|
||||
def create_doc_and_read_xml(&block)
|
||||
content = nil
|
||||
Tempfile.create(["test", ".docx"]) do |file|
|
||||
Ezdoc::Document.create(file.path, &block)
|
||||
Zip::File.open(file.path) do |zip|
|
||||
content = zip.read("word/document.xml").force_encoding("UTF-8")
|
||||
end
|
||||
end
|
||||
content
|
||||
end
|
||||
|
||||
# Helper to create a document and return all XML files
|
||||
def create_doc_and_read_all_xml(&block)
|
||||
result = {}
|
||||
Tempfile.create(["test", ".docx"]) do |file|
|
||||
Ezdoc::Document.create(file.path, &block)
|
||||
Zip::File.open(file.path) do |zip|
|
||||
zip.each do |entry|
|
||||
result[entry.name] = zip.read(entry.name).force_encoding("UTF-8") if entry.name.end_with?(".xml")
|
||||
end
|
||||
end
|
||||
end
|
||||
result
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user