From 0e59ed55b7fb97834a3ef44c5f94c74aac690aa0 Mon Sep 17 00:00:00 2001 From: Andrew Bruce Date: Thu, 31 Oct 2024 15:08:09 +0000 Subject: [PATCH] Extract output test helpers --- test/mudbrick/text_block_test.exs | 52 +------------------------------ test/mudbrick_test.exs | 6 ---- test/test_helper.exs | 46 +++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 57 deletions(-) diff --git a/test/mudbrick/text_block_test.exs b/test/mudbrick/text_block_test.exs index adbbc0b..586ef8f 100644 --- a/test/mudbrick/text_block_test.exs +++ b/test/mudbrick/text_block_test.exs @@ -1,18 +1,11 @@ defmodule Mudbrick.TextBlockTest do use ExUnit.Case, async: true - import Mudbrick.TestHelper, - only: [ - bodoni_regular: 0, - bodoni_bold: 0, - franklin_regular: 0 - ] + import Mudbrick.TestHelper, only: [output: 1] - alias Mudbrick.Page alias Mudbrick.TextBlock alias Mudbrick.TextBlock.Line alias Mudbrick.TextBlock.Line.Part - alias Mudbrick.TextBlock.Output test "single write is divided into lines" do block = @@ -293,47 +286,4 @@ defmodule Mudbrick.TextBlockTest do defp operations(ops) do Enum.map(ops, &Mudbrick.TestHelper.show/1) end - - defp output(f) when is_function(f) do - import Mudbrick - - {doc, _contents_obj} = - context = - Mudbrick.new( - title: "My thing", - compress: false, - fonts: %{ - a: [file: bodoni_regular()], - b: [file: bodoni_bold()], - c: [file: franklin_regular()] - } - ) - |> page(size: Page.size(:letter)) - - fonts = Mudbrick.Document.root_page_tree(doc).value.fonts - - block = - f.(%{ - fonts: %{ - regular: Map.fetch!(fonts, :a).value, - bold: Map.fetch!(fonts, :b).value, - franklin_regular: Map.fetch!(fonts, :c).value - } - }) - - ops = Output.from(block).operations - - context - |> Mudbrick.ContentStream.put(operations: ops) - |> render() - |> output() - - ops - end - - defp output(chain) do - tap(chain, fn rendered -> - File.write("test.pdf", rendered) - end) - end end diff --git a/test/mudbrick_test.exs b/test/mudbrick_test.exs index d60db69..b4641fd 100644 --- a/test/mudbrick_test.exs +++ b/test/mudbrick_test.exs @@ -399,10 +399,4 @@ defmodule MudbrickTest do "0000000000000000000000000000000000000000000" ) end - - def output(chain) do - tap(chain, fn rendered -> - File.write("test.pdf", rendered) - end) - end end diff --git a/test/test_helper.exs b/test/test_helper.exs index accd3f9..d22c88b 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -5,6 +5,9 @@ defmodule Mudbrick.TestHelper do @flower Path.join(__DIR__, "fixtures/JPEG_example_flower.jpg") |> File.read!() @example_png Path.join(__DIR__, "fixtures/Example.png") |> File.read!() + alias Mudbrick.Page + alias Mudbrick.TextBlock.Output + def show(o) do Mudbrick.Object.from(o) |> to_string() end @@ -34,6 +37,49 @@ defmodule Mudbrick.TestHelper do def example_png do @example_png end + + def output(f) when is_function(f) do + import Mudbrick + + {doc, _contents_obj} = + context = + Mudbrick.new( + title: "My thing", + compress: false, + fonts: %{ + a: [file: bodoni_regular()], + b: [file: bodoni_bold()], + c: [file: franklin_regular()] + } + ) + |> page(size: Page.size(:letter)) + + fonts = Mudbrick.Document.root_page_tree(doc).value.fonts + + block = + f.(%{ + fonts: %{ + regular: Map.fetch!(fonts, :a).value, + bold: Map.fetch!(fonts, :b).value, + franklin_regular: Map.fetch!(fonts, :c).value + } + }) + + ops = Output.from(block).operations + + context + |> Mudbrick.ContentStream.put(operations: ops) + |> render() + |> output() + + ops + end + + def output(chain) do + tap(chain, fn rendered -> + File.write("test.pdf", rendered) + end) + end end ExUnit.start()