Sablon.Test.Assertions (sablon v0.4.3)

Copy Markdown View Source

Helpers for asserting on generated documents.

Comparing two .docx files byte for byte is not useful: the archives differ in timestamps and compression, and equivalent XML can be written in several ways. These helpers compare the parts of a package instead, canonicalising the XML ones first.

import Sablon.Test.Assertions

test "renders the invoice" do
  Sablon.render_to_file(template, "tmp/invoice.docx", context)
  assert_docx_equal("test/fixtures/invoice_sample.docx", "tmp/invoice.docx")
end

Summary

Functions

Raises unless the two .docx files hold the same content.

Rewrites XML into a canonical form: indentation between elements is dropped, attributes are sorted by name, line endings are normalised and the prolog is ignored. What remains is what Word actually sees.

Returns the names of the parts that differ between two .docx files, along with the canonical form of each side.

Functions

assert_docx_equal(expected_path, actual_path)

@spec assert_docx_equal(Path.t(), Path.t()) :: true

Raises unless the two .docx files hold the same content.

The failure message names every part that differs, so it is clear whether a change is in the body, a header or the relationships.

canonicalize(xml)

@spec canonicalize(binary() | Sablon.XML.Document.t() | Sablon.XML.Node.t()) ::
  binary()

Rewrites XML into a canonical form: indentation between elements is dropped, attributes are sorted by name, line endings are normalised and the prolog is ignored. What remains is what Word actually sees.

docx_differences(expected_path, actual_path)

@spec docx_differences(Path.t(), Path.t()) :: [{binary(), binary(), binary()}]

Returns the names of the parts that differ between two .docx files, along with the canonical form of each side.

Only XML parts are compared textually; other parts, images for instance, are compared byte for byte.