Files
Notare/test/table_test.rb
mathias234 843466549a
All checks were successful
CI Pipeline / build (pull_request) Successful in 13s
Support table and table column sizing
2025-12-03 13:50:56 +01:00

304 lines
7.5 KiB
Ruby

# frozen_string_literal: true
require "test_helper"
class TableTest < Minitest::Test
include NotareTestHelpers
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
def test_table_with_auto_layout
xml = create_doc_and_read_xml do |doc|
doc.table(layout: :auto) do
doc.tr do
doc.td "Short"
doc.td "Much longer content here"
end
end
end
assert_includes xml, '<w:tblLayout w:type="autofit"/>'
assert_includes xml, '<w:tblW w:w="0" w:type="auto"/>'
assert_includes xml, '<w:tcW w:w="0" w:type="auto"/>'
end
def test_table_with_fixed_layout
xml = create_doc_and_read_xml do |doc|
doc.table(layout: :fixed) do
doc.tr do
doc.td "Cell 1"
doc.td "Cell 2"
end
end
end
assert_includes xml, '<w:tblLayout w:type="fixed"/>'
end
def test_table_with_explicit_column_widths_in_inches
xml = create_doc_and_read_xml do |doc|
doc.table(columns: %w[2in 3in]) do
doc.tr do
doc.td "A"
doc.td "B"
end
end
end
# 2 inches = 2880 twips, 3 inches = 4320 twips
assert_includes xml, '<w:tblW w:w="7200" w:type="dxa"/>'
assert_includes xml, '<w:tcW w:w="2880" w:type="dxa"/>'
assert_includes xml, '<w:tcW w:w="4320" w:type="dxa"/>'
end
def test_table_with_percentage_columns
xml = create_doc_and_read_xml do |doc|
doc.table(columns: ["25%", "75%"]) do
doc.tr do
doc.td "A"
doc.td "B"
end
end
end
# 25% = 1250 (fiftieths), 75% = 3750 (fiftieths)
assert_includes xml, '<w:tblW w:w="5000" w:type="pct"/>'
assert_includes xml, '<w:tcW w:w="1250" w:type="pct"/>'
assert_includes xml, '<w:tcW w:w="3750" w:type="pct"/>'
end
def test_table_with_centimeter_columns
xml = create_doc_and_read_xml do |doc|
doc.table(columns: ["2.54cm", "5cm"]) do
doc.tr do
doc.td "A"
doc.td "B"
end
end
end
# 2.54cm = ~1440 twips (1 inch), 5cm = ~2835 twips
assert_includes xml, '<w:tcW w:w="1440" w:type="dxa"/>'
assert_includes xml, '<w:tcW w:w="2835" w:type="dxa"/>'
end
def test_cell_with_explicit_width
xml = create_doc_and_read_xml do |doc|
doc.table do
doc.tr do
doc.td("Narrow", width: "1in")
doc.td("Wide", width: "4in")
end
end
end
# 1 inch = 1440 twips, 4 inches = 5760 twips
assert_includes xml, '<w:tcW w:w="1440" w:type="dxa"/>'
assert_includes xml, '<w:tcW w:w="5760" w:type="dxa"/>'
end
def test_cell_width_with_block
xml = create_doc_and_read_xml do |doc|
doc.table do
doc.tr do
doc.td(width: "2in") { doc.b { doc.text "Bold content" } }
end
end
end
assert_includes xml, '<w:tcW w:w="2880" w:type="dxa"/>'
assert_includes xml, "Bold content"
assert_includes xml, "<w:b/>"
end
def test_mixed_cell_widths_explicit_and_auto
xml = create_doc_and_read_xml do |doc|
doc.table do
doc.tr do
doc.td("Fixed", width: "2in")
doc.td "Auto"
end
end
end
# First cell has explicit width, second gets auto
assert_includes xml, '<w:tcW w:w="2880" w:type="dxa"/>'
assert_includes xml, '<w:tcW w:w="0" w:type="auto"/>'
assert_includes xml, '<w:tblW w:w="0" w:type="auto"/>'
end
def test_table_columns_override_cell_widths
xml = create_doc_and_read_xml do |doc|
doc.table(columns: %w[3in 3in]) do
doc.tr do
doc.td("A", width: "1in") # This width is ignored
doc.td "B"
end
end
end
# Table-level columns take precedence
assert_includes xml, '<w:tcW w:w="4320" w:type="dxa"/>'
refute_includes xml, '<w:tcW w:w="1440" w:type="dxa"/>'
end
def test_table_layout_with_columns
xml = create_doc_and_read_xml do |doc|
doc.table(layout: :fixed, columns: %w[2in 2in 2in]) do
doc.tr do
doc.td "A"
doc.td "B"
doc.td "C"
end
end
end
assert_includes xml, '<w:tblLayout w:type="fixed"/>'
assert_includes xml, '<w:tcW w:w="2880" w:type="dxa"/>'
end
def test_default_behavior_unchanged
xml = create_doc_and_read_xml do |doc|
doc.table do
doc.tr do
doc.td "A"
doc.td "B"
end
end
end
# Default: equal percentage widths (5000 / 2 = 2500 per cell)
assert_includes xml, '<w:tblW w:w="5000" w:type="pct"/>'
assert_includes xml, '<w:tcW w:w="2500" w:type="pct"/>'
refute_includes xml, "<w:tblLayout" # No layout element by default
end
end