Skip to content

Commit

Permalink
Extract output test helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
camelpunch committed Oct 31, 2024
1 parent 48f9087 commit 0e59ed5
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 57 deletions.
52 changes: 1 addition & 51 deletions test/mudbrick/text_block_test.exs
Original file line number Diff line number Diff line change
@@ -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 =
Expand Down Expand Up @@ -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
6 changes: 0 additions & 6 deletions test/mudbrick_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -399,10 +399,4 @@ defmodule MudbrickTest do
"<xmpMM:\\1ID>0000000000000000000000000000000000000000000</xmpMM:\\2ID>"
)
end

def output(chain) do
tap(chain, fn rendered ->
File.write("test.pdf", rendered)
end)
end
end
46 changes: 46 additions & 0 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()

0 comments on commit 0e59ed5

Please sign in to comment.