Implement table styles
All checks were successful
CI Pipeline / build (pull_request) Successful in 14s

This commit is contained in:
2025-12-03 12:14:31 +01:00
parent 67a60c8c6e
commit 9a70d91fd5
12 changed files with 555 additions and 13 deletions

224
test/table_style_test.rb Normal file
View File

@@ -0,0 +1,224 @@
# frozen_string_literal: true
require "test_helper"
class TableStyleTest < Minitest::Test
include NotareTestHelpers
# --- TableStyle class tests ---
def test_table_style_id_generation
style = Notare::TableStyle.new(:my_table_style)
assert_equal "MyTableStyle", style.style_id
end
def test_table_style_display_name
style = Notare::TableStyle.new(:my_table_style)
assert_equal "My Table Style", style.display_name
end
def test_invalid_border_style_raises_error
assert_raises(ArgumentError) do
Notare::TableStyle.new(:bad, borders: { style: "invalid" })
end
end
def test_invalid_color_raises_error
assert_raises(ArgumentError) do
Notare::TableStyle.new(:bad, shading: "invalid")
end
end
def test_color_normalizes_hash_prefix
style = Notare::TableStyle.new(:test, shading: "#ff0000")
assert_equal "FF0000", style.shading
end
def test_invalid_alignment_raises_error
assert_raises(ArgumentError) do
Notare::TableStyle.new(:bad, align: :invalid)
end
end
# --- Document registration tests ---
def test_define_table_style
xml_files = create_doc_and_read_all_xml do |doc|
doc.define_table_style :custom, borders: { style: "double", color: "FF0000", size: 8 }
doc.table(style: :custom) do
doc.tr { doc.td "Test" }
end
end
styles_xml = xml_files["word/styles.xml"]
assert_includes styles_xml, 'w:styleId="Custom"'
assert_includes styles_xml, 'w:type="table"'
assert_includes styles_xml, 'w:val="double"'
assert_includes styles_xml, 'w:color="FF0000"'
end
def test_unknown_table_style_raises_error
assert_raises(ArgumentError) do
Tempfile.create(["test", ".docx"]) do |file|
Notare::Document.create(file.path) do |doc|
doc.table(style: :nonexistent) { doc.tr { doc.td "Test" } }
end
end
end
end
# --- Style application tests ---
def test_table_with_style_reference
xml = create_doc_and_read_xml do |doc|
doc.define_table_style :bordered, borders: { style: "single", color: "000000", size: 4 }
doc.table(style: :bordered) do
doc.tr { doc.td "Cell" }
end
end
assert_includes xml, '<w:tblStyle w:val="Bordered"'
end
def test_table_without_style_uses_default_borders
xml = create_doc_and_read_xml do |doc|
doc.table do
doc.tr { doc.td "Cell" }
end
end
assert_includes xml, "<w:tblBorders>"
refute_includes xml, "<w:tblStyle"
end
def test_borderless_table_style
xml_files = create_doc_and_read_all_xml do |doc|
doc.define_table_style :no_borders, borders: :none
doc.table(style: :no_borders) do
doc.tr { doc.td "Cell" }
end
end
styles_xml = xml_files["word/styles.xml"]
assert_includes styles_xml, 'w:val="nil"'
end
def test_table_style_with_shading
xml_files = create_doc_and_read_all_xml do |doc|
doc.define_table_style :shaded, shading: "EEEEEE"
doc.table(style: :shaded) do
doc.tr { doc.td "Cell" }
end
end
styles_xml = xml_files["word/styles.xml"]
assert_includes styles_xml, 'w:fill="EEEEEE"'
end
def test_table_style_with_cell_margins
xml_files = create_doc_and_read_all_xml do |doc|
doc.define_table_style :padded, cell_margins: 100
doc.table(style: :padded) do
doc.tr { doc.td "Cell" }
end
end
styles_xml = xml_files["word/styles.xml"]
assert_includes styles_xml, "<w:tblCellMar>"
assert_includes styles_xml, 'w:w="100"'
end
def test_table_style_with_alignment
xml_files = create_doc_and_read_all_xml do |doc|
doc.define_table_style :centered, align: :center
doc.table(style: :centered) do
doc.tr { doc.td "Cell" }
end
end
styles_xml = xml_files["word/styles.xml"]
assert_includes styles_xml, '<w:jc w:val="center"'
end
def test_combined_table_style_properties
xml_files = create_doc_and_read_all_xml do |doc|
doc.define_table_style :fancy,
borders: { style: "double", color: "0000FF", size: 8 },
shading: "F0F0F0",
cell_margins: { top: 50, bottom: 50, left: 100, right: 100 },
align: :center
doc.table(style: :fancy) do
doc.tr { doc.td "Fancy" }
end
end
styles_xml = xml_files["word/styles.xml"]
assert_includes styles_xml, 'w:styleId="Fancy"'
assert_includes styles_xml, 'w:val="double"'
assert_includes styles_xml, 'w:fill="F0F0F0"'
assert_includes styles_xml, '<w:jc w:val="center"'
assert_includes styles_xml, "<w:tblCellMar>"
end
def test_per_edge_borders
xml_files = create_doc_and_read_all_xml do |doc|
doc.define_table_style :mixed_borders,
borders: {
top: { style: "double", color: "FF0000", size: 8 },
bottom: { style: "single", color: "000000", size: 4 },
left: { style: "none" },
right: { style: "none" },
insideH: { style: "dotted", color: "CCCCCC", size: 2 },
insideV: { style: "dotted", color: "CCCCCC", size: 2 }
}
doc.table(style: :mixed_borders) do
doc.tr { doc.td "Cell" }
end
end
styles_xml = xml_files["word/styles.xml"]
assert_includes styles_xml, '<w:top w:val="double"'
assert_includes styles_xml, '<w:bottom w:val="single"'
assert_includes styles_xml, '<w:left w:val="nil"'
end
# --- Built-in styles tests ---
def test_built_in_grid_style_exists
xml_files = create_doc_and_read_all_xml do |doc|
doc.table(style: :grid) do
doc.tr { doc.td "Test" }
end
end
styles_xml = xml_files["word/styles.xml"]
assert_includes styles_xml, 'w:styleId="Grid"'
end
def test_built_in_borderless_style_exists
xml_files = create_doc_and_read_all_xml do |doc|
doc.table(style: :borderless) do
doc.tr { doc.td "Test" }
end
end
styles_xml = xml_files["word/styles.xml"]
assert_includes styles_xml, 'w:styleId="Borderless"'
end
def test_cell_margins_as_hash
xml_files = create_doc_and_read_all_xml do |doc|
doc.define_table_style :asymmetric_padding,
cell_margins: { top: 100, bottom: 200, left: 150, right: 150 }
doc.table(style: :asymmetric_padding) do
doc.tr { doc.td "Cell" }
end
end
styles_xml = xml_files["word/styles.xml"]
assert_includes styles_xml, "<w:tblCellMar>"
assert_match(/<w:top[^>]*w:w="100"/, styles_xml)
assert_match(/<w:bottom[^>]*w:w="200"/, styles_xml)
end
end