All checks were successful
CI Pipeline / build (pull_request) Successful in 12s
Adds these new styling and formatting nodes * strike * highlight * linebreaks * pagebreaks * Hyperlinks
224 lines
6.3 KiB
Ruby
224 lines
6.3 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require "test_helper"
|
|
|
|
class StyleTest < Minitest::Test
|
|
include NotareTestHelpers
|
|
|
|
def test_define_custom_style
|
|
xml_files = create_doc_and_read_all_xml do |doc|
|
|
doc.define_style :warning, bold: true, color: "FF0000"
|
|
doc.p "Test", style: :warning
|
|
end
|
|
|
|
styles_xml = xml_files["word/styles.xml"]
|
|
assert_includes styles_xml, 'w:styleId="Warning"'
|
|
assert_includes styles_xml, "<w:b/>"
|
|
assert_includes styles_xml, 'w:val="FF0000"'
|
|
end
|
|
|
|
def test_apply_style_to_paragraph
|
|
xml = create_doc_and_read_xml do |doc|
|
|
doc.p "Quote text", style: :quote
|
|
end
|
|
|
|
assert_includes xml, '<w:pStyle w:val="Quote"'
|
|
assert_includes xml, "Quote text"
|
|
end
|
|
|
|
def test_apply_style_to_text_run
|
|
xml = create_doc_and_read_xml do |doc|
|
|
doc.p do
|
|
doc.text "Normal "
|
|
doc.text "code", style: :code
|
|
end
|
|
end
|
|
|
|
assert_includes xml, '<w:rStyle w:val="Code"'
|
|
assert_includes xml, "code"
|
|
end
|
|
|
|
def test_built_in_styles_exist
|
|
xml_files = create_doc_and_read_all_xml do |doc|
|
|
doc.p "Test"
|
|
end
|
|
|
|
styles_xml = xml_files["word/styles.xml"]
|
|
assert_includes styles_xml, 'w:styleId="Heading1"'
|
|
assert_includes styles_xml, 'w:styleId="Heading2"'
|
|
assert_includes styles_xml, 'w:styleId="Title"'
|
|
assert_includes styles_xml, 'w:styleId="Subtitle"'
|
|
assert_includes styles_xml, 'w:styleId="Quote"'
|
|
assert_includes styles_xml, 'w:styleId="Code"'
|
|
end
|
|
|
|
def test_style_with_color
|
|
xml_files = create_doc_and_read_all_xml do |doc|
|
|
doc.define_style :red_text, color: "FF0000"
|
|
doc.p "Red", style: :red_text
|
|
end
|
|
|
|
styles_xml = xml_files["word/styles.xml"]
|
|
assert_includes styles_xml, 'w:val="FF0000"'
|
|
end
|
|
|
|
def test_style_with_font
|
|
xml_files = create_doc_and_read_all_xml do |doc|
|
|
doc.define_style :mono, font: "Courier New"
|
|
doc.p "Mono", style: :mono
|
|
end
|
|
|
|
styles_xml = xml_files["word/styles.xml"]
|
|
assert_includes styles_xml, 'w:ascii="Courier New"'
|
|
end
|
|
|
|
def test_style_with_size
|
|
xml_files = create_doc_and_read_all_xml do |doc|
|
|
doc.define_style :big, size: 24
|
|
doc.p "Big", style: :big
|
|
end
|
|
|
|
styles_xml = xml_files["word/styles.xml"]
|
|
# 24pt = 48 half-points
|
|
assert_includes styles_xml, 'w:val="48"'
|
|
end
|
|
|
|
def test_style_with_alignment
|
|
xml_files = create_doc_and_read_all_xml do |doc|
|
|
doc.define_style :centered, align: :center
|
|
doc.p "Centered", style: :centered
|
|
end
|
|
|
|
styles_xml = xml_files["word/styles.xml"]
|
|
assert_includes styles_xml, '<w:jc w:val="center"'
|
|
end
|
|
|
|
def test_styles_xml_generated
|
|
xml_files = create_doc_and_read_all_xml do |doc|
|
|
doc.p "Test"
|
|
end
|
|
|
|
assert xml_files["word/styles.xml"], "styles.xml should be generated"
|
|
end
|
|
|
|
def test_styles_xml_in_content_types
|
|
xml_files = create_doc_and_read_all_xml do |doc|
|
|
doc.p "Test"
|
|
end
|
|
|
|
content_types = xml_files["[Content_Types].xml"]
|
|
assert_includes content_types, "/word/styles.xml"
|
|
assert_includes content_types, "wordprocessingml.styles+xml"
|
|
end
|
|
|
|
def test_styles_xml_in_relationships
|
|
xml_files = create_doc_and_read_all_xml do |doc|
|
|
doc.p "Test"
|
|
end
|
|
|
|
rels = xml_files["word/_rels/document.xml.rels"]
|
|
assert_includes rels, "styles.xml"
|
|
assert_includes rels, "relationships/styles"
|
|
end
|
|
|
|
def test_unknown_style_raises_error
|
|
assert_raises(ArgumentError) do
|
|
Tempfile.create(["test", ".docx"]) do |file|
|
|
Notare::Document.create(file.path) do |doc|
|
|
doc.p "Test", style: :nonexistent
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
def test_invalid_color_raises_error
|
|
assert_raises(ArgumentError) do
|
|
Notare::Style.new(:bad, color: "invalid")
|
|
end
|
|
end
|
|
|
|
def test_invalid_alignment_raises_error
|
|
assert_raises(ArgumentError) do
|
|
Notare::Style.new(:bad, align: :invalid)
|
|
end
|
|
end
|
|
|
|
def test_color_normalizes_hash
|
|
style = Notare::Style.new(:test, color: "#ff0000")
|
|
assert_equal "FF0000", style.color
|
|
end
|
|
|
|
def test_combined_style_properties
|
|
xml_files = create_doc_and_read_all_xml do |doc|
|
|
doc.define_style :fancy,
|
|
bold: true,
|
|
italic: true,
|
|
color: "0000FF",
|
|
size: 16,
|
|
font: "Arial",
|
|
align: :center
|
|
doc.p "Fancy", style: :fancy
|
|
end
|
|
|
|
styles_xml = xml_files["word/styles.xml"]
|
|
assert_includes styles_xml, "<w:b/>"
|
|
assert_includes styles_xml, "<w:i/>"
|
|
assert_includes styles_xml, 'w:val="0000FF"'
|
|
assert_includes styles_xml, 'w:val="32"' # 16pt = 32 half-points
|
|
assert_includes styles_xml, 'w:ascii="Arial"'
|
|
assert_includes styles_xml, '<w:jc w:val="center"'
|
|
end
|
|
|
|
def test_style_with_highlight
|
|
xml_files = create_doc_and_read_all_xml do |doc|
|
|
doc.define_style :highlighted, highlight: "yellow"
|
|
doc.p "Highlighted", style: :highlighted
|
|
end
|
|
|
|
styles_xml = xml_files["word/styles.xml"]
|
|
assert_includes styles_xml, '<w:highlight w:val="yellow"'
|
|
end
|
|
|
|
def test_style_with_strikethrough
|
|
xml_files = create_doc_and_read_all_xml do |doc|
|
|
doc.define_style :deleted, strike: true
|
|
doc.p "Deleted", style: :deleted
|
|
end
|
|
|
|
styles_xml = xml_files["word/styles.xml"]
|
|
assert_includes styles_xml, "<w:strike/>"
|
|
end
|
|
|
|
def test_invalid_highlight_raises_error
|
|
assert_raises(ArgumentError) do
|
|
Notare::Style.new(:bad, highlight: "invalid_color")
|
|
end
|
|
end
|
|
|
|
def test_valid_highlight_colors
|
|
# Test a few valid highlight colors
|
|
%w[yellow red blue green cyan magenta].each do |color|
|
|
style = Notare::Style.new(:test, highlight: color)
|
|
assert_equal color, style.highlight
|
|
end
|
|
end
|
|
|
|
def test_highlight_in_text_run
|
|
xml_files = create_doc_and_read_all_xml do |doc|
|
|
doc.define_style :yellow_highlight, highlight: "yellow"
|
|
doc.p do
|
|
doc.text "Normal "
|
|
doc.text "highlighted", style: :yellow_highlight
|
|
end
|
|
end
|
|
|
|
# Highlight is in the style definition, not inline when using style reference
|
|
styles_xml = xml_files["word/styles.xml"]
|
|
assert_includes styles_xml, '<w:highlight w:val="yellow"'
|
|
|
|
document_xml = xml_files["word/document.xml"]
|
|
assert_includes document_xml, '<w:rStyle w:val="YellowHighlight"'
|
|
assert_includes document_xml, "highlighted"
|
|
end
|
|
end
|