Files
Notare/test/table_test.rb

138 lines
3.1 KiB
Ruby

# 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