# 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, '" refute_includes xml, "" 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, '" 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, '" assert_match(/]*w:w="100"/, styles_xml) assert_match(/]*w:w="200"/, styles_xml) end end