# frozen_string_literal: true
require "test_helper"
class ListTest < Minitest::Test
include NotareTestHelpers
#
# Bullet List Tests
#
def test_bullet_list_basic
xml_files = create_doc_and_read_all_xml do |doc|
doc.ul do
doc.li "Item 1"
doc.li "Item 2"
end
end
doc_xml = xml_files["word/document.xml"]
assert_includes doc_xml, "Item 1"
assert_includes doc_xml, "Item 2"
assert_includes doc_xml, ""
assert_includes xml, ""
end
#
# Numbered List Tests
#
def test_numbered_list_basic
xml_files = create_doc_and_read_all_xml do |doc|
doc.ol do
doc.li "First"
doc.li "Second"
doc.li "Third"
end
end
doc_xml = xml_files["word/document.xml"]
assert_includes doc_xml, "First"
assert_includes doc_xml, "Second"
assert_includes doc_xml, "Third"
numbering_xml = xml_files["word/numbering.xml"]
assert_includes numbering_xml, "decimal"
end
def test_numbered_list_with_formatted_items
xml = create_doc_and_read_xml do |doc|
doc.ol do
doc.li { doc.b { doc.text "Bold numbered" } }
doc.li "Plain numbered"
end
end
assert_includes xml, "Bold numbered"
assert_includes xml, "Plain numbered"
assert_includes xml, ""
end
#
# Multiple Lists Tests
#
def test_multiple_bullet_lists
xml_files = create_doc_and_read_all_xml do |doc|
doc.ul do
doc.li "List 1 Item 1"
doc.li "List 1 Item 2"
end
doc.p "Separator paragraph"
doc.ul do
doc.li "List 2 Item 1"
doc.li "List 2 Item 2"
end
end
doc_xml = xml_files["word/document.xml"]
assert_includes doc_xml, "List 1 Item 1"
assert_includes doc_xml, "List 2 Item 1"
assert_includes doc_xml, "Separator paragraph"
end
def test_mixed_list_types
xml_files = create_doc_and_read_all_xml do |doc|
doc.ul do
doc.li "Bullet 1"
doc.li "Bullet 2"
end
doc.ol do
doc.li "Number 1"
doc.li "Number 2"
end
end
numbering_xml = xml_files["word/numbering.xml"]
assert_includes numbering_xml, "bullet"
assert_includes numbering_xml, "decimal"
end
def test_document_without_lists_has_no_numbering_xml
xml_files = create_doc_and_read_all_xml do |doc|
doc.p "Just a paragraph"
doc.table do
doc.tr { doc.td "Cell" }
end
end
refute xml_files.key?("word/numbering.xml"), "numbering.xml should not exist without lists"
end
#
# Nested List Tests
#
def test_nested_bullet_list
xml = create_doc_and_read_xml do |doc|
doc.ul do
doc.li "Parent item"
doc.li "Item with nested list" do
doc.ul do
doc.li "Nested item 1"
doc.li "Nested item 2"
end
end
doc.li "Another parent item"
end
end
assert_includes xml, "Parent item"
assert_includes xml, "Nested item 1"
assert_includes xml, "Nested item 2"
assert_includes xml, "Another parent item"
# Check for level 0 and level 1
assert_includes xml, 'w:ilvl w:val="0"'
assert_includes xml, 'w:ilvl w:val="1"'
end
def test_nested_numbered_list
xml = create_doc_and_read_xml do |doc|
doc.ol do
doc.li "First"
doc.li "Second with nested" do
doc.ol do
doc.li "Nested 1"
doc.li "Nested 2"
end
end
doc.li "Third"
end
end
assert_includes xml, "First"
assert_includes xml, "Nested 1"
assert_includes xml, "Third"
assert_includes xml, 'w:ilvl w:val="0"'
assert_includes xml, 'w:ilvl w:val="1"'
end
def test_mixed_nested_list
xml_files = create_doc_and_read_all_xml do |doc|
doc.ol do
doc.li "Numbered item 1"
doc.li "Numbered item 2" do
doc.ul do
doc.li "Bullet inside numbered"
end
end
doc.li "Numbered item 3"
end
end
doc_xml = xml_files["word/document.xml"]
assert_includes doc_xml, "Numbered item 1"
assert_includes doc_xml, "Bullet inside numbered"
assert_includes doc_xml, "Numbered item 3"
assert_includes doc_xml, 'w:ilvl w:val="0"'
assert_includes doc_xml, 'w:ilvl w:val="1"'
end
def test_deeply_nested_list
xml = create_doc_and_read_xml do |doc|
doc.ul do
doc.li "Level 0"
doc.li "Has nested" do
doc.ul do
doc.li "Level 1"
doc.li "Has deeper nested" do
doc.ul do
doc.li "Level 2"
end
end
end
end
end
end
assert_includes xml, "Level 0"
assert_includes xml, "Level 1"
assert_includes xml, "Level 2"
assert_includes xml, 'w:ilvl w:val="0"'
assert_includes xml, 'w:ilvl w:val="1"'
assert_includes xml, 'w:ilvl w:val="2"'
end
end