Files
Notare/test/document_test.rb

75 lines
2.1 KiB
Ruby

# frozen_string_literal: true
require "test_helper"
class DocumentTest < Minitest::Test
include EzdocTestHelpers
def test_creates_valid_docx_structure
Tempfile.create(["test", ".docx"]) do |file|
Ezdoc::Document.create(file.path) do |doc|
doc.p "Test"
end
assert File.exist?(file.path)
assert File.size(file.path).positive?
Zip::File.open(file.path) do |zip|
assert zip.find_entry("[Content_Types].xml"), "Missing [Content_Types].xml"
assert zip.find_entry("_rels/.rels"), "Missing _rels/.rels"
assert zip.find_entry("word/document.xml"), "Missing word/document.xml"
assert zip.find_entry("word/_rels/document.xml.rels"), "Missing word/_rels/document.xml.rels"
end
end
end
def test_document_xml_has_correct_namespaces
xml = create_doc_and_read_xml { |doc| doc.p "Test" }
assert_includes xml, 'xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"'
assert_includes xml, "<w:document"
assert_includes xml, "<w:body>"
end
def test_empty_document
Tempfile.create(["test", ".docx"]) do |file|
Ezdoc::Document.create(file.path) { |_doc| } # rubocop:disable Lint/EmptyBlock
assert File.exist?(file.path)
Zip::File.open(file.path) do |zip|
assert zip.find_entry("word/document.xml")
end
end
end
def test_document_create_returns_document
result = nil
Tempfile.create(["test", ".docx"]) do |file|
result = Ezdoc::Document.create(file.path) do |doc|
doc.p "Test"
end
end
assert_instance_of Ezdoc::Document, result
end
def test_document_has_nodes
doc = Ezdoc::Document.new
doc.p "Test"
assert_equal 1, doc.nodes.count
assert_instance_of Ezdoc::Nodes::Paragraph, doc.nodes.first
end
def test_document_lists_helper
doc = Ezdoc::Document.new
doc.p "Paragraph"
doc.ul { doc.li "Bullet" }
doc.ol { doc.li "Number" }
doc.table { doc.tr { doc.td "Cell" } }
assert_equal 2, doc.lists.count
assert(doc.lists.all? { |l| l.is_a?(Ezdoc::Nodes::List) })
end
end