Skip to content

Pattern generation: Generate patterns using record macros if available (Elixir 1.18+) #108

@zachallaun

Description

@zachallaun

Given a file point.ex containing:

defmodule Point do
  import Record

  defrecord :point, [:x, :y]
end

You can get information about the point record macro from the docs chunk:

# iex -S mix
iex> Code.fetch_docs(Point)
{:docs_v1, 2, :elixir, "text/markdown", :hidden,
 %{behaviours: [], source_annos: [{1, 1}], source_path: ~c"/home/zacha/dev/libs/mneme/lib/point.ex"},
 [
   {{:macro, :point, 1}, [generated: true, location: 5], ["point(args \\\\ [])"], :none,
    %{record: {:point, [x: nil, y: nil]}, source_annos: [5], defaults: 1}},
   {{:macro, :point, 2}, [generated: true, location: 5], ["point(record, args)"], :none,
    %{source_annos: [5]}}
 ]}

This information combined with the macro environment could be used to potentially generate patterns using record syntax:

defmodule PointTest do
  use ExUnit.Case
  use Mneme

  import Point

  test "add/2" do
    auto_assert point(x: 5, y: 5) <- Point.add(point(x: 1, y: 4), point(x: 4, y: 1))
  end
end

(Note that this auto-assert would currently succeed, but when the asserted value changed, Mneme would only offer tuple suggestions.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions