From a4cb21206efd89ab4fe885d64889795206899e3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Fri, 25 Oct 2024 19:54:50 +0200 Subject: [PATCH] Reduce the amount of modules define in Erlang suite in more than half --- test/ex_doc/language/erlang_test.exs | 84 ++++++++++++++-------------- 1 file changed, 43 insertions(+), 41 deletions(-) diff --git a/test/ex_doc/language/erlang_test.exs b/test/ex_doc/language/erlang_test.exs index a440d512a..2588a1347 100644 --- a/test/ex_doc/language/erlang_test.exs +++ b/test/ex_doc/language/erlang_test.exs @@ -1,9 +1,27 @@ defmodule ExDoc.Language.ErlangTest do # ExDoc.Refs is global use ExUnit.Case, async: false - import TestHelper + setup_all c do + # erlang_bar is shared across all tests. + # Each test defines an erlang_foo for their specific purposes. + :code.purge(:erlang_bar) + + c + |> Map.put(:tmp_dir, "test/tmp/#{inspect(__MODULE__)}") + |> erlc(:erlang_bar, """ + -module(erlang_bar). + -export([bar/0, nil/0]). + -export_type([t/0]). + -type t() :: atom(). + nil() -> []. + bar() -> ok. + """) + + :ok + end + @moduletag :otp_has_docs @moduletag :tmp_dir @@ -56,14 +74,14 @@ defmodule ExDoc.Language.ErlangTest do {:a, [href: "array#anchor", rel: "https://erlang.org/doc/link/seeerl"], [{:code, [], ["array"], %{}}], %{}} - assert do_autolink_doc(ast) == + assert autolink(ast) == ~s|array| ast = {:a, [href: "stdlib:array#anchor", rel: "https://erlang.org/doc/link/seeerl"], [{:code, [], ["array"], %{}}], %{}} - assert do_autolink_doc(ast) == + assert autolink(ast) == ~s|array| end @@ -339,12 +357,14 @@ defmodule ExDoc.Language.ErlangTest do end test "linking to local nil works", c do - assert autolink_doc( - "[`[]`](`t:nil/0`)", - c, - extra_foo_code: "-export_type([nil/0]).\n-type nil() :: [].\n" - ) == - ~s|[]| + if :erlang.system_info(:otp_release) >= ~c"26" do + assert autolink_doc( + "[`[]`](`t:nil/0`)", + c, + extra_foo_code: "-export_type([nil/0]).\n-type nil() :: [].\n" + ) == + ~s|[]| + end end test "linking to local nil function works", c do @@ -359,8 +379,7 @@ defmodule ExDoc.Language.ErlangTest do test "linking to exported nil function works", c do assert autolink_doc( "[`nil`](`erlang_bar:nil/0`)", - c, - extra_bar_code: "-export([nil/0]).\nnil() -> [].\n" + c ) == ~s|nil| end @@ -537,8 +556,8 @@ defmodule ExDoc.Language.ErlangTest do describe "autolink_doc/2 for extra" do test "function", c do - assert autolink_extra("`erlang_foo:foo/0`", c) == - ~s|erlang_foo:foo/0| + assert autolink_extra("`erlang_bar:bar/0`", c) == + ~s|erlang_bar:bar/0| end test "OTP function", c do @@ -557,8 +576,8 @@ defmodule ExDoc.Language.ErlangTest do end test "module", c do - assert autolink_extra("`m:erlang_foo`", c) == - ~s|erlang_foo| + assert autolink_extra("`m:erlang_bar`", c) == + ~s|erlang_bar| end test "OTP module", c do @@ -830,13 +849,11 @@ defmodule ExDoc.Language.ErlangTest do end end - defp autolink_spec(binary, c, opts \\ []) when is_binary(binary) do - fixtures(c, "") - + defp autolink_spec(binary, _c, opts \\ []) when is_binary(binary) do opts = opts - |> Keyword.put_new(:current_module, :erlang_foo) - |> Keyword.put_new(:current_kfa, {:function, :foo, 1}) + |> Keyword.put_new(:current_module, :erlang_bar) + |> Keyword.put_new(:current_kfa, {:function, :bar, 1}) {:ok, tokens, _} = :erl_scan.string(String.to_charlist(binary)) {:ok, ast} = :erl_parse.parse_form(tokens) @@ -845,14 +862,10 @@ defmodule ExDoc.Language.ErlangTest do end defp autolink_extra(text, c) do - # Markdown is usually not valid EDoc - fixtures(c, "") - [{:p, _, [ast], _}] = ExDoc.Markdown.to_ast(text, []) - opts = c |> Map.take([:warnings]) |> Enum.to_list() - do_autolink_doc( + autolink( ast, [current_module: nil, file: nil, module_id: nil, file: "extra.md"] ++ opts ) @@ -869,8 +882,7 @@ defmodule ExDoc.Language.ErlangTest do end opts = Keyword.merge(opts, c |> Map.take([:warnings]) |> Enum.to_list()) - - do_autolink_doc(ast, opts) + autolink(ast, opts) end defp autolink_edoc(doc, c, opts \\ []) do @@ -884,7 +896,7 @@ defmodule ExDoc.Language.ErlangTest do html = doc |> ExDoc.DocAST.parse!("application/erlang+html") - |> do_autolink_doc(opts) + |> autolink(opts) # OTP 27 wraps edoc in

html @@ -892,7 +904,7 @@ defmodule ExDoc.Language.ErlangTest do |> String.trim_trailing("

") end - defp do_autolink_doc(doc, opts \\ []) do + defp autolink(doc, opts \\ []) do opts = opts |> Keyword.put(:language, ExDoc.Language.Erlang) @@ -901,7 +913,7 @@ defmodule ExDoc.Language.ErlangTest do |> Keyword.put_new(:file, "erlang_foo.erl") |> Keyword.put_new(:module_id, "erlang_foo") |> Keyword.put_new(:deps, foolib: "https://foolib.com") - |> Keyword.drop([:extra_foo_code, :extra_bar_code]) + |> Keyword.drop([:extra_foo_code]) doc |> ExDoc.Language.Erlang.autolink_doc(opts) @@ -923,9 +935,8 @@ defmodule ExDoc.Language.ErlangTest do message end - defp fixtures(c, doc, opts \\ []) do + defp fixtures(c, doc, opts) do :code.purge(:erlang_foo) - :code.purge(:erlang_bar) erlc(c, :erlang_foo, """ %% @doc @@ -938,14 +949,5 @@ defmodule ExDoc.Language.ErlangTest do #{opts[:extra_foo_code]} foo() -> ok. """) - - erlc(c, :erlang_bar, """ - -module(erlang_bar). - -export([bar/0]). - -export_type([t/0]). - -type t() :: atom(). - #{opts[:extra_bar_code]} - bar() -> ok. - """) end end