# 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, "" assert_includes xml, "" assert_includes xml, "" 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, "" assert_includes xml, "").count assert_equal 6, xml.scan("").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, "" assert_includes xml, "" assert_includes xml, '' 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("").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("").count assert_equal 25, xml.scan("").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, '' assert_includes xml, '' assert_includes xml, '' 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, '' 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, '' assert_includes xml, '' assert_includes xml, '' 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, '' assert_includes xml, '' assert_includes xml, '' 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, '' assert_includes xml, '' 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, '' assert_includes xml, '' 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, '' assert_includes xml, "Bold content" assert_includes xml, "" 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, '' assert_includes xml, '' assert_includes xml, '' 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, '' refute_includes xml, '' 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, '' assert_includes xml, '' 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, '' assert_includes xml, '' refute_includes xml, "