# frozen_string_literal: true require "test_helper" class DocumentTest < Minitest::Test include NotareTestHelpers def test_creates_valid_docx_structure Tempfile.create(["test", ".docx"]) do |file| Notare::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, "" end def test_empty_document Tempfile.create(["test", ".docx"]) do |file| Notare::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 = Notare::Document.create(file.path) do |doc| doc.p "Test" end end assert_instance_of Notare::Document, result end def test_document_has_nodes doc = Notare::Document.new doc.p "Test" assert_equal 1, doc.nodes.count assert_instance_of Notare::Nodes::Paragraph, doc.nodes.first end def test_document_lists_helper doc = Notare::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?(Notare::Nodes::List) }) end end