From 42f11b573dafdf9961e529702419d88628f9fd72 Mon Sep 17 00:00:00 2001 From: Andrew Bruce Date: Thu, 31 Oct 2024 15:30:48 +0000 Subject: [PATCH] Vector drawing bootstrapping --- lib/mudbrick/drawing.ex | 18 ++++++++++++++++++ lib/mudbrick/drawing/output.ex | 7 +++++++ test/mudbrick/drawing_test.exs | 21 +++++++++++++++++++++ test/mudbrick/text_block_test.exs | 4 +++- test/test_helper.exs | 5 ++--- 5 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 lib/mudbrick/drawing.ex create mode 100644 lib/mudbrick/drawing/output.ex create mode 100644 test/mudbrick/drawing_test.exs diff --git a/lib/mudbrick/drawing.ex b/lib/mudbrick/drawing.ex new file mode 100644 index 0000000..c9ff4cb --- /dev/null +++ b/lib/mudbrick/drawing.ex @@ -0,0 +1,18 @@ +defmodule Mudbrick.Drawing do + defstruct lines: [] + + def new() do + struct!(__MODULE__, []) + end + + defmodule Path do + @enforce_keys [:from, :to] + defstruct [:from, :to] + + def new(drawing, opts) do + Map.update!(drawing, :lines, fn lines -> + [struct!(__MODULE__, opts) | lines] + end) + end + end +end diff --git a/lib/mudbrick/drawing/output.ex b/lib/mudbrick/drawing/output.ex new file mode 100644 index 0000000..de1a3f3 --- /dev/null +++ b/lib/mudbrick/drawing/output.ex @@ -0,0 +1,7 @@ +defmodule Mudbrick.Drawing.Output do + defstruct operations: [] + + def from(_) do + %__MODULE__{} + end +end diff --git a/test/mudbrick/drawing_test.exs b/test/mudbrick/drawing_test.exs new file mode 100644 index 0000000..c7fe078 --- /dev/null +++ b/test/mudbrick/drawing_test.exs @@ -0,0 +1,21 @@ +defmodule Mudbrick.DrawingTest do + use ExUnit.Case, async: true + + import Mudbrick.TestHelper, only: [output: 2] + + alias Mudbrick.Drawing + + test "can make an empty line drawing" do + assert [] = + output(fn -> + Drawing.new() + end) + |> operations() + end + + defp output(f), do: output(fn _ -> f.() end, Mudbrick.Drawing.Output) + + defp operations(ops) do + Enum.map(ops, &Mudbrick.TestHelper.show/1) + end +end diff --git a/test/mudbrick/text_block_test.exs b/test/mudbrick/text_block_test.exs index 586ef8f..441ae3e 100644 --- a/test/mudbrick/text_block_test.exs +++ b/test/mudbrick/text_block_test.exs @@ -1,7 +1,7 @@ defmodule Mudbrick.TextBlockTest do use ExUnit.Case, async: true - import Mudbrick.TestHelper, only: [output: 1] + import Mudbrick.TestHelper, only: [output: 2] alias Mudbrick.TextBlock alias Mudbrick.TextBlock.Line @@ -283,6 +283,8 @@ defmodule Mudbrick.TextBlockTest do end end + defp output(f), do: output(f, Mudbrick.TextBlock.Output) + defp operations(ops) do Enum.map(ops, &Mudbrick.TestHelper.show/1) end diff --git a/test/test_helper.exs b/test/test_helper.exs index d22c88b..2e8bce8 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -6,7 +6,6 @@ defmodule Mudbrick.TestHelper do @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() @@ -38,7 +37,7 @@ defmodule Mudbrick.TestHelper do @example_png end - def output(f) when is_function(f) do + def output(f, output_mod) when is_function(f) do import Mudbrick {doc, _contents_obj} = @@ -65,7 +64,7 @@ defmodule Mudbrick.TestHelper do } }) - ops = Output.from(block).operations + ops = output_mod.from(block).operations context |> Mudbrick.ContentStream.put(operations: ops)