Elixir. How can you use an alias in a doctrine?

Is there a way to use module aliases in doctrines? I do not want to enter a long name every time.

defmodule SomeLongModuleName.SubModule do
  alias SomeLongModuleName.SubModule, as: SubModule

  @doc """
      iex> SubModule.method(%{property_a: 1, property_b: 2) # CompileError
      3
  """
  def method(%{property_a: a, property_b: b) do
    a + b
  end
end

The above example shows a situation where I can use an alias to avoid long lines. Is it even possible to use an alias in a doctrine?

+6
source share
4 answers

There are two ways I can imagine not to enter the module name again and again.

  • Use interpolation in your documents and use aliases:

    defmodule SomeLongModuleName.SubModule do
      alias SomeLongModuleName.SubModule, as: SubModule
    
      @doc """
          iex> #{SubModule}.method(%{property_a: 1, property_b: 2})
          3
      """
      def method(%{property_a: a, property_b: b}) do
        a + b
      end
    end
    
  • Use only the function name without a module and in your call doctestfrom your tests add import: true:

    defmodule SomeLongModuleName.SubModule do
      @doc """
          iex> method(%{property_a: 1, property_b: 2})
          3
      """
      def method(%{property_a: a, property_b: b}) do
        a + b
      end
    end
    
    doctest SomeLongModuleName.SubModule, import: true
    
+11
source

, alias SomeLongModuleName.SubModule, as: SubModule .

- , . alias SomeLongModuleName.SubModule, as: SubModule .

+1

dylanthepiguy, , , , doctest.

- .

, as: Submodule , , .

+1

lab419 dylanthepiguy:

doctest:

defmodule SomeLongModuleName.SubModule do
  @doc """
      iex> SubModule.add(x, y)
      3
  """
  def add(x, y) do
    x + y
  end
end

doctest:

defmodule SomeLongModuleName.SubModuleTest do
  use ExUnit.Case, async: true

  # Alias the submodule so we don't have to use the fully qualified name 
  alias SomeLongModuleName.SubModule

  doctest SomeLongModuleName.SubModule, import: true
end
+1

Source: https://habr.com/ru/post/1016343/


All Articles